o
    b"                  
   @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 zeedZeedZeed	Zeed
ZeedZW n eyM Z zeedZ[ww eeG dd dejejZdd ZddgZdS )z
An epoll() based implementation of the twisted main loop.

To install the event loop (and you should do this before any connections,
listeners or connectors are added)::

    from twisted.internet import epollreactor
    epollreactor.install()
    N)implementer)	posixbase)IReactorFDSet)logepollEPOLLHUPEPOLLERREPOLLINEPOLLOUTc                   @   s|   e Zd ZdZeeB ZeZe	Z
dd Zdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd ZeZdS )EPollReactora  
    A reactor that uses epoll(7).

    @ivar _poller: A C{epoll} which will be used to check for I/O
        readiness.

    @ivar _selectables: A dictionary mapping integer file descriptors to
        instances of C{FileDescriptor} which have been registered with the
        reactor.  All C{FileDescriptors} which are currently receiving read or
        write readiness notifications will be present as values in this
        dictionary.

    @ivar _reads: A set containing integer file descriptors.  Values in this
        set will be registered with C{_poller} for read readiness notifications
        which will be dispatched to the corresponding C{FileDescriptor}
        instances in C{_selectables}.

    @ivar _writes: A set containing integer file descriptors.  Values in this
        set will be registered with C{_poller} for write readiness
        notifications which will be dispatched to the corresponding
        C{FileDescriptor} instances in C{_selectables}.

    @ivar _continuousPolling: A L{_ContinuousPolling} instance, used to handle
        file descriptors (e.g. filesystem files) that are not supported by
        C{epoll(7)}.
    c                 C   s<   t d| _t | _t | _i | _t| | _tj	
|  dS )zm
        Initialize epoll object, file descriptor tracking dictionaries, and the
        base class.
        i   N)r   _pollerset_reads_writes_selectablesr   _ContinuousPolling_continuousPollingPosixReactorBase__init__self r   ?/usr/lib/python3/dist-packages/twisted/internet/epollreactor.pyr   G   s   
zEPollReactor.__init__c           	      C   s\   |  }||vr,|}||v r||O }| j|| n| j|| || |||< dS dS )z
        Private method for adding a descriptor from the event loop.

        It takes care of adding it if  new or modifying it if already added
        for another state (read -> read/write for example).
        N)filenor   modifyregisteradd)	r   xerprimaryotherselectablesevent	antieventfdflagsr   r   r   _addV   s   
zEPollReactor._addc              
   C   d   z|  || j| j| jtt W dS  ty1 } z|jtjkr%| j	
| n W Y d}~dS d}~ww )zR
        Add a FileDescriptor for notification of data available to read.
        N)r%   r   r   r   r	   r
   OSErrorerrnoEPERMr   	addReader)r   readerer   r   r   r*   p      
zEPollReactor.addReaderc              
   C   r&   )zS
        Add a FileDescriptor for notification of data available to write.
        N)r%   r   r   r   r
   r	   r'   r(   r)   r   	addWriter)r   writerr,   r   r   r   r.      r-   zEPollReactor.addWriterc           
      C   sz   |  }|dkr| D ]
\}}||u r nqdS ||v r;||v r+|}	| j||	 n	||= | j| || dS dS )z
        Private method for removing a descriptor from the event loop.

        It does the inverse job of _add, and also add a check in case of the fd
        has gone away.
        N)r   itemsr   r   
unregisterremove)
r   r   r   r   r    r!   r"   r#   fdesr$   r   r   r   _remove   s   zEPollReactor._removec                 C   :   | j |r| j | dS | || j| j| jtt dS )zQ
        Remove a Selectable for notification of data available to read.
        N)	r   	isReadingremoveReaderr5   r   r   r   r	   r
   )r   r+   r   r   r   r8         zEPollReactor.removeReaderc                 C   r6   )zR
        Remove a Selectable for notification of data available to write.
        N)	r   	isWritingremoveWriterr5   r   r   r   r
   r	   )r   r/   r   r   r   r;      r9   zEPollReactor.removeWriterc                    s6      fdd jD  fdd jD  j  S )zD
        Remove all selectables, and return a list of them.
        c                       g | ]} j | qS r   r   .0r#   r   r   r   
<listcomp>       z*EPollReactor.removeAll.<locals>.<listcomp>c                    r<   r   r=   r>   r   r   r   r@      rA   )
_removeAllr   r   r   	removeAllr   r   r   r   rC      s   zEPollReactor.removeAllc                        fdd j D  j  S )Nc                    r<   r   r=   r>   r   r   r   r@          
z+EPollReactor.getReaders.<locals>.<listcomp>)r   r   
getReadersr   r   r   r   rF      
   
zEPollReactor.getReadersc                    rD   )Nc                    r<   r   r=   r>   r   r   r   r@      rE   z+EPollReactor.getWriters.<locals>.<listcomp>)r   r   
getWritersr   r   r   r   rH      rG   zEPollReactor.getWritersc              
   C   s   |du rd}z| j |t| j}W n ty, } z|jtjkr'W Y d}~dS  d}~ww | j}|D ]\}}z| j| }W n	 tyF   Y q2w t	
||||| q2dS )z1
        Poll the poller for new events.
        Nr0   )r   polllenr   r'   r(   EINTR_doReadOrWriteKeyErrorr   callWithLogger)r   timeoutlerr_drdwr#   r!   
selectabler   r   r   doPoll   s&   	zEPollReactor.doPollN)__name__
__module____qualname____doc__r   r   _POLL_DISCONNECTEDr	   _POLL_INr
   	_POLL_OUTr   r%   r*   r.   r5   r8   r;   rC   rF   rH   rT   doIterationr   r   r   r   r   %   s"    r   c                  C   s   t  } ddlm} ||  dS )z&
    Install the epoll() reactor.
    r   )installReactorN)r   twisted.internet.mainr]   )pr]   r   r   r   install   s   r`   )rX   r(   selectzope.interfacer   twisted.internetr   twisted.internet.interfacesr   twisted.pythonr   getattrr   r   r   r	   r
   AttributeErrorr,   ImportErrorr   _PollLikeMixinr   r`   __all__r   r   r   r   <module>   s,   




 T
