
    Mh^/                         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	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZmZmZmZmZmZmZ ej        rdd
lmZ dZ G d d          Z G d d          Z dS )z'A non-blocking TCP connection factory.
    N)Futurefuture_add_done_callback)IOLoop)IOStream)gen)Resolver)TimeoutError)AnyUnionDictTupleListCallableIteratorOptional)Setg333333?c            
          e Zd ZdZdee         deej        egee	df         f         ddfdZ
edee         deeeej        ef                  eeej        ef                  f         fd            Zedfd	ed
eeeej        f                  ddfdZdeeej        ef                  ddfdZdeeej        ef                  dej        deddddf
dZd	eddfdZddZddZd
eeej        f         ddfdZddZddZddZdS )
_Connectora  A stateless implementation of the "Happy Eyeballs" algorithm.

    "Happy Eyeballs" is documented in RFC6555 as the recommended practice
    for when both IPv4 and IPv6 addresses are available.

    In this implementation, we partition the addresses by family, and
    make the first connection attempt to whichever address was
    returned first by ``getaddrinfo``.  If that connection fails or
    times out, we begin a connection in parallel to the first address
    of the other family.  If there are additional failures we retry
    with other addresses, keeping one connection attempt per family
    in flight at a time.

    http://tools.ietf.org/html/rfc6555

    addrinfoconnectFuture[IOStream]returnNc                 &   t          j                    | _        || _        t	                      | _        d | _        d | _        d | _        t          |          | _
        |                     |          \  | _        | _        t                      | _        d S N)r   currentio_loopr   r   futuretimeoutconnect_timeout
last_errorlen	remainingsplitprimary_addrssecondary_addrssetstreams)selfr   r   s      Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/tornado/tcpclient.py__init__z_Connector.__init__;   sz     ~'' HH 	 #X37::h3G3G0D0uu    c                     g }g }| d         d         }| D ]:\  }}||k    r|                     ||f           #|                     ||f           ;||fS )a  Partition the ``addrinfo`` list by address family.

        Returns two lists.  The first list contains the first entry from
        ``addrinfo`` and all others with the same family, and the
        second list contains all other addresses (normally one list will
        be AF_INET and the other AF_INET6, although non-standard resolvers
        may return additional families).
        r   )append)r   primary	secondary
primary_afafaddrs         r)   r#   z_Connector.splitO   sx     	a[^
  	- 	-HBZDz****  "d,,,,	!!r+   r   r   z2Future[Tuple[socket.AddressFamily, Any, IOStream]]c                     |                      t          | j                             |                     |           ||                     |           | j        S r   )try_connectiterr$   set_timeoutset_connect_timeoutr   )r(   r   r   s      r)   startz_Connector.starth   sY    
 	d011222!!!&$$_555{r+   addrsc           	         	 t          |          \  }}nc# t          $ rV | j        dk    rG| j                                        s.| j                            | j        pt          d                     Y d S w xY w|                     ||          \  }}| j	        
                    |           t          |t          j        | j        |||                     d S )Nr   zconnection failed)nextStopIterationr"   r   doneset_exceptionr    IOErrorr   r'   addr   	functoolspartialon_connect_done)r(   r9   r1   r2   streamr   s         r)   r4   z_Connector.try_connects   s    
	E{{HB 	 	 	 ~""4;+;+;+=+="))OCw/B'C'C   FF	 b$//    I%d&:E2tLL	
 	
 	
 	
 	
s    AA54A5r1   r2   r   c                    | xj         dz  c_         	 |                                }n# t          $ r}| j                                        rY d }~d S || _        |                     |           | j        3| j        	                    | j                   | 
                                 Y d }~d S d }~ww xY w|                                  | j                                        r|                                 d S | j                            |           | j                            |||f           |                                  d S )N   )r"   result	Exceptionr   r=   r    r4   r   r   remove_timeout
on_timeoutclear_timeoutscloser'   discard
set_resultclose_streams)r(   r9   r1   r2   r   rD   es          r)   rC   z_Connector.on_connect_done   sL    	!	]]__FF 	 	 	{!!   DOU###|' ++DL999!!!FFFFF	 	; 	!LLNNNNNL  (((K""Bf#5666     s   ' 
B1B,AB,,B1c                     | j                             | j                                         |z   | j                  | _        d S r   )r   add_timeouttimerJ   r   )r(   r   s     r)   r6   z_Connector.set_timeout   s9    |//L')4?
 
r+   c                     d | _         | j                                        s)|                     t	          | j                             d S d S r   )r   r   r=   r4   r5   r%   r(   s    r)   rJ   z_Connector.on_timeout   sN    {!! 	9T$"67788888	9 	9r+   c                 V    | j         !| j                            | j                    d S d S r   )r   r   rI   rU   s    r)   clear_timeoutz_Connector.clear_timeout   s0    <#L''55555 $#r+   c                 P    | j                             || j                  | _        d S r   )r   rR   on_connect_timeoutr   )r(   r   s     r)   r7   z_Connector.set_connect_timeout   s,      $|77T4 
  
r+   c                     | j                                         s&| j                             t                                 |                                  d S r   )r   r=   r>   r	   rO   rU   s    r)   rY   z_Connector.on_connect_timeout   sJ    {!! 	6K%%lnn555r+   c                     | j         | j                            | j                    | j        !| j                            | j                   d S d S r   )r   r   rI   r   rU   s    r)   rK   z_Connector.clear_timeouts   sR    <#L''555+L''(<===== ,+r+   c                 B    | j         D ]}|                                 d S r   )r'   rL   )r(   rD   s     r)   rO   z_Connector.close_streams   s,    l 	 	FLLNNNN	 	r+   r   N)__name__
__module____qualname____doc__r   r   r   socketAddressFamilyr   r*   staticmethodr#   _INITIAL_CONNECT_TIMEOUTfloatr   r   datetime	timedeltar8   r   r4   rC   r6   rJ   rW   r7   rY   rK   rO    r+   r)   r   r   )   so        "u+ !5)5;M1M+NN
 
   ( "u+"	U6'./0U6'./0	2
" " " \"4 2FJ	 		 "%x/A(A"BC	 
>		 	 	 	
%0De0K*L!M 
RV 
 
 
 
$!f2E9:;!  ! 	!
 #! 
! ! ! !@
5 
T 
 
 
 

9 9 9 9
6 6 6 6
$UH,>%>?
	
 
 
 
   
> > > >     r+   r   c                   ^   e Zd ZdZddee         ddfdZddZej	        dddddfde
ded	ej        d
eeee
ef         ej        f                  dee         dee
         dee         deeeej        f                  defdZ	 	 dded	ej        dedee
         dee         deedf         fdZdS )	TCPClientzA non-blocking TCP connection factory.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.
    Nresolverr   c                 ^    ||| _         d| _        d S t                      | _         d| _        d S )NFT)rl   _own_resolverr   )r(   rl   s     r)   r*   zTCPClient.__init__   s6    $DM!&D$JJDM!%Dr+   c                 J    | j         r| j                                         d S d S r   )rn   rl   rL   rU   s    r)   rL   zTCPClient.close   s1     	"M!!!!!	" 	"r+   hostportr1   ssl_optionsmax_buffer_size	source_ipsource_portr   c	           	      T  K   |t          |t          j                  r)t          j                                                    |z   }ngt          |t          j                  r;t          j                                                    |                                z   }nt          d|z            |6t          j        || j                            |||                     d{V }	n"| j                            |||           d{V }	t          |	t          j        | j        |||                    }
|
                    |           d{V \  }}}|R|2t          j        ||                    d||                     d{V }n|                    d||           d{V }|S )a  Connect to the given host and port.

        Asynchronously returns an `.IOStream` (or `.SSLIOStream` if
        ``ssl_options`` is not None).

        Using the ``source_ip`` kwarg, one can specify the source
        IP address to use when establishing the connection.
        In case the user needs to resolve and
        use a specific interface, it has to be handled outside
        of Tornado as this depends very much on the platform.

        Raises `TimeoutError` if the input future does not complete before
        ``timeout``, which may be specified in any form allowed by
        `.IOLoop.add_timeout` (i.e. a `datetime.timedelta` or an absolute time
        relative to `.IOLoop.time`)

        Similarly, when the user requires a certain source port, it can
        be specified using the ``source_port`` arg.

        .. versionchanged:: 4.5
           Added the ``source_ip`` and ``source_port`` arguments.

        .. versionchanged:: 5.0
           Added the ``timeout`` argument.
        NzUnsupported timeout %r)rt   ru   )r   F)rr   server_hostname)
isinstancenumbersRealr   r   rS   rg   rh   total_seconds	TypeErrorr   with_timeoutrl   resolver   rA   rB   _create_streamr8   	start_tls)r(   rp   rq   r1   rr   rs   rt   ru   r   r   	connectorr2   rD   s                r)   r   zTCPClient.connect   s&     H '7<00 D .**//11G;GX%788 D .**//11G4I4I4K4KK 87 BCCC -..tT2>>       HH "]224rBBBBBBBBH##'	  
 
	 "+!I!IIIIIIID& """/$$; %             &//{D  0           r+   r2   r   c                    t          |t                    r|nd}|}|r|s|t          j        k    rdnd}t          j        |          }|s|r<	 |                    ||f           n## t
          $ r |                                  w xY w	 t          ||          }	|	|	                    |          fS # t
          $ r1}
t                      }|
                    |
           |	|fcY d }
~
S d }
~
ww xY w)Nr   z::1z	127.0.0.1)rs   )rx   intrb   AF_INET6bindOSErrorrL   r   r   r   r>   )r(   rs   r1   r2   rt   ru   source_port_bindsource_ip_bind
socket_objrD   rP   fus               r)   r   zTCPClient._create_stream)  sA    +5[#*F*FM;;A" 	MI 	M ')FO&;&;UUN ]2&&
 	~ 	1A BCCCC     """	0j/JJJF 6>>$////  	 	 	BQ2:	s*   A% % B	B1 1
C,;&C'!C,'C,r   r]   )NN)r^   r_   r`   ra   r   r   r*   rL   rb   	AF_UNSPECstrr   rc   r   r   r
   ssl
SSLContextrf   rg   rh   r   r   r   r   ri   r+   r)   rk   rk      s        & &(!3 &t & & & &" " " " $*#3GK)-#'%)>BJ JJ J  	J
 eDcNCN$BCDJ "#J C=J c]J %x'9 9:;J 
J J J Jb $(%)#0 #0#0  #0 	#0
 C=#0 c]#0 
x++	,#0 #0 #0 #0 #0 #0r+   rk   )!ra   rA   rb   ry   rg   r   typingtornado.concurrentr   r   tornado.ioloopr   tornado.iostreamr   tornador   tornado.netutilr   tornado.genr	   r
   r   r   r   r   r   r   r   TYPE_CHECKINGr   re   r   rk   ri   r+   r)   <module>r      s            



  ? ? ? ? ? ? ? ? ! ! ! ! ! ! % % % % % %       $ $ $ $ $ $ $ $ $ $ $ $ N N N N N N N N N N N N N N N N N N N N	  ^ ^ ^ ^ ^ ^ ^ ^BB0 B0 B0 B0 B0 B0 B0 B0 B0 B0r+   