K i^/dZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z ddl m Z ddlmZddlmZddlmZdd lmZmZmZmZmZmZmZmZej8rdd lmZd ZGd d ZGddZ y)z'A non-blocking TCP connection factory. N)Futurefuture_add_done_callback)IOLoop)IOStream)gen)Resolver) TimeoutError)AnyUnionDictTupleListCallableIteratorOptional)Setg333333?c eZdZdZdeedeejegee dffddfdZ e deedeeeejefeeejefffdZ e dfd ed eeeej$fdd fd Zd eeejefddfdZd eeejefdejdeddddf dZd eddfdZddZddZd eeej$fddfdZddZddZddZy) _ConnectoraA 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 addrinfoconnectFuture[IOStream]returnNctj|_||_t |_d|_d|_d|_t||_ |j|\|_ |_ t|_yN)rcurrentio_looprrfuturetimeoutconnect_timeout last_errorlen remainingsplit primary_addrssecondary_addrssetstreams)selfrrs W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/tcpclient.py__init__z_Connector.__init__;sl~~'   H  #X37::h3G0D0u cg}g}|dd}|D]1\}}||k(r|j||f|j||f3||fS)aPartition 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)rprimary secondary primary_afafaddrs r)r#z_Connector.splitOse a[^   -HBZDz*  "d,  -  !!r+rrz2Future[Tuple[socket.AddressFamily, Any, IOStream]]c|jt|j|j|||j ||j Sr) try_connectiterr$ set_timeoutset_connect_timeoutr)r(rrs r)startz_Connector.starthsI d0012 !  &  $ $_ 5{{r+addrsc  t|\}}|j||\}}|jj|t|tj|j|||y#t$r^|jdk(rL|jj s2|jj |j xs tdYywxYw)Nrzconnection failed)next StopIterationr"rdone set_exceptionr IOErrorrr'addr functoolspartialon_connect_done)r(r9r1r2streamrs r)r4z_Connector.try_connectss E{HBb$/   I%%d&:&:E2tL  ~~"4;;+;+;+= ))OOCw/B'C  sA--A$CCr1r2rct|xjdzc_ |j}|j|jj r|jy|jj||jj|||f|j!y#t$r}|jj rYd}~y||_|j ||j5|jj|j|jYd}~yd}~wwxYw)N)r"result Exceptionrr=r r4rrremove_timeout on_timeoutclear_timeoutscloser'discard set_result close_streams)r(r9r1r2rrDes r)rCz_Connector.on_connect_dones ! ]]_F  ;;    LLN LL  ( KK " "Bf#5 6    ) {{! DO   U #||' ++DLL9!  sB,, D75D2AD22D7c|jj|jj|z|j|_yr)r add_timeouttimerJr)r(rs r)r6z_Connector.set_timeouts3||// LL   ' )4??  r+cd|_|jjs%|jt |j yyr)rrr=r4r5r%r(s r)rJz_Connector.on_timeouts7 {{!   T$"6"67 8"r+ch|j&|jj|jyyr)rrrIrUs r) clear_timeoutz_Connector.clear_timeouts' << # LL ' ' 5 $r+cZ|jj||j|_yr)rrRon_connect_timeoutr)r(rs r)r7z_Connector.set_connect_timeouts& $||77 T44 r+c|jjs#|jjt|j yr)rr=r>r rOrUs r)rYz_Connector.on_connect_timeouts2{{! KK % %ln 5 r+c|j%|jj|j|j&|jj|jyyr)rrrIrrUs r)rKz_Connector.clear_timeoutssK << # LL ' ' 5    + LL ' '(<(< = ,r+cF|jD]}|jyr)r'rL)r(rDs r)rOz_Connector.close_streamssll F LLN r+rN)__name__ __module__ __qualname____doc__rr rsocket AddressFamilyrr* staticmethodr#_INITIAL_CONNECT_TIMEOUTfloatrr datetime timedeltar8rr4rCr6rJrWr7rYrKrOr+r)rr)s"u+  ! !5 )5;M1M+N N   ("u+"  U6''. /0 U6''. /0 2 ""42FJ  "%x/A/A(A"BC  >  %0D0De0K*L!M RV $!f22E9:;!  ! ! # !  !@ 5 T 9 6 $UH,>,>%>?   > r+rcFeZdZdZddeeddfdZddZejdddddfde de d ejd ee ee efej"fd ee d ee d ee dee eej(fdefdZ dd e d ejded ee d ee deedff dZy) TCPClientzA non-blocking TCP connection factory. .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed. NresolverrcR|||_d|_yt|_d|_y)NFT)rl _own_resolverr)r(rls r)r*zTCPClient.__init__s(  $DM!&D $JDM!%D r+cR|jr|jjyyr)rnrlrLrUs r)rLzTCPClient.closes    MM   ! r+hostportr1 ssl_optionsmax_buffer_size source_ip source_portrc ZK|t|tjr&tjj |z}n\t|t jr4tjj |jz}ntd|z|:tj||jj|||d{} n%|jj|||d{} t| tj |j"|||} | j%|d{\}} } |P|2tj|| j'd||d{} | S| j'd||d{} | S777`7*7w)aConnect 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. NzUnsupported timeout %r)rtru)rF)rrserver_hostname) isinstancenumbersRealrrrSrgrh total_seconds TypeErrorr with_timeoutrlresolverrArB_create_streamr8 start_tls) r(rprqr1rrrsrtrurr connectorr2rDs r)rzTCPClient.connectsH  '7<<0 ..*//1G;GX%7%78 ..*//1G4I4I4KK 87 BCC   --..tT2>H"]]224rBBH    ###'   "+!IID&  """//$$;%   &//{D 0  =CJ s[CF+F!%F+=F#>AF+F%7F+<F'=F+F)F+#F+%F+'F+)F+r2rct|tr|nd}|}|r|s|tjk(rdnd}tj|}|s|r |j ||f t||} | | j|fS#t $r|j wxYw#t $r)} t} | j|  | fcYd} ~ Sd} ~ wwxYw)Nrz::1z 127.0.0.1)rs) rxintrbAF_INET6bindOSErrorrLrrrr>) r(rsr1r2rtrusource_port_bindsource_ip_bind socket_objrDrPfus r)rzTCPClient._create_stream)s+5[#*F;A" I')FOO&;UN]]2& ~ 1A BC  0j/JF 6>>$// /   "   B   Q 2:  s* B BB C'C C Crr])NN)r^r_r`rarrr*rLrb AF_UNSPECstrrrcr r r ssl SSLContextrfrgrhrrr rrir+r)rkrksH &(!3&t&"$*#3#3GK)-#'%)>BJJJ  J eDcNCNN$BCD J "# JC=Jc]J%x'9'9 9:;J Jb$(%) #0#0  #0 #0 C= #0 c] #0 x++ ,#0r+rk)!rarArbryrgrtypingtornado.concurrentrrtornado.iolooprtornado.iostreamrtornadortornado.netutilr tornado.genr r r r r rrrr TYPE_CHECKINGrrerrkrir+r)rsd   ?!%$$NNN ^^BB0B0r+