U
    <æ{fß  ã                   @   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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                       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 )ÚAsyncioQuicStreamc                    s   t ƒ  ||¡ t ¡ | _d S ©N)ÚsuperÚ__init__ÚasyncioÚ	ConditionÚ_wake_up)ÚselfÚ
connectionÚ	stream_id©Ú	__class__© úP/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/quic/_asyncio.pyr
      s    zAsyncioQuicStream.__init__c              
   Ã   s4   | j 4 I d H š | j  ¡ I d H  W 5 Q I d H R X d S r   )r   Úwait©r   r   r   r   Ú_wait_for_wake_up   s    z#AsyncioQuicStream._wait_for_wake_upc                 Ã   sd   |   |¡}| j |¡rd S || _zt |  ¡ |¡I d H  W n tk
rV   tj	j
‚Y nX d| _q d S )Nr   )Z_timeout_from_expirationÚ_bufferZhaveZ
_expectingr   Úwait_forr   ÚTimeoutErrorÚdnsÚ	exceptionÚTimeout)r   ÚamountÚ
expirationÚtimeoutr   r   r   r   "   s    
zAsyncioQuicStream.wait_forNc                 Ã   sP   |   |¡}|  d|¡I d H  t d| j d¡¡\}|  ||¡I d H  | j |¡S )Né   z!H)Z_expiration_from_timeoutr   ÚstructÚunpackr   Úget)r   r    r   Úsizer   r   r   Úreceive.   s
    
zAsyncioQuicStream.receiveFc                 Ã   s&   |   |¡}| j | j||¡I d H  d S r   )Z_encapsulateÚ_connectionÚwriteZ
_stream_id)r   ÚdatagramÚis_endÚdatar   r   r   Úsend5   s    
zAsyncioQuicStream.sendc              
   Ã   s:   |   ||¡r6| j4 I d H š | j ¡  W 5 Q I d H R X d S r   )Z_common_add_inputr   Únotify)r   r+   r*   r   r   r   Ú
_add_input9   s    zAsyncioQuicStream._add_inputc                 Ã   s   |   ¡  d S r   )Ú_closer   r   r   r   Úclose>   s    zAsyncioQuicStream.closec                 Ã   s   | S r   r   r   r   r   r   Ú
__aenter__C   s    zAsyncioQuicStream.__aenter__c              
   Ã   s<   |   ¡ I d H  | j4 I d H š | j ¡  W 5 Q I d H R X dS ©NF)r0   r   r-   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__F   s    zAsyncioQuicStream.__aexit__)N)F)Ú__name__Ú
__module__Ú__qualname__r
   r   r   r&   r,   r.   r0   r1   r6   Ú__classcell__r   r   r   r   r      s   

r   c                       sb   e Zd Zd‡ f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                    sJ   t ƒ  ||||||¡ d | _t ¡ | _t ¡ | _t ¡ | _d | _	d | _
d S r   )r	   r
   Ú_socketr   ÚEventÚ_handshake_completeÚ_socket_createdr   Ú_wake_timerÚ_receiver_taskÚ_sender_task)r   r   ÚaddressÚportÚsourceÚsource_portÚmanagerr   r   r   r
   N   s    


zAsyncioQuicConnection.__init__c                 Ã   sZ  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 šŠ | j sì| j td ¡I d H \}}|d | jd ksh|d | jd kr¬qh| j ||t ¡ ¡ | j4 I d H š | j ¡  W 5 Q I d H R X qhW 5 Q I d H R X W n tk
r   Y nX W 5 d| _ | j4 I d H š | j ¡  W 5 Q I d H R X | j ¡  X d S )NTr   r   é   )Ú_doner@   Ú
notify_allr>   Úsetr   ZinetZaf_for_addressÚ_addressZasyncbackendÚget_backendZmake_socketÚsocketÚ
SOCK_DGRAMÚ_sourceÚ_peerr<   r?   Úrecvfromr   r'   Zreceive_datagramÚtimeÚ	Exception)r   ÚafÚbackendr)   rC   r   r   r   Ú	_receiverW   s8        ÿ
 ÿ$0
zAsyncioQuicConnection._receiverc              
   Ã   s4   | j 4 I d H š | j  ¡ I d H  W 5 Q I d H R X d S r   )r@   r   r   r   r   r   Ú_wait_for_wake_timeru   s    z*AsyncioQuicConnection._wait_for_wake_timerc                 Ã   s´   | j  ¡ I d H  | js°| j t ¡ ¡}|D ].\}}|| jks@t‚| j 	|| jd ¡I d H  q*|  
¡ \}}zt |  ¡ |¡I d H  W n tk
r”   Y nX |  |¡ |  ¡ I d H  qd S r   )r?   r   rI   r'   Zdatagrams_to_sendrS   rQ   ÚAssertionErrorr<   ÚsendtoZ_get_timer_valuesr   r   rX   rT   Z_handle_timerÚ_handle_events)r   Z	datagramsr)   rC   r   Úintervalr   r   r   Ú_sendery   s    
zAsyncioQuicConnection._senderc                 Ã   sð   d}| j  ¡ }|d krd S t|tjjjƒrT| j |j	¡}|rÆ| 
|j|j¡I d H  nrt|tjjjƒrp| j ¡  nVt|tjjjƒr’d| _| j ¡  n4t|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d S )Nr   Tó    rH   é
   )r'   Z
next_eventÚ
isinstanceÚaioquicZquicÚeventsZStreamDataReceivedÚ_streamsr$   r   r.   r+   Z
end_streamZHandshakeCompletedr>   rK   ZConnectionTerminatedrI   rA   ÚcancelZStreamResetr   Úsleep)r   ÚcountÚeventÚstreamr   r   r   r[   ˆ   s*    
z$AsyncioQuicConnection._handle_eventsFc              
   Ã   s>   | j  |||¡ | j4 I d H š | j ¡  W 5 Q I d H R X d S r   )r'   Zsend_stream_datar@   rJ   )r   rh   r+   r*   r   r   r   r(   ¢   s    zAsyncioQuicConnection.writec                 C   s.   | j r
d S t |  ¡ ¡| _t |  ¡ ¡| _d S r   )Ú_closedr   ÚTaskrW   rA   r]   rB   r   r   r   r   Úrun§   s    zAsyncioQuicConnection.runc                 Ã   sh   zt  | j ¡ |¡I d H  W n tk
r8   tjj‚Y nX | jrDt	‚| j
 d¡}t| |ƒ}|| j|< |S r2   )r   r   r>   r   r   r   r   r   rI   r   r'   Zget_next_available_stream_idr   rc   )r   r    r   rh   r   r   r   Úmake_stream­   s    

z!AsyncioQuicConnection.make_streamc              
   Ã   sÈ   | j sÄ| j | jd | jd ¡ d| _ | j ¡  | j ¡  | j4 I d H š | j 	¡  W 5 Q I d H R X z| j
I d H  W n tjk
rŠ   Y nX z| jI d H  W n tjk
r²   Y nX | j ¡ I d H  d S )Nr   rH   T)ri   Ú_managerÚclosedrQ   r'   r0   r?   rK   r@   rJ   rA   r   ÚCancelledErrorrB   r<   r   r   r   r   r0   ¹   s     

zAsyncioQuicConnection.close)N)F)N)r7   r8   r9   r
   rW   rX   r]   r[   r(   rk   rl   r0   r:   r   r   r   r   r;   M   s   	

r;   c                       s@   e Zd Zdejdf‡ fdd„	Zddd„Zd	d
„ Zdd„ Z‡  Z	S )ÚAsyncioQuicManagerNc                    s   t ƒ  ||t|¡ d S r   )r	   r
   r;   )r   ÚconfÚverify_modeZserver_namer   r   r   r
   Î   s    zAsyncioQuicManager.__init__éU  r   Tc                 C   s&   |   |||||¡\}}|r"| ¡  |S r   )Ú_connectrk   )r   rC   rD   rE   rF   Zwant_session_ticketr   Ústartr   r   r   ÚconnectÑ   s        ÿzAsyncioQuicManager.connectc                 Ã   s   | S r   r   r   r   r   r   r1   Û   s    zAsyncioQuicManager.__aenter__c                 Ã   s*   t | j ¡ ƒ}|D ]}| ¡ I d H  qdS r2   )ÚlistZ_connectionsÚvaluesr0   )r   r3   r4   r5   Úconnectionsr   r   r   r   r6   Þ   s    zAsyncioQuicManager.__aexit__)rs   Nr   T)
r7   r8   r9   ÚsslÚCERT_REQUIREDr
   rv   r1   r6   r:   r   r   r   r   rp   Í   s          ÿ

rp   )r   rN   rz   r"   rS   Zaioquic.quic.configurationra   Zaioquic.quic.connectionZaioquic.quic.eventsZdns.asyncbackendr   Zdns.exceptionZdns.inetZdns.quic._commonr   r   r   r   r   r   r;   rp   r   r   r   r   Ú<module>   s   	4 