U
    d                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlZd dlZd dl	Z
dZG dd deZG dd dZG dd	 d	ZG d
d dZG dd deZG dd dZG dd deZdS )    N)AnyOptionali   c                   @   s   e Zd ZdS )UnexpectedEOFN)__name__
__module____qualname__ r   r   4/tmp/pip-unpacked-wheel-pk5slln3/dns/quic/_common.pyr      s   r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )Bufferc                 C   s   d| _ d| _d S )N    F)_buffer	_seen_endselfr   r   r	   __init__   s    zBuffer.__init__c                 C   s&   | j r
d S |  j|7  _|r"d| _ d S )NT)r   r   r   dataZis_endr   r   r	   put   s
    z
Buffer.putc                 C   s    t | j|krdS | jrtdS )NTF)lenr   r   r   )r   amountr   r   r	   have    s
    zBuffer.havec                 C   s   | j S N)r   r   r   r   r	   seen_end'   s    zBuffer.seen_endc                 C   s0   |  |st| jd | }| j|d  | _|S r   )r   AssertionErrorr   )r   r   r   r   r   r	   get*   s    z
Buffer.getN)r   r   r   r   r   r   r   r   r   r   r   r	   r
      s
   r
   c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )BaseQuicStreamc                 C   s   || _ || _t | _d| _d S Nr   )_connection
_stream_idr
   r   
_expecting)r   
connection	stream_idr   r   r	   r   2   s    zBaseQuicStream.__init__c                 C   s   | j S r   )r   r   r   r   r	   id8   s    zBaseQuicStream.idc                 C   s   |d k	rt   | }nd }|S r   )time)r   timeout
expirationr   r   r	   _expiration_from_timeout;   s    z'BaseQuicStream._expiration_from_timeoutc                 C   s$   |d k	rt |t  d}nd }|S )Ng        )maxr#   )r   r%   r$   r   r   r	   _timeout_from_expirationB   s    z'BaseQuicStream._timeout_from_expirationc                 C   s   t |}td|| S )Nz!H)r   structpack)r   Zdatagramlr   r   r	   _encapsulateL   s    zBaseQuicStream._encapsulatec                 C   s&   | j || | jdko$| j | jS r   )r   r   r   r   r   r   r   r	   _common_add_inputP   s    z BaseQuicStream._common_add_inputc                 C   s    | j | j | jdd d S )Nr   T)r   close_streamr   r   r   r   r   r   r	   _closeT   s    zBaseQuicStream._closeN)
r   r   r   r   r"   r&   r(   r,   r-   r/   r   r   r   r	   r   1   s   
r   c                   @   s0   e Zd ZdddZdd Zddd	Zd
d ZdS )BaseQuicConnectionNr   c                 C   s   d| _ || _|| _|| _d| _|| _i | _tj	|| _
tj||f| _|d kr|dkr| j
tjkrld}n| j
tjkr~d}nt|r||f| _nd | _d S )NFr   z0.0.0.0z::)_doner   Z_addressZ_port_closed_manager_streamsdnsZinetZaf_for_addressZ_afZlow_level_address_tupleZ_peersocketAF_INETAF_INET6NotImplementedError_source)r   r    addressportsourcesource_portmanagerr   r   r	   r   Z   s$    zBaseQuicConnection.__init__c                 C   s   | j |= d S r   )r4   )r   r!   r   r   r	   r.   r   s    zBaseQuicConnection.close_streamTc                 C   sL   t   }| j }|d kr"|d }t|| d}| jrD|rDt|d}||fS )Ni  r   g?)r#   r   Z	get_timerr'   r2   min)r   Zclosed_is_specialnowr%   intervalr   r   r	   _get_timer_valuesu   s    


z$BaseQuicConnection._get_timer_valuesc                 C   s    t   }||kr| j| d S r   )r#   r   Zhandle_timer)r   r%   rA   r   r   r	   _handle_timer   s    z BaseQuicConnection._handle_timer)Nr   N)T)r   r   r   r   r.   rC   rD   r   r   r   r	   r0   Y   s        

r0   c                   @   s"   e Zd Zdee edddZdS )AsyncQuicConnectionN)r$   returnc                    s   d S r   r   )r   r$   r   r   r	   make_stream   s    zAsyncQuicConnection.make_stream)N)r   r   r   r   floatr   rG   r   r   r   r	   rE      s   rE   c                   @   s(   e Zd Zd
ddZdddZdd	 ZdS )BaseQuicManagerNc                 C   s^   i | _ || _|d krTd }t|tr*|}d}tjjjddg||d}|d k	rT|| || _	d S )NTZdoqzdoq-i03)alpn_protocolsverify_modeserver_name)
_connections_connection_factory
isinstancestraioquicquicconfigurationZQuicConfigurationload_verify_locations_conf)r   confrK   connection_factoryrL   Zverify_pathr   r   r	   r      s    

zBaseQuicManager.__init__U  r   c                 C   sl   | j ||f}|d k	r |dfS tjjj| jd}||t  | 	|||||| }|| j ||f< |dfS )NF)rS   T)
rM   r   rQ   rR   r    ZQuicConnectionrU   connectr#   rN   )r   r;   r<   r=   r>   r    Zqconnr   r   r	   _connect   s         zBaseQuicManager._connectc                 C   s*   z| j ||f= W n tk
r$   Y nX d S r   )rM   KeyError)r   r;   r<   r   r   r	   closed   s    zBaseQuicManager.closed)N)rX   Nr   )r   r   r   r   rZ   r\   r   r   r   r	   rI      s   

rI   c                   @   s   e Zd ZdddZdS )AsyncQuicManagerrX   Nr   c                 C   s   t d S r   )r9   )r   r;   r<   r=   r>   r   r   r	   rY      s    zAsyncQuicManager.connect)rX   Nr   )r   r   r   rY   r   r   r   r	   r]      s   r]   )r6   r)   r#   typingr   r   Zaioquic.quic.configurationrQ   Zaioquic.quic.connectionZdns.inetr5   ZQUIC_MAX_DATAGRAM	Exceptionr   r
   r   r0   rE   rI   r]   r   r   r   r	   <module>   s   (/%