a
    Qfy                     @   s  d 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 ddlmZ ejdk r|ddlZejej_dddddee e
ee  e
e e
e e
ej ejd	d
dZdeje	e	e  ee
ee  ejdddZdee ee	e dddZdS )zBase implementation.    N)	staggered)ListOptionalSequence   )AddrInfoType)         )local_addr_infoshappy_eyeballs_delay
interleaveloop)
addr_infosr   r   r   r   returnc             	      s  |  st   t| dk}|dur0|du r0d}|rB|sBt| |} d}g |du sV|r| D ]:}zt |I dH }W  qW qZ ty   Y qZY qZ0 qZn.tj fdd| D | dI dH \}}}|du r~dd D }	z|	d }
t|	dkr|
nnt|
t	fd	d|	D r|
d

ddd |	D }|
jt	fdd|	D r`t|t|W d}	dn
d}	d0 |S )aM  
    Connect to a TCP server.

    Create a socket connection to a specified destination.  The
    destination is specified as a list of AddrInfoType tuples as
    returned from getaddrinfo().

    The arguments are, in order:

    * ``family``: the address family, e.g. ``socket.AF_INET`` or
        ``socket.AF_INET6``.
    * ``type``: the socket type, e.g. ``socket.SOCK_STREAM`` or
        ``socket.SOCK_DGRAM``.
    * ``proto``: the protocol, e.g. ``socket.IPPROTO_TCP`` or
        ``socket.IPPROTO_UDP``.
    * ``canonname``: the canonical name of the address, e.g.
        ``"www.python.org"``.
    * ``sockaddr``: the socket address

    This method is a coroutine which will try to establish the connection
    in the background. When successful, the coroutine returns a
    socket.

    The expected use case is to use this method in conjunction with
    loop.create_connection() to establish a connection to a server::

            socket = await start_connection(addr_infos)
            transport, protocol = await loop.create_connection(
                MyProtocol, sock=socket, ...)
    r   Nc                 3   s    | ]}t t |V  qd S N)	functoolspartial_connect_sock).0addrinfo)current_loop
exceptionsr    N/var/www/ai-form-bot/venv/lib/python3.9/site-packages/aiohappyeyeballs/impl.py	<genexpr>Y   s   
z#start_connection.<locals>.<genexpr>)r   c                 S   s   g | ]}|D ]}|qqS r   r   )r   subexcr   r   r   
<listcomp>d       z$start_connection.<locals>.<listcomp>r   c                 3   s   | ]}t | kV  qd S r   strr   r   )modelr   r   r   l   r   zMultiple exceptions: {}z, c                 s   s   | ]}t |V  qd S r   r    r"   r   r   r   r   q   r   c                 3   s"   | ]}t |to|j kV  qd S r   )
isinstanceOSErrorerrnor"   )first_errnor   r   r   v   s   )asyncioZget_running_looplen_interleave_addrinfosr   r%   r   Zstaggered_racer!   allformatjoinr&   )r   r   r   r   r   Zsingle_addr_infosockr   _Zall_exceptionsZfirst_exceptionmsgr   )r   r   r'   r   r#   r   start_connection   s`    &






r1   )r   r   	addr_infor   r   c                    s  g }| | |\}}}}}	d}
zVztj|||d}
|
d |dur|D ]~\}}}}}||krdqLz|
| W  qW qL ty } z8d|d|j  }t|j|}| | W Y d}~qLd}~0 0 qL|r| ntd|d| 	|
|	I dH  |
W W d }}S  tyN } z*| | |
dur8|

   W Y d}~n(d}~0    |
durf|

   Y n0 W d }}n
d }}0 dS )z$Create, bind and connect one socket.N)familytypeprotoFz*error while attempting to bind on address z: z&no matching local address with family=z found)appendsocketsetblockingbindr%   strerrorlowerr&   popZsock_connectclose)r   r   r2   r   Zmy_exceptionsr3   type_r5   r/   addressr.   Zlfamilyladdrr   r0   r   r   r   r      sP    



"




r   )	addrinfosfirst_address_family_countr   c                 C   s   t  }| D ]*}|d }||vr(g ||< || | qt| }g }|dkr|||d d|d   |d d|d = |dd tjtj	| D  |S )z-Interleave list of addrinfo tuples by family.r   r   Nc                 s   s   | ]}|d ur|V  qd S r   r   )r   ar   r   r   r      s   z(_interleave_addrinfos.<locals>.<genexpr>)
collectionsOrderedDictr6   listvaluesextend	itertoolschainfrom_iterablezip_longest)rA   rB   Zaddrinfos_by_familyaddrr3   Zaddrinfos_listsZ	reorderedr   r   r   r*      s     
r*   )N)r   )__doc__r(   rD   r   rI   r7   sysr   typingr   r   r   typesr   version_infoZasyncio.futuresTimeoutErrorZfuturesfloatintZAbstractEventLoopr1   r%   r   r*   r   r   r   r   <module>   sJ   


n 

3 