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__© úI/var/www/html/chatgpt/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   )Ú_bufferZhaveZ
_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   Z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	   )Z_encapsulater   ÚwriteZ
_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	   )Z_common_add_inputr   Únotify)r   r.   r-   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)r3   r   r0   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__Q   s   €ÿþzTrioQuicStream.__aexit__r	   ©F)Ú__name__Ú
__module__Ú__qualname__r   r   r   r*   r/   r1   r3   r5   r;   Ú__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)Ú_sourcerE   Úbindr$   ZinetZlow_level_address_tuplerC   ÚconnectÚ_peerÚ_doneZ_get_timer_valuesrJ   r   ZCancelScopeÚcurrent_timerI   Úrecvr   r   Zreceive_datagramÚtimeZ_handle_timerÚ_handle_eventsZdatagrams_to_sendr/   r3   rG   Úset)r   Z
expirationÚintervalr,   Z	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   Z
next_eventÚ
isinstanceÚaioquicZquicÚeventsZStreamDataReceivedr   Z_h3_connZhandle_eventÚh3ZHeadersReceivedÚ_streamsr#   r   Ú_headersÚheadersZ	_trailersZstream_endedr1   ZDataReceivedr.   Z
end_streamZHandshakeCompletedrG   rZ   ZConnectionTerminatedrU   rE   r3   ZStreamResetr   Úsleep)r   ÚcountÚeventZ	h3_eventsZh3_eventÚstreamr   r   r   rY   †   s^   €



€
ÿ€ó€×z!TrioQuicConnection._handle_eventsFc                 Ã   s4   �| j  |||¡ d| _| jd ur| j ¡  d S d S )NT)r   Zsend_stream_datarJ   rI   Úcancel)r   rk   r.   r-   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   Zopen_nurseryÚ
start_soonr]   rH   rZ   )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‚r7   )r   r   r   rG   r   rU   r   r   Zget_next_available_stream_idr   re   r$   r%   r&   )r   r'   r(   r   rk   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   r_   T)rm   Ú_managerÚclosedrT   r   r3   rJ   rI   rl   rH   r   r   r   r   r   r3   Ï   s   €



øzTrioQuicConnection.closer	   r<   )r=   r>   r?   r   r]   rY   r+   rp   rq   r3   r@   r   r   r   r   rA   X   s    %
-
rA   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   rA   Ú_nursery)r   ro   ZconfÚverify_modeZserver_namerd   r   r   r   r   Ü   s   
zTrioQuicManager.__init__éU  r   Tc                 C   s,   |   |||||¡\}}|r| j |j¡ |S r	   )Ú_connectru   rn   rp   )r   rK   rL   rM   rN   Zwant_session_ticketr   Ústartr   r   r   rS   ç   s   
ÿzTrioQuicManager.connectc                 Ã   r4   r	   r   r   r   r   r   r5   ñ   r6   zTrioQuicManager.__aenter__c                 Ã   s,   �t | j ¡ ƒ}|D ]	}| ¡ I d H  q
dS r7   )ÚlistZ_connectionsÚvaluesr3   )r   r8   r9   r:   Úconnectionsr   r   r   r   r;   ô   s
   €zTrioQuicManager.__aexit__)rw   Nr   T)
r=   r>   r?   ÚsslÚCERT_REQUIREDr   rS   r5   r;   r@   r   r   r   r   rt   Û   s    ú
ÿ
rt   )rB   r}   r!   rX   Zaioquic.h3.connectionrb   Zaioquic.h3.eventsZaioquic.quic.configurationZaioquic.quic.connectionZaioquic.quic.eventsr   Zdns.exceptionr$   Zdns.inetZdns._asyncbackendr   Zdns.quic._commonr   r   r   r   r   r   rA   rt   r   r   r   r   Ú<module>   s$   	= 