ϪfSdZddlZddlZddlmZddlmZmZddlm Z m Z m Z ddl m Z ddlmZmZmZmZmZmZmZmZmZmZddlmZmZdd lmZmZdd lm Z m!Z!d d l"m#Z#m$Z$m%Z%m&Z&m'Z'e jPd Z)e jPdZ* ddl+m,Z-e-Z, ddlm/Z0e0Z/e!dk(Z1dZ2e1r ddlm3Z3m4Z4dZ2e jjr ddl6Z6dZ2GddZ7eeeeGdde7e Z8GddZ9eeGdde9e7Z:e,e/ ee8ee1r ee8eee2r ee8ee;edd ee8edgZ Mixin providing the C{_disconnectSelectable} method. c|j||j|j}|ri|rD|jtjk(r't j |r|j|y|j||j|y|j||jtj|y)z Utility function for disconnecting a selectable. Supports half-close notification, isRead should be boolean indicating whether error resulted from doRead(). N) removeReaderget __class__rConnectionDoner providedByreadConnectionLost removeWriterconnectionLostrFailure)self selectablewhyisReadfaildictfs > >00? ? =FNN: 224QA11nmWA r9c||jvrtj|tr4|tj k(r!t jj|||Stjj||||S)zg @see: L{twisted.internet.interfaces.IReactorSocket.adoptStreamConnection} ) rrrrurrr$Server_fromConnectedSocketr)r1rrrys r7adoptStreamConnectionz&PosixReactorBase.adoptStreamConnectionMsj  > > >00? ? =FNN:;;33NGTR R::22 w r9c|tjtjfvrtj|t j j|||||}|j|S)N)rk) rAF_INETAF_INET6rrrrfrrg)r1rrrirkrls r7adoptDatagramPortz"PosixReactorBase.adoptDatagramPort\sa  A A00? ? HH - - .- .  r9cXtj|||||}|j|SrD)rrfrg)r1rhryrrjrls r7 listenTCPzPosixReactorBase.listenTCPjs) HHT7GY = r9cZtj||||||}|j|SrD)rrvrw)r1hostrhryrzrr|s r7 connectTCPzPosixReactorBase.connectTCPos( MM$gw T J r9c t,tj|d|}|j|||||St-tj |||||||}|j |SJd)NTSSL support is not present)r TLSMemoryBIOFactoryrr!rvrw) r1rrhrycontextFactoryrzr tlsFactoryr|s r7 connectSSLzPosixReactorBase.connectSSLvsq ?00wOJ??4z7KP P _ dG^Wk4A IIKH 66 65r9ct4tj|d|}|j||||}d|_|St,tj ||||||}|j |SJd)NFTLSr)r rr_typer!rfrg)r1rhryrrrjrrls r7 listenSSLzPosixReactorBase.listenSSLst ?00PJ>>$ GYGDDJK _wDQA   H 66 65r9ct||jz }|D]}|j|t|}|D]}|j|t ||zS)ag Remove all readers and writers, and list of removed L{IReadDescriptor}s and L{IWriteDescriptor}s. Meant for calling from subclasses, to implement removeAll, like:: def removeAll(self): return self._removeAll(self._reads, self._writes) where C{self._reads} and C{self._writes} are iterables. )setrIr(r.list)r1readerswritersremovedReadersreaderremovedWriterswriters r7 _removeAllzPosixReactorBase._removeAllsoW(=(==$ &F   f % &W$ &F   f % &N^344r9) )rrF)r)2r)rr)rrN)r)rr)rN)#r:r;r<r= _childWakerrrFrLrrOrdrmrqr}rrrrurrrrrr AddressFamily__annotations__rrrrrrrr __classcell__)r*s@r7rArAtsKw '<    6tPU "  NR " NN OO NNE !8F,@,@#A  NN OO% ! *  FJ  LP 7 75r9rAceZdZdZdZy)_PollLikeMixina Mixin for poll-like reactors. Subclasses must define the following attributes:: - _POLL_DISCONNECTED - Bitmask for events indicating a connection was lost. - _POLL_IN - Bitmask for events indicating there is input to read. - _POLL_OUT - Bitmask for events indicating output can be written. Must be mixed in to a subclass of PosixReactorBase (for _disconnectSelectable). cd}d}||jzr-||jzs||jvr d}t}nft}n_ |j dk(rt }nD||jzr|j}d}|s!||jzr|j}d}|r|j|||yy#t$r.tjd}tjYMwxYw)zg fd is available for read or write, do the work and raise errors if necessary. NFTr)_POLL_DISCONNECTED_POLL_IN_readsrrfileno _NO_FILEDESCdoRead _POLL_OUTdoWrite BaseExceptionsysexc_inforerrr8)r1r2fdeventr3inReads r7_doReadOrWritez_PollLikeMixin._doReadOrWrites  4** *EDMM4IT[[ %& $$&",'Ct}},(//1!%54>>#9)002!&   & &z3 ?  ! llnQ'  sAB774C.-C.N)r:r;r<r=rr?r9r7rrs  7@r9rcdeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZy)_ContinuousPollinga Schedule reads and writes based on the passage of time, rather than notification. This is useful for supporting polling filesystem files, which C{epoll(7)} does not support. The implementation uses L{_PollLikeMixin}, which is a bit hacky, but re-implementing and testing the relevant code yet again is unappealing. @ivar _reactor: The L{EPollReactor} that is using this instance. @ivar _loop: A C{LoopingCall} that drives the polling, or L{None}. @ivar _readers: A C{set} of C{FileDescriptor} objects that should be read from. @ivar _writers: A C{set} of C{FileDescriptor} objects that should be written to. rc\||_d|_t|_t|_yrD)_reactor_loopr_readers_writers)r1reactors r7__init__z_ContinuousPolling.__init__s"    r9cZ|js |jre|jXddlm}m}||j |_|j|j_|jj|dyy|jr"|jjd|_yy)zh Start or stop a C{LoopingCall} based on whether there are readers and writers. Nr)_EPSILON LoopingCallF)now) rrrtwisted.internet.taskrriteraterclockstartstop)r1rrs r7 _checkLoopz_ContinuousPolling._checkLoops ==DMMzz!G(6 #'==     u 5"ZZ JJOO DJr9ct|jD]}|j|||j!t|jD]}|j|||j !y)zX Call C{doRead} and C{doWrite} on all readers and writers respectively. N)rrrrrr)r1rrs r7rz_ContinuousPolling.iterate&sb4==) ?F    > ?4==) @F    ? @r9cZ|jj||jy)zU Add a C{FileDescriptor} for notification of data available to read. N)rrJrr1rs r7rKz_ContinuousPolling.addReader/  &! r9cZ|jj||jy)zV Add a C{FileDescriptor} for notification of data available to write. N)rrJrr1rs r7 addWriterz_ContinuousPolling.addWriter6rr9cz |jj||jy#t$rYywxYw)zY Remove a C{FileDescriptor} from notification of data available to read. N)rremoveKeyErrorrrs r7r(z_ContinuousPolling.removeReader=s9  MM  (     . ::cz |jj||jy#t$rYywxYw)zb Remove a C{FileDescriptor} from notification of data available to write. N)rrrrrs r7r.z_ContinuousPolling.removeWriterGs9   MM  (    rct|j|jz}|jj|jj|S)z1 Remove all readers and writers. )rrrclear)r1results r7 removeAllz_ContinuousPolling.removeAllRsBdmmdmm34   r9c,t|jS)z/ Return a list of the readers. )rrrEs r7 getReadersz_ContinuousPolling.getReaders]DMM""r9c,t|jS)z/ Return a list of the writers. )rrrEs r7 getWritersz_ContinuousPolling.getWriterscrr9c||jvS)aj Checks if the file descriptor is currently being observed for read readiness. @param fd: The file descriptor being checked. @type fd: L{twisted.internet.abstract.FileDescriptor} @return: C{True} if the file descriptor is being observed for read readiness, C{False} otherwise. @rtype: C{bool} )rr1rs r7 isReadingz_ContinuousPolling.isReadingiT]]""r9c||jvS)al Checks if the file descriptor is currently being observed for write readiness. @param fd: The file descriptor being checked. @type fd: L{twisted.internet.abstract.FileDescriptor} @return: C{True} if the file descriptor is being observed for write readiness, C{False} otherwise. @rtype: C{bool} )rrs r7 isWritingz_ContinuousPolling.isWritingvrr9N)r:r;r<r=rrrrrrrKrr(r.rrrrrr?r9r7rrsV,HI $@  # # # #r9rfromfd)=r=rrtypingrzope.interfacerrtwisted.internetrrrtwisted.internet.baser twisted.internet.interfacesr r r r rrrrrrtwisted.internet.mainrrtwisted.pythonrrtwisted.python.runtimerr_signalsrrrrrConnectionFdescWentAway _NO_FILENOrtwisted.protocolsr _tls ImportErrorr!_sslruprocessEnabledr#r$ isWindowsrXr&rArrgetattr__all__r?r9r7rs   7,,-   C'9+U * *+I J ,u,,-CD - C, Cg% .N8 #<#