o
    Þý°jì$  ã                   @   s¼   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d dlZd dl	Zd dl
Z
d dlZd dlZd dlmZ d dlmZmZmZmZmZ G dd„ deƒZG dd„ deƒZG dd	„ d	eƒZdS )
é    N)ÚNullContext)ÚQUIC_MAX_DATAGRAMÚAsyncQuicConnectionÚAsyncQuicManagerÚBaseQuicStreamÚUnexpectedEOFc                       s`   e Zd Z‡ fdd„Zdd„ Zdd„ Zddd	„Zddd„Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚTrioQuicStreamc                    s   t ƒ  ||¡ t ¡ | _d S ©N)ÚsuperÚ__init__ÚtrioÚ	ConditionÚ_wake_up)ÚselfÚ
connectionÚ	stream_id©Ú	__class__© ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/dns/quic/_trio.pyr      s   zTrioQuicStream.__init__c              	   Ã   sl   �	 | j  |¡r
d S || _| j4 I d H š | j ¡ I d H  W d   ƒI d H  n1 I d H s-w   Y  d| _q)NTr   )Ú_bufferÚhaveÚ
_expectingr   Úwait)r   Úamountr   r   r   Úwait_for    s   €(ÿúzTrioQuicStream.wait_forc              	   Ã   s^   �	 | j  ¡ r	d S | j4 I d H š | j ¡ I d H  W d   ƒI d H  n1 I d H s)w   Y  qr	   )r   Úseen_endr   r   ©r   r   r   r   Úwait_for_end)   s   €
(ÿýzTrioQuicStream.wait_for_endNc                 Ã   s¼   �|d u r
t d ƒ}nt |¡}|�A | j ¡ r*|  ¡ I d H  | j ¡ W  d   ƒ S |  d¡I d H  t	 
d| j d¡¡\}|  |¡I d H  | j |¡W  d   ƒ S 1 sUw   Y  tjj‚)Né   z!H)r   r   Úmove_on_afterÚ_connectionÚis_h3r   r   Úget_allr   ÚstructÚunpackÚgetÚdnsÚ	exceptionÚTimeout)r   ÚtimeoutÚcontextÚsizer   r   r   Úreceive0   s   €


ý
 ø	zTrioQuicStream.receiveFc                 Ã   s(   �|   |¡}| j | j||¡I d H  d S r	   )Ú_encapsulater!   ÚwriteÚ
_stream_id)r   ÚdatagramÚis_endÚdatar   r   r   Úsend@   s   €
zTrioQuicStream.sendc              	   Ã   s\   �|   ||¡r,| j4 I d H š | j ¡  W d   ƒI d H  d S 1 I d H s%w   Y  d S d S r	   )Ú_common_add_inputr   Únotify)r   r3   r2   r   r   r   Ú
_add_inputD   s   €.ÿÿzTrioQuicStream._add_inputc                 Ã   s   �|   ¡  d S r	   )Ú_closer   r   r   r   ÚcloseI   s   €zTrioQuicStream.closec                 Ã   ó   �| S r	   r   r   r   r   r   Ú
__aenter__N   ó   €zTrioQuicStream.__aenter__c              	   Ã   sZ   �|   ¡ I d H  | j4 I d H š | j ¡  W d   ƒI d H  dS 1 I d H s&w   Y  dS ©NF)r9   r   r6   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__Q   s   €ÿþzTrioQuicStream.__aexit__r	   ©F)Ú__name__Ú
__module__Ú__qualname__r   r   r   r-   r4   r7   r9   r;   rA   Ú__classcell__r   r   r   r   r      s    	

r   c                       sR   e Zd Zd‡ fdd„	Zdd„ Zdd„ Zdd	d
„Zdd„ Zddd„Zdd„ Z	‡  Z
S )ÚTrioQuicConnectionNc                    sP   t ƒ  ||||||¡ tj | jtjd¡| _t ¡ | _t ¡ | _	d | _
d| _d S )Nr   F)r
   r   r   ÚsocketÚ_afÚ
SOCK_DGRAMÚ_socketÚEventÚ_handshake_completeÚ	_run_doneÚ_worker_scopeÚ_send_pending)r   r   ÚaddressÚportÚsourceÚsource_portÚmanagerr   r   r   r   Y   s   


zTrioQuicConnection.__init__c                 Ã   sp  �z¨| j r| j tj | j | j¡¡I d H  | j | j¡I d H  | j	sš|  
d¡\}}| jr.d}tjt ¡ | d��| _| j t¡I d H }| j || jt ¡ ¡ W d   ƒ n1 sXw   Y  d | _|  |¡ |  ¡ I d H  d| _| j t ¡ ¡}|D ]\}}| j |¡I d H  qy| j	r"W d| _	| j ¡  | j ¡  d S W d| _	| j ¡  | j ¡  d S d| _	| j ¡  | j ¡  w )NFg        )ÚdeadlineT)Ú_sourcerK   Úbindr'   ÚinetÚlow_level_address_tuplerI   ÚconnectÚ_peerÚ_doneÚ_get_timer_valuesrP   r   ÚCancelScopeÚcurrent_timerO   Úrecvr   r!   Úreceive_datagramÚtimeÚ_handle_timerÚ_handle_eventsÚdatagrams_to_sendr4   r9   rM   Úset)r   Ú
expirationÚintervalr1   Ú	datagramsÚ_r   r   r   Ú_workera   sH   €
ÿ
ÿü
è
ä
þ
zTrioQuicConnection._workerc                 Ã   s¸  �d}	 | j  ¡ }|d u rd S t|tjjjƒr�|  ¡ r{| jd us"J ‚| j 	|¡}|D ]O}t|tj
jjƒr]| j |j¡}|r\|jd u rG|j|_n	|jd u rP|j|_|jr\| dd¡I d H  q*t|tj
jjƒry| j |j¡}|ry| |j|j¡I d H  q*nN| j |j¡}|r�| |j|j¡I d H  n9t|tjjjƒrž| j ¡  n+t|tjjjƒr¯d| _| j ¡  nt|tjjjƒrÉ| j |j¡}|rÉ| dd¡I d H  |d7 }|dkrÛd}t  d¡I d H  q)Nr   Tó    é   é
   )!r!   Ú
next_eventÚ
isinstanceÚaioquicÚquicÚeventsÚStreamDataReceivedr"   Ú_h3_connÚhandle_eventÚh3ÚHeadersReceivedÚ_streamsr&   r   Ú_headersÚheadersÚ	_trailersÚstream_endedr7   ÚDataReceivedr3   Ú
end_streamÚHandshakeCompletedrM   rg   ÚConnectionTerminatedr]   rK   r9   ÚStreamResetr   Úsleep)r   ÚcountÚeventÚ	h3_eventsÚh3_eventÚstreamr   r   r   re   †   s^   €



€
ÿ€ó€×z!TrioQuicConnection._handle_eventsFc                 Ã   s4   �| j  |||¡ d| _| jd ur| j ¡  d S d S )NT)r!   Úsend_stream_datarP   rO   Úcancel)r   r‰   r3   r2   r   r   r   r/   ³   s   €
ÿzTrioQuicConnection.writec              	   Ã   sb   �| j rd S t ¡ 4 I d H š}| | j¡ W d   ƒI d H  n1 I d H s%w   Y  | j ¡  d S r	   )Ú_closedr   Úopen_nurseryÚ
start_soonrl   rN   rg   )r   Únurseryr   r   r   Úrun¹   s   €(ÿzTrioQuicConnection.runc                 Ã   sˆ   �|d u r
t d ƒ}nt |¡}|�' | j ¡ I d H  | jrt‚| j d¡}t	| |ƒ}|| j
|< |W  d   ƒ S 1 s;w   Y  tjj‚r=   )r   r   r    rM   r   r]   r   r!   Úget_next_available_stream_idr   rz   r'   r(   r)   )r   r*   r+   r   r‰   r   r   r   Úmake_streamÀ   s   €



 ùzTrioQuicConnection.make_streamc                 Ã   sn   �| j s5| jd ur| j | jd | jd ¡ d| _ | j ¡  d| _| jd ur+| j ¡  | j	 
¡ I d H  d S d S )Nr   rn   T)rŒ   Ú_managerÚclosedr\   r!   r9   rP   rO   r‹   rN   r   r   r   r   r   r9   Ï   s   €



øzTrioQuicConnection.closer	   rB   )rC   rD   rE   r   rl   re   r/   r�   r’   r9   rF   r   r   r   r   rG   X   s    %
-
rG   c                       sD   e Zd Zdejddf‡ fdd„	Z	ddd	„Zd
d„ Zdd„ Z‡  Z	S )ÚTrioQuicManagerNFc                    s   t ƒ  ||t||¡ || _d S r	   )r
   r   rG   Ú_nursery)r   r�   ÚconfÚverify_modeÚserver_namerx   r   r   r   r   Ü   s   
zTrioQuicManager.__init__éU  r   Tc                 C   s,   |   |||||¡\}}|r| j |j¡ |S r	   )Ú_connectr–   rŽ   r�   )r   rQ   rR   rS   rT   Úwant_session_ticketr   Ústartr   r   r   r[   ç   s   
ÿzTrioQuicManager.connectc                 Ã   r:   r	   r   r   r   r   r   r;   ñ   r<   zTrioQuicManager.__aenter__c                 Ã   s,   �t | j ¡ ƒ}|D ]	}| ¡ I d H  q
dS r=   )ÚlistÚ_connectionsÚvaluesr9   )r   r>   r?   r@   Úconnectionsr   r   r   r   rA   ô   s
   €zTrioQuicManager.__aexit__)rš   Nr   T)
rC   rD   rE   ÚsslÚCERT_REQUIREDr   r[   r;   rA   rF   r   r   r   r   r•   Û   s    ú
ÿ
r•   )rH   r¢   r$   rc   Úaioquic.h3.connectionrr   Úaioquic.h3.eventsÚaioquic.quic.configurationÚaioquic.quic.connectionÚaioquic.quic.eventsr   Údns.exceptionr'   Údns.inetÚdns._asyncbackendr   Údns.quic._commonr   r   r   r   r   r   rG   r•   r   r   r   r   Ú<module>   s$   	= 