U
    
W[:3  ã                   @   sª   d Z ddlmZmZmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlZddlmZ dd	lmZ dd
lmZ eejejejejƒG dd„ deeƒƒZdgZdS )z
Abstract file handle class
é    )ÚmainÚerrorÚ
interfaces)Ú_ConsumerMixinÚ	_LogOwner)Úfailure)Úunicode)ÚimplementerN)ÚERROR_HANDLE_EOF)ÚERROR_IO_PENDING)Úiocpsupportc                   @   s^  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdZdZdZdZdZdZdZdd„ Zd d!„ Zd"d#„ Zd$d%„ Z d&d'„ Z!d(d)„ Z"d*d+„ Z#d,d-„ Z$d.d/„ Z%d0d1„ Z&d2d3„ Z'd4d5„ Z(dZ)dZ*dZ+d6Z,d7Z-dHd8d9„Z.d:d;„ Z/d<d=„ Z0e1 2e3j4¡fd>d?„Z5d@dA„ Z6dBdC„ Z7dDdE„ Z8dFdG„ Z9dS )IÚ
FileHandlez<
    File handle that can read and write asynchronously
    é   i   FTr   Nc                 C   s4   | j  | ¡ | js0| js0d| _| j  d| j¡| _d S ©NTr   )ÚreactorÚaddActiveHandleÚ_readScheduledÚreadingÚ	callLaterÚ_resumeReading©Úself© r   úG/usr/lib/python3/dist-packages/twisted/internet/iocpreactor/abstract.pyÚstartReading&   s    ÿzFileHandle.startReadingc                 C   s    | j r| j  ¡  d | _ d| _d S ©NF)r   Úcancelr   r   r   r   r   ÚstopReading.   s    
zFileHandle.stopReadingc                 C   s    d | _ |  ¡ r| js|  ¡  d S ©N)r   Ú_dispatchDataÚ_readScheduledInOSÚdoReadr   r   r   r   r   5   s    zFileHandle._resumeReadingc                 C   sâ   | j s| jS | j }|| j }| j|k rR|  | j| j ¡ |  jd7  _| jsdS q|| j }|rx|  | j| d|… ¡ | jrÐ| jt| jƒ }||| j k r¦| jd= n*||krÐt| jƒ| jk rÐ| j 	t
| jƒ¡ d| _d| _ | jS )zt
        Dispatch previously read data. Return True if self.reading and we don't
        have any more data
        é   Fr   éÿÿÿÿ)Ú	_readSizer   ÚreadBufferSizeÚ_readNextBufferÚdataReceivedÚ_readBuffersÚdynamicReadBuffersÚlenÚmaxReadBuffersÚappendÚ	bytearray)r   ÚsizeZfull_buffersZ	remainderZtotal_buffer_sizer   r   r   r   ;   s.    



ÿzFileHandle._dispatchDatac                 C   s    d| _ |  |||¡r|  ¡  d S r   )r    Ú_handleReadr!   ©r   ÚrcÚdataÚevtr   r   r   Ú_cbReadZ   s    zFileHandle._cbReadc                 C   s    | j r
dS |s|r |tjtfkrB| j | ¡ |  t t	j
¡¡ dS |rr|  t t dtj |d¡|f ¡¡¡ dS | jdks€t‚| jdksŽt‚|| _|  ¡ S dS )zA
        Returns False if we should stop reading for now
        Fzread error -- %s (%s)Úunknownr   N)ÚdisconnectedÚerrnoZ
WSAEDISCONr
   r   ÚremoveActiveHandleÚreadConnectionLostr   ÚFailurer   ÚCONNECTION_DONEÚconnectionLostr   ÚConnectionLostÚ	errorcodeÚgetr$   ÚAssertionErrorr&   r   r0   r   r   r   r/   `   s"    ÿÿzFileHandle._handleReadc                 C   sP   t  | j| ¡}| j |_}|  ||¡\}}|r6|tkr>d| _n|  |||¡ d S ©NT)	Ú_iocpÚEventr4   r(   ÚbuffÚreadFromHandler   r    r/   )r   r3   rD   r1   ZnumBytesReadr   r   r   r!   y   s    zFileHandle.doReadc                 C   s
   t ƒ ‚d S r   ©ÚNotImplementedError)r   Zbufflistr3   r   r   r   rE   …   s    zFileHandle.readFromHandlec                 C   s   t ‚d S r   rF   ©r   r2   r   r   r   r'   ‰   s    zFileHandle.dataReceivedc                 C   s   |   |¡ d S r   ©r<   ©r   Úreasonr   r   r   r9      s    zFileHandle.readConnectionLostó    i   c                 C   s   d| _ |  ¡  d S rA   )Ú_writeDisconnectingÚstartWritingr   r   r   r   ÚloseWriteConnection›   s    zFileHandle.loseWriteConnectionc                 C   s   d S r   r   r   r   r   r   Ú_closeWriteConnection    s    z FileHandle._closeWriteConnectionc                 C   s   |   |¡ d S r   rI   rJ   r   r   r   ÚwriteConnectionLost¥   s    zFileHandle.writeConnectionLostc                 C   s.   | j  | ¡ d| _| js*| j  d| j¡| _d S r   )r   r   ÚwritingÚ_writeScheduledr   Ú_resumeWritingr   r   r   r   rN   ª   s    ÿzFileHandle.startWritingc                 C   s    | j r| j  ¡  d | _ d| _d S r   )rS   r   rR   r   r   r   r   ÚstopWriting²   s    
zFileHandle.stopWritingc                 C   s   d | _ |  ¡  d S r   )rS   ÚdoWriter   r   r   r   rT   ¹   s    zFileHandle._resumeWritingc                 C   s   |   |||¡r|  ¡  d S r   )Ú_handleWriterV   ©r   r1   ZnumBytesWrittenr3   r   r   r   Ú_cbWrite¾   s    zFileHandle._cbWritec                 C   sÚ   | j s| jrdS |r@|  t t dtj 	|d¡|f ¡¡¡ dS |  j
|7  _
| j
t| jƒkrÒ| jsÒd| _d| _
|  ¡  | jdk	r | jrŽ| jr d| _| j ¡  n.| jrº|  t tj¡¡ n| jrÎd| _|  ¡  dS dS dS )zA
        Returns false if we should stop writing for now
        Fzwrite error -- %s (%s)r5   rL   r   NT)r6   Ú_writeDisconnectedr<   r   r:   r   r=   r7   r>   r?   Úoffsetr*   Ú
dataBufferÚ_tempDataLenrU   ÚproducerÚstreamingProducerÚproducerPausedÚresumeProducingÚdisconnectingr   r;   rM   rP   rX   r   r   r   rW   Ã   s4    ÿÿÿzFileHandle._handleWritec                 C   s´   t | jƒ| j | jk rF| j| jd … d | j¡ | _d| _g | _d| _t | j	| ¡}| jrzt
| jƒ}|| jd …  |_}n| j |_}|  ||¡\}}|r°|tkr°|  |||¡ d S )NrL   r   )r*   r\   r[   Ú
SEND_LIMITÚjoinÚ_tempDataBufferr]   rB   rC   rY   Ú
memoryviewrD   ÚwriteToHandler   rW   )r   r3   ZsendViewrD   r1   r2   r   r   r   rV   ë   s    
ÿ
zFileHandle.doWritec                 C   s
   t ƒ ‚d S r   rF   )r   rD   r3   r   r   r   rg     s    zFileHandle.writeToHandlec                 C   s†   t |tƒrtdƒ‚| jr| jr"dS |r‚| j |¡ |  jt|ƒ7  _| j	dk	rz| j
rzt| jƒ| j | jkrzd| _| j	 ¡  |  ¡  dS )zpReliably write some data.

        The data is buffered until his file descriptor is ready for writing.
        úData must not be unicodeNT)Ú
isinstancer   Ú	TypeErrorÚ	connectedrZ   re   r,   r]   r*   r^   r_   r\   ÚwriteBufferSizer`   ÚpauseProducingrN   rH   r   r   r   Úwrite  s    
ÿ
zFileHandle.writec                 C   sš   |D ]}t |tƒrtdƒ‚q| jr,|r,| jr0d S | j |¡ |D ]}|  jt|ƒ7  _q@| j	d k	rŽ| j
rŽt| jƒ| j | jkrŽd| _| j	 ¡  |  ¡  d S )Nrh   T)ri   r   rj   rk   rZ   re   Úextendr]   r*   r^   r_   r\   rl   r`   rm   rN   )r   ZiovecÚir   r   r   ÚwriteSequence  s    


zFileHandle.writeSequenceZUninitializedi   c                 C   s4   |sddl m} || _g | _d| _t| jƒg| _d S )Nr   )r   )Útwisted.internetr   re   r]   r-   r%   r(   )r   r   r   r   r   Ú__init__3  s    zFileHandle.__init__c                 C   sF   d| _ d| _| jdk	r&| j ¡  d| _|  ¡  |  ¡  | j | ¡ dS )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)r6   rk   r^   ÚstopProducingr   rU   r   r8   rJ   r   r   r   r<   <  s    

zFileHandle.connectionLostc                 C   s   dS )Nr#   r   r   r   r   r   ÚgetFileHandleR  s    zFileHandle.getFileHandlec                 C   sF   | j rB| jsB| jr,|  ¡  | j |  |¡ n|  ¡  |  ¡  d| _dS )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)rk   rb   rZ   r   rU   r<   rN   )r   Z	_connDoner   r   r   ÚloseConnectionV  s    zFileHandle.loseConnectionc                 C   s   |   ¡  |  ¡  dS )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)ZunregisterProducerrv   r   r   r   r   ÚstopConsumings  s    zFileHandle.stopConsumingc                 C   s   | j r| js|  ¡  d S r   )rk   rb   r   r   r   r   r   ra     s    zFileHandle.resumeProducingc                 C   s   |   ¡  d S r   )r   r   r   r   r   rm   †  s    zFileHandle.pauseProducingc                 C   s   |   ¡  d S r   )rv   r   r   r   r   rt   Š  s    zFileHandle.stopProducing)N):Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r%   r   r)   r&   r$   r   r    r   r   r   r   r4   r/   r!   rE   r'   r9   r\   r[   rR   rS   rM   rZ   rl   rO   rP   rQ   rN   rU   rT   rY   rW   rV   rg   rn   rq   rk   r6   rb   Zlogstrrc   rs   r<   ru   r   r:   r   r;   rv   rw   ra   rm   rt   r   r   r   r   r      sf   (
	r   )r{   rr   r   r   r   Ztwisted.internet.abstractr   r   Ztwisted.pythonr   Ztwisted.python.compatr   Zzope.interfacer	   r7   Z"twisted.internet.iocpreactor.constr
   r   Ztwisted.internet.iocpreactorr   rB   ZIPushProducerZ	IConsumerZ
ITransportZIHalfCloseableDescriptorr   Ú__all__r   r   r   r   Ú<module>   s"   
 ÿ  y