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
