o
    ¯bW"  ã                   @   sœ   d Z ddlmZ ddlmZmZ dZdZG dd„ dƒZG dd	„ d	ƒZ	dd
l
Z
dd
lZdd
lZdd
lZeeƒG dd„ deƒƒZdZeeƒG dd„ deƒƒZd
S )zˆ
Implements a simple polling interface for file descriptors that don't work with
select() - this is pretty much only useful on Windows.
é    )Úimplementer)Ú	IConsumerÚIPushProducerg•Ö&è.>gš™™™™™¹?c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_PollableResourceTc                 C   ó
   d| _ d S ©NT©Úactive©Úself© r   ú?/usr/lib/python3/dist-packages/twisted/internet/_pollingfile.pyÚactivate   ó   
z_PollableResource.activatec                 C   r   ©NFr   r
   r   r   r   Ú
deactivate   r   z_PollableResource.deactivateN)Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r   r      s    r   c                   @   sT   e 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S )Ú_PollingTimerc                 C   s"   || _ g | _d | _t| _d| _d S r   )ÚreactorÚ
_resourcesÚ
_pollTimerÚMAX_TIMEOUTÚ_currentTimeoutÚ_paused)r   r   r   r   r   Ú__init__    s
   
z_PollingTimer.__init__c                 C   s   | j  |¡ |  ¡  d S ©N)r   ÚappendÚ_checkPollingState)r   Úresr   r   r   Ú_addPollableResource'   s   z"_PollingTimer._addPollableResourcec                 C   s,   | j D ]}|jr|  ¡   d S q|  ¡  d S r   )r   r	   Ú_startPollingÚ_stopPolling)r   Úresourcer   r   r   r   +   s   
þz _PollingTimer._checkPollingStatec                 C   s   | j d u r|  ¡ | _ d S d S r   )r   Ú_rescheduler
   r   r   r   r"   3   s   
ÿz_PollingTimer._startPollingc                 C   s"   | j d ur| j  ¡  d | _ d S d S r   )r   Úcancelr
   r   r   r   r#   7   s   


þz_PollingTimer._stopPollingc                 C   r   r   )r   r
   r   r   r   Ú_pause<   r   z_PollingTimer._pausec                 C   s   d| _ |  ¡  d S r   )r   r   r
   r   r   r   Ú_unpause?   s   z_PollingTimer._unpausec                 C   s   | j s| j | j| j¡S d S r   )r   r   Ú	callLaterr   Ú
_pollEventr
   r   r   r   r%   C   s   ÿz_PollingTimer._reschedulec                 C   sŽ   d}g }| j D ]}|jr|| ¡ 7 }|jr| |¡ q| j}|r.| j|d  }|tk r-t}n| jd }|tkr9t}|| _|rE|  ¡ | _d S d S )Ng        g      ð?g       @)	r   r	   Ú	checkWorkr   r   ÚMIN_TIMEOUTr   r%   r   )r   Ú	workUnitsÚ	anyActiver$   Ú
newTimeoutr   r   r   r*   G   s*   

€€
ÿz_PollingTimer._pollEventN)r   r   r   r   r!   r   r"   r#   r'   r(   r%   r*   r   r   r   r   r      s    r   Nc                   @   sD   e 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S )Ú_PollableReadPipec                 C   s   || _ || _|| _d S r   )ÚpipeÚreceivedCallbackÚlostCallback)r   r1   r2   r3   r   r   r   r   j   s   
z_PollableReadPipe.__init__c           	      C   s�   d}g }	 zt  | jd¡\}}}|sW nt | j|d ¡\}}| |¡ W n tjy0   d}Y nw qd |¡}|r>|  	|¡ |rD|  
¡  t|ƒS )Nr   é   ó    )Ú	win32pipeÚPeekNamedPiper1   Ú	win32fileÚReadFiler   Úwin32apiÚerrorÚjoinr2   ÚcleanupÚlen)	r   ÚfinishedÚfullDataReadÚbufferÚbytesToReadÚresultÚhrÚdataÚdataBufr   r   r   r+   p   s(   þø

z_PollableReadPipe.checkWorkc                 C   s   |   ¡  |  ¡  d S r   )r   r3   r
   r   r   r   r=   ‡   s   z_PollableReadPipe.cleanupc                 C   s*   z	t  | j¡ W d S  tjy   Y d S w r   )r:   ÚCloseHandler1   Ú
pywintypesr;   r
   r   r   r   Úclose‹   s
   þz_PollableReadPipe.closec                 C   ó   |   ¡  d S r   )rI   r
   r   r   r   ÚstopProducing’   ó   z_PollableReadPipe.stopProducingc                 C   rJ   r   )r   r
   r   r   r   ÚpauseProducing•   rL   z _PollableReadPipe.pauseProducingc                 C   rJ   r   )r   r
   r   r   r   ÚresumeProducing˜   rL   z!_PollableReadPipe.resumeProducingN)
r   r   r   r   r+   r=   rI   rK   rM   rN   r   r   r   r   r0   h   s    r0   i   c                   @   s\   e 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S )Ú_PollableWritePipec                 C   sZ   d| _ d | _d| _d| _g | _|| _|| _zt |tj	d d ¡ W d S  t
jy,   Y d S w )NFr   )ÚdisconnectingÚproducerÚproducerPausedÚstreamingProducerÚoutQueueÚ	writePiper3   r6   ÚSetNamedPipeHandleStateÚPIPE_NOWAITrH   r;   )r   rU   r3   r   r   r   r   ¡   s   

ÿþz_PollableWritePipe.__init__c                 C   r   r   )rP   r
   r   r   r   rI   ±   r   z_PollableWritePipe.closec                 C   s"   | j d urd| _| j  ¡  d S d S r   )rQ   rR   rM   r
   r   r   r   Ú
bufferFull´   s   
þz_PollableWritePipe.bufferFullc                 C   s0   | j d ur| jr| jrd| j _| j  ¡  dS dS )NFT)rQ   rS   rR   rN   r
   r   r   r   ÚbufferEmpty¹   s   
ÿÿ
z_PollableWritePipe.bufferEmptyc                 C   sN   | j durtd|| j f ƒ‚| js| ¡  dS || _ || _|s%| ¡  dS dS )aw  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(). A producer should implement the IProducer
        interface.

        FileDescriptor provides some infrastructure for producer methods.
        NzHCannot register producer %s, because producer %s was never unregistered.)rQ   ÚRuntimeErrorr	   rK   rS   rN   )r   rQ   Ú	streamingr   r   r   ÚregisterProducerÄ   s   

ÿÿÿz#_PollableWritePipe.registerProducerc                 C   s
   d| _ dS )z;Stop consuming data from a producer, without disconnecting.N)rQ   r
   r   r   r   ÚunregisterProducerÛ   s   
z%_PollableWritePipe.unregisterProducerc                 C   s:   |   ¡  zt | j¡ W n
 tjy   Y nw |  ¡  d S r   )r   r:   rG   rU   rH   r;   r3   r
   r   r   r   ÚwriteConnectionLostß   s   þz&_PollableWritePipe.writeConnectionLostc                 C   s&   t tt|ƒv rtdƒ‚| j |¡ dS )zõ
        Append a C{list} or C{tuple} of bytes to the output buffer.

        @param seq: C{list} or C{tuple} of C{str} instances to be appended to
            the output buffer.

        @raise TypeError: If C{seq} contains C{unicode}.
        ú%Unicode not allowed in output buffer.N)ÚstrÚmapÚtypeÚ	TypeErrorrT   Úextend)r   Úseqr   r   r   ÚwriteSequenceè   s   	z _PollableWritePipe.writeSequencec                 C   sL   t |tƒr	tdƒ‚| jrdS | j |¡ ttt| jƒƒt	kr$|  
¡  dS dS )zÞ
        Append some bytes to the output buffer.

        @param data: C{str} to be appended to the output buffer.
        @type data: C{str}.

        @raise TypeError: If C{data} is C{unicode} instead of C{str}.
        r_   N)Ú
isinstancer`   rc   rP   rT   r   Úsumra   r>   ÚFULL_BUFFER_SIZErX   )r   rE   r   r   r   Úwriteõ   s   
	ÿz_PollableWritePipe.writec                 C   sô   d}| j s)| jr|  ¡  dS z
t | jdd ¡ W n tjy(   |  ¡  | Y S w | j rk| j  d¡}d}zt | j|d ¡\}}W n t	jyO   |  ¡  Y |S w ||7 }t
|ƒ|krh| j  d||d … ¡ 	 |S | j s,|  ¡ }|sx| jrx|  ¡  |S )Nr   r5   )rT   rP   r^   r8   Ú	WriteFilerU   rH   r;   Úpopr:   r>   ÚinsertrY   )r   ÚnumBytesWrittenrE   ÚerrCodeÚnBytesWrittenÚresumedr   r   r   r+     s>   þóî
z_PollableWritePipe.checkWorkN)r   r   r   r   rI   rX   rY   r\   r]   r^   rf   rj   r+   r   r   r   r   rO   Ÿ   s    	rO   )Ú__doc__Úzope.interfacer   Útwisted.internet.interfacesr   r   r,   r   r   r   rH   r:   r8   r6   r0   ri   rO   r   r   r   r   Ú<module>   s    
E3