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mZ d dl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)ÚBaseQuicStreamÚAsyncQuicConnectionÚAsyncQuicManagerÚQUIC_MAX_DATAGRAMc                       sX   e Zd Z‡ f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__© ú\/home/www/findmeahotel.co.uk/times_travel/venv/lib/python3.8/site-packages/dns/quic/_trio.pyr
      s    zTrioQuicStream.__init__c              
   Ã   sR   | j  |¡rd S || _| j4 I d H š | j ¡ I d H  W 5 Q I d H R X d| _q d S ©Nr   )Ú_bufferZhaveZ
_expectingr   Úwait)r   Úamountr   r   r   Úwait_for   s     zTrioQuicStream.wait_forNc              
   Ã   sz   |d krt d ƒ}n
t |¡}|P |  d¡I d H  t d| j d¡¡\}|  |¡I d H  | j |¡W  5 Q R £ S Q R X d S )Né   z!H)r   r   Zmove_on_afterr   ÚstructÚunpackr   Úget)r   ÚtimeoutÚcontextÚsizer   r   r   Úreceive%   s    

zTrioQuicStream.receiveFc                 Ã   s&   |   |¡}| j | j||¡I d H  d S r   )Z_encapsulateÚ_connectionÚwriteZ
_stream_id)r   ÚdatagramÚis_endÚdatar   r   r   Úsend0   s    
zTrioQuicStream.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_input4   s    zTrioQuicStream._add_inputc                 Ã   s   |   ¡  d S r   )Ú_close©r   r   r   r   Úclose9   s    zTrioQuicStream.closec                 Ã   s   | S r   r   r+   r   r   r   Ú
__aenter__>   s    zTrioQuicStream.__aenter__c              
   Ã   s<   |   ¡ I d H  | j4 I d H š | j ¡  W 5 Q I d H R X dS ©NF)r,   r   r(   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú	__aexit__A   s    zTrioQuicStream.__aexit__)N)F)Ú__name__Ú
__module__Ú__qualname__r
   r   r!   r'   r)   r,   r-   r2   Ú__classcell__r   r   r   r   r      s   	

r   c                       sP   e Zd Zd‡ fdd„	Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	‡  Z
S )ÚTrioQuicConnectionNc                    sj   t ƒ  ||||||¡ tj | jtjd¡| _| jrLtj t	j
 | j| j¡¡ t ¡ | _t ¡ | _d | _d S r   )r	   r
   r   ÚsocketZ_afÚ
SOCK_DGRAMÚ_socketÚ_sourceÚbindÚdnsZinetZlow_level_address_tupleÚEventÚ_handshake_completeÚ	_run_doneÚ_worker_scope)r   r   ÚaddressÚportÚsourceÚsource_portÚmanagerr   r   r   r
   I   s    

zTrioQuicConnection.__init__c              	   Ã   sÊ   | j  | j¡I d H  | jsÆ|  d¡\}}tjt ¡ | d4| _| j  	t
¡I d H }| j || jd t ¡ ¡ W 5 Q R X d | _|  |¡ | j t ¡ ¡}|D ]\}}| j  |¡I d H  qš|  ¡ I d H  qd S )NF)Údeadliner   )r:   ÚconnectÚ_peerÚ_doneZ_get_timer_valuesr   ZCancelScopeÚcurrent_timerA   Úrecvr   r"   Zreceive_datagramÚtimeZ_handle_timerZdatagrams_to_sendr'   Ú_handle_events)r   Z
expirationÚintervalr$   Z	datagramsÚ_r   r   r   Ú_workerR   s    
ÿ$
zTrioQuicConnection._workerc                 Ã   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   Té   é
   )r"   Z
next_eventÚ
isinstanceÚaioquicZquicÚeventsZStreamDataReceivedÚ_streamsr   r   r)   r&   Z
end_streamZHandshakeCompletedr?   ÚsetZConnectionTerminatedZStreamResetrJ   r:   r,   r   Úsleep)r   ÚcountÚeventÚstreamr   r   r   rN   b   s,    
 ÿþ
z!TrioQuicConnection._handle_eventsFc                 Ã   s(   | j  |||¡ | jd k	r$| j ¡  d S r   )r"   Zsend_stream_datarA   Úcancel)r   r\   r&   r%   r   r   r   r#   y   s    
zTrioQuicConnection.writec              
   Ã   sF   | j r
d S t ¡ 4 I d H š}| | j¡ W 5 Q I d H R X | j ¡  d S r   )Ú_closedr   Zopen_nurseryÚ
start_soonrQ   r@   rX   )r   Únurseryr   r   r   Úrun~   s
    zTrioQuicConnection.runc                 Ã   s4   | j  ¡ I d H  | j d¡}t| |ƒ}|| j|< |S r.   )r?   r   r"   Zget_next_available_stream_idr   rW   )r   r   r\   r   r   r   Úmake_stream…   s
    

zTrioQuicConnection.make_streamc                 Ã   sX   | j sT| j | jd | jd ¡ d| _ | j ¡  | jd k	rD| j ¡  | j 	¡ I d H  d S )Nr   rR   T)
r^   Ú_managerÚclosedrI   r"   r,   rA   r]   r@   r   r+   r   r   r   r,   Œ   s    


zTrioQuicConnection.close)N)F)r3   r4   r5   r
   rQ   rN   r#   ra   rb   r,   r6   r   r   r   r   r7   H   s   	
r7   c                       s>   e Zd Zdejf‡ fdd„	Zddd„Zdd	„ Zd
d„ Z‡  Z	S )ÚTrioQuicManagerNc                    s   t ƒ  ||t¡ || _d S r   )r	   r
   r7   Ú_nursery)r   r`   ÚconfÚverify_moder   r   r   r
   —   s    zTrioQuicManager.__init__éU  r   c                 C   s*   |   ||||¡\}}|r&| j |j¡ |S r   )Ú_connectrf   r_   ra   )r   rB   rC   rD   rE   r   Ústartr   r   r   rH   ›   s    zTrioQuicManager.connectc                 Ã   s   | S r   r   r+   r   r   r   r-   ¡   s    zTrioQuicManager.__aenter__c                 Ã   s*   t | j ¡ ƒ}|D ]}| ¡ I d H  qdS r.   )ÚlistZ_connectionsÚvaluesr,   )r   r/   r0   r1   Úconnectionsr   r   r   r   r2   ¤   s    zTrioQuicManager.__aexit__)ri   Nr   )
r3   r4   r5   ÚsslÚCERT_REQUIREDr
   rH   r-   r2   r6   r   r   r   r   re   –   s   
re   )r8   ro   r   rM   Zaioquic.quic.configurationrU   Zaioquic.quic.connectionZaioquic.quic.eventsr   Zdns.inetr=   Zdns._asyncbackendr   Zdns.quic._commonr   r   r   r   r   r7   re   r   r   r   r   Ú<module>   s   1N