'Qc@`sdZddlmZmZddlZddlZddlZddlZddlm Z m Z ddl m Z ddl mZmZddl mZmZmZmZddl mZmZdd l mZdd lmZmZmZdd lmZmZmZdd lm Z m!Z!dd l"m#Z#m$Z$ddl%m&Z&m'Z'ej(dZ)ej(dZ*yddl+m,Z,WnEe-k re.Z,yddlm/Z/Wne-k re.Z/nXnXe dkZ0e1Z2e0rddlm3Z3e rddlm4Z4m5Z5e6Z2nne!j7rTyddl8Z8e6Z2Wne-k rPe.Z8nXndej9fdYZ:dej9e;fdYZ<de<fdYZ=e dkre=Z>ne:Z>de<fdYZ?de;fdYZ@e eeed e$e@e#fd!YZAd"e;fd#YZBe,e.k p?e/e.k rRe eAene0rke eAeene2re eAeneCed$e.e.k re eAend gZDdS(%s Posix reactor base class i(tdivisiontabsolute_importN(t implementertclassImplements(t_PY3(t IReactorUNIXtIReactorUNIXDatagram(t IReactorTCPt IReactorUDPt IReactorSSLtIReactorSocket(tIReactorProcesstIReactorMulticast(tIHalfCloseableDescriptor(terrortudpttcp(tlogtfailuretutil(t platformTypetplatform(t ReactorBaset_SignalReactorMixin(tCONNECTION_DONEtCONNECTION_LOSTsHandler has no fileno methodsFile descriptor lost(ttls(tssltposix(tfdesc(tprocesst_signalst _SocketWakercB`s8eZdZdZdZdZdZdZRS(s The I{self-pipe trick}, implemented using a pair of sockets rather than pipes (due to the lack of support in select() on Windows for pipes), used to wake up the main loop from another thread. icC`s||_tjtjtj}tjtjtj}|jtjtjd|jd|jd|j |j |j \}}|j d|j d||_ ||_|j j|_dS(sInitialize. is 127.0.0.1iN(s 127.0.0.1i(treactortsockettAF_INETt SOCK_STREAMt setsockoptt IPPROTO_TCPt TCP_NODELAYtbindtlistentconnectt getsocknametacceptt setblockingtrtwtfileno(tselfR!tservertclienttreadert clientaddr((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt__init__Js       cC`sSytj|jjdWn2tjk rN}|jdtjkrOqOnXdS(s&Send a byte to my connection. txiN( RtuntilConcludesR/tsendR"RtargsterrnotWSAEWOULDBLOCK(R1te((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytwakeUp\s cC`s/y|jjdWntjk r*nXdS(s+Read some data from my connection. i N(R.trecvR"R(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytdoReadescC`s|jj|jjdS(N(R.tcloseR/(R1treason((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytconnectionLostms (t__name__t __module__t__doc__t disconnectedR6R>R@RC(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR As   t_FDWakercB`s;eZdZdZdZdZdZdZdZ RS(s The I{self-pipe trick}, used to wake up the main loop from another thread or a signal handler. L{_FDWaker} is a base class for waker implementations based on writing to a pipe being monitored by the reactor. @ivar o: The file descriptor for the end of the pipe which can be written to to wake up a reactor monitoring this waker. @ivar i: The file descriptor which should be monitored in order to be awoken by this waker. ic`sw|_tj\__tjjtjjtjjtjjfd_dS(sInitialize. c`sjS(N(ti((R1(s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytsN( R!tostpipeRItoRtsetNonBlockingt_setCloseOnExecR0(R1R!((R1s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR6s cC`stj|jddS(sA Read some bytes from the pipe and discard them. cS`sdS(N(tNone(tdata((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRJsN(Rt readFromFDR0(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR@scC`set|dsdSx?|j|jfD]+}ytj|Wq&tk rPq&Xq&W|`|`dS(s$Close both ends of my pipe. RMN(thasattrRIRMRKRAtIOError(R1RBtfd((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRCs N( RDRERFRGRPRIRMR6R@RC(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRHss  t _UnixWakercB`seZdZdZRS(s This class provides a simple interface to wake up the event loop. This is used by threads or signals to wake up the event loop. cC`sa|jdk r]ytjtj|jdWq]tk rY}|jtjkrZqZq]XndS(s2Write one byte to the pipe, and flush it. R7N( RMRPRR8RKtwritetOSErrorR;tEAGAIN(R1R=((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR>s (RDRERFR>(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRVst _SIGCHLDWakercB`s2eZdZdZdZdZdZRS(s} L{_SIGCHLDWaker} can wake up a reactor whenever C{SIGCHLD} is received. @see: L{twisted.internet._signals} cC`stj||dS(N(RHR6(R1R!((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR6scC`stj|jdS(sJ Install the handler necessary to make this waker active. N(RtinstallHandlerRM(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytinstallscC`stjddS(sC Remove the handler which makes this waker active. iN(RR[(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt uninstallscC`stj|tjdS(s Having woken up the reactor in response to receipt of C{SIGCHLD}, reap the process which exited. This is called whenever the reactor notices the waker pipe is writeable, which happens soon after any call to the C{wakeUp} method. N(RHR@RtreapAllProcesses(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR@s (RDRERFR6R\R]R@(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRZs    t_DisconnectSelectableMixincB`sLeZdZiejejej6ejejej6dZRS(s> Mixin providing the C{_disconnectSelectable} method. cC`s|j||j|j}|ry|r\|jtjkr\tj|r\|j|q|j||j |n#|j||j t j |dS(s Utility function for disconnecting a selectable. Supports half-close notification, isRead should be boolean indicating whether error resulted from doRead(). N( t removeReadertgett __class__RtConnectionDoneR t providedBytreadConnectionLostt removeWriterRCRtFailure(R1t selectabletwhytisReadtfaildicttf((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt_disconnectSelectables   ( RDRERFRRgRRctConnectionLostRm(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR_stPosixReactorBasecB`seZdZeZdZdZdZdZ diddddddZ dddZ dde d Z d dd Zd d ddZdd dZdd ddZdZdZddZd ddZd ddZd ddZd ddZdZRS(s A basis for reactors that use file descriptors. @ivar _childWaker: C{None} or a reference to the L{_SIGCHLDWaker} which is used to properly notice child process termination. cC`sE|jsA|j||_|jj|j|j|jndS(s Install a `waker' to allow threads and signals to wake up the IO thread. We use the self-pipe trick (http://cr.yp.to/docs/selfpipe.html) to wake the reactor. On Windows we use a pair of sockets. N(twakert _wakerFactoryt_internalReaderstaddt addReader(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt installWakers cC`s{tj|tdkrwtrw|js]t||_|jj|j|j|jn|jj t j ndS(s Extend the basic signal handling logic to also support handling SIGCHLD to know when to try to reap child processes. RN( Rt_handleSignalsRtprocessEnabledt _childWakerRZRrRsRtR\RR^(R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRv's   cC`s|jr|jjndS(s If a child waker was created and installed, uninstall it now. Since this disables reactor functionality and is only called when the reactor is stopping, it doesn't provide any directly useful functionality, but the cleanup of reactor-related process-global state that it does helps in unit tests involving multiple reactors and is generally just a nice thing. N(RxR](R1((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt_uninstallHandler:s ic C`sL|j||\}}tdkr|rj| dk rEtdntj||||||||| Stj||||||||| Sntdkr<|dk rtdn|dk rtdn|rtdn| rtdntr-dd lm} | ||||||St d n t d dS( NRs1Using childFDs is not supported with usePTY=True.twin32s,Setting UID is unsupported on this platform.s,Setting GID is unsupported on this platform.s1The usePTY parameter is not supported on Windows.s1Customizing childFDs is not supported on Windows.i(tProcesss:spawnProcess not available since pywin32 is not installed.s0spawnProcess only available on Windows or POSIX.( t_checkProcessArgsRRPt ValueErrorRt PTYProcessR{t win32processttwisted.internet._dumbwin32proctNotImplementedError( R1tprocessProtocolt executableR:tenvtpathtuidtgidtusePTYtchildFDsR{((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt spawnProcessMs2      ti cC`s)tj|||||}|j|S(sConnects a given L{DatagramProtocol} to the given numeric UDP port. @returns: object conforming to L{IListeningPort}. (RtPorttstartListening(R1tporttprotocolt interfacet maxPacketSizetp((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt listenUDPps cC`s,tj||||||}|j|S(sConnects a given DatagramProtocol to the given numeric UDP port. EXPERIMENTAL. @returns: object conforming to IListeningPort. (Rt MulticastPortR(R1RRRRtlistenMultipleR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytlistenMulticast{s icC`sKtstdddlm}|j|||||}|j|S(NsUNIX support is not presenti(tunix(t unixEnabledtAssertionErrorttwisted.internetRt ConnectorR*(R1taddresstfactoryttimeouttcheckPIDRtc((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt connectUNIXs  i2icC`sNtstdddlm}|j||||||}|j|S(NsUNIX support is not presenti(R(RRRRRR(R1RRtbacklogtmodetwantPIDRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt listenUNIXs  cC`sKtstdddlm}|j|||||}|j|S(s Connects a given L{DatagramProtocol} to the given path. EXPERIMENTAL. @returns: object conforming to L{IListeningPort}. sUNIX support is not presenti(R(RRRRt DatagramPortR(R1RRRRRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytlistenUNIXDatagrams  cC`sNtstdddlm}|j||||||}|j|S(sd Connects a L{ConnectedDatagramProtocol} instance to a path. EXPERIMENTAL. sUNIX support is not presenti(R(RRRRtConnectedDatagramPortR(R1RRRRt bindAddressRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytconnectUNIXDatagrams  cC`sS|tjtjfkr*tj|ntjj||||}|j|S(s0 Create a new L{IListeningPort} from an already-initialized socket. This just dispatches to a suitable port implementation (eg from L{IReactorTCP}, etc) based on the specified C{addressFamily}. @see: L{twisted.internet.interfaces.IReactorSocket.adoptStreamPort} ( R"R#tAF_INET6RtUnsupportedAddressFamilyRRt_fromListeningDescriptorR(R1tfileDescriptort addressFamilyRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytadoptStreamPorts   cC`sC|tjtjfkr*tj|ntjj||||S(sg @see: L{twisted.internet.interfaces.IReactorSocket.adoptStreamConnection} (R"R#RRRRtServert_fromConnectedSocket(R1RRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytadoptStreamConnections cC`sY|tjtjfkr*tj|ntjj||||d|}|j|S(NR( R"R#RRRRRRR(R1RRRRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytadoptDatagramPorts   cC`s)tj|||||}|j|S(N(RRR(R1RRRRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt listenTCPs cC`s,tj||||||}|j|S(N(RRR*(R1thostRRRRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt connectTCPs c C`stdk r:tj|t|}|j|||||Stdk rutj|||||||}|j|Stst ddS(NsSSL support is not present( RRPtTLSMemoryBIOFactorytTrueRRRR*tFalseR( R1RRRtcontextFactoryRRt tlsFactoryR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt connectSSLs   cC`stdk rFtj|t|}|j||||}d|_|Stdk r~tj||||||}|j|Stst ddS(NtTLSsSSL support is not present( RRPRRRt_typeRRRR(R1RRRRRRR((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt listenSSLs    cC`sit||j}x|D]}|j|qWt|}x|D]}|j|qDWt||BS(sg 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. (tsetRrR`Rftlist(R1treaderstwriterstremovedReadersR4tremovedWriterstwriter((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt _removeAlls    N((RDRERFt_WakerRqRuRPRxRvRyRRRRRRRRRRRRRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRo s2   !      t_PollLikeMixincB`seZdZdZRS(s 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). cC`sd}t}||j@rN||j@ rN||jkrEt}t}qt}nyj|jdkrlt }nK||j@r|j }t}n| r||j @r|j }t}nWn!t jd}tjnX|r|j|||ndS(sg fd is available for read or write, do the work and raise errors if necessary. iiN(RPRt_POLL_DISCONNECTEDt_POLL_INt_readsRRRR0t _NO_FILEDESCR@t _POLL_OUTtdoWritetsystexc_infoRterrRm(R1RhRUteventRitinRead((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt_doReadOrWrite<s*        (RDRERFR(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR-s tfromfd(ERFt __future__RRR"R;RKRtzope.interfaceRRttwisted.python.compatRttwisted.internet.interfacesRRRRR R R R R RRRRttwisted.pythonRRRttwisted.python.runtimeRRttwisted.internet.baseRRttwisted.internet.mainRRtConnectionFdescWentAwayt _NO_FILENORttwisted.protocolsRt ImportErrorRPRRRRwRRRRt isWindowsRtLoggerR tobjectRHRVRRZR_RoRtgetattrt__all__(((s>/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pytsz    "         24  (!J