o b3@sdZddlZddlmZddlmZmZmZddlm Z m Z m Z ddl m ZddlmZmZddlmZeejejejejGd d d e e Zd gZdS) z Abstract file handle class N) implementer)error interfacesmain)_ConsumerMixin_dataMustBeBytes _LogOwner) iocpsupport)ERROR_HANDLE_EOFERROR_IO_PENDING)failurec@sneZdZdZdZdZdZdZdZdZ dZ dZ dd Z d d Z d d ZddZddZddZddZddZddZddZdZdZdZdZdZdZdZddZd d!Zd"d#Zd$d%Z d&d'Z!d(d)Z"d*d+Z#d,d-Z$d.d/Z%d0d1Z&d2d3Z'd4d5Z(dZ)dZ*dZ+d6Z,d7Z-dLd8d9Z.d:d;Z/dd?Z5d@dAZ6dBdCZ7dDdEZ8dFdGZ9dHdIZ:dJdKZ;dS)M FileHandlez< File handle that can read and write asynchronously iFTrNcC<|j||js|jsd|_|jd|j|_dSdSdSNTr)reactoraddActiveHandle_readScheduledreading callLater_resumeReadingselfrG/usr/lib/python3/dist-packages/twisted/internet/iocpreactor/abstract.py startReading(s  zFileHandle.startReadingcC |jr |jd|_d|_dSNF)rcancelrrrrr stopReading.  zFileHandle.stopReadingcCs(d|_|r|js|dSdSdSN)r _dispatchData_readScheduledInOSdoReadrrrrr4s zFileHandle._resumeReadingcCs|js|jS|j}||j}|j|kr-||j|j|jd7_|js(dS|j|ks||j}|r@||j|d||jrl|jt|j}|||jkrW|jd=n||krlt|j|jkrl|j t |jd|_d|_|jS)zt Dispatch previously read data. Return True if self.reading and we don't have any more data Fr) _readSizerreadBufferSize_readNextBuffer dataReceived _readBuffersdynamicReadBufferslenmaxReadBuffersappend bytearray)rsize full_buffers remaindertotal_buffer_sizerrrr"9s.    zFileHandle._dispatchDatacCs$d|_||||r|dSdSr)r# _handleReadr$rrcdataevtrrr_cbReadYs zFileHandle._cbReadc Cs|jrdS|s |r|tjtfvr!|j||tt j dS|r9| tt dtj|d|fdS|jdks@J|jdksGJ||_|S)zA Returns False if we should stop reading for now Fzread error -- %s (%s)unknownr) disconnectederrno WSAEDISCONr rremoveActiveHandlereadConnectionLostr FailurerCONNECTION_DONEconnectionLostrConnectionLost errorcodegetr'r)r"r6rrrr5^s* zFileHandle._handleReadcCsRt|j|}|j|_}|||\}}|r|tkr d|_dS||||dSNT) _iocpEventr:r+buffreadFromHandler r#r5)rr9rJr7 numBytesReadrrrr${s    zFileHandle.doReadcCtr!NotImplementedError)rbufflistr9rrrrKzFileHandle.readFromHandlecCstr!rNrr8rrrr*zFileHandle.dataReceivedcC||dSr!rCrreasonrrrr@szFileHandle.readConnectionLosticCsd|_|dSrG)_writeDisconnecting startWritingrrrrloseWriteConnection zFileHandle.loseWriteConnectioncCsdSr!rrrrr_closeWriteConnectionsz FileHandle._closeWriteConnectioncCrTr!rUrVrrrwriteConnectionLostszFileHandle.writeConnectionLostcCrr)rr_writeScheduledwritingr_resumeWritingrrrrrZs  zFileHandle.startWritingcCrr)r_rr`rrrr stopWritingr zFileHandle.stopWritingcCsd|_|dSr!)r_doWriterrrrrar\zFileHandle._resumeWritingcCs||||r |dSdSr!) _handleWritercrr7numBytesWrittenr9rrr_cbWrites zFileHandle._cbWritec Cs|js|jrdS|r |ttdtj |d|fdS|j |7_ |j t |j krk|j skd|_ d|_ ||jdurQ|jrG|jrQd|_|jdS|jr_|ttjdS|jrid|_|dSdS)zA Returns false if we should stop writing for now Fzwrite error -- %s (%s)r;rXrNT)r<_writeDisconnectedrCr rArrDr=rErFoffsetr- dataBuffer _tempDataLenrbproducerstreamingProducerproducerPausedresumeProducing disconnectingrrBrYr]rerrrrdsD    zFileHandle._handleWritecCst|j|j|jkr#|j|jdd|j|_d|_g|_d|_t|j |}|jr=t |j}||jd|_ }n|j|_ }| ||\}}|rZ|t kr\||||dSdSdS)NrXr)r-rjri SEND_LIMITjoin_tempDataBufferrkrHrIrg memoryviewrJ writeToHandler rd)rr9sendViewrJr7r8rrrrcs    zFileHandle.doWritecCrMr!rN)rrJr9rrrrurQzFileHandle.writeToHandlecCst||jr |jr dS|r>|j||jt|7_|jdur8|jr8t|j |j|j kr8d|_ |j | dSdS)zpReliably write some data. The data is buffered until his file descriptor is ready for writing. NT)r connectedrhrsr/rkr-rlrmrjwriteBufferSizernpauseProducingrZrRrrrwrites    zFileHandle.writecCs|D]}t|q|jr|r|jrdS|j||D] }|jt|7_q|jdurB|jrBt|j |j|j krBd|_ |j | dSrG)rrwrhrsextendrkr-rlrmrjrxrnryrZ)riovecirrr writeSequences    zFileHandle.writeSequence UninitializedicCs4|sddlm}||_g|_d|_t|jg|_dS)Nr)r)twisted.internetrrsrkr0r(r+)rrrrr__init__'s  zFileHandle.__init__cCsFd|_d|_|jdur|jd|_|||j|dS)a The 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. TFN)r<rwrl stopProducingrrbrr?rVrrrrC/s   zFileHandle.connectionLostcCsdS)Nr&rrrrr getFileHandleDrSzFileHandle.getFileHandlecCsP|jr$|js&|jr||j||dS||d|_dSdSdS)ad 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. r%N)rwrprhrrbrCrZ)r _connDonerrrloseConnectionGs  zFileHandle.loseConnectioncCs||dS)z Stop 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)unregisterProducerrrrrr stopConsumingcs zFileHandle.stopConsumingcCs |jr |js|dSdSdSr!)rwrprrrrrrops  zFileHandle.resumeProducingcC |dSr!)rrrrrryt zFileHandle.pauseProducingcCrr!)rrrrrrwrzFileHandle.stopProducingcCrMr!rNrrrrgetHostzzFileHandle.getHostcCrMr!rNrrrrgetPeer~rzFileHandle.getPeerr!)<__name__ __module__ __qualname____doc__r.r(rr,r)r'rr#rrrr"r:r5r$rKr*r@rjrir`r_rYrhrxr[r]r^rZrbrargrdrcrurzr~rwr<rplogstrrqrrCrr rArrBrrroryrrrrrrrr sl  -   r )rr=zope.interfacerrrrrtwisted.internet.abstractrrrtwisted.internet.iocpreactorr rH"twisted.internet.iocpreactor.constr r twisted.pythonr IPushProducer IConsumer ITransportIHalfCloseableDescriptorr __all__rrrrs$    l