a
    zeL                     @   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 )zEvent 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   -/usr/lib/python3.9/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	   ZSSLProtocolr*   Z_app_transport)r$   Zrawsockr,   
sslcontextr-   r/   r0   r'   r(   r1   Z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)Z
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   r9   ]   s    

z&BaseSelectorEventLoop._close_self_pipec                 C   sN   t  \| _| _| jd | jd |  jd7  _| | j | j d S )NFr   )	socket
socketpairr=   r?   setblockingr@   _add_readerr>   _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   )r=   recvrH   InterruptedErrorBlockingIOErrorrF   r   r   r   rE   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)r?   sendOSError_debugr   r   )r$   Zcsockr   r   r   _write_to_self|   s    z$BaseSelectorEventLoop._write_to_selfd   c              
   C   s"   |  | | j|||||| d S r)   )rD   r>   _accept_connection)r$   protocol_factoryr+   r2   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	exceptionrA   peername)rangeacceptrQ   r   r   rC   rK   rJ   ConnectionAbortedErrorrP   errnoZEMFILEZENFILEZENOBUFSZENOMEMcall_exception_handlerr   TransportSocketr<   r>   Z
call_laterr   ZACCEPT_RETRY_DELAYrW   _accept_connection2Zcreate_task)r$   rU   r+   r2   r(   rV   r1   _connaddrexcr'   r\   r   r   r   rT      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rd|
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)rX   rY   r,   	transport)	create_futurer3   r.   BaseExceptionr:   
SystemExitKeyboardInterruptrQ   r_   )r$   rU   rc   r'   r2   r(   r1   r,   rf   r-   re   contextr   r   r   ra      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 | st	d|d|d S )NzInvalid file object: zFile descriptor z is used by transport )

isinstanceintr>   AttributeError	TypeError
ValueErrorr#   r   
is_closingr7   )r$   r   r>   rf   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_READrG   modifycancel	r$   r   callbackargshandler   maskreaderwriterr   r   r   rD      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)r8   r   r   r   r   rG   r   rv   
unregisterrw   rx   r$   r   r   r}   r~   r   r   r   r   r<     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)   )rs   r   rt   r   r   r   ru   r   EVENT_WRITErG   rw   rx   ry   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)r8   r   r   r   r   rG   r   r   r   rw   rx   r   r   r   r   _remove_writer1  s    z$BaseSelectorEventLoop._remove_writerc                 G   s"   |  | | j||g|R   dS )zAdd a reader callback.N)rr   rD   r$   r   rz   r{   r   r   r   
add_readerH  s    
z BaseSelectorEventLoop.add_readerc                 C   s   |  | | |S )zRemove a reader callback.)rr   r<   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)rr   r   r   r   r   r   
add_writerR  s    
z BaseSelectorEventLoop.add_writerc                 C   s   |  | | |S )r   )rr   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 )zReceive 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_socketrQ   
gettimeoutrp   rI   rK   rJ   rg   r>   rr   rD   
_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)   )	donerI   rK   rJ   ri   rj   rh   set_exception
set_result)r$   r   r+   r   rG   re   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 )zReceive 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   rQ   r   rp   	recv_intorK   rJ   rg   r>   rr   rD   _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   rK   rJ   ri   rj   rh   r   r   )r$   r   r+   r   nbytesre   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   rQ   r   rp   rO   rK   rJ   lenrg   r>   rr   r   _sock_sendall
memoryviewr   r   r   _sock_write_done)r$   r+   rG   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   rO   rK   rJ   ri   rj   rh   r   r   r   )r$   r   r+   viewposstartr   re   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   rQ   r   rp   r   rA   AF_INETZ	_HAS_IPv6AF_INET6Z_ensure_resolvedr   r   rg   _sock_connect)r$   r+   r5   Zresolvedrb   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   )r>   connectrK   rJ   rr   r   _sock_connect_cbr   r   r   r   ri   rj   rh   r   r   )r$   r   r+   r5   r   r|   re   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   
getsockoptrA   
SOL_SOCKETSO_ERRORrP   rK   rJ   ri   rj   rh   r   r   )r$   r   r+   r5   errre   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   rQ   r   rp   rg   _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   )r>   r\   rC   rK   rJ   rr   rD   r   r   r   r   r   ri   rj   rh   r   r   )r$   r   r+   r   rc   r5   r|   re   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_waiterZsock_sendfile_sock_reset_empty_waiterresume_reading)r$   Z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)   )	fileobjrG   r   rv   Z
_cancelledr<   Z_add_callbackr   r   )r$   Z
event_listr   r}   r   r~   r   r   r   r   _process_eventsP  s    
z%BaseSelectorEventLoop._process_eventsc                 C   s   |  |  |  d S r)   )r<   r>   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   ZSSL_HANDSHAKE_TIMEOUTr3   r6   r:   r9   r    rH   rE   rR   rW   rT   ra   rr   rD   r<   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rz| | 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 )NrA   ZsocknamerZ   Fr   )r   r   r   r`   _extragetsocknamerP   getpeernamerA   errorr   r>   r   _protocol_connectedset_protocol_server_buffer_factory_buffer
_conn_lost_closingZ_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 st| jj	| jt
j}|rz|d n
|d t| jj	| jt
j}|rd}nd}|  }|d| d	| d
 dd|S )Nclosedclosingzfd=zread=pollingz	read=idlepollingZidlezwrite=<z
, bufsize=>z<{}> )r   r   r   appendr   r   _loopr8   r   r   r   rv   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   rq     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   r<   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: %sTrM   )rX   rY   rf   r,   )	rl   rP   r   	get_debugr   r   r_   r   r   )r$   re   rX   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   r<   r   r   r$   re   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   Zconnection_lostr   r:   r   r   Z_detach)r$   re   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   rD   r   r   r   r   rD     s    z_SelectorTransport._add_reader)NN)r   )r   r   r   max_size	bytearrayr   r   r   r   r   r   r   rq   r:   warningswarnr   r   r   r   r   rD   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   Z_set_nodelayr   r   r   r   connection_maderD   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)   )rl   r   Z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   r<   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   rD   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 )Nz%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   Z
get_bufferr   r7   ri   rj   rh   r   r   r   rK   rJ   _read_ready__on_eofZbuffer_updated)r$   r   re   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   rI   r   rK   rJ   ri   rj   rh   r   r  r   Zdata_received)r$   rG   re   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   Zeof_receivedri   rj   rh   r   r<   r   r:   )r$   Z	keep_openre   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)rl   bytesr   r   ro   r   r   r   r7   r   r   r   !LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr   r   rO   rK   rJ   ri   rj   rh   r   r   r   r   _write_readyextend_maybe_pause_protocol)r$   rG   r   re   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   rO   rK   rJ   ri   rj   rh   r   r   r   r   r   r   r   _maybe_resume_protocolr   r   r   r   shutdownrA   SHUT_WR)r$   r   re   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   r  rA   r  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   r7   r   rg   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   Z_start_tls_compatibler   Z_SendfileModeZ
TRY_NATIVEZ_sendfile_compatibler   r   r   r   r   r  r  r  r  r  r  r  r  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 )r4   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  rD   r   r  r   r  )r$   r   r+   r,   r5   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   ).0rG   rb   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   rK   rJ   rP   r   error_receivedri   rj   rh   r   Zdatagram_receivedr$   rG   rd   re   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   rZ   'Fatal write error on datagram transport)"rl   r  r   r   ro   r   r   r  rp   r   r   r  r   r  r   r   r   rO   sendtorK   rJ   r   r   r   _sendto_readyrP   r   r  ri   rj   rh   r   r   r  r   r   r   r   r"    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 qY 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 )NrZ   r!  )r   popleftr   r   rO   r"  rK   rJ   
appendleftrP   r   r  ri   rj   rh   r   r  r   r   r   r   r   r   r   r   r   r#  0  s0    
z(_SelectorDatagramTransport._sendto_ready)NNN)N)r   r   r   collectionsdequer   r   r   r  r"  r#  r   r   r   r%   r   r4     s     
+r4   )r   __all__r&  r^   r   r   rA   r   r!   ZsslImportError r   r   r   r   r   r	   r
   r   logr   r   ZBaseEventLoopr   Z_FlowControlMixinZ	Transportr   r*   r4   r   r   r   r   <module>   sD   
    <  o