o
    `^h                     @   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m	Z	m
Z
mZ ddl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d	e
e d
e	e
e  de	e de	e de	ej dejfddZ	ddejdeeeeef   ded
e	e
e  dejf
ddZ	dde
e dedee fddZdS )zBase implementation.    N)ListOptionalSequenceUnion   )
_staggered)AddrInfoType)         )local_addr_infoshappy_eyeballs_delay
interleaveloop
addr_infosr   r   r   r   returnc             
      s  |  s	t   t| dk}|dur|du rd}|r"|s"t| |} d}g |du s,|rJ| D ]}zt |I dH }W  n ttfyH   Y q.w nt fdd| D |I dH \}}}|du rdd D }	z\|	d }
t|	dkrw|
t	|
t
fdd|	D r|
d	d
dd |	D }t|
tr|
jt
fdd|	D rt|t|t|
trt
dd |	D rt|t|d}	dw |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    S/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/aiohappyeyeballs/impl.py	<genexpr>Z   s    

z#start_connection.<locals>.<genexpr>c                 S   s   g | ]	}|D ]}|qqS r   r   )r   subexcr   r   r   
<listcomp>d   s    z$start_connection.<locals>.<listcomp>r   c                 3   s    | ]	}t | kV  qd S r   strr   r   )modelr   r   r   l   s    zMultiple exceptions: {}z, c                 s   s    | ]}t |V  qd S r   r    r"   r   r   r   r   q   s    c                 3   s$    | ]}t |to|j kV  qd S r   )
isinstanceOSErrorerrnor"   )first_errnor   r   r   w   s
    
c                 s   s    | ]}t |tV  qd S r   )r$   RuntimeErrorr"   r   r   r   r   |   s    

)asyncioget_running_looplen_interleave_addrinfosr   r(   r%   r   staggered_racer!   allformatjoinr$   r&   )r   r   r   r   r   single_addr_infosockr   _all_exceptionsfirst_exceptionmsgr   )r   r   r'   r   r#   r   start_connection   sl   &




r7   r   	addr_infoc                    s  g }| | |\}}}}}	d}
zzotj|||d}
|
d |durq|D ]<\}}}}}||kr2q&z|
| W  n6 tyb } zd|d|j  }t|j|}| | W Y d}~q&d}~ww |ri| td|d| 	|
|	I dH  |
W W d }}S  t
tfy } z#| | |
durz|
  W   ty } z| |  d}~ww  d}~w   |
durz|
  W   ty } z| |  d}~ww  d }}w )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&   popsock_connectr(   close)r   r   r8   r   my_exceptionsr9   type_r;   r3   addressr2   lfamilyladdrr   r6   er   r   r   r      sr   


	






r   	addrinfosfirst_address_family_countc                 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OrderedDictr<   listvaluesextend	itertoolschainfrom_iterablezip_longest)rK   rL   addrinfos_by_familyaddrr9   addrinfos_lists	reorderedr   r   r   r,      s    
r,   r   )r   )__doc__r)   rN   r   rS   r=   systypingr   r   r   r    r   typesr   version_infoasyncio.futuresTimeoutErrorfuturesfloatintAbstractEventLoopr7   r%   r(   r   r,   r   r   r   r   <module>   sd    



v

;