ϪfkOdZddlmZddlZddlZddlZddlmZmZm Z ddl m Z m Z ddl mZmZmZmZmZddlmZmZddlmZmZdd lmZdd lmZmZdd lm Z m!Z!m"Z"m#Z#m$Z$dd l%m&Z&dd l'm(Z(ddl)m*Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4m5Z5 ddl6m7Z8e8Z9erddl;m>r7ct|}tj|j|d|jj |S)z Send C{buff} to current file handle using C{_iocp.send}. The buffer sent is limited to a size of C{self.SEND_LIMIT}. r) memoryviewrDsendr0 SEND_LIMITtobytes)r2buffrG writeViews r6 writeToHandlezConnection.writeToHandleasC t$ zz    )A"@"H"H"JC  r7cR |jjdtj|j d}|r |j yy#t$rY@wxYw#t$r<tj}tj|j|YywxYw)N) r.shutdownOSErrorr IHalfCloseableProtocolr1writeConnectionLost BaseExceptionr#Failurer$errconnectionLost)r2pfs r6_closeWriteConnectionz Connection._closeWriteConnectionks  KK  #  - -dmmT B  '%%'     ! 'OO% ##A& 's$AA! AA!AB&%B&ctj|jd}|r |jy|j|y#t$r:t j |jtjYywxYwr+) r rUr1readConnectionLostrWr$rYrZr#rX)r2reasonr[s r6r_zConnection.readConnectionLostysk  - -dmmT B  7$$&    ' ! 7 ##GOO$56 7sAAB  B c|jrytjj|||duxs |j t j  }|j||j}|`|` |` |j|yr+) disconnectedrr,rZcheckr ConnectionAborted _closeSocketr1r.r0)r2r`isCleanr1s r6rZzConnection.connectionLostsy    **48D.M U5L5L(M$M '"== M K  'r7c|jS)zN Return the prefix to log with when I own the logging thread. )logstrr9s r6 logPrefixzConnection.logPrefix{{r7ct|jjtjtjSr+)boolr. getsockopt IPPROTO_TCP TCP_NODELAYr9s r6 getTcpNoDelayzConnection.getTcpNoDelays*DKK**6+=+=v?Q?QRSSr7cv|jjtjtj|yr+)r. setsockoptrnror2enableds r6 setTcpNoDelayzConnection.setTcpNoDelays$ v1163E3EwOr7ct|jjtjtjSr+)rlr.rm SOL_SOCKET SO_KEEPALIVEr9s r6getTcpKeepAlivezConnection.getTcpKeepAlives*DKK**6+<+Q>QRSSr7cv|jjtjtj|yr+)r.rrrwrxrss r6setTcpKeepAlivezConnection.setTcpKeepAlives$ v00&2E2EwOr7c<t|||tjy)z9 @see: L{ITLSTransport.startTLS} N) _startTLSrr,)r2contextFactorynormals r6r&zConnection.startTLSs dNFH4G4G Hr7c|jry|jr|jj|ytj j||y)z Write some data, either directly to the underlying handle or, if TLS has been started, to the L{TLSMemoryBIOProtocol} for it to encrypt and send. @see: L{twisted.internet.interfaces.ITransport.write} N)rbTLSr1writerr,)r2datas r6rzConnection.writes?     88 MM   %    % %dD 1r7c|jry|jr|jj|ytj j||y)z Write some data, either directly to the underlying handle or, if TLS has been started, to the L{TLSMemoryBIOProtocol} for it to encrypt and send. @see: L{twisted.internet.interfaces.ITransport.writeSequence} N)rbrr1 writeSequencerr,)r2iovecs r6rzConnection.writeSequences?     88 MM ' ' .    - -dE :r7c|jr5|jr(|js|jj yyyt j j ||y)z Close the underlying handle or, if TLS has been started, first shut it down. @see: L{twisted.internet.interfaces.ITransport.loseConnection} N)r connected disconnectingr1loseConnectionrr,)r2r`s r6rzConnection.loseConnectionsI 88~~d&8&8 ,,.'9~    . .tV +>r?+S&TU  KK " "!!) C!3!3!56  !NN88HDM!DN**4==9I#i/DK}}$!)  ##% ,,T2!!#r7cjt|dsytjsJ|jj |tj |j |}tj|jj|j|}|r|tk7r|j |d|yyy)Nrr) hasattrrDhave_connectexr5addActiveHandleEventrconnectr.r/ realAddressr)r2rGrs r6 doConnectzClient.doConnectGst[) #### $$T*kk$..$/ ]]4;;--/1A1A3 G "(( NN2q# &)2r7N)rrrrr.AF_INETr SOCK_STREAMr_tlsClientDefaultr)_commonConnectionr-rrrrrrr7r6rrsI NNM##J"SN,.$< 'r7rcHeZdZdZdZ ddZd dZdZdZy) ServeraV Serverside socket-stream connection class. I am a serverside network connection transport; a socket which came from an accept() on a server. @ivar _tlsClientDefault: Always C{False}, indicating that this is a server connection, and by default when TLS is negotiated this class will act as a TLS server. Fctj||||||_||_||_|j |j }|d|d|jj|_dj|j jj|j|jj|_ d|_|jy)a  Server(sock, protocol, client, server, sessionno) Initialize me with a socket, a protocol, a descriptor for my peer (a tuple of host, port describing the other end of the connection), an instance of Port, and a session number. ,z<{} #{} on {}>TN)r)r- serverAddr clientAddr sessionnorr1rrhformat __class__rrrepstrrr)r2r3r1rrrr5ris r6r-zServer.__init__cs D$':$$"&&t}}5 " 1YKq1E1E0FG +22 MM # # , , NN OO    r7c|jS)z= A string representation of this connection. )rr9s r6__repr__zServer.__repr__rjr7c|jS)zW Returns an IPv4Address. This indicates the server's address. )rr9s r6rzServer.getHost r7c|jS)zW Returns an IPv4Address. This indicates the client's address. )rr9s r6rzServer.getPeerrr7N) r3z socket.socketr1rrUnion[IPv4Address, IPv6Address]rrrintr5r'returnstr) rrrrrr-rrrrr7r6rrUs] 4  4   < r7rceZdZdZy)rcpt|j|j|j||jSr+)rrrrr5r9s r6_makeTransportzConnector._makeTransports'diiD,<,z<{} of {} (not listening)>)rrrrr9s r6rz Port.__repr__sg    +%,, &&$$  066 && r7c |jj|j|j}|jtj k(r!t |j|j}n|j|jf}|j|tj|j|_|j#d|_t'j(|j+|j,d|j$|j,j/|j1|j2d|_d|_|jj9|||_|jj|_|j=y#t$r0}tj|j|j|d}~wwxYw)NrRz starting on TF)r5rrrr.rr!rrbindrTr CannotListenErrorrD maxAddrLenr/addrLen getsocknamerr$msgrrdoStartlistenrrrbrr0doAccept)r2sktaddrles r6startListeningzPort.startListenings] I,,++D,>,>PC!!V__4#DNNDII> 2 HHTN'' 5  #03 !!$,,/1E1E G  4<< ! $$T* ![[// + I))$..$))RH H IsBF'' G 0+GG cd|_|jrLtj|_|j j d|j||jSy)z Stop accepting connections on this port. This will shut down my socket and call self.connectionLost(). It returns a deferred which will fire successfully when the port is actually closed. TrN)rrr Deferreddeferredr5 callLaterrZ)r2connDones r6rzPort.loseConnectionsL" >>!NN,DM LL " "1d&9&98 D==  r7cdtjd|jd|jdy)z. Log message for closing port (z Port z Closed)N)r$r_typerr9s r6_logConnectionLostMsgzPort._logConnectionLostMsgs* !DJJrrNr5rrrracceptr.r/rr )r2rGrNrrs r6rz Port.doAcceptcskk$--.$A):$;<<4"ll77      V\\$++,,. s K "((   b# &)2r7)2rNr)"rrrrrbrr.rrrrr rrrr__annotations__rr-rrr#rXr CONNECTION_DONEr stopListeningrrZrirr r rrr7r6rrsILMNNM##J&&LI&*O]) E4 @'6good6J6J&K !#MF !>4 D 1f 'r7r)Ir __future__rrr.rtypingrrrzope.interfacerrtwisted.internetr r r r r twisted.internet.abstractrrtwisted.internet.addressrrtwisted.internet.interfacesrtwisted.internet.iocpreactorrrrD"twisted.internet.iocpreactor.constrrrrr'twisted.internet.iocpreactor.interfacesrtwisted.internet.protocolrtwisted.internet.tcpr TCPConnectorrrrr r!r"twisted.pythonr#r$r%twisted.internet._newtlsr& __startTLSr} ImportError$twisted.internet.iocpreactor.reactorr'WSAECONNREFUSEDWSAENETUNREACHr ITCPTransport ISystemHandler,r) ITLSTransportrrIListeningPortrrr7r6r:s[# 117DD>=1GE.10?I@e33u33  z779Q9QRx $$m^x Sx v J 8 89V'_njV'r@Z@FR R  Z & &'Q'=)Q'(Q'e Is EEE