
    g                        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Z	 ddej,                  deeeeef         ded
e	e
e      dej
                  f
dZ	 dde
e   dedee   fdZy)zBase implementation.    N)ListOptionalSequenceUnion   )
_staggered)AddrInfoType)         )local_addr_infoshappy_eyeballs_delay
interleaveloop
addr_infosr   r   r   r   returnc                h  K   |xst        j                         t        |       dk(  }||d}|r|st        | |      } d}g ||r | D ]  }	 t	        |       d{   } n/ n-t        j                  fd| D        |       d{   \  }}}|D 	
cg c]  }	|	D ]  }
|
  }}	}
	 |d   }t        |      dk(  r|t        |      t        fd|D              r|dj                  dj                  d |D                    }t        |t              r,|j                  t        fd	|D              r9t        |      t        |t
              rt        d
 |D              rt        |      t        |      |S 7 $# t
        t        f$ r Y Nw xY w7 c c}
}	w # d}dw xY ww)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   X   K   | ]!  }t        j                  t        |       # y wN)	functoolspartial_connect_sock).0addrinfocurrent_loop
exceptionsr   s     J/var/www/openai/venv/lib/python3.12/site-packages/aiohappyeyeballs/impl.py	<genexpr>z#start_connection.<locals>.<genexpr>Z   s4       !+H !!!<XGW !+s   '*r   c              3   :   K   | ]  }t        |      k(    y wr   str)r   excmodels     r   r   z#start_connection.<locals>.<genexpr>l   s     CNSs3x5(Ns   zMultiple exceptions: {}z, c              3   2   K   | ]  }t        |        y wr   r    r   r"   s     r   r   z#start_connection.<locals>.<genexpr>q   s     A.3c#h.s   c              3   `   K   | ]%  }t        |t              xr |j                  k(   ' y wr   )
isinstanceOSErrorerrno)r   r"   first_errnos     r   r   z#start_connection.<locals>.<genexpr>w   s1      #1C #30MSYY+5MM#1s   +.c              3   <   K   | ]  }t        |t                y wr   )r'   RuntimeErrorr%   s     r   r   z#start_connection.<locals>.<genexpr>|   s      G=KcJsL1^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   _subr"   all_exceptionsfirst_exceptionmsgr   r   r*   r#   s    `            @@@@r   start_connectionr<      s    L ! L //1:!+'J,>
**:zB
$(D;=J#'7"H* *h8H   # &44 !+	 !
 

a |)3C#s#s#C!	,Q/O>"a'%% O,CNCC)) 066IIA.AA
 ow7"1"7"7K #1  &k377>3 G=KG D 's++ cl"
 Km !'* 
 DB "NJss   A
F2F
#F$F
((F2F F2 F#0F24CF) F2F

FF2FF2#F2)F//F2r   	addr_infoc                 j  K   g }|j                  |       |\  }}}}}	d}
	 t        j                  |||      }
|
j                  d       |G|D ]!  \  }}}}}||k7  r	 |
j                  |        n" |r|j                         t	        d|d      | j                  |
|	       d{    |
dx}}S # t        $ rQ}d|d|j
                  j                          }t	        |j                  |      }|j                  |       Y d}~d}~ww xY w7 g# t        t        f$ rN}|j                  |       |
5	 |
j                           # t        $ r}|j                  |        d}~ww xY w d}~w |
5	 |
j                           # t        $ r}|j                  |        d}~ww xY w xY w# dx}}w xY w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   r=   r   my_exceptionsr?   type_rA   r7   addressr6   lfamilyladdrr"   r;   es                   r   r   r      s     9;Mm$'0$FE5!WD,*}}FeD'+;'Aq%f$
.IIe$ ,<  !'++--!$KF9F"STTg...& &*)
]C  .##()2<<--/02 
 "#))S1C!((--. 	/'" S!

 	  $$Q' 	

 	  $$Q' 	%))
]s   F3<D B3/9D (D)D .F33	D<ADD DD F'!E*5EE*	E&E!!E&&E**F'2FF'	F#FF##F''F* *F00F3	addrinfosfirst_address_family_countc                    t        j                         }| D ]$  }|d   }||vrg ||<   ||   j                  |       & t        |j	                               }g }|dkD  r%|j                  |d   d|dz
          |d   d|dz
  = |j                  d t        j                  j                  t        j                  |       D               |S )z-Interleave list of addrinfo tuples by family.r   r   Nc              3   $   K   | ]  }|| 
 y wr    )r   as     r   r   z(_interleave_addrinfos.<locals>.<genexpr>   s      WA= 	
Ws   )
collectionsOrderedDictrB   listvaluesextend	itertoolschainfrom_iterablezip_longest)rQ   rR   addrinfos_by_familyaddrr?   addrinfos_lists	reordereds          r   r0   r0      s     	!  a,,*,'F#**40	 
 .5578O$&I!A%+,L.H1.LMNA?!;a!??@ ..y/D/Do/VW 
     r   )r   )__doc__r-   rW   r   r\   rC   systypingr   r   r   r    r   typesr	   version_infoasyncio.futuresTimeoutErrorfuturesfloatintAbstractEventLoopr<   r(   r,   r   r0   rU   rd   r   <module>rq      sU         
 2 2  i #*#7#7GOO  :>,0 $04o&o x56o #5/	o
 o 7,,
-o ]]ol :>	7*

#
#7*T% 56787* 7* x56	7*
 ]]7*v JK%CF	,rd   