o
    gN                     @   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
y1   dZ	Y nw 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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   s2   z|  |}W n
 ty   Y dS w t|j|@ S NF)get_keyKeyErrorboolr   )selectorfdeventkey r   M/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/asyncio/selector_events.py_test_selector_event    s   r   c                 C   s$   t d urt| t jrtdd S d S )Nz"Socket cannot be of type SSLSocket)ssl
isinstanceZ	SSLSocket	TypeError)sockr   r   r   _check_ssl_socket+   s   r   c                       s  e Zd ZdZdS fdd	ZdSdddddZ	dSddddejd	d
dZ	dTd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dSd5d6Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dSdEdFZ%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+  Z,S )Ur   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   6   s   
zBaseSelectorEventLoop.__init__extraserverc                C      t | |||||S N)_SelectorSocketTransport)r)   r   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)r6   r+   )r	   ZSSLProtocolr0   Z_app_transport)r)   Zrawsockr1   
sslcontextr2   r4   r5   r,   r-   r6   Zssl_protocolr   r   r   _make_ssl_transportE   s   z)BaseSelectorEventLoop._make_ssl_transportc                 C   r.   r/   )_SelectorDatagramTransport)r)   r   r1   addressr2   r,   r   r   r   _make_datagram_transportR   s   z.BaseSelectorEventLoop._make_datagram_transportc                    sP   |   rtd|  rd S |   t   | jd ur&| j  d | _d S d S )Nz!Cannot close a running event loop)Z
is_runningRuntimeError	is_closed_close_self_piper   closer$   r)   r*   r   r   r?   W   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>   b   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%   j   s
   z%BaseSelectorEventLoop._make_self_pipec                 C   s   d S r/   r   r)   datar   r   r   _process_self_datar      z(BaseSelectorEventLoop._process_self_datac                 C   sN   	 z| j d}|sW d S | | W n ty   Y q  ty%   Y d S w q)NTi   )rB   recvrM   InterruptedErrorBlockingIOErrorrK   r   r   r   rJ   u   s   z%BaseSelectorEventLoop._read_from_selfc                 C   sR   | j }|d u r	d S z|d W d S  ty(   | jr%tjddd Y d S Y d S w )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_selfd   c              
   C   s"   |  | | j|||||| d S r/   )rI   rC   _accept_connection)r)   protocol_factoryr   r7   r-   backlogr6   r   r   r   _start_serving   s   
z$BaseSelectorEventLoop._start_servingc                 C   s  t |D ]}}z| \}}	| jrtd||	| |d W nO tttfy,   Y  d S  t	ym }
 z6|
j
t
jt
jt
jt
jfv rb| d|
t|d | |  | tj| j|||||| n W Y d }
~
qd }
~
ww d|	i}| ||||||}| | qd S )Nz#%r got a new connection from %r: %rFz&socket.accept() out of system resource)message	exceptionrF   peername)rangeacceptrW   r   r!   rH   rQ   rP   ConnectionAbortedErrorrV   errnoZEMFILEZENFILEZENOBUFSZENOMEMcall_exception_handlerr   TransportSocketrA   rC   Z
call_laterr   ZACCEPT_RETRY_DELAYr]   _accept_connection2Zcreate_task)r)   r[   r   r7   r-   r\   r6   _connaddrexcr,   rb   r   r   r   rZ      sH   
z(BaseSelectorEventLoop._accept_connectionc              
      s   d }d }z7| }|   }	|r| j||||	d|||d}n
| j|||	||d}z	|	I d H  W W d S  ty<   |   w  ttfyF     ty| }
 z+| jrqd|
d}|d ur]||d< |d ure||d< | | W Y d }
~
d S W Y d }
~
d S d }
~
ww )NT)r2   r4   r,   r-   r6   )r2   r,   r-   z3Error on transport creation for incoming connection)r^   r_   r1   	transport)	create_futurer8   r3   BaseExceptionr?   
SystemExitKeyboardInterruptrW   re   )r)   r[   ri   r,   r7   r-   r6   r1   rl   r2   rk   contextr   r   r   rg      sJ   z)BaseSelectorEventLoop._accept_connection2c              
   C   s   |}t |ts"zt| }W n tttfy!   td|d w z| j| }W n
 ty3   Y d S w | sBt	d|d|d S )NzInvalid file object: zFile descriptor z is used by transport )
r   intrC   AttributeErrorr   
ValueErrorr(   r   
is_closingr<   )r)   r   rC   rl   r   r   r   _ensure_fd_no_transport   s&   
z-BaseSelectorEventLoop._ensure_fd_no_transportc           	   	   G   s   |    t||| d }z| j|}W n ty)   | j|tj|d f Y |S w |j|j	}\}}| j
||tjB ||f |d urH|  |S r/   )_check_closedr   Handler$   r   r   registerr   
EVENT_READrL   modifycancel	r)   r   callbackargshandler   maskreaderwriterr   r   r   rI     "   z!BaseSelectorEventLoop._add_readerc                 C   s   |   rdS z| j|}W n
 ty   Y dS w |j|j}\}}|tj M }|s1| j| n
| j	||d |f |d urE|
  dS dS )NFT)r=   r$   r   r   r   rL   r   rz   
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y)   | j|tjd |f Y |S w |j|j	}\}}| j
||tjB ||f |d urH|  |S r/   )rw   r   rx   r$   r   r   ry   r   EVENT_WRITErL   r{   r|   r}   r   r   r   _add_writer&  r   z!BaseSelectorEventLoop._add_writerc                 C   s   |   rdS z| j|}W n
 ty   Y dS w |j|j}\}}|tj M }|s1| j| n
| j	|||df |durE|
  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_writer6  s    z$BaseSelectorEventLoop._remove_writerc                 G   "   |  | | j||g|R   dS )zAdd a reader callback.N)rv   rI   r)   r   r~   r   r   r   r   
add_readerM     
z BaseSelectorEventLoop.add_readerc                 C      |  | | |S )zRemove a reader callback.N)rv   rA   r)   r   r   r   r   remove_readerR     

z#BaseSelectorEventLoop.remove_readerc                 G   r   )zAdd a writer callback..N)rv   r   r   r   r   r   
add_writerW  r   z BaseSelectorEventLoop.add_writerc                 C   r   )r   N)rv   r   r   r   r   r   remove_writer\  r   z#BaseSelectorEventLoop.remove_writerc              	         t | | jr| dkrtdz||W S  ttfy#   Y nw |  }| }| 	| | 
|| 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   rW   
gettimeoutrt   rO   rQ   rP   rm   rC   rv   rI   
_sock_recvadd_done_callback	functoolspartial_sock_read_done)r)   r   nfutr   r   r   r   r   	sock_recva  s"   

zBaseSelectorEventLoop.sock_recvc                 C   "   |d u s|  s| | d S d S r/   )	cancelledr   r)   r   r   r   r   r   r   r   w     z%BaseSelectorEventLoop._sock_read_donec              
   C      |  rd S z||}W n, ttfy   Y d S  ttfy"     ty9 } z|| W Y d }~d S d }~ww || d S r/   )	donerO   rQ   rP   ro   rp   rn   set_exception
set_result)r)   r   r   r   rL   rk   r   r   r   r   {  s   z BaseSelectorEventLoop._sock_recvc              	      r   )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   rW   r   rt   	recv_intorQ   rP   rm   rC   rv   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   r   r/   )	r   r   rQ   rP   ro   rp   rn   r   r   )r)   r   r   r   nbytesrk   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y&   d}Y nw |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   rW   r   rt   rU   rQ   rP   lenrm   rC   rv   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y!   Y d S  ttfy*     tyA } z|| W Y d }~d S d }~ww ||7 }|t|krS|	d  d S ||d< d S )Nr   )
r   rU   rQ   rP   ro   rp   rn   r   r   r   )r)   r   r   viewposstartr   rk   r   r   r   r     s$   
z#BaseSelectorEventLoop._sock_sendallc                    s   t | | jr| dkrtdttdr|jtjkr4| 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   AF_UNIX)familyprotoloopN)r   rW   r   rt   hasattrrF   r   r   Z_ensure_resolvedr   rm   _sock_connect)r)   r   r:   Zresolvedrh   r   r   r   r   sock_connect  s   
z"BaseSelectorEventLoop.sock_connectc              
   C   s   |  }z|| W nG ttfy2   | | | || j|||}|tj	| j
||d Y d S  ttfy;     tyR } z|| W Y d }~d S d }~ww |d  d S )Nr   )rC   connectrQ   rP   rv   r   _sock_connect_cbr   r   r   r   ro   rp   rn   r   r   )r)   r   r   r:   r   r   rk   r   r   r   r     s$   

z#BaseSelectorEventLoop._sock_connectc                 C   r   r/   )r   r   r   r   r   r   r     r   z&BaseSelectorEventLoop._sock_write_donec              
   C   s   |  rd S z|tjtj}|dkrt|d| W n, ttfy(   Y d S  tt	fy1     t
yH } z|| W Y d }~d S d }~ww |d  d S )Nr   zConnect call failed )r   
getsockoptrF   
SOL_SOCKETSO_ERRORrV   rQ   rP   ro   rp   rn   r   r   )r)   r   r   r:   errrk   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   rW   r   rt   rm   _sock_accept)r)   r   r   r   r   r   sock_accept$  s   
z!BaseSelectorEventLoop.sock_acceptc              
   C   s   |  }z| \}}|d W nF ttfy7   | | | || j||}|t	j
| j||d Y d S  ttfy@     tyW } z|| W Y d }~d S d }~ww |||f d S )NFr   )rC   rb   rH   rQ   rP   rv   rI   r   r   r   r   r   ro   rp   rn   r   r   )r)   r   r   r   ri   r:   r   rk   r   r   r   r   3  s"   

z"BaseSelectorEventLoop._sock_acceptc              	      s   | j |j= | }|  | I d H  z| j|j|||ddI d H W |  |r.|  || j |j< S |  |r?|  || j |j< w )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_nativeD  s"   
z&BaseSelectorEventLoop._sendfile_nativec                 C   s   |D ];\}}|j |j}\}}|tj@ r&|d ur&|jr!| | n| | |tj@ r=|d ur=|jr8| | q| | qd S r/   )	fileobjrL   r   rz   Z
_cancelledrA   Z_add_callbackr   r   )r)   Z
event_listr   r   r   r   r   r   r   r   _process_eventsR  s   

z%BaseSelectorEventLoop._process_eventsc                 C   s   |  |  |  d S r/   )rA   rC   r?   )r)   r   r   r   r   _stop_serving`  s   z#BaseSelectorEventLoop._stop_servingr/   NNN)-r#   
__module____qualname____doc__r   r3   r   ZSSL_HANDSHAKE_TIMEOUTr8   r;   r?   r>   r%   rM   rJ   rX   r]   rZ   rg   rv   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   0   sl    


	
.
)

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y&   d | jd< Y nw d| jvrEz	| | jd< W n tj	yD   d | jd< Y nw || _
| | _d| _| | || _|  | _d| _d| _| jd urm| j  | |j| j< d S )NrF   Zsocknamer`   Fr   )r   r   r   rf   _extragetsocknamerV   getpeernamerF   errorr   rC   r   _protocol_connectedset_protocol_server_buffer_factory_buffer
_conn_lost_closingZ_attachr(   )r)   r   r   r1   r,   r-   r*   r   r   r   q  s0   





z_SelectorTransport.__init__c                 C   s   | j jg}| jd u r|d n| jr|d |d| j  | jd urc| j sct| jj	| jt
j}|r=|d n|d t| jj	| jt
j}|rQd}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   _loopr=   r   r$   r   rz   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)   r1   r   r   r   r     s   
z_SelectorTransport.set_protocolc                 C      | j S r/   )r   r@   r   r   r   get_protocol     z_SelectorTransport.get_protocolc                 C   r   r/   )r   r@   r   r   r   ru     r   z_SelectorTransport.is_closingc                 C   sX   | j rd S d| _ | j| j | js*|  jd7  _| j| j | j| jd  d S d S NTr   )	r   r   rA   r   r   r   r   	call_soon_call_connection_lostr@   r   r   r   r?     s   z_SelectorTransport.closec                 C   s0   | j d ur|d| t| d | j   d S 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: %sTrS   )r^   r_   rl   r1   )	r   rV   r   	get_debugr   r!   re   r   r   )r)   rk   r^   r   r   r   _fatal_error  s   

z_SelectorTransport._fatal_errorc                 C   sd   | j rd S | jr| j  | j| j | js!d| _| j| j |  j d7  _ | j| j	| d S r   )
r   r   clearr   r   r   r   rA   r   r   r)   rk   r   r   r   r     s   
z_SelectorTransport._force_closec                 C   s   z*| j r
| j| W | j  d | _d | _d | _| j}|d ur)|  d | _d S d S | j  d | _d | _d | _| j}|d urG|  d | _w r/   )r   r   Zconnection_lostr   r?   r   r   Z_detach)r)   rk   r-   r   r   r   r     s*   


z(_SelectorTransport._call_connection_lostc                 C   s
   t | jS r/   )r   r   r@   r   r   r   r        
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   ru   r?   warningswarnr  r  r   r   r   rI   r   r   r   r*   r   r   e  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 )$r0   TNc                    s   d | _ t ||||| d| _d| _d | _t| j | j	
| jj|  | j	
| j| j| j |d ur?| j	
tj|d  d S d S r   )_read_ready_cbr   r   _eof_paused_empty_waiterr   Z_set_nodelayr   r   r   r   connection_maderI   r   _read_readyr   _set_result_unless_cancelled)r)   r   r   r1   r2   r,   r-   r*   r   r   r     s   

z!_SelectorSocketTransport.__init__c                    s.   t |tjr| j| _n| j| _t | d S r/   )r   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   sB   | j s| jrd S d| _| j| j | j rtd|  d S 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   sD   | j s| jsd S d| _| | j| j | j r td|  d S 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   sD  | j rd S z| jd}t|stdW n" ttfy     ty7 } z| |d W Y d }~d S d }~ww z| j	
|}W n- ttfyL   Y d S  ttfyU     tym } z| |d W Y d }~d S d }~ww |sv|   d S z	| j| W d S  ttfy     ty } z| |d W Y d }~d S d }~ww )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   r<   ro   rp   rn   r  r   r   rQ   rP   _read_ready__on_eofZbuffer_updated)r)   r   rk   r   r   r   r   r  /  sP   z0_SelectorSocketTransport._read_ready__get_bufferc              
   C   s   | j rd S z	| j| j}W n- ttfy   Y d S  ttfy#     ty; } z| 	|d W Y d }~d S d }~ww |sD| 
  d S z	| j| W d S  ttfyW     tyo } z| 	|d W Y d }~d S d }~ww )Nr  z2Fatal error: protocol.data_received() call failed.)r   r   rO   r  rQ   rP   ro   rp   rn   r  r  r   Zdata_received)r)   rL   rk   r   r   r   r  T  s4   z3_SelectorSocketTransport._read_ready__data_receivedc              
   C   s   | j  rtd|  z| j }W n" ttfy     ty4 } z| 	|d W Y d }~d S d }~ww |r@| j 
| j d S |   d S )Nz%r received EOFz1Fatal error: protocol.eof_received() call failed.)r   r  r   r!   r   Zeof_receivedro   rp   rn   r  rA   r   r?   )r)   Z	keep_openrk   r   r   r   r  m  s    
z,_SelectorSocketTransport._read_ready__on_eofc              
   C   s(  t |tttfstdt|j| jrtd| j	d ur"td|s&d S | j
r=| j
tjkr4td |  j
d7  _
d S | jsz| j|}W n, ttfyS   Y n, ttfy\     tyt } z| |d W Y d }~d S d }~ww ||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)r   bytesr	  r   r   typer#   r  r<   r  r   r   !LOG_THRESHOLD_FOR_CONNLOST_WRITESr   warningr   r   rU   rQ   rP   ro   rp   rn   r  r   r   r   _write_readyextend_maybe_pause_protocol)r)   rL   r   rk   r   r   r   write  sB   

z_SelectorSocketTransport.writec              
   C   s*  | j rd S z	| j| j}W nK ttfy   Y d S  ttfy#     tyY } z+| j	
| j | j  | |d | jd urN| j| W Y d }~d S W Y d }~d S d }~ww |rb| jd |= |   | js| j	
| j | jd ur{| jd  | jr| d  d S | jr| jtj d S d S d S )Nr  )r   r   rU   r   rQ   rP   ro   rp   rn   r   r   r   r  r  r  r   _maybe_resume_protocolr   r   r   r  shutdownrF   SHUT_WR)r)   r   rk   r   r   r   r    s<   


z%_SelectorSocketTransport._write_readyc                 C   s2   | j s| jrd S d| _| js| jtj d S d S r   )r   r  r   r   r$  rF   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  rN   z&_SelectorSocketTransport.can_write_eofc                    s.   t  | | jd ur| jtd d S 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s| j d  | j S )NzEmpty waiter is already set)r  r<   r   rm   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     r  z,_SelectorSocketTransport._reset_empty_waiterr   )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   r0     s*    %'r0   c                       sJ   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 )r9   Nc                    sb   t  |||| || _| j| jj|  | j| j| j| j	 |d ur/| jt
j|d  d S d S r/   )r   r   _addressr   r   r   r  rI   r   r  r   r  )r)   r   r   r1   r:   r2   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   rh   r   r   r   	<genexpr>  s    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 nE ttfy   Y d S  ty4 } z| j| W Y d }~d S d }~w t	t
fy=     tyU } z| |d W Y d }~d S d }~ww | j|| d S )Nz&Fatal read error on datagram transport)r   r   recvfromr  rQ   rP   rV   r   error_receivedro   rp   rn   r  Zdatagram_receivedr)   rL   rj   rk   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r+|d | jfvr(td| j | j}| j	rE| jrE| j	t
jkr<td |  j	d7  _	d S | jsz| jd rW| j| W d S | j|| W d S  ttfyt   | j| j| j Y n: ty } z| j| W Y d }~d S d }~w ttfy     ty } z| |d W Y d }~d S d }~ww | j t||f | !  d S )Nr  z!Invalid address: must be None or r  r   r`   'Fatal write error on datagram transport)"r   r  r	  r   r   r  r#   r)  rt   r   r   r  r   r  r   r   r   rU   sendtorQ   rP   r   r   r   _sendto_readyrV   r   r.  ro   rp   rn   r  r   r!  r/  r   r   r   r1    sR   


z!_SelectorDatagramTransport.sendtoc              
   C   s  | j ro| j  \}}z| jd r| j| n| j|| W nL ttfy2   | j ||f Y qo t	yJ } z| j
| W Y d }~d S d }~w ttfyS     tyk } z| |d W Y d }~d S d }~ww | j s|   | j s| j| j | jr| d  d S d S d S )Nr`   r0  )r   popleftr   r   rU   r1  rQ   rP   
appendleftrV   r   r.  ro   rp   rn   r  r#  r   r   r   r   r   r/  r   r   r   r2  2  s>   
z(_SelectorDatagramTransport._sendto_readyr   r/   )r#   r   r   collectionsdequer   r   r   r  r1  r2  r   r   r   r*   r   r9     s    
+r9   )r   __all__r5  rd   r   r   rF   r
  r&   r   ImportError r   r   r   r   r   r	   r
   r   logr   r   r   ZBaseEventLoopr   Z_FlowControlMixinZ	Transportr   r0   r9   r   r   r   r   <module>   sJ        9  o