
    kjs&                         d dl Z d dlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZ eegdf   Zee j&                  e j(                  gdf   Z G d de j,                        Z G d	 d
e j0                        Zy)    N)AnyCallableDictOptionalTextTupleUnioncast   )events)NetworkAddressQuicConnection)QuicErrorCodec                      e Zd Z	 ddedee   fdZd dZej                  dfde
d	eddfd
Zd!deddfdZ	 d"dedeej$                  ej&                  f   fdZd dZd dZd dZd dZd dZdej4                  ddfdZdeeef   deddfdZde jB                  ddfdZ"de
deej$                  ej&                  f   fdZ#d dZ$d dZ%d dZ&y)#QuicConnectionProtocolNquicstream_handlerc                 >   t        j                         }t        j                         | _        d| _        d | _        || _        i | _        || _        i | _	        d | _
        d | _        d | _        d | _        d | _        d | _        d | _        ||| _        y d | _        y )NFc                      y N cs    a/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/aioquic/asyncio/protocol.py<lambda>z1QuicConnectionProtocol.__init__.<locals>.<lambda>           c                      y r   r   r   s    r   r   z1QuicConnectionProtocol.__init__.<locals>.<lambda>    r   r   c                       y r   r   r   r   r   r   z1QuicConnectionProtocol.__init__.<locals>.<lambda>!   r   r   c                      y r   r   )rws     r   r   z1QuicConnectionProtocol.__init__.<locals>.<lambda>%   r   r   )asyncioget_event_loopEvent_closed
_connected_connected_waiter_loop_ping_waiters_quic_stream_readers_timer	_timer_at_transmit_task
_transport_connection_id_issued_handler_connection_id_retired_handler_connection_terminated_handler_stream_handler)selfr   r   loops       r   __init__zQuicConnectionProtocol.__init__   s     %%'}}AE
>@
@B59*.8<?C GU*GU+BN+%#1D #4D r   returnc                 X    | j                   j                          | j                          y)z
        Change the connection ID used to communicate with the peer.

        The previous connection ID will be retired.
        N)r+   change_connection_idtransmitr5   s    r   r:   z+QuicConnectionProtocol.change_connection_id'   s     	

'')r    
error_codereason_phrasec                 ^    | j                   j                  ||       | j                          y)a  
        Close the connection.

        :param error_code: An error code indicating why the connection is
                           being closed.
        :param reason_phrase: A human-readable explanation of why the
                              connection is being closed.
        )r>   r?   N)r+   closer;   )r5   r>   r?   s      r   rA   zQuicConnectionProtocol.close0   s,     	

!' 	 	
 	r   addrc                     | j                   j                  || j                  j                                |r| j	                          yy)zt
        Initiate the TLS handshake.

        This method can only be called for clients and a single time.
        nowN)r+   connectr)   timer;   )r5   rB   r;   s      r   rF   zQuicConnectionProtocol.connectC   s7     	

4TZZ__%67MMO r   is_unidirectionalc                 d   K   | j                   j                  |      }| j                  |      S w)z
        Create a QUIC stream and return a pair of (reader, writer) objects.

        The returned reader and writer objects are instances of
        :class:`asyncio.StreamReader` and :class:`asyncio.StreamWriter` classes.
        )rH   )r+   get_next_available_stream_id_create_stream)r5   rH   	stream_ids      r   create_streamz$QuicConnectionProtocol.create_streamM   s7      JJ;;/ < 
	 ""9--s   .0c                 X    | j                   j                          | j                          y)z;
        Request an update of the encryption keys.
        N)r+   request_key_updater;   r<   s    r   rO   z)QuicConnectionProtocol.request_key_update[   s     	

%%'r   c                   K   | j                   j                         }t        |      }|| j                  |<   | j                  j                  |       | j                          t        j                  |       d{    y7 w)z:
        Ping the peer and wait for the response.
        N)	r)   create_futureidr*   r+   	send_pingr;   r#   shield)r5   waiteruids      r   pingzQuicConnectionProtocol.pingb   s`      ))+j"(3

S!nnV$$$s   A8B:B ;Bc                    d| _         | j                  j                  | j                  j	                               D ]!  \  }}| j
                  j                  ||       # | j                  j                         }| j                  0| j                  |k7  r!| j                  j                          d| _        | j                  -|+| j                  j                  || j                        | _        || _	        y)a  
        Send pending datagrams to the peer and arm the timer if needed.

        This method is called automatically when data is received from the peer
        or when a timer goes off. If you interact directly with the underlying
        :class:`~aioquic.quic.connection.QuicConnection`, make sure you call this
        method whenever data needs to be sent out to the network.
        NrD   )r/   r+   datagrams_to_sendr)   rG   r0   sendto	get_timerr-   r.   cancelcall_at_handle_timer)r5   datarB   timer_ats       r   r;   zQuicConnectionProtocol.transmitm   s     # **664::??;L6M 	/JD$OO""4.	/ ::'');;"t~~'AKK DK;;8#7**,,Xt7I7IJDK!r   c                 T   K   | j                   j                          d{    y7 w)z7
        Wait for the connection to be closed.
        N)r&   waitr<   s    r   wait_closedz"QuicConnectionProtocol.wait_closed   s      ll!!!s   (&(c                    K   | j                   J d       | j                  sG| j                  j                         | _         t	        j
                  | j                          d{    yy7 w)z9
        Wait for the TLS handshake to complete.
        Nzalready awaiting connected)r(   r'   r)   rQ   r#   rT   r<   s    r   wait_connectedz%QuicConnectionProtocol.wait_connected   s\      %%-K/KK-%)ZZ%=%=%?D"..!7!7888 8s   A!A,#A*$A,	transportc                 B    t        t        j                  |      | _        y):meta private:N)r
   r#   DatagramTransportr0   )r5   rf   s     r   connection_madez&QuicConnectionProtocol.connection_made   s    w88)Dr   r_   c                     | j                   j                  t        t        |      || j                  j                                | j                          | j                          y)rh   rD   N)r+   receive_datagramr
   bytesr)   rG   _process_eventsr;   )r5   r_   rB   s      r   datagram_receivedz(QuicConnectionProtocol.datagram_received   sB    

##D$5tAR#Sr   eventc                    t        |t        j                        r0| j                  j	                         D ]  }|j                           yt        |t        j                        r| j                  j                  |j                  d      }|0| j                  |j                        \  }}| j                  ||       |j                  |j                         |j                  r|j                          yyy)zx
        Called when a QUIC event is received.

        Reimplement this in your subclass to handle the events.
        N)
isinstancer   ConnectionTerminatedr,   valuesfeed_eofStreamDataReceivedgetrL   rK   r4   	feed_datar_   
end_stream)r5   rp   readerwriters       r   quic_event_receivedz*QuicConnectionProtocol.quic_event_received   s     eV889..557 "!"v889))--eootDF~!%!4!4U__!E$$VV4UZZ(!   :r   rL   c                     t        | |      }t        j                         }t        j                  j	                  |      }t        j
                  |||| j                        }|| j                  |<   ||fS r   )QuicStreamAdapterr#   StreamReaderstreamsStreamReaderProtocolStreamWriterr)   r,   )r5   rL   adapterrz   protocolr{   s         r   rK   z%QuicConnectionProtocol._create_stream   sf     $D)4%%'??77?%%gxL*0Y'v~r   c                     t        | j                  | j                  j                               }d | _        d | _        | j
                  j                  |       | j                          | j                          y )NrD   )	maxr.   r)   rG   r-   r+   handle_timerrn   r;   )r5   rE   s     r   r^   z$QuicConnectionProtocol._handle_timer   sU    $..$**//"34

C(r   c                 R   | j                   j                         }|
t        |t        j                        r| j                  |j                         nt        |t        j                        r| j                  |j                         nlt        |t        j                        r| j                          | j                  (| j                  }d | _        |j                  t               | j                  j                         D ]  }|j                  t                | j                  j!                          | j"                  j%                          nt        |t        j&                        r8| j                  | j                  }d| _        d | _        |j+                  d        nSt        |t        j,                        r9| j                  j/                  |j0                  d       }||j+                  d        | j3                  |       | j                   j                         }|	y y NT)r+   
next_eventrr   r   ConnectionIdIssuedr1   connection_idConnectionIdRetiredr2   rs   r3   r(   set_exceptionConnectionErrorr*   rt   clearr&   setHandshakeCompletedr'   
set_resultPingAcknowledgedpoprV   r|   )r5   rp   rU   s      r   rn   z&QuicConnectionProtocol._process_events   s   

%%'%!:!:;2253F3FGE6#=#=>33E4G4GHE6#>#>?335 ))5!33F-1D*((9 #00779 :F((9:""((*  "E6#<#<=))5!33F&*DO-1D*%%d+E6#:#:;++//		4@%%%d+$$U+JJ))+E? r   c                 r    | j                   +| j                  j                  | j                        | _         y y r   )r/   r)   	call_soonr;   r<   s    r   _transmit_soonz%QuicConnectionProtocol._transmit_soon   s.    &"&**"6"6t}}"ED 'r   r   )r8   N)T)F)'__name__
__module____qualname__r   r   QuicStreamHandlerr7   r:   r   NO_ERRORintstrrA   r   rF   boolr   r#   r   r   rM   rO   rW   r;   rc   re   BaseTransportrj   r	   rm   r   ro   r   	QuicEventr|   rK   r^   rn   r   r   r   r   r   r      sE   RV5"54<=N4O54 (00  
	&N d  )..!%.	w##W%9%99	:.	%"0"9E)>)> E4 EeE4K&8  SW ")9)9 "d "*	w##W%9%99	:!,FFr   r   c                   \    e Zd ZdedefdZdefdZddede	de	fd	Z
d
 Zd Zd ZdefdZy)r~   r   rL   c                 .    || _         || _        d| _        y )NF)r   rL   _closing)r5   r   rL   s      r   r7   zQuicStreamAdapter.__init__   s     "r   r8   c                      yr   r   r<   s    r   can_write_eofzQuicStreamAdapter.can_write_eof   s    r   Nnamedefaultc                 &    |dk(  r| j                   S y)zC
        Get information about the underlying QUIC stream.
        rL   N)rL   )r5   r   r   s      r   get_extra_infoz QuicStreamAdapter.get_extra_info   s     ;>>! r   c                     | j                   j                  j                  | j                  |       | j                   j	                          y r   )r   r+   send_stream_datarL   r   )r5   r_   s     r   writezQuicStreamAdapter.write  s0    ,,T^^TB$$&r   c                     | j                   ry d| _         | j                  j                  j                  | j                  dd       | j                  j                          y )NTr   )ry   )r   r   r+   r   rL   r   r<   s    r   	write_eofzQuicStreamAdapter.write_eof  sF    ==,,T^^ST,R$$&r   c                 $    | j                          y r   )r   r<   s    r   rA   zQuicStreamAdapter.close  s    r   c                     | j                   S r   )r   r<   s    r   
is_closingzQuicStreamAdapter.is_closing  s    }}r   r   )r   r   r   r   r   r7   r   r   r   r   r   r   r   rA   r   r   r   r   r~   r~      sW    !7 C 
t "3 " " "''D r   r~   )r#   typingr   r   r   r   r   r   r	   r
   r   r   quic.connectionr   r   quic.packetr   rm   QuicConnectionIdHandlerr   r   r   DatagramProtocolr   	Transportr~   r   r   r   <module>r      su     J J J  < '"E7D=1 g22G4H4HI4OP bFW55 bFJ)) r   