a
    ƒcLš  ã                   @   s$  d Z dZddlZddlZddlZddlZddlZddlZddlZzddl	Z	W n e
yb   dZ	Y n0 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ dd„ ZG dd„ dejƒZG dd„ dejejƒZG dd„ deƒZG dd„ deƒZdS )zÄEvent loop using a selector and related classes.

A selector is a "notify-when-ready" multiplexer.  For a subclass which
also includes support for signal handling, see the unix_events sub-module.
)ÚBaseSelectorEventLoopé    Né   )Úbase_events)Ú	constants)Úevents)Úfutures)Ú	protocols)Ússlproto)Ú
transports)Útrsock)Úloggerc                 C   s6   z|   |¡}W n ty"   Y dS 0 t|j|@ ƒS d S ©NF)Úget_keyÚKeyErrorÚboolr   )ÚselectorÚfdÚeventÚkey© r   úh/home/jack/SDK/XuTao-WR3000-OpenWrt-21.02/build_dir/hostpkg/Python-3.9.16/Lib/asyncio/selector_events.pyÚ_test_selector_event    s
    r   c                       s’  e Zd ZdZdS‡ fdd„	ZdTdddœdd„ZdUddddejd	œd
d„ZdVdd„Z	‡ fdd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdddejfdd„Zdddejfdd„Zddejf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/d0„ Zd1d2„ Zd3d4„ ZdWd5d6„Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dXdEdF„Z%dGdH„ Z&dIdJ„ Z'dKdL„ Z(dMdN„ Z)dOdP„ Z*dQdR„ Z+‡  Z,S )Yr   zJSelector event loop.

    See events.EventLoop for API specification.
    Nc                    sF   t ƒ  ¡  |d u rt ¡ }t d|jj¡ || _|  	¡  t
 ¡ | _d S )NzUsing selector: %s)ÚsuperÚ__init__Ú	selectorsÚDefaultSelectorr   ÚdebugÚ	__class__Ú__name__Ú	_selectorÚ_make_self_pipeÚweakrefÚWeakValueDictionaryÚ_transports)Úselfr   ©r   r   r   r   1   s    
zBaseSelectorEventLoop.__init__©ÚextraÚserverc                C   s   t | |||||ƒS ©N)Ú_SelectorSocketTransport)r$   ÚsockÚprotocolÚwaiterr'   r(   r   r   r   Ú_make_socket_transport;   s    
ÿz,BaseSelectorEventLoop._make_socket_transportF)Úserver_sideÚserver_hostnamer'   r(   Ússl_handshake_timeoutc             	   C   s0   t j| ||||||	d}
t| ||
||d |
jS )N)r1   r&   )r	   ÚSSLProtocolr*   Ú_app_transport)r$   Úrawsockr,   Ú
sslcontextr-   r/   r0   r'   r(   r1   Ússl_protocolr   r   r   Ú_make_ssl_transport@   s    ýÿz)BaseSelectorEventLoop._make_ssl_transportc                 C   s   t | |||||ƒS r)   )Ú_SelectorDatagramTransport)r$   r+   r,   Úaddressr-   r'   r   r   r   Ú_make_datagram_transportM   s    ÿz.BaseSelectorEventLoop._make_datagram_transportc                    sL   |   ¡ rtdƒ‚|  ¡ rd S |  ¡  tƒ  ¡  | jd urH| j ¡  d | _d S )Nz!Cannot close a running event loop)Ú
is_runningÚRuntimeErrorÚ	is_closedÚ_close_self_piper   Úcloser   ©r$   r%   r   r   r?   R   s    


zBaseSelectorEventLoop.closec                 C   sB   |   | j ¡ ¡ | j ¡  d | _| j ¡  d | _|  jd8  _d S )Nr   )Ú_remove_readerÚ_ssockÚfilenor?   Ú_csockÚ_internal_fdsr@   r   r   r   r>   ]   s    

z&BaseSelectorEventLoop._close_self_pipec                 C   sN   t  ¡ \| _| _| j d¡ | j d¡ |  jd7  _|  | j ¡ | j¡ d S )NFr   )	ÚsocketÚ
socketpairrB   rD   ÚsetblockingrE   Ú_add_readerrC   Ú_read_from_selfr@   r   r   r   r    e   s
    z%BaseSelectorEventLoop._make_self_pipec                 C   s   d S r)   r   ©r$   Údatar   r   r   Ú_process_self_datam   s    z(BaseSelectorEventLoop._process_self_datac                 C   sT   z"| j  d¡}|sW qP|  |¡ W q  ty8   Y q Y q  tyL   Y qPY q 0 q d S )Ni   )rB   ÚrecvrM   ÚInterruptedErrorÚBlockingIOErrorrK   r   r   r   rJ   p   s    z%BaseSelectorEventLoop._read_from_selfc                 C   sL   | j }|d u rd S z| d¡ W n& tyF   | jrBtjddd Y n0 d S )Nó    z3Fail to write a null byte into the self-pipe socketT©Úexc_info)rD   ÚsendÚOSErrorÚ_debugr   r   )r$   Zcsockr   r   r   Ú_write_to_self|   s    þz$BaseSelectorEventLoop._write_to_selféd   c              
   C   s"   |   | ¡ | j||||||¡ d S r)   )rI   rC   Ú_accept_connection)r$   Úprotocol_factoryr+   r5   r(   Úbacklogr1   r   r   r   Ú_start_servingŽ   s    
þz$BaseSelectorEventLoop._start_servingc                 C   s  t |ƒD ]ú}z0| ¡ \}}	| jr0t d||	|¡ | d¡ W nž tttfyX   Y  d S  t	yÚ }
 zl|
j
t
jt
jt
jt
jfv rÄ|  d|
t |¡dœ¡ |  | ¡ ¡ |  tj| j||||||¡ n‚ W Y d }
~
qd }
~
0 0 d|	i}|  ||||||¡}|  |¡ qd S )Nz#%r got a new connection from %r: %rFz&socket.accept() out of system resource)ÚmessageÚ	exceptionrF   Úpeername)ÚrangeÚacceptrV   r   r   rH   rP   rO   ÚConnectionAbortedErrorrU   ÚerrnoZEMFILEZENFILEZENOBUFSZENOMEMÚcall_exception_handlerr   ÚTransportSocketrA   rC   Ú
call_laterr   ÚACCEPT_RETRY_DELAYr\   Ú_accept_connection2Úcreate_task)r$   rZ   r+   r5   r(   r[   r1   Ú_ÚconnÚaddrÚexcr'   ra   r   r   r   rY   •   s@    ÿÿýý
þz(BaseSelectorEventLoop._accept_connectionc              
   Ã   sô   d }d }zr|ƒ }|   ¡ }	|r8| j||||	d|||d}n| j|||	||d}z|	I d H  W n tyv   | ¡  ‚ Y n0 W nt ttfy’   ‚ Y n^ tyî }
 zF| jrÚd|
dœ}|d urÀ||d< |d urÐ||d< |  |¡ W Y d }
~
n
d }
~
0 0 d S )NT)r-   r/   r'   r(   r1   )r-   r'   r(   z3Error on transport creation for incoming connection)r]   r^   r,   Ú	transport)	Úcreate_futurer7   r.   ÚBaseExceptionr?   Ú
SystemExitÚKeyboardInterruptrV   rd   )r$   rZ   rk   r'   r5   r(   r1   r,   rn   r-   rm   Úcontextr   r   r   rh   Á   s@    ýþýz)BaseSelectorEventLoop._accept_connection2c              
   C   sŠ   |}t |tƒsHzt| ¡ ƒ}W n( tttfyF   td|›ƒd ‚Y n0 z| j| }W n tyh   Y n0 | ¡ s†t	d|›d|›ƒ‚d S )NzInvalid file object: zFile descriptor z is used by transport )
Ú
isinstanceÚintrC   ÚAttributeErrorÚ	TypeErrorÚ
ValueErrorr#   r   Ú
is_closingr<   )r$   r   rC   rn   r   r   r   Ú_ensure_fd_no_transportê   s     
ÿÿz-BaseSelectorEventLoop._ensure_fd_no_transportc           	   	   G   s’   |   ¡  t ||| d ¡}z| j |¡}W n( tyP   | j |tj|d f¡ Y n>0 |j|j	 }\}}| j 
||tjB ||f¡ |d urŽ| ¡  |S r)   )Ú_check_closedr   ÚHandler   r   r   Úregisterr   Ú
EVENT_READrL   ÚmodifyÚcancel©	r$   r   ÚcallbackÚargsÚhandler   ÚmaskÚreaderÚwriterr   r   r   rI   ü   s    ÿ
ÿz!BaseSelectorEventLoop._add_readerc                 C   s’   |   ¡ rdS z| j |¡}W n ty0   Y dS 0 |j|j }\}}|tj M }|sb| j |¡ n| j 	||d |f¡ |d urŠ| 
¡  dS dS d S )NFT)r=   r   r   r   r   rL   r   r~   Ú
unregisterr   r€   ©r$   r   r   r…   r†   r‡   r   r   r   rA     s    z$BaseSelectorEventLoop._remove_readerc           	   	   G   s’   |   ¡  t ||| d ¡}z| j |¡}W n( tyP   | j |tjd |f¡ Y n>0 |j|j	 }\}}| j 
||tjB ||f¡ |d urŽ| ¡  |S r)   )r{   r   r|   r   r   r   r}   r   ÚEVENT_WRITErL   r   r€   r   r   r   r   Ú_add_writer!  s    ÿ
ÿz!BaseSelectorEventLoop._add_writerc                 C   s’   |   ¡ rdS z| j |¡}W n ty0   Y dS 0 |j|j }\}}|tj M }|sb| j |¡ n| j 	|||df¡ |durŠ| 
¡  dS dS dS )úRemove a writer callback.FNT)r=   r   r   r   r   rL   r   rŠ   rˆ   r   r€   r‰   r   r   r   Ú_remove_writer1  s    z$BaseSelectorEventLoop._remove_writerc                 G   s"   |   |¡ | j||g|¢R Ž  dS )zAdd a reader callback.N)rz   rI   ©r$   r   r‚   rƒ   r   r   r   Ú
add_readerH  s    
z BaseSelectorEventLoop.add_readerc                 C   s   |   |¡ |  |¡S )zRemove a reader callback.)rz   rA   ©r$   r   r   r   r   Úremove_readerM  s    
z#BaseSelectorEventLoop.remove_readerc                 G   s"   |   |¡ | j||g|¢R Ž  dS )zAdd a writer callback..N)rz   r‹   rŽ   r   r   r   Ú
add_writerR  s    
z BaseSelectorEventLoop.add_writerc                 C   s   |   |¡ |  |¡S )rŒ   )rz   r   r   r   r   r   Úremove_writerW  s    
z#BaseSelectorEventLoop.remove_writerc              	   Ã   s˜   t  |¡ | jr$| ¡ dkr$tdƒ‚z| |¡W S  ttfyF   Y n0 |  ¡ }| 	¡ }|  
|¡ |  || j|||¡}| tj| j||d¡ |I dH S )zÌReceive data from the socket.

        The return value is a bytes object representing the data received.
        The maximum amount of data to be received at once is specified by
        nbytes.
        r   úthe socket must be non-blocking©r„   N)r   Ú_check_ssl_socketrV   Ú
gettimeoutrx   rN   rP   rO   ro   rC   rz   rI   Ú
_sock_recvÚadd_done_callbackÚ	functoolsÚpartialÚ_sock_read_done)r$   r+   ÚnÚfutr   r„   r   r   r   Ú	sock_recv\  s    

ÿzBaseSelectorEventLoop.sock_recvc                 C   s   |d u s|  ¡ s|  |¡ d S r)   )Ú	cancelledr‘   ©r$   r   rž   r„   r   r   r   rœ   r  s    z%BaseSelectorEventLoop._sock_read_donec              
   C   s„   |  ¡ rd S z| |¡}W nZ ttfy2   Y d S  ttfyH   ‚ Y n8 tyt } z| |¡ W Y d }~nd }~0 0 | |¡ d S r)   )	ÚdonerN   rP   rO   rq   rr   rp   Úset_exceptionÚ
set_result)r$   rž   r+   r   rL   rm   r   r   r   r˜   v  s     z BaseSelectorEventLoop._sock_recvc              	   Ã   s˜   t  |¡ | jr$| ¡ dkr$tdƒ‚z| |¡W S  ttfyF   Y n0 |  ¡ }| 	¡ }|  
|¡ |  || j|||¡}| tj| j||d¡ |I dH S )z¥Receive data from the socket.

        The received data is written into *buf* (a writable buffer).
        The return value is the number of bytes written.
        r   r”   r•   N)r   r–   rV   r—   rx   Ú	recv_intorP   rO   ro   rC   rz   rI   Ú_sock_recv_intor™   rš   r›   rœ   )r$   r+   Úbufrž   r   r„   r   r   r   Úsock_recv_into†  s    

ÿz$BaseSelectorEventLoop.sock_recv_intoc              
   C   s„   |  ¡ rd S z| |¡}W nZ ttfy2   Y d S  ttfyH   ‚ Y n8 tyt } z| |¡ W Y d }~nd }~0 0 | |¡ d S r)   )	r¢   r¥   rP   rO   rq   rr   rp   r£   r¤   )r$   rž   r+   r§   Únbytesrm   r   r   r   r¦   ›  s     z%BaseSelectorEventLoop._sock_recv_intoc              	   Ã   s¶   t  |¡ | jr$| ¡ dkr$tdƒ‚z| |¡}W n ttfyL   d}Y n0 |t|ƒkr^dS |  	¡ }| 
¡ }|  |¡ |  || j||t|ƒ|g¡}| tj| j||d¡ |I dH S )aš  Send data to the socket.

        The socket must be connected to a remote socket. This method continues
        to send data from data until either all data has been sent or an
        error occurs. None is returned on success. On error, an exception is
        raised, and there is no way to determine how much data, if any, was
        successfully processed by the receiving end of the connection.
        r   r”   Nr•   )r   r–   rV   r—   rx   rT   rP   rO   Úlenro   rC   rz   r‹   Ú_sock_sendallÚ
memoryviewr™   rš   r›   Ú_sock_write_done)r$   r+   rL   r   rž   r   r„   r   r   r   Úsock_sendall¬  s&    	



ÿÿz"BaseSelectorEventLoop.sock_sendallc              
   C   s´   |  ¡ rd S |d }z| ||d … ¡}W n\ ttfyB   Y d S  ttfyX   ‚ Y n0 ty† } z| |¡ W Y d }~d S d }~0 0 ||7 }|t|ƒkr¨| 	d ¡ n||d< d S )Nr   )
r¢   rT   rP   rO   rq   rr   rp   r£   rª   r¤   )r$   rž   r+   ÚviewÚposÚstartr   rm   r   r   r   r«   Ë  s     
z#BaseSelectorEventLoop._sock_sendallc                 Ã   s”   t  |¡ | jr$| ¡ dkr$tdƒ‚|jtjksBt jrt|jtj	krt| j
||j|j|j| dI dH }|d \}}}}}|  ¡ }|  |||¡ |I dH S )zTConnect to a remote socket at address.

        This method is a coroutine.
        r   r”   )ÚfamilyÚtypeÚprotoÚloopN)r   r–   rV   r—   rx   r²   rF   ÚAF_INETÚ	_HAS_IPv6ÚAF_INET6Ú_ensure_resolvedr³   r´   ro   Ú_sock_connect)r$   r+   r9   Úresolvedrj   rž   r   r   r   Úsock_connectá  s     
ÿ
ÿþz"BaseSelectorEventLoop.sock_connectc              
   C   s´   |  ¡ }z| |¡ W nŽ ttfyb   |  |¡ |  || j|||¡}| tj	| j
||d¡ Y nN ttfyx   ‚ Y n8 ty¤ } z| |¡ W Y d }~nd }~0 0 | d ¡ d S )Nr•   )rC   ÚconnectrP   rO   rz   r‹   Ú_sock_connect_cbr™   rš   r›   r­   rq   rr   rp   r£   r¤   )r$   rž   r+   r9   r   r„   rm   r   r   r   rº   ö  s     
ÿÿ z#BaseSelectorEventLoop._sock_connectc                 C   s   |d u s|  ¡ s|  |¡ d S r)   )r    r“   r¡   r   r   r   r­     s    z&BaseSelectorEventLoop._sock_write_donec              
   C   s    |  ¡ rd S z,| tjtj¡}|dkr6t|d|› ƒ‚W nX ttfyN   Y nN tt	fyd   ‚ Y n8 t
y } z| |¡ W Y d }~nd }~0 0 | d ¡ d S )Nr   zConnect call failed )r¢   Ú
getsockoptrF   Ú
SOL_SOCKETÚSO_ERRORrU   rP   rO   rq   rr   rp   r£   r¤   )r$   rž   r+   r9   Úerrrm   r   r   r   r¾     s     z&BaseSelectorEventLoop._sock_connect_cbc                 Ã   sB   t  |¡ | jr$| ¡ dkr$tdƒ‚|  ¡ }|  ||¡ |I dH S )aW  Accept a connection.

        The socket must be bound to an address and listening for connections.
        The return value is a pair (conn, address) where conn is a new socket
        object usable to send and receive data on the connection, and address
        is the address bound to the socket on the other end of the connection.
        r   r”   N)r   r–   rV   r—   rx   ro   Ú_sock_accept)r$   r+   rž   r   r   r   Úsock_accept"  s    
z!BaseSelectorEventLoop.sock_acceptc              
   C   sÂ   |  ¡ }z| ¡ \}}| d¡ W nŒ ttfyl   |  |¡ |  || j||¡}| t	j
| j||d¡ Y nR ttfy‚   ‚ Y n< ty® } z| |¡ W Y d }~nd }~0 0 | ||f¡ d S )NFr•   )rC   ra   rH   rP   rO   rz   rI   rÃ   r™   rš   r›   rœ   rq   rr   rp   r£   r¤   )r$   rž   r+   r   rk   r9   r„   rm   r   r   r   rÃ   1  s    
ÿ z"BaseSelectorEventLoop._sock_acceptc              	   Ã   sœ   | j |j= | ¡ }| ¡  | ¡ I d H  zL| j|j|||ddI d H W | ¡  |rZ| ¡  || j |j< S || j |j< n"| ¡  |rŠ| ¡  || j |j< 0 d S )NF)Úfallback)	r#   Ú_sock_fdÚ
is_readingÚpause_readingÚ_make_empty_waiterÚsock_sendfileÚ_sockÚ_reset_empty_waiterÚresume_reading)r$   ÚtranspÚfileÚoffsetÚcountrÍ   r   r   r   Ú_sendfile_nativeB  s*    
ÿú   ýz&BaseSelectorEventLoop._sendfile_nativec                 C   s€   |D ]v\}}|j |j }\}}|tj@ rL|d urL|jrB|  |¡ n
|  |¡ |tj@ r|d ur|jrp|  |¡ q|  |¡ qd S r)   )	ÚfileobjrL   r   r~   Ú
_cancelledrA   Ú_add_callbackrŠ   r   )r$   Ú
event_listr   r…   rÓ   r†   r‡   r   r   r   Ú_process_eventsP  s    
z%BaseSelectorEventLoop._process_eventsc                 C   s   |   | ¡ ¡ | ¡  d S r)   )rA   rC   r?   )r$   r+   r   r   r   Ú_stop_serving^  s    z#BaseSelectorEventLoop._stop_serving)N)N)N)NNN)N)N)-r   Ú
__module__Ú__qualname__Ú__doc__r   r.   r   ÚSSL_HANDSHAKE_TIMEOUTr7   r:   r?   r>   r    rM   rJ   rW   r\   rY   rh   rz   rI   rA   r‹   r   r   r‘   r’   r“   rŸ   rœ   r˜   r¨   r¦   r®   r«   r¼   rº   r­   r¾   rÄ   rÃ   rÒ   r×   rØ   Ú__classcell__r   r   r%   r   r   +   sj   
ÿ ÿü ÿ
þ
	ý
.ý
)

r   c                       s’   e Zd ZdZeZdZd‡ fdd„	Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zejfdd„Zddd„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) Ú_SelectorTransporti   Nc                    sê   t ƒ  ||¡ t |¡| jd< z| ¡ | jd< W n tyL   d | jd< Y n0 d| jvrŠz| ¡ | jd< W n tj	yˆ   d | jd< Y n0 || _
| ¡ | _d| _|  |¡ || _|  ¡ | _d| _d| _| jd urÚ| j ¡  | |j| j< d S )NrF   Zsocknamer_   Fr   )r   r   r   re   Ú_extraÚgetsocknamerU   ÚgetpeernamerF   ÚerrorrË   rC   rÆ   Ú_protocol_connectedÚset_protocolÚ_serverÚ_buffer_factoryÚ_bufferÚ
_conn_lostÚ_closingÚ_attachr#   )r$   rµ   r+   r,   r'   r(   r%   r   r   r   o  s,    





z_SelectorTransport.__init__c                 C   sÖ   | j jg}| jd u r | d¡ n| jr0| d¡ | d| j› ¡ | jd urÆ| j ¡ sÆt| jj	| jt
jƒ}|rz| d¡ n
| d¡ t| jj	| jt
jƒ}|r¢d}nd}|  ¡ }| d|› d	|› d
¡ d d |¡¡S )NÚclosedÚclosingzfd=zread=pollingz	read=idleÚpollingÚidlezwrite=<z
, bufsize=ú>z<{}>ú )r   r   rË   Úappendré   rÆ   Ú_loopr=   r   r   r   r~   rŠ   Úget_write_buffer_sizeÚformatÚjoin)r$   Úinforí   ÚstateÚbufsizer   r   r   Ú__repr__‰  s.    


ÿ
þz_SelectorTransport.__repr__c                 C   s   |   d ¡ d S r)   )Ú_force_closer@   r   r   r   Úabort¥  s    z_SelectorTransport.abortc                 C   s   || _ d| _d S ©NT)Ú	_protocolrã   ©r$   r,   r   r   r   rä   ¨  s    z_SelectorTransport.set_protocolc                 C   s   | j S r)   )rý   r@   r   r   r   Úget_protocol¬  s    z_SelectorTransport.get_protocolc                 C   s   | j S r)   )ré   r@   r   r   r   ry   ¯  s    z_SelectorTransport.is_closingc                 C   sT   | j r
d S d| _ | j | j¡ | jsP|  jd7  _| j | j¡ | j | jd ¡ d S ©NTr   )	ré   rò   rA   rÆ   rç   rè   r   Ú	call_soonÚ_call_connection_lostr@   r   r   r   r?   ²  s    z_SelectorTransport.closec                 C   s,   | j d ur(|d| ›t| d | j  ¡  d S )Nzunclosed transport )Úsource)rË   ÚResourceWarningr?   )r$   Ú_warnr   r   r   Ú__del__¼  s    
z_SelectorTransport.__del__úFatal error on transportc                 C   sN   t |tƒr(| j ¡ r@tjd| |dd n| j ||| | jdœ¡ |  |¡ d S )Nz%r: %sTrR   )r]   r^   rn   r,   )	rt   rU   rò   Ú	get_debugr   r   rd   rý   rú   )r$   rm   r]   r   r   r   Ú_fatal_errorÁ  s    

üz_SelectorTransport._fatal_errorc                 C   sd   | j r
d S | jr(| j ¡  | j | j¡ | jsBd| _| j | j¡ |  j d7  _ | j | j	|¡ d S r   )
rè   rç   Úclearrò   r   rÆ   ré   rA   r  r  ©r$   rm   r   r   r   rú   Ï  s    
z_SelectorTransport._force_closec                 C   sŽ   zN| j r| j |¡ W | j ¡  d | _d | _d | _| j}|d urŠ| ¡  d | _n:| j ¡  d | _d | _d | _| j}|d urˆ| ¡  d | _0 d S r)   )rã   rý   Úconnection_lostrË   r?   rò   rå   Ú_detach)r$   rm   r(   r   r   r   r  Û  s&    
ù
z(_SelectorTransport._call_connection_lostc                 C   s
   t | jƒS r)   )rª   rç   r@   r   r   r   ró   é  s    z(_SelectorTransport.get_write_buffer_sizec                 G   s$   | j r
d S | jj||g|¢R Ž  d S r)   )ré   rò   rI   rŽ   r   r   r   rI   ì  s    z_SelectorTransport._add_reader)NN)r  )r   rÙ   rÚ   Úmax_sizeÚ	bytearrayræ   rË   r   rù   rû   rä   rÿ   ry   r?   ÚwarningsÚwarnr  r	  rú   r  ró   rI   rÝ   r   r   r%   r   rÞ   c  s    

rÞ   c                       sª   e Zd ZdZejjZd#‡ fdd„	Z‡ f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d„ Zdd„ Z‡ fdd„Zdd „ Zd!d"„ Z‡  ZS )$r*   TNc                    s~   d | _ tƒ  |||||¡ d| _d| _d | _t | j¡ | j	 
| jj| ¡ | j	 
| j| j| j¡ |d urz| j	 
tj|d ¡ d S r   )Ú_read_ready_cbr   r   Ú_eofÚ_pausedÚ_empty_waiterr   Ú_set_nodelayrË   rò   r  rý   Úconnection_maderI   rÆ   Ú_read_readyr   Ú_set_result_unless_cancelled)r$   rµ   r+   r,   r-   r'   r(   r%   r   r   r   ø  s    
ÿ
ÿz!_SelectorSocketTransport.__init__c                    s.   t |tjƒr| j| _n| j| _tƒ  |¡ d S r)   )rt   r   ÚBufferedProtocolÚ_read_ready__get_bufferr  Ú_read_ready__data_receivedr   rä   rþ   r%   r   r   rä     s    
z%_SelectorSocketTransport.set_protocolc                 C   s   | j  o| j S r)   )r  ré   r@   r   r   r   rÇ     s    z#_SelectorSocketTransport.is_readingc                 C   s>   | j s| jrd S d| _| j | j¡ | j ¡ r:t d| ¡ d S )NTz%r pauses reading)ré   r  rò   rA   rÆ   r  r   r   r@   r   r   r   rÈ     s    
z&_SelectorSocketTransport.pause_readingc                 C   s@   | j s| jsd S d| _|  | j| j¡ | j ¡ r<t d| ¡ d S )NFz%r resumes reading)	ré   r  rI   rÆ   r  rò   r  r   r   r@   r   r   r   rÍ   "  s    
z'_SelectorSocketTransport.resume_readingc                 C   s   |   ¡  d S r)   )r  r@   r   r   r   r  *  s    z$_SelectorSocketTransport._read_readyc              
   C   sV  | j r
d S z | j d¡}t|ƒs(tdƒ‚W nH ttfyB   ‚ Y n2 tyr } z|  |d¡ W Y d }~d S d }~0 0 z| j	 
|¡}W n^ ttfyœ   Y d S  ttfy²   ‚ Y n2 tyâ } z|  |d¡ W Y d }~d S d }~0 0 | sö|  ¡  d S z| j |¡ W nJ ttfy    ‚ Y n2 tyP } z|  |d¡ W Y d }~n
d }~0 0 d S )Néÿÿÿÿz%get_buffer() returned an empty bufferz/Fatal error: protocol.get_buffer() call failed.ú$Fatal read error on socket transportz3Fatal error: protocol.buffer_updated() call failed.)rè   rý   Ú
get_bufferrª   r<   rq   rr   rp   r	  rË   r¥   rP   rO   Ú_read_ready__on_eofÚbuffer_updated)r$   r§   rm   r©   r   r   r   r  -  sB    ÿÿz0_SelectorSocketTransport._read_ready__get_bufferc              
   C   sè   | j r
d S z| j | j¡}W n^ ttfy4   Y d S  ttfyJ   ‚ Y n2 tyz } z|  	|d¡ W Y d }~d S d }~0 0 |sŒ|  
¡  d S z| j |¡ W nF ttfy´   ‚ Y n0 tyâ } z|  	|d¡ W Y d }~n
d }~0 0 d S )Nr  z2Fatal error: protocol.data_received() call failed.)rè   rË   rN   r  rP   rO   rq   rr   rp   r	  r   rý   Údata_received)r$   rL   rm   r   r   r   r  R  s,    ÿz3_SelectorSocketTransport._read_ready__data_receivedc              
   C   sŽ   | j  ¡ rt d| ¡ z| j ¡ }W nH ttfy<   ‚ Y n2 tyl } z|  	|d¡ W Y d }~d S d }~0 0 |r‚| j  
| j¡ n|  ¡  d S )Nz%r received EOFz1Fatal error: protocol.eof_received() call failed.)rò   r  r   r   rý   Úeof_receivedrq   rr   rp   r	  rA   rÆ   r?   )r$   Ú	keep_openrm   r   r   r   r   k  s    
ÿz,_SelectorSocketTransport._read_ready__on_eofc              
   C   s0  t |tttfƒs$tdt|ƒj›ƒ‚| jr2tdƒ‚| j	d urDtdƒ‚|sLd S | j
rz| j
tjkrht d¡ |  j
d7  _
d S | jsz| j |¡}W n\ ttfy¨   Y n^ ttfy¾   ‚ Y nH tyî } z|  |d¡ W Y d }~d S d }~0 0 ||d … }|sd S | j | j| j¡ | j |¡ |  ¡  d S )Nú/data argument must be a bytes-like object, not z%Cannot call write() after write_eof()z(unable to write; sendfile is in progressúsocket.send() raised exception.r   ú%Fatal write error on socket transport)rt   Úbytesr  r¬   rw   r³   r   r  r<   r  rè   r   Ú!LOG_THRESHOLD_FOR_CONNLOST_WRITESr   Úwarningrç   rË   rT   rP   rO   rq   rr   rp   r	  rò   r‹   rÆ   Ú_write_readyÚextendÚ_maybe_pause_protocol)r$   rL   r   rm   r   r   r   Úwrite€  s>    ÿ

z_SelectorSocketTransport.writec              
   C   s&  | j sJ dƒ‚| jrd S z| j | j ¡}W nˆ ttfy@   Y nâ ttfyV   ‚ Y nÌ ty² } zF| j	 
| j¡ | j  ¡  |  |d¡ | jd urž| j |¡ W Y d }~nxd }~0 0 |rÄ| j d |…= |  ¡  | j s"| j	 
| j¡ | jd urø| j d ¡ | jr|  d ¡ n| jr"| j tj¡ d S )NzData should not be emptyr'  )rç   rè   rË   rT   rP   rO   rq   rr   rp   rò   r   rÆ   r
  r	  r  r£   Ú_maybe_resume_protocolr¤   ré   r  r  ÚshutdownrF   ÚSHUT_WR)r$   r   rm   r   r   r   r+  §  s4    

"
z%_SelectorSocketTransport._write_readyc                 C   s.   | j s| jrd S d| _| js*| j tj¡ d S rü   )ré   r  rç   rË   r0  rF   r1  r@   r   r   r   Ú	write_eofÅ  s
    z"_SelectorSocketTransport.write_eofc                 C   s   dS rü   r   r@   r   r   r   Úcan_write_eofÌ  s    z&_SelectorSocketTransport.can_write_eofc                    s*   t ƒ  |¡ | jd ur&| j tdƒ¡ d S )NzConnection is closed by peer)r   r  r  r£   ÚConnectionErrorr  r%   r   r   r  Ï  s
    
ÿz._SelectorSocketTransport._call_connection_lostc                 C   s6   | j d urtdƒ‚| j ¡ | _ | js0| j  d ¡ | j S )NzEmpty waiter is already set)r  r<   rò   ro   rç   r¤   r@   r   r   r   rÉ   Õ  s    
z+_SelectorSocketTransport._make_empty_waiterc                 C   s
   d | _ d S r)   )r  r@   r   r   r   rÌ   Ý  s    z,_SelectorSocketTransport._reset_empty_waiter)NNN)r   rÙ   rÚ   Ú_start_tls_compatibler   Ú_SendfileModeÚ
TRY_NATIVEÚ_sendfile_compatibler   rä   rÇ   rÈ   rÍ   r  r  r  r   r.  r+  r2  r3  r  rÉ   rÌ   rÝ   r   r   r%   r   r*   ó  s(     ÿ%'r*   c                       sF   e Zd ZejZd‡ fdd„	Zdd„ Zdd„ Zddd	„Z	d
d„ Z
‡  ZS )r8   Nc                    s^   t ƒ  ||||¡ || _| j | jj| ¡ | j | j| j| j	¡ |d urZ| j t
j|d ¡ d S r)   )r   r   Ú_addressrò   r  rý   r  rI   rÆ   r  r   r  )r$   rµ   r+   r,   r9   r-   r'   r%   r   r   r   å  s    
ÿ
ÿz#_SelectorDatagramTransport.__init__c                 C   s   t dd„ | jD ƒƒS )Nc                 s   s   | ]\}}t |ƒV  qd S r)   )rª   )Ú.0rL   rj   r   r   r   Ú	<genexpr>ó  ó    zC_SelectorDatagramTransport.get_write_buffer_size.<locals>.<genexpr>)Úsumrç   r@   r   r   r   ró   ò  s    z0_SelectorDatagramTransport.get_write_buffer_sizec              
   C   s¼   | j r
d S z| j | j¡\}}W nˆ ttfy6   Y n‚ tyd } z| j |¡ W Y d }~n\d }~0  t	t
fyz   ‚ Y n> ty¨ } z|  |d¡ W Y d }~nd }~0 0 | j ||¡ d S )Nz&Fatal read error on datagram transport)rè   rË   Úrecvfromr  rP   rO   rU   rý   Úerror_receivedrq   rr   rp   r	  Údatagram_received©r$   rL   rl   rm   r   r   r   r  õ  s     "z&_SelectorDatagramTransport._read_readyc              
   C   s„  t |tttfƒs$tdt|ƒj›ƒ‚|s,d S | jrV|d | jfvrPtd| j› ƒ‚| j}| j	rŠ| jrŠ| j	t
jkrxt d¡ |  j	d7  _	d S | jsdz,| jd r¬| j |¡ n| j ||¡ W d S  ttfyæ   | j | j| j¡ Y n~ ty } z| j |¡ W Y d }~d S d }~0  ttfy0   ‚ Y n4 tyb } z|  |d¡ W Y d }~d S d }~0 0 | j  t|ƒ|f¡ |  !¡  d S )Nr%  z!Invalid address: must be None or r&  r   r_   ú'Fatal write error on datagram transport)"rt   r(  r  r¬   rw   r³   r   r9  rx   rè   r   r)  r   r*  rç   rß   rË   rT   ÚsendtorP   rO   rò   r‹   rÆ   Ú_sendto_readyrU   rý   r?  rq   rr   rp   r	  rñ   r-  rA  r   r   r   rC    sJ    ÿ
ÿ

ÿz!_SelectorDatagramTransport.sendtoc              
   C   s  | j râ| j  ¡ \}}z*| jd r.| j |¡ n| j ||¡ W q  ttfyh   | j  ||f¡ Y qâY q  t	y˜ } z| j
 |¡ W Y d }~d S d }~0  ttfy®   ‚ Y q  tyÞ } z|  |d¡ W Y d }~d S d }~0 0 q |  ¡  | j s| j | j¡ | jr|  d ¡ d S )Nr_   rB  )rç   Úpopleftrß   rË   rT   rC  rP   rO   Ú
appendleftrU   rý   r?  rq   rr   rp   r	  r/  rò   r   rÆ   ré   r  rA  r   r   r   rD  0  s0    
ÿz(_SelectorDatagramTransport._sendto_ready)NNN)N)r   rÙ   rÚ   ÚcollectionsÚdequeræ   r   ró   r  rC  rD  rÝ   r   r   r%   r   r8   á  s     ÿ
+r8   )rÛ   Ú__all__rG  rc   rš   r   rF   r  r!   ÚsslÚImportErrorÚ r   r   r   r   r   r	   r
   r   Úlogr   r   ÚBaseEventLoopr   Ú_FlowControlMixinÚ	TransportrÞ   r*   r8   r   r   r   r   Ú<module>   sD   
    <ÿ  o