U
    î¤ùdC  ã                   @   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__© ú5/tmp/pip-unpacked-wheel-pk5slln3/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)r1   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/   r1   r2   r7   Ú__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                 Ã   sb  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sj|d | jd kr®qj| j || jd t ¡ ¡ | j4 I d H š | j ¡  W 5 Q I d H R X qjW 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   é   )Ú_donerA   Ú
notify_allr?   Úsetr   ZinetZaf_for_addressZ_addressZasyncbackendÚget_backendZmake_socketÚsocketÚ
SOCK_DGRAMÚ_sourceÚ_peerr=   r@   Úrecvfromr   r(   Zreceive_datagramÚtimeÚ	Exception)r   ÚafÚbackendr*   rD   r   r   r   Ú	_receiverW   s@        ÿ
 ÿ$  ÿ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   )rA   r   r   r   r   r   Ú_wait_for_wake_timerw   s    z*AsyncioQuicConnection._wait_for_wake_timerc                 Ã   s¸   | j  ¡ I d H  | js´| j t ¡ ¡}|D ]2\}}|| jd ksDt‚| 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   rJ   r(   Zdatagrams_to_sendrS   rQ   ÚAssertionErrorr=   ÚsendtoZ_get_timer_valuesr   r   rX   rT   Z_handle_timerÚ_handle_events)r   Z	datagramsr*   rD   r    Úintervalr   r   r   Ú_sender{   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  nLt|tjjjƒrp| j ¡  n0t|tjjjƒst|tjjjƒr d| _| j ¡  |d7 }|dkrd}t d¡I d H  qd S )Nr   TrI   é
   )r(   Z
next_eventÚ
isinstanceÚaioquicZquicÚeventsZStreamDataReceivedÚ_streamsr%   r   r/   r,   Z
end_streamZHandshakeCompletedr?   rL   ZConnectionTerminatedZStreamResetrJ   rB   Úcancelr   Ú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_datarA   rK   )r   rg   r,   r+   r   r   r   r)   ¡   s    zAsyncioQuicConnection.writec                 C   s.   | j r
d S t |  ¡ ¡| _t |  ¡ ¡| _d S r   )Ú_closedr   ZTaskrW   rB   r]   rC   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 r3   )r   r   r?   r   r   r   r   r   rJ   r   r(   Zget_next_available_stream_idr   rb   )r   r!   r   rg   r   r   r   Úmake_stream¬   s    

z!AsyncioQuicConnection.make_streamc              
   Ã   sÈ   | j sÄ| j | jd | jd ¡ d| _ | j ¡  | j ¡  | j ¡ I d H  | 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 d S )Nr   rI   T)rh   Ú_managerÚclosedrQ   r(   r1   r@   rL   r=   rA   rK   rB   r   ZCancelledErrorrC   r   r   r   r   r1   ¸   s     

zAsyncioQuicConnection.close)N)F)N)r8   r9   r:   r
   rW   rX   r]   r[   r)   ri   rj   r1   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   c                 C   s$   |   ||||¡\}}|r | ¡  |S r   )Ú_connectri   )r   rD   rE   rF   rG   r   Ústartr   r   r   ÚconnectÐ   s    zAsyncioQuicManager.connectc                 Ã   s   | S r   r   r   r   r   r   r2   Ö   s    zAsyncioQuicManager.__aenter__c                 Ã   s*   t | j ¡ ƒ}|D ]}| ¡ I d H  qdS r3   )ÚlistZ_connectionsÚvaluesr1   )r   r4   r5   r6   Úconnectionsr   r   r   r   r7   Ù   s    zAsyncioQuicManager.__aexit__)rp   Nr   )
r8   r9   r:   ÚsslÚCERT_REQUIREDr
   rs   r2   r7   r;   r   r   r   r   rm   Ì   s   
rm   )r   rN   rw   r#   rS   Zaioquic.quic.configurationr`   Zaioquic.quic.connectionZaioquic.quic.eventsZdns.asyncbackendr   Zdns.exceptionZdns.inetZdns.quic._commonr   r   r   r   r   r   r<   rm   r   r   r   r   Ú<module>   s   	4