
    \dB*                        d Z ddlZddlZddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZ 	  eed          Z eed          Z eed	          Z eed
          Z eed          Zn# e$ rZ ee          dZ[ww xY w G d de          Z ej        ee            eee           G d de
j                              Zd ZddgZdS )am  
A kqueue()/kevent() based implementation of the Twisted main loop.

To use this reactor, start your application specifying the kqueue reactor::

   twistd --reactor kqueue ...

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

   from twisted.internet import kqreactor
   kqreactor.install()
    N)	Attribute	Interfacedeclarationsimplementer)main	posixbase)IReactorDaemonizeIReactorFDSet)failurelog	KQ_EV_ADDKQ_EV_DELETE	KQ_EV_EOFKQ_FILTER_READKQ_FILTER_WRITEc                   >    e Zd ZdZ ed          Z ed          ZdS )_IKQueuez2
    An interface for KQueue implementations.
    zAn implementation of kqueue(2).zAn implementation of kevent(2).N)__name__
__module____qualname____doc__r   kqueuekevent     :lib/python3.11/site-packages/twisted/internet/kqreactor.pyr   r   *   s:          Y899FY899FFFr   r   c                   h    e Zd ZdZefdZd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd ZeZdS )KQueueReactoram  
    A reactor that uses kqueue(2)/kevent(2) and relies on Python 2.6 or higher
    which has built in support for kqueue in the select module.

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

    @ivar _impl: The implementation of L{_IKQueue} to use.

    @ivar _selectables: A dictionary mapping integer file descriptors to
        instances of L{FileDescriptor} which have been registered with the
        reactor.  All L{FileDescriptor}s 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{_kq} for read readiness notifications
        which will be dispatched to the corresponding L{FileDescriptor}
        instances in C{_selectables}.

    @ivar _writes: A set containing integer file descriptors.  Values in this
        set will be registered with C{_kq} for write readiness notifications
        which will be dispatched to the corresponding L{FileDescriptor}
        instances in C{_selectables}.
    c                     || _         | j                                         | _        t                      | _        t                      | _        i | _        t          j        	                    |            dS )a  
        Initialize kqueue object, file descriptor tracking dictionaries, and
        the base class.

        See:
            - http://docs.python.org/library/select.html
            - www.freebsd.org/cgi/man.cgi?query=kqueue
            - people.freebsd.org/~jlemon/papers/kqueue.pdf

        @param _kqueueImpl: The implementation of L{_IKQueue} to use. A
            hook for testing.
        N)
_implr   _kqset_reads_writes_selectablesr   PosixReactorBase__init__)self_kqueueImpls     r   r'   zKQueueReactor.__init__Q   s[     !
:$$&&eeuu"++D11111r   c                 t    | j                             | j                            |||          gdd           dS )z
        Private method for changing kqueue registration on a given FD
        filtering for events given filter/op. This will never block and
        returns nothing.
        r   N)r!   controlr    r   )r(   fdfilterops       r   _updateRegistrationz!KQueueReactor._updateRegistratione   s;     	$*++B;;<aCCCCCr   c                 F    | j                                          d| _         dS )zA
        Implement L{IReactorDaemonize.beforeDaemonize}.
        N)r!   closer(   s    r   beforeDaemonizezKQueueReactor.beforeDaemonizem   s!     	r   c                     | j                                         | _        | j        D ]#}|                     |t
          t                     $| j        D ]#}|                     |t          t                     $dS )z@
        Implement L{IReactorDaemonize.afterDaemonize}.
        N)	r    r   r!   r#   r/   r   r   r$   r   )r(   r,   s     r   afterDaemonizezKQueueReactor.afterDaemonize}   s     :$$&&+ 	D 	DB$$RCCCC, 	E 	EB$$R)DDDD	E 	Er   c                 D   |                                 }|| j        vr	 |                     |t          t                     n# t
          $ r Y nw xY w|| j        |<   | j                            |           dS # || j        |<   | j                            |           w xY wdS )z7
        Implement L{IReactorFDSet.addReader}.
        N)filenor#   r/   r   r   OSErrorr%   add)r(   readerr,   s      r   	addReaderzKQueueReactor.addReader   s     ]]__T[  $((^YGGGG    )/!"%##### )/!"%#### ! )   !A  A7 
AA7 AA7 7&Bc                 D   |                                 }|| j        vr	 |                     |t          t                     n# t
          $ r Y nw xY w|| j        |<   | j                            |           dS # || j        |<   | j                            |           w xY wdS )z7
        Implement L{IReactorFDSet.addWriter}.
        N)r7   r$   r/   r   r   r8   r%   r9   )r(   writerr,   s      r   	addWriterzKQueueReactor.addWriter   s     ]]__T\!!%((_iHHHH    )/!"%  $$$$$ )/!"%  $$$$ "!r<   c                    d}	 |                                 }n# t          $ r d}Y nw xY w|dk    r)| j                                        D ]\  }}||u rd} ndS || j        v rb| j                            |           || j        vr| j        |= |s7	 |                     |t          t                     dS # t          $ r Y dS w xY wdS dS )z:
        Implement L{IReactorFDSet.removeReader}.
        FTN)r7   BaseExceptionr%   itemsr#   remover$   r/   r   r   r8   )r(   r:   wasLostr,   fdess        r   removeReaderzKQueueReactor.removeReader   s%    	BB 	 	 	BBB	88 -3355  DT>>"GE " Kr"""%%%b) ,,RNNNNN   DD      ((!B4 4
CCc                    d}	 |                                 }n# t          $ r d}Y nw xY w|dk    r)| j                                        D ]\  }}||u rd} ndS || j        v rb| j                            |           || j        vr| j        |= |s7	 |                     |t          t                     dS # t          $ r Y dS w xY wdS dS )z:
        Implement L{IReactorFDSet.removeWriter}.
        FrA   TN)r7   rB   r%   rC   r$   rD   r#   r/   r   r   r8   )r(   r>   rE   r,   rF   s        r   removeWriterzKQueueReactor.removeWriter   s%    	BB 	 	 	BBB	88 -3355  DT>>"GE " L###$$%b) ,,R,OOOOO   DD  rH   c                 t                            fd j        D              fd j        D                       S )z7
        Implement L{IReactorFDSet.removeAll}.
        c                 *    g | ]}j         |         S r   r%   .0r,   r(   s     r   
<listcomp>z+KQueueReactor.removeAll.<locals>.<listcomp>   s!    999rTr"999r   c                 *    g | ]}j         |         S r   rM   rN   s     r   rP   z+KQueueReactor.removeAll.<locals>.<listcomp>   s!    :::rTr":::r   )
_removeAllr#   r$   r2   s   `r   	removeAllzKQueueReactor.removeAll   sM     9999T[999::::T\:::
 
 	
r   c                 *      fd j         D             S )z8
        Implement L{IReactorFDSet.getReaders}.
        c                 *    g | ]}j         |         S r   rM   rN   s     r   rP   z,KQueueReactor.getReaders.<locals>.<listcomp>   s!    <<<"!"%<<<r   )r#   r2   s   `r   
getReaderszKQueueReactor.getReaders   s!     =<<<<<<<r   c                 *      fd j         D             S )z8
        Implement L{IReactorFDSet.getWriters}.
        c                 *    g | ]}j         |         S r   rM   rN   s     r   rP   z,KQueueReactor.getWriters.<locals>.<listcomp>   s!    ==="!"%===r   )r$   r2   s   `r   
getWriterszKQueueReactor.getWriters   s!     >=======r   c                 \   |d}	 | j                             g t          | j                  |          }n.# t          $ r!}|j        t
          j        k    rY d}~dS  d}~ww xY w| j        }|D ]?}|j        }	 | j        |         }t          j
        |||||           0# t          $ r Y <w xY wdS )z1
        Poll the kqueue for new events.
        N   )r!   r+   lenr%   r8   errnoEINTR_doWriteOrReadidentr   callWithLoggerKeyError)r(   timeouteventse_drdweventr,   
selectables           r   doKEventzKQueueReactor.doKEvent   s     ?G		X%%b#d.?*@*@'JJFF 	 	 	 w%+%%	 # 		M 		MEBM!.r2
 ":uj"eLLLL     			M 		Ms,   .5 
A AAA 6B
B)(B)c                    d}d}|j         |j        |j        |j        f\  }}}}	|t          z  r|r|	rt
          j        }n	 |                                dk    rd}t          j	        }nB|t          k    rd}|                                }|t          k    rd}|                                }n8# t          $ r+ t          j                    }t#          j        |d           Y nw xY w|r|                     |||           dS dS )z
        Private method called when a FD is ready for reading, writing or was
        lost. Do the work and raise errors where necessary.
        NFrA   TzSAn exception was raised from application code while processing a reactor selectable)r-   flagsdatafflagsr   r   CONNECTION_LOSTr7   r   _NO_FILEDESCr   doReadr   doWriterB   r   Failurer   err_disconnectSelectable)
r(   rh   r,   rg   whyinReadr-   rk   rl   rm   s
             r   r_   zKQueueReactor._doWriteOrRead  sL   
 LKJL	)
%f 9 	 	& 	&CC$$&&",,"F#0CC//!%(//1100!&(0022     o''=    	  	@&&z3?????	@ 	@s    A)B* *2CCN)r   r   r   r   selectr'   r/   r3   r5   r;   r?   rG   rJ   rS   rV   rY   ri   r_   doIterationr   r   r   r   r   6   s         2 $* 2 2 2 2(D D D   E E E$ $ $% % %  4  4
 
 
= = => > >M M M<'@ '@ '@R KKKr   r   c                  D    t                      } ddlm}  ||            dS )z'
    Install the kqueue() reactor.
    r   )installReactorN)r   twisted.internet.mainrz   )prz   s     r   installr}   :  s5     	A444444N1r   r}   ) r   r]   rw   zope.interfacer   r   r   r   twisted.internetr   r   twisted.internet.interfacesr	   r
   twisted.pythonr   r   getattrr   r   r   r   r   AttributeErrorre   ImportErrorr   directlyProvidesr&   r   r}   __all__r   r   r   <module>r      s  
    J J J J J J J J J J J J , , , , , , , , H H H H H H H H ' ' ' ' ' ' ' '
 ,,I76>22L,,IWV%566Ngf&788OO   
+a..: : : : :y : : :  fh / / / ]-..@ @ @ @ @I. @ @ /.@F   I
&s   <A- -B2A==B