o
    Þý°jJ(  ã                   @   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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)ÚQUIC_MAX_DATAGRAMÚAsyncQuicConnectionÚAsyncQuicManagerÚBaseQuicStreamÚUnexpectedEOFc                       sh   e Zd Z‡ fdd„Z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 )ÚAsyncioQuicStreamc                    s   t ƒ  ||¡ t ¡ | _d S ©N)ÚsuperÚ__init__ÚasyncioÚ	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/_asyncio.pyr
      s   zAsyncioQuicStream.__init__c              	   Ã   sR   �| j 4 I d H š | j  ¡ I d H  W d   ƒI d H  d S 1 I d H s"w   Y  d S r   )r   Úwait©r   r   r   r   Ú_wait_for_wake_up    s   €.ÿz#AsyncioQuicStream._wait_for_wake_upc                 Ã   s^   �	 |   |¡}| j |¡rd S || _zt |  ¡ |¡I d H  W n ty*   tj	j
‚w d| _q)NTr   )Ú_timeout_from_expirationÚ_bufferÚhaveÚ
_expectingr   Úwait_forr   ÚTimeoutErrorÚdnsÚ	exceptionÚTimeout)r   ÚamountÚ
expirationÚtimeoutr   r   r   r   $   s   €
ÿ÷zAsyncioQuicStream.wait_forc                 Ã   sP   �	 |   |¡}| j ¡ rd S zt |  ¡ |¡I d H  W n ty&   tjj	‚w qr   )
r   r   Úseen_endr   r   r   r   r   r   r    )r   r"   r#   r   r   r   Úwait_for_end0   s   €

ÿúzAsyncioQuicStream.wait_for_endNc                 Ã   sv   �|   |¡}| j ¡ r|  |¡I d H  | j ¡ S |  d|¡I d H  t d| j 	d¡¡\}|  ||¡I d H  | j 	|¡S )Né   z!H)
Ú_expiration_from_timeoutÚ_connectionÚis_h3r%   r   Úget_allr   ÚstructÚunpackÚget)r   r#   r"   Úsizer   r   r   Úreceive:   s   €


zAsyncioQuicStream.receiveFc                 Ã   s(   �|   |¡}| j | j||¡I d H  d S r   )Ú_encapsulater(   ÚwriteÚ
_stream_id)r   ÚdatagramÚis_endÚdatar   r   r   ÚsendE   s   €
zAsyncioQuicStream.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   r5   r4   r   r   r   Ú
_add_inputI   s   €.ÿÿzAsyncioQuicStream._add_inputc                 Ã   s   �|   ¡  d S r   )Ú_closer   r   r   r   ÚcloseN   s   €zAsyncioQuicStream.closec                 Ã   ó   �| S r   r   r   r   r   r   Ú
__aenter__S   ó   €zAsyncioQuicStream.__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)r;   r   r8   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__V   s   €ÿþzAsyncioQuicStream.__aexit__r   ©F)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r%   r/   r6   r9   r;   r=   rC   Ú__classcell__r   r   r   r   r      s    


r   c                       sj   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d„Z	dd„ Z
ddd„Zdd„ Z‡  ZS )ÚAsyncioQuicConnectionNc                    sP   t ƒ  ||||||¡ d | _t ¡ | _t ¡ | _t ¡ | _d | _	d | _
d| _d S r?   )r	   r
   Ú_socketr   ÚEventÚ_handshake_completeÚ_socket_createdr   Ú_wake_timerÚ_receiver_taskÚ_sender_taskÚ_wake_pending)r   r   ÚaddressÚportÚsourceÚsource_portÚmanagerr   r   r   r
   ^   s   



zAsyncioQuicConnection.__init__c              	   Ã   s|  �z¬z~t j | j¡}t j d¡}| |tjd| j	| j
d | j
d f¡I d H | _| j ¡  | j4 I d H šA | jsj| j td ¡I d H \}}|d | j
d ksU|d | j
d krVq4| j ||t ¡ ¡ |  ¡ I d H  | jr7W d   ƒI d H  n1 I d H szw   Y  W n	 ty‰   Y nw W d| _|  ¡ I d H  | j ¡  d S W d| _|  ¡ I d H  | j ¡  d S d| _|  ¡ I d H  | j ¡  w )Nr   r   é   T)r   ÚinetÚaf_for_addressÚ_addressÚasyncbackendÚget_backendÚmake_socketÚsocketÚ
SOCK_DGRAMÚ_sourceÚ_peerrJ   rM   ÚsetÚ_doneÚrecvfromr   r(   Úreceive_datagramÚtimeÚ_wakeupÚ	ExceptionrL   )r   ÚafÚbackendr3   rR   r   r   r   Ú	_receiverh   sD   €ÿ
ÿ$÷(ÿ€ÿ€üþzAsyncioQuicConnection._receiverc              	   Ã   sR   �d| _ | j4 I d H š | j ¡  W d   ƒI d H  d S 1 I d H s"w   Y  d S )NT)rQ   rN   Ú
notify_allr   r   r   r   rg   „   s
   €.ÿzAsyncioQuicConnection._wakeupc              	   Ã   s\   �| j 4 I d H š | js| j  ¡ I d H  W d   ƒI d H  n1 I d H s$w   Y  d| _d S r?   )rN   rQ   r   r   r   r   r   Ú_wait_for_wake_timer‰   s   €€(þ
z*AsyncioQuicConnection._wait_for_wake_timerc                 Ã   sÊ   �| j  ¡ I d H  | jsc| j t ¡ ¡}|D ]\}}|| jks!J ‚| jd us(J ‚| j || jd ¡I d H  q|  	¡ \}}zt
 |  ¡ |¡I d H  W n	 tyQ   Y nw |  |¡ |  ¡ I d H  | jrd S d S r   )rM   r   rc   r(   Údatagrams_to_sendrf   ra   rJ   ÚsendtoÚ_get_timer_valuesr   r   rm   rh   Ú_handle_timerÚ_handle_events)r   Ú	datagramsr3   rR   r"   Úintervalr   r   r   Ú_sender�   s"   €ÿ
ôzAsyncioQuicConnection._senderc                 Ã   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*nS| j |j¡}|r�| |j|j¡I d H  n>t|tjjjƒrž| j ¡  n0t|tjjjƒr´d| _| jd ur³| 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ó    rW   é
   )!r(   Ú
next_eventÚ
isinstanceÚaioquicÚquicÚeventsÚStreamDataReceivedr)   Ú_h3_connÚhandle_eventÚh3ÚHeadersReceivedÚ_streamsr-   r   Ú_headersÚheadersÚ	_trailersÚstream_endedr9   ÚDataReceivedr5   Ú
end_streamÚHandshakeCompletedrL   rb   ÚConnectionTerminatedrc   rO   ÚcancelÚStreamResetr   Úsleep)r   ÚcountÚeventÚ	h3_eventsÚh3_eventÚstreamr   r   r   rr   Ÿ   sb   €



€
ÿ€ó€

€Õz$AsyncioQuicConnection._handle_eventsFc                 Ã   s$   �| j  |||¡ |  ¡ I d H  d S r   )r(   Úsend_stream_datarg   )r   r’   r5   r4   r   r   r   r1   Î   s   €zAsyncioQuicConnection.writec                 C   s.   | j rd S t |  ¡ ¡| _t |  ¡ ¡| _d S r   )Ú_closedr   ÚTaskrk   rO   ru   rP   r   r   r   r   ÚrunÒ   s   zAsyncioQuicConnection.runc                 Ã   sd   �zt  | j ¡ |¡I d H  W n ty   tjj‚w | jr t	‚| j
 d¡}t| |ƒ}|| j|< |S r?   )r   r   rL   r   r   r   r   r    rc   r   r(   Úget_next_available_stream_idr   r‚   )r   r#   r   r’   r   r   r   Úmake_streamØ   s   €ÿ

z!AsyncioQuicConnection.make_streamc                 Ã   sÚ   �| j si| jd ur| j | jd | jd ¡ d| _ | j ¡  | j ¡  |  ¡ I d H  z| j	d ur6| j	I d H  W n
 t
jyA   Y nw z| jd urN| jI d H  W n
 t
jyY   Y nw | jd urk| j ¡ I d H  d S d S d S )Nr   rW   T)r”   Ú_managerÚclosedra   r(   r;   rM   rb   rg   rO   r   ÚCancelledErrorrP   rJ   r   r   r   r   r;   ä   s4   €



€ÿ
€ÿ
ízAsyncioQuicConnection.closer   rD   )rE   rF   rG   r
   rk   rg   rm   ru   rr   r1   r–   r˜   r;   rH   r   r   r   r   rI   ]   s    

/
rI   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 )ÚAsyncioQuicManagerNFc                    s   t ƒ  ||t||¡ d S r   )r	   r
   rI   )r   ÚconfÚverify_modeÚserver_namer€   r   r   r   r
   ü   s   zAsyncioQuicManager.__init__éU  r   Tc                 C   s&   |   |||||¡\}}|r| ¡  |S r   )Ú_connectr–   )r   rR   rS   rT   rU   Úwant_session_ticketr   Ústartr   r   r   Úconnect  s   
ÿzAsyncioQuicManager.connectc                 Ã   r<   r   r   r   r   r   r   r=     r>   zAsyncioQuicManager.__aenter__c                 Ã   s,   �t | j ¡ ƒ}|D ]	}| ¡ I d H  q
dS r?   )ÚlistÚ_connectionsÚvaluesr;   )r   r@   rA   rB   Úconnectionsr   r   r   r   rC     s
   €zAsyncioQuicManager.__aexit__)r    Nr   T)
rE   rF   rG   ÚsslÚCERT_REQUIREDr
   r¤   r=   rC   rH   r   r   r   r   rœ   û   s    
ÿ
ÿ
rœ   )r   r^   r©   r+   rf   Úaioquic.h3.connectionrz   Úaioquic.h3.eventsÚaioquic.quic.configurationÚaioquic.quic.connectionÚaioquic.quic.eventsÚdns.asyncbackendr   Údns.exceptionÚdns.inetÚdns.quic._commonr   r   r   r   r   r   rI   rœ   r   r   r   r   Ú<module>   s$   	B 