ϪfYKdZddlmZmZmZddlmZmZmZddl m Z ddl m Z m Z ddlmZmZddlmZdZd ZGd d Ze e j,Gd d Ze e j0e j2e j4e j6e j8GddeeZefdedede fdZ!dede fdZ"gdZ#y)z+ Support for generic select()able objects. )AF_INETAF_INET6 inet_pton)IterableListOptional) implementer) interfacesmain)failurereflect) lazyByteSlicec:t|ts tdy)NzData must be bytes) isinstancebytes TypeError)objs ;/usr/lib/python3/dist-packages/twisted/internet/abstract.py_dataMustBeBytesrs c5 !,-- "cDdjt||dg|zS)Nr)join memoryview)bObjoffsetbArrays r _concatenaters% 88Z%fg./&8 99rc.eZdZdZdZdZdZdZdZdZ y)_ConsumerMixina L{IConsumer} implementations can mix this in to get C{registerProducer} and C{unregisterProducer} methods which take care of keeping track of a producer's state. Subclasses must provide three attributes which L{_ConsumerMixin} will read but not write: - connected: A C{bool} which is C{True} as long as the consumer has someplace to send bytes (for example, a TCP connection), and then C{False} when it no longer does. - disconnecting: A C{bool} which is C{False} until something like L{ITransport.loseConnection} is called, indicating that the send buffer should be flushed and the connection lost afterwards. Afterwards, C{True}. - disconnected: A C{bool} which is C{False} until the consumer no longer has a place to send bytes, then C{True}. Subclasses must also override the C{startWriting} method. @ivar producer: L{None} if no producer is registered, otherwise the registered producer. @ivar producerPaused: A flag indicating whether the producer is currently paused. @type producerPaused: L{bool} @ivar streamingProducer: A flag indicating whether the producer was registered as a streaming (ie push) producer or not (ie a pull producer). This will determine whether the consumer may ever need to pause and resume it, or if it can merely call C{resumeProducing} on it when buffer space is available. @ivar streamingProducer: C{bool} or C{int} NFctd)a Override in a subclass to cause the reactor to monitor this selectable for write events. This will be called once in C{unregisterProducer} if C{loseConnection} has previously been called, so that the connection can actually close. z!%r did not implement startWritingNotImplementedErrorselfs r startWritingz_ConsumerMixin.startWritingLs""EFFrc|jtd|d|jd|jr|jy||_||_|s|j yy)a" Register to receive data from a producer. This sets this selectable to be a consumer for a producer. When this selectable runs out of data on a write() call, it will ask the producer to resumeProducing(). When the FileDescriptor's internal data buffer is filled, it will ask the producer to pauseProducing(). If the connection is lost, FileDescriptor calls producer's stopProducing() method. If streaming is true, the producer should provide the IPushProducer interface. Otherwise, it is assumed that producer provides the IPullProducer interface. In this case, the producer won't be asked to pauseProducing(), but it has to be careful to write() data only when its resumeProducing() method is called. NzCannot register producer z, because producer z was never unregistered.)producer RuntimeError disconnected stopProducingstreamingProducerresumeProducing)r$r' streamings rregisterProducerz_ConsumerMixin.registerProducerUsc == $#+T]]<      " " $$DM%.D "((*rcfd|_|jr|jr|jyyy)zM Stop consuming data from a producer, without disconnecting. N)r' connected disconnectingr%r#s runregisterProducerz!_ConsumerMixin.unregisterProducerrs- >>d00    1>r) __name__ __module__ __qualname____doc__r'producerPausedr+r%r.r2rrrr!s*$LHNG+: rrc&eZdZdZdedefdZdZy) _LogOwnerz Mixin to help implement L{interfaces.ILoggingContext} for transports which have a protocol, the log prefix of which should also appear in the transport's log prefix. applicationObjectreturnctjj|r|jS|jj S)z Determine the log prefix to use for messages related to C{applicationObject}, which may or may not be an L{interfaces.ILoggingContext} provider. @return: A C{str} giving the log prefix to use. )r ILoggingContext providedBy logPrefix __class__r3)r$r;s r _getLogPrefixz_LogOwner._getLogPrefixs:  % % 0 01B C$..0 0 **333rcy)z Override this method to insert custom logging behavior. Its return value will be inserted in front of every line. It may be called more times than the number of output lines. -r8r#s rr@z_LogOwner.logPrefixs rN)r3r4r5r6objectstrrBr@r8rrr:r:{s 4v 4# 4rr:c8eZdZdZdZdZdZdZdZdZ dZ dZ d'de e jfdZd Zd ed dfd Zd ZdZdZdZdZdej2d dfdZdZdZdZdZd ed dfdZde ed dfdZ!dZ"dZ#dZ$dZ%dZ&d Z'dZ(d!Z)d"Z*d#Z+d$Z,d%Z-d&Z.y)(FileDescriptorz An object which can be operated on by select(). This is an abstract superclass of all objects which may be notified when they are readable or writable; e.g. they have a file-descriptor that is valid to be passed to select(2). rFriNreactorcB|sddlm}|}||_g|_d|_y)z @param reactor: An L{IReactorFDSet} provider which this descriptor will use to get readable and writeable event notifications. If no value is given, the global reactor will be used. r)rIN)twisted.internetrI_tempDataBuffer _tempDataLen)r$rI_reactors r__init__zFileDescriptor.__init__s&  <G ,.rcd|_d|_|j!|jjd|_|j |j y)azThe connection was lost. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. Clean up state here, but make sure to call back up to FileDescriptor. rN)r)r0r'r* stopReading stopWritingr$reasons rconnectionLostzFileDescriptor.connectionLostsL == $ MM ' ' ) DM  rdatar<cXtdtj|jz)a Write as much as possible of the given data, immediately. This is called to invoke the lower-level writing functionality, such as a socket's send() method, or a file's write(); this method returns an integer or an exception. If an integer, it is the number of bytes written (possibly zero); if an exception, it indicates the connection was lost. z#%s does not implement writeSomeDatar"r qualrAr$rWs r writeSomeDatazFileDescriptor.writeSomeDatas&" 1GLL4P P  rcXtdtj|jz)z Called when data is available for reading. Subclasses must override this method. The result will be interpreted in the same way as a result of doWrite(). z%s does not implement doReadrYr#s rdoReadzFileDescriptor.doReads&" *W\\$..-I I  rct|j|jz |jkrEt |j|j|j |_d|_g|_d|_|jr0|jt|j|j}n|j|j}t|ts|dkr|S|xj|z c_|jt|jk(r|j sd|_d|_|j|j:|jr |jr"d|_|jjy|j r|j#S|j$rd|_|j)}|Sy)z Called when data can be written. @return: L{None} on success, an exception or a negative integer on failure. @see: L{twisted.internet.interfaces.IWriteDescriptor.doWrite}. rrNFT)len dataBufferr SEND_LIMITrrLrMr\rr ExceptionrSr'r+r7r,r1_postLoseConnection_writeDisconnecting_writeDisconnected_closeWriteConnection)r$lresults rdoWritezFileDescriptor.doWritesu t $++ - ?+d.B.BDODK#%D !D  ;;""=$++#NOA""4??3A a #q1uH q ;;#doo. .t7H7H!DODK    }}(++0C0C',# --/##//11)) +/'335 rc"tjS)zCalled after a loseConnection(), when all data has been written. Whatever this returns is then returned by doWrite. )r CONNECTION_DONEr#s rrdz"FileDescriptor._postLoseConnection's ###rcyNr8r#s rrgz$FileDescriptor._closeWriteConnection/s rc&|j|yrnrVrTs rwriteConnectionLostz"FileDescriptor.writeConnectionLost3 F#rrUc&|j|yrnrprTs rreadConnectionLostz!FileDescriptor.readConnectionLost7rrrctrnr!r#s rgetHostzFileDescriptor.getHost; !##rctrnr!r#s rgetPeerzFileDescriptor.getPeer?rwrc`t|j|jz|jkDS)ay Determine whether the user-space send buffer for this transport is full or not. When the buffer contains more than C{self.bufferSize} bytes, it is considered full. This might be improved by considering the size of the kernel send buffer and how much of it is free. @return: C{True} if it is full, C{False} otherwise. )r`rarM bufferSizer#s r_isSendBufferFullz FileDescriptor._isSendBufferFullCs'4??#d&7&77$//IIrc|j@|jr3|jr"d|_|jj yyyy)zY Possibly pause a producer, if there is one and the send buffer is full. NT)r'r+r|r7pauseProducingr#s r_maybePauseProducerz"FileDescriptor._maybePauseProducerPsH == $)?)?%%'&*# ,,.(*@ $rct||jr |jry|rZ|jj ||xj t |z c_|j|jyy)a7Reliably write some data. The data is buffered until the underlying file descriptor is ready for writing. If there is more than C{self.bufferSize} data in the buffer and this descriptor has a registered streaming producer, its C{pauseProducing()} method will be called. N) rr0rfrLappendrMr`rr%r[s rwritezFileDescriptor.write\sg ~~!8!8    ' ' -   T *   $ $ &     riovecc|D] }t||jr|r |jry|jj ||D] }|xj t |z c_"|j|jy)a Reliably write a sequence of data. Currently, this is a convenience method roughly equivalent to:: for chunk in iovec: fd.write(chunk) It may have a more efficient implementation at a later time or in a different reactor. As with the C{write()} method, if a buffer size limit is reached and a streaming producer is registered, it will be paused until the buffered data is written to the underlying file descriptor. N) rr0rfrLextendrMr`rr%)r$ris r writeSequencezFileDescriptor.writeSequencems  A Q  ~~Ud.E.E  ##E* (A   Q '  (   " rcD|jr|js|jrS|j|j |j t jtjy|j|jd|_yyy)a[Close the connection at the next available opportunity. Call this to cause this FileDescriptor to lose its connection. It will first write any data that it has buffered. If there is data buffered yet to be written, this method will cause the transport to lose its connection as soon as it's done flushing its write buffer. If you have a producer registered, the connection won't be closed until the producer is finished. Therefore, make sure you unregister your producer when it's finished, or the connection will never close. rQN) r0r1rfrRrSrVr Failurer rlr%r#s rloseConnectionzFileDescriptor.loseConnectionsx >>$"4"4&&  "  "##GOOD4H4H$IJ  "!!#%&"#5>rc2d|_|jy)NT)rer%r#s rloseWriteConnectionz"FileDescriptor.loseWriteConnections#'  rc:|jj|y)zStop waiting for read availability. Call this to remove this selectable from being notified when it is ready for reading. N)rI removeReaderr#s rrRzFileDescriptor.stopReading !!$'rc:|jj|y)zStop waiting for write availability. Call this to remove this selectable from being notified when it is ready for writing. N)rI removeWriterr#s rrSzFileDescriptor.stopWritingrrc:|jj|y)z$Start waiting for read availability.N)rI addReaderr#s r startReadingzFileDescriptor.startReadings t$rc:|jj|y)zStart waiting for write availability. Call this to have this FileDescriptor be notified whenever it is ready for writing. N)rI addWriterr#s rr%zFileDescriptor.startWritings t$ricD|j|jy)zStop consuming data. This is called when a producer has lost its connection, to tell the consumer to go lose its connection (and break potential circular references). N)r2rr#s r stopConsumingzFileDescriptor.stopConsumings ! rcX|jr|js|jyyyrn)r0r1rr#s rr,zFileDescriptor.resumeProducings$ >>$"4"4    #5>rc$|jyrn)rRr#s rr~zFileDescriptor.pauseProducings rc$|jyrn)rr#s rr*zFileDescriptor.stopProducings rcy)zFile Descriptor number for select(). This method must be overridden or assigned in subclasses to indicate a valid file descriptor for the operating system. r8r#s rfilenozFileDescriptor.filenos rrn)/r3r4r5r6r0r)r1rerfrarrbrr IReactorFDSetrOrVrr\r^rjrdrgrqr rrtrvryr|rrrrrrrRrSrr%r'r{rr,r~r*rr8rrrHrHsILMJ FJ )A)A B $  %  D    9v$ $$$T$$$ J / % D "8E?t4'2((%%HJ rrHaddrfamilyr<c@t|tr |jd}|tk(r|j ddd}n,|t k(r|jddk7rytd| t||y #t$rYywxYw#ttf$rYywxYw) a> Determine whether the given string represents an IP address of the given family; by default, an IPv4 address. @param addr: A string which may or may not be the decimal dotted representation of an IPv4 address. @param family: The address family to test for; one of the C{AF_*} constants from the L{socket} module. (This parameter has only been available since Twisted 17.1.0; previously L{isIPAddress} could only test for IPv4 addresses.) @return: C{True} if C{addr} represents an IPv4 address, C{False} otherwise. asciiF%rQr.zunknown address family T) rrdecodeUnicodeDecodeErrorrsplitrcount ValueErrorrOSError)rrs r isIPAddressrs$ ;;w'Dzz#q!!$ 7  ::c?a 26*=>> &$ )"  $  s#A</ B < BB BBc"t|tS)a2 Determine whether the given string represents an IPv6 address. @param addr: A string which may or may not be the hex representation of an IPv6 address. @type addr: C{str} @return: C{True} if C{addr} represents an IPv6 address, C{False} otherwise. @rtype: C{bool} )rr)rs r isIPv6Addressrs tX &&r)rHrrN)$r6socketrrrtypingrrrzope.interfacer rKr r twisted.pythonr r twisted.python.compatrrrrr>r: IPushProducerIReadWriteDescriptor IConsumer ITransportIHalfCloseableDescriptorrHrFintboolrr__all__r8rrrs  0/++&-+0.:W W t Z ' '()8 ##'' E^YEEP *1%c%3%T%P ' ' ' =r