
    !nit                         d dl Z d dlm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 ddl	mZ ddl	mZ da e       Zd	 Z G d
 de
j$                        Zy# e$ r dZY Cw xY w)    N)SimpleCookie   )base_client)
exceptions)packet)payloadFc                  >    d } t        j                   |               y)z>SIGINT handler.

    Disconnect all active async clients.
    c                    K   t         j                  d d  D ]+  } | j                         s| j                          d {    - t	        j
                         D cg c]  }|t	        j                         us| }}|D ]  }|j                           t	        j                  |ddi d {    t	        j                         j                          y 7 c c}w 7 .w)Nreturn_exceptionsT)r   connected_clientsis_asyncio_based
disconnectasyncio	all_taskscurrent_taskcancelgatherget_running_loopstop)ctasktaskss      U/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/engineio/async_client.py_handlerz&async_signal_handler.<locals>._handler   s     ..q1 	%A!!#lln$$	%
 #*"3"3"5 )$%%':( ) ) 	DKKM	nne<t<<<  "'') %) 	=s8   'CCCCC6C:1C+C,(CCNr   ensure_future)r   s    r   async_signal_handlerr      s    
* (*%    c                        e Zd ZdZd Z	 	 ddZd Zd ZddZd Z	ddZ
d	 Zd
 Zd Z fdZd Zd Zd Zd Zd Z	 	 ddZd Zd Zd Zd Z xZS )AsyncClienta  An Engine.IO client for asyncio.

    This class implements a fully compliant Engine.IO web client with support
    for websocket and long-polling transports, compatible with the asyncio
    framework on Python 3.5 or newer.

    :param logger: To enable logging set to ``True`` or pass a logger object to
                   use. To disable logging set to ``False``. The default is
                   ``False``. Note that fatal errors are logged even when
                   ``logger`` is ``False``.
    :param json: An alternative json module to use for encoding and decoding
                 packets. Custom json modules must have ``dumps`` and ``loads``
                 functions that are compatible with the standard library
                 versions.
    :param request_timeout: A timeout in seconds for requests. The default is
                            5 seconds.
    :param http_session: an initialized ``aiohttp.ClientSession`` object to be
                         used when sending requests to the server. Use it if
                         you need to add special client options such as proxy
                         servers, SSL certificates, custom CA bundle, etc.
    :param ssl_verify: ``True`` to verify SSL certificates, or ``False`` to
                       skip SSL certificate verification, allowing
                       connections to servers with self signed certificates.
                       The default is ``True``.
    :param handle_sigint: Set to ``True`` to automatically handle disconnection
                          when the process is interrupted, or to ``False`` to
                          leave interrupt handling to the calling application.
                          Interrupt handling can only be enabled when the
                          client instance is created in the main thread.
    :param websocket_extra_options: Dictionary containing additional keyword
                                    arguments passed to
                                    ``aiohttp.ws_connect()``.
    :param timestamp_requests: If ``True`` a timestamp is added to the query
                               string of Socket.IO requests as a cache-busting
                               measure. Set to ``False`` to disable.
    c                      y)NT selfs    r   r   zAsyncClient.is_asyncio_basedT   s    r   c                 p  K   | j                   rft        s`t        j                         t        j                         k(  r7	 t        j                         j                  t        j                  t               da| j                  dk7  rt        d      ddg}|3t!        |t"              r|g}|D cg c]  }||v r|
 }}|st        d      |xs || _         t'        | d	| j$                  d
   z         ||xs i |       d{   S # t        $ r | j                  j                  d       Y w xY wc c}w 7 3w)a  Connect to an Engine.IO server.

        :param url: The URL of the Engine.IO server. It can include custom
                    query string parameters if required by the server.
        :param headers: A dictionary with custom headers to send with the
                        connection request.
        :param transports: The list of allowed transports. Valid transports
                           are ``'polling'`` and ``'websocket'``. If not
                           given, the polling transport is connected first,
                           then an upgrade to websocket is attempted.
        :param engineio_path: The endpoint where the Engine.IO server is
                              installed. The default value is appropriate for
                              most cases.

        Note: this method is a coroutine.

        Example usage::

            eio = engineio.Client()
            await eio.connect('http://localhost:5000')
        zSignal handler is unsupportedTdisconnectedz%Client is not in a disconnected statepolling	websocketNzNo valid transports provided	_connect_r   )handle_sigintasync_signal_handler_set	threadingcurrent_threadmain_threadr   r   add_signal_handlersignalSIGINTr   NotImplementedErrorloggerwarningstate
ValueError
isinstancestr
transportsgetattr)r$   urlheadersr9   engineio_pathvalid_transports	transports          r   connectzAsyncClient.connectW   s;    0 &>((*i.C.C.EEE((*==MM#79 $( ::'DEE%{3!*c*(\
5? <	&*:: $ <J < !?@@$8(8DWT;1C#CDB/ / 	/ ' E##$CDE<
/sA   <D66D 59D6.D/;AD6 D4D6$D,)D6+D,,	D6c                 R   K   | j                   r| j                    d{    yy7 w)zWait until the connection with the server ends.

        Client applications can use this function to block the main thread
        during the life of the connection.

        Note: this method is a coroutine.
        N)read_loop_taskr#   s    r   waitzAsyncClient.wait   s(      %%%% %s   '%'c                    K   | j                  t        j                  t        j                  |             d{    y7 w)a/  Send a message to the server.

        :param data: The data to send to the server. Data can be of type
                     ``str``, ``bytes``, ``list`` or ``dict``. If a ``list``
                     or ``dict``, the data will be serialized as JSON.

        Note: this method is a coroutine.
        dataN)_send_packetr   PacketMESSAGE)r$   rF   s     r   sendzAsyncClient.send   s+      fnn4 HIIIs   8AA Ac                   K   | j                   dk(  r| j                  t        j                  t        j                               d{    | j
                  j                  d       d{    d| _         | j                  d|xs | j                  j                  d       d{    | j                  dk(  r"| j                  j                          d{    |s| j                   d{    d| _         	 t        j                  j!                  |        | j%                          d{    y7 7 7 7 `7 L# t"        $ r Y .w xY w7 w)	zDisconnect from the server.

        :param abort: If set to ``True``, do not wait for background tasks
                      associated with the connection to end.

        Note: this method is a coroutine.
        	connectedNdisconnectingr   F	run_asyncr(   r&   )r5   rG   r   rH   CLOSEqueueput_trigger_eventreasonCLIENT_DISCONNECTcurrent_transportwscloserB   r   r   remover6   _reset)r$   abortrT   s      r   r   zAsyncClient.disconnect   s     ::$##FMM&,,$?@@@**..&&&(DJ%%l&,&M0M0M05 & 7 7 7 %%4ggmmo%%))))'DJ--44T: kkm A&7 &)  s   AED2	"E+D4,:E&D6'0ED8E-D:.E:D< E,E-E4E6E8E:E<	EEEEc                 8    t        j                   ||i |      S )am  Start a background task.

        This is a utility function that applications can use to start a
        background task.

        :param target: the target function to execute.
        :param args: arguments to pass to the function.
        :param kwargs: keyword arguments to pass to the function.

        The return value is a ``asyncio.Task`` object.
        r   )r$   targetargskwargss       r   start_background_taskz!AsyncClient.start_background_task   s     $$VT%<V%<==r   c                 H   K   t        j                  |       d{   S 7 w)z[Sleep for the requested amount of time.

        Note: this method is a coroutine.
        N)r   sleep)r$   secondss     r   rb   zAsyncClient.sleep   s     
 ]]7++++s   " "c                 ,    t        j                  |i |S )zCreate a queue object.)r   Queue)r$   r^   r_   s      r   create_queuezAsyncClient.create_queue   s    }}d-f--r   c                 "    t         j                  S )zlReturn the queue empty exception raised by queues created by the
        ``create_queue()`` method.
        )r   
QueueEmptyr#   s    r   get_queue_empty_exceptionz%AsyncClient.get_queue_empty_exception   s     !!!r   c                 *    t        j                         S )zCreate an event object.)r   Eventr#   s    r   create_eventzAsyncClient.create_event   s    }}r   c                 j  K   t         |           	 	 | j                  j                          | j                  j	                          6# | j
                  $ r Y nw xY w| j                  sH| j                  r;| j                  j                  s$| j                  j                          d {  7   y y y y wN)
superrZ   rQ   
get_nowait	task_donequeue_emptyexternal_httphttpclosedrX   )r$   	__class__s    r   rZ   zAsyncClient._reset   s     

%%'

$$&  ## !!yy!1!1iioo''' "2y "s5   B34A	 B3	AB3AAB3)B,*	B3c                 J   | j                   r| j                   j                  sy	 t        j                         }|j	                         r*|j                  | j                   j                                y |j                  | j                   j                                y y y #  Y y xY wrn   )rt   ru   r   get_event_loop
is_runningr   rX   run_until_complete)r$   loops     r   __del__zAsyncClient.__del__   su    99TYY----/??$&&tyy'89++DIIOO,=> .9s   AB 2)B B"c                   K   t         | j                  j                  d       y| j                  ||d      | _        | j                  j                  d| j                  z          | j                  d| j                  | j                         z   || j                         d{   }|t        |t              r1| j                          d{    t        j                  |xs d      |j                  dk  s|j                  d	k\  r`| j                          d{    	 |j                          d{   }t        j                  d
j#                  |j                        |      	 t%        j&                  |j)                          d{   j+                  d            }|j.                  d   }|j0                  t2        j4                  k7  rt        j                  d      | j                  j                  dt        |j6                        z          |j6                  d   | _        |j6                  d   | _        t=        |j6                  d         dz  | _        t=        |j6                  d         dz  | _         d| _!        | xj                  d| j8                  z   z  c_        d| _"        tF        jH                  jK                  |        | jM                  dd       d{    |j.                  dd D ]  }| jO                  |       d{     d| j:                  v r*d| jP                  v r| jS                  |||       d{   ry| jU                  | jV                        | _,        | jU                  | jZ                        | _.        y7 7 7 y7 c# t         j                   $ r d}Y yw xY w7 +# t,        $ r t        j                  d      dw xY w7 7 7 w)z<Establish a long-polling connection to the Engine.IO server.Nz3aiohttp not installed -- cannot make HTTP requests!r'   z!Attempting polling connection to GET)r<   timeoutz Connection refused by the server   ,  z,Unexpected status code {} in server responseutf-8encoded_payloadzUnexpected response from serverr   z"OPEN packet not returned by serverz!Polling connection accepted with sidupgradespingInterval     @@pingTimeout&sid=rL   r@   FrN   r   r(   )/aiohttpr3   error_get_engineio_urlbase_urlinfo_send_request_get_url_timestamprequest_timeoutr7   r8   rZ   r   ConnectionErrorstatusjsonClientErrorformatr   Payloadreaddecoder6   packetspacket_typer   OPENrF   r   r   intping_intervalping_timeoutrV   r5   r   r   appendrS   _receive_packetr9   _connect_websocketr`   _write_loopwrite_loop_task_read_loop_pollingrB   )	r$   r;   r<   r=   rargpopen_packetpkts	            r   _connect_pollingzAsyncClient._connect_polling   sp    ?KK * +..sM9M<t}}LM$$4==4#:#:#<<g(( % * * 9
1c*++-,,779 988c>QXX_++-FFHn ,,>EEHH"$ $	=qvvx0G0G1 A
 iil""fkk1,,46 6/#k6F6F2GG	I##E*#((4 !1!1.!ABVK 0 0 ?@6I!*488++ 
%%,,T2!!)u!===99QR= 	,C&&s+++	, $--'K4??,J,,S'=III#99$:J:JK"88##%c*    $&&  2@ 	=,,138<=	=$ 	> , Js   B O""N#)O"NAO"NO"!N 4N5N 9/O")"N9 N6N9 $D?O"#O$*O"O8O"O AO"O"O"N N3/O"2N33O"6N9 9 OO"O" O"c           	      L  K   t         | j                  j                  d       y| j                  ||d      }| j                  r3| j                  j                  d|z          d}|d| j                  z   z  }n'd}|| _        | j                  j                  d|z          | j                  | j                  j                  rt        j                         | _        |j                         D ]|  \  }}|j                         d	k(  st        ||         }| j                  j                  j                  |j                         D 	
ci c]  \  }	}
|	|
j                   c}
}	       ||=  n d
t        j                   | j"                        i}| j$                  s5t'        j(                         }d|_        t&        j,                  |_        ||d<   |j1                  | j2                  j5                  di              ||d<   |j1                  | j2                         	  | j                  j6                  || j9                         z   fi | d{   }|r=tI        jJ                  tH        jL                  d      jO                         }	 |jQ                  |       d{    	 |jW                          d{   jX                  }tI        jJ                  |      }|jZ                  tH        j\                  k7  s|jX                  dk7  r| j                  jC                  d       ytI        jJ                  tH        j^                        jO                         }	 |jQ                  |       d{    d| _0        | j                  j                  d       nL	 |jW                          d{   jX                  }tI        jJ                  |      }|jZ                  tH        jb                  k7  rtE        jF                  d      | j                  j                  dtU        |jX                        z          |jX                  d   | _        |jX                  d   | _2        tg        |jX                  d         dz  | _4        tg        |jX                  d         dz  | _5        d| _0        d| _6        tn        jp                  js                  |        | ju                  d d!       d{    || _;        | jy                  | jz                        | _>        | jy                  | j~                        | _@        yc c}
}	w 7 # t         j:                  j<                  t         j:                  j>                  t         j:                  j@                  f$ r5 |r| j                  jC                  d       Y ytE        jF                  d      w xY w7 # tR        $ r/}| j                  jC                  dtU        |             Y d}~yd}~ww xY w7 ?# tR        $ r/}| j                  jC                  dtU        |             Y d}~yd}~ww xY w7 # tR        $ r/}| j                  jC                  dtU        |             Y d}~yd}~ww xY w7 # tR        $ r&}tE        jF                  dtU        |      z         d}~ww xY w7 ͭw)"z?Establish or upgrade to a WebSocket connection with the server.Nzaiohttp package not installedFr(   z Attempting WebSocket upgrade to Tr   z#Attempting WebSocket connection to cookier   )ws_closesslr<   z*WebSocket upgrade failed: connection errorzConnection errorproberE   z7WebSocket upgrade failed: unexpected send exception: %sz7WebSocket upgrade failed: unexpected recv exception: %sencoded_packetz(WebSocket upgrade failed: no PONG packetz WebSocket upgrade was successfulzUnexpected recv exception: zno OPEN packetz#WebSocket connection accepted with r   r   r   r   r   rL   r@   rN   )Ar   r3   r   r   r   r   r   rt   ru   ClientSessionitemslowerr   
cookie_jarupdate_cookiesvalueClientWSTimeoutr   
ssl_verifyr   create_default_contextcheck_hostname	CERT_NONEverify_modeupdatewebsocket_extra_optionspop
ws_connectr   client_exceptionsWSServerHandshakeErrorServerConnectionErrorClientConnectionErrorr4   r   r   r   rH   PINGencodesend_str	Exceptionr8   receiverF   r   PONGUPGRADErV   r   r   r   r   r   r5   r   r   r   rS   rW   r`   r   r   _read_loop_websocketrB   )r$   r;   r<   r=   websocket_urlupgradeheaderr   ckkmextra_optionsssl_contextrW   r   er   r   s                     r   r   zAsyncClient._connect_websocket-  s    ?KK=>..sM/:<88KK2]BDGWtxx//MG)DMKK5EG 99		 0 0--/DI %]]_ 	MFE||~)!'&/2		$$33,.HHJ7DAqQZ79FO	 w..8L8LMO446K).K&&)mmK##.M%  	t3377	2FG#*i T99:	E+tyy++ 7 7 99L=JL LB fkk8??AAkk!n$$::<'-- --q1C&++-W1D##>@fnn-446Akk!n$$ &1D"KK?@<::<'-- !--q9K&&&++5 001ABBKK5K<L<L8MMO"''.DH',,Z8DM!$[%5%5n%E!F!OD #K$4$4]$C Dv MD%0D"$DJ))006%%i5%AAA#99$:J:JK"88%%'m 8&L))@@))??))??A 	E ##@B 001CDD	E % ##MF 	 ( ##MF 	 % ##MF 	 ( < 001CF:< <<  BsD  D Z$A Z$T!B<Z$1T* T'	T* 6Z$V5 V2V5 W3 1W02W3  BZ$X1 $X.%X1 )$Z$Y/ !Y,"Y/ 0D$Z$Z!AZ$'T* *A-V/Z$V//Z$2V5 5	W->%W(#Z$(W--Z$0W3 3	X+<%X&!Z$&X++Z$.X1 1	Y):%Y$Z$$Y))Z$,Y/ /	Z8!ZZZ$c                   K   |j                   t        t        j                        k  rt        j                  |j                      nd}| j                  j                  d|t        |j                  t              s|j                  nd       |j                   t        j                  k(  r'| j                  d|j                  d       d{    y|j                   t        j                  k(  rF| j                  t        j                  t        j                  |j                               d{    y|j                   t        j                  k(  r0| j!                  d| j"                  j$                         d{    y|j                   t        j&                  k(  ry| j                  j)                  d	|j                          y7 7 7 Nw)
z(Handle incoming packets from the server.UNKNOWNzReceived packet %s data %s<binary>messageTrN   N)r[   rT   z%Received unexpected packet of type %s)r   lenr   packet_namesr3   r   r7   rF   bytesrI   rS   r   rG   rH   r   rP   r   rT   SERVER_DISCONNECTNOOPr   )r$   r   packet_names      r   r   zAsyncClient._receive_packet  sN     V%8%8!99 ))#//:?H 	(+&sxx7CHHZ	I ??fnn,%%iT%JJJ__+##FMM&++sxx$HIII__,//)-)F)F " H H H__+KKE!oo/ KIHs9   CG
	G
A"G
,G-AG
9G:AG
G
G
c                 <  K   | j                   dk7  ry| j                  j                  |       d{    | j                  j	                  dt
        j                  |j                     t        |j                  t              s|j                         yd       y7 hw)z(Queue a packet to be sent to the server.rL   NzSending packet %s data %sr   )r5   rQ   rR   r3   r   r   r   r   r7   rF   r   )r$   r   s     r   rG   zAsyncClient._send_packet  sx     ::$jjnnS!!!'0&sxx7CHH	I >H	I 	"s   /BBA)Bc                 >  K   | j                   | j                   j                  rt        j                         | _         t	        | j                   |j                               }	 | j                  s) ||||t        j                  |      d       d {   S  ||||t        j                  |             d {   S 7 ,7 # t        j                  t        j                  f$ r3}| j                  j                  d|||       t        |      cY d }~S d }~ww xY ww)N)totalF)r<   rF   r   r   )r<   rF   r   z+HTTP %s request to %s failed with error %s.)rt   ru   r   r   r:   r   r   ClientTimeoutr   r   TimeoutErrorr3   r   r8   )r$   methodr;   r<   bodyr   http_methodexcs           r   r   zAsyncClient._send_request  s      99		 0 0--/DIdii8	??(t#11@eM M M )t#11@B B B	MB ##W%9%9: 	KKJ#S#/s8O	sf   A D#0C C C D#C ;C<C ?D C C #D'(DDDDDc                 0   K   |j                  dd      }d} j                  v rt        j                   j                           du rw|rV  j                   j                     g }t
        j                  |       |j                  t
        j                         |S 	 	   j                       d{   }	 |S |rN fd	} j	                  |      }t
        j                  |       |j                  t
        j                         |S 	 	   j                      }|S |S 7 o# t        $ r4 dk(  r-t              dk(  r  j                             d{  7  cY S  w xY w# t        j                  $ r Y |S   j                  j                  dz          dk(  rY yY |S xY w# t        $ r- dk(  r#t              dk(  r  j                            }n Y |S w xY w#   j                  j                  d
z          dk(  rY yY |S xY ww)zInvoke an event handler.rO   FNTr   r   z async handler errorr@   c                  0   K    j                        S wrn   )handlers)r^   eventr$   s   r   async_handlerz1AsyncClient._trigger_event.<locals>.async_handler  s     3t}}U3T::s   z handler error)r   r   inspectiscoroutinefunctionr`   task_reference_holderaddadd_done_callbackdiscard	TypeErrorr   r   CancelledErrorr3   	exception)r$   r   r^   r_   rO   retr   r   s   ```     r   rS   zAsyncClient._trigger_event  sE    JJ{E2	DMM!**4==+?@DH5455dmmE6J =7;=D)--d3**+@+H+HIK)	&(<e(<d(C"CCX 
5 ;  55mDD)--d3**+@+H+HIK)	&"6$--"6"=C 
s
Y #D( &$4$'IN .BT]]5-A-C'C'C C %& #11 D 
C)--e6L.LM I- $) .> 
  ) &$4$'IN ';dmmE&:&< % !$ 
&)--e6F.FG I- $) . 
s   B H'D. <D,=D. AHF0 (H,D. .3E+!E$"E+'E. (H)E++E. .F-H$F-(H02G&"G) #H%G&&G) )%HHc                 F  K   | j                   dk(  r| j                  r| j                  j                  d| j                  z          | j                  d| j                  | j                         z   t        | j                  | j                        dz          d{   }|t        |t              rC| j                  j                  |xs d       | j                  j                  d       d{    n|j                  dk  s|j                  d	k\  rJ| j                  j                  d
|j                         | j                  j                  d       d{    n	 t!        j"                  |j%                          d{   j'                  d            }|j*                  D ]  }| j-                  |       d{     | j                   dk(  r| j                  r| j                  r/| j                  j                  d       | j                   d{    | j                   dk(  rh| j/                  d| j0                  j2                  d       d{    	 t4        j6                  j9                  |        | j;                          d{    | j                  j                  d       y7 7 7 ]7 7# t(        $ rC | j                  j                  d       | j                  j                  d       d{  7   Y %w xY w7 M7 7 # t(        $ r Y w xY w7 w)z-Read packets by polling the Engine.IO server.rL   zSending polling GET request to r~      r   N*Connection refused by the server, abortingr   r   6Unexpected status code %s in server response, abortingr   r   z'Unexpected packet from server, aborting"Waiting for write loop task to endr   FrN   Exiting read loop task)r5   r   r3   r   r   r   r   maxr   r   r7   r8   r4   rQ   rR   r   r   r   r   r   r6   r   r   rS   rT   TRANSPORT_ERRORr   r   rY   rZ   )r$   r   r   r   s       r   r   zAsyncClient._read_loop_polling  s    jjK'D,@,@KK1DMMAC((t}}t'>'>'@@D..0A0ABQF ) H HA yJq#.##EEGjjnnT***xx#~S## %9:;((DjjnnT***OO1668^4K4K5  yy 0**3///01 jjK'D,@,@6 KKAB&&&&::$%%dkk99U & L L L--44T: ++-12GH +
 + 6D ##=?jjnnT***	 0 'L  s   BL!J.AL!/J10A'L!J4L!"J:  J7J: #L!<L	=!L! 6L!L>L!LL!L :L!L!L!1L!4L!7J: :AL<K?=LL!LL!L!L!	LL!LL!c                   K   | j                   dk(  rgd}	 t        j                  | j                  j	                         | j
                  | j                  z          d{   }t        |j                  t        t        f      s| j                  j                  d|j                  t        j                  j                   t        j                  j"                  fv rdnt        |j                        t        |j                               | j$                  j'                  d       d{    nM|j                  }	 t3        j4                  |	      }| j7                  |       d{    | j                   dk(  rg| j8                  r/| j                  j/                  d       | j8                   d{    | j                   dk(  rh| j;                  d| j<                  j>                  d       d{    	 t@        jB                  jE                  |        | jI                          d{    | j                  j/                  d       y7 7 $# t        j(                  $ rC | j                  j                  d       | j$                  j'                  d       d{  7   Y )t        j*                  j,                  $ rC | j                  j/                  d       | j$                  j'                  d       d{  7   Y t0        $ rT}| j                  j/                  dt        |             | j$                  j'                  d       d{  7   Y d}~d}~ww xY w# t0        $ rT}| j                  j/                  d
t        |             | j$                  j'                  d       d{  7   Y d}~>d}~ww xY w7 \7 7 # tF        $ r Y w xY w7 w)z5Read packets from the Engine.IO WebSocket connection.rL   Nr   z'Server sent %s packet data %s, abortingrX   z*Server has stopped communicating, abortingz4Read loop: WebSocket connection was closed, abortingz1Unexpected error receiving packet: "%s", abortingr   z0Unexpected error decoding packet: "%s", abortingr   r   FrN   r  )%r5   r   wait_forrW   r   r   r   r7   rF   r8   r   r3   r4   typer   	WSMsgTyperP   CLOSINGrQ   rR   r   r   ServerDisconnectedErrorr   r   r   rH   r   r   rS   rT   r  r   r   rY   r6   rZ   )r$   r   r   r   s       r   r   z AsyncClient._read_loop_websocket;  s    jjK'A!**GGOO% ..1B1BBD D "!&&3,7KK''A#$66g.?.?.E.E.5.?.?.G.G.I $I [#aff+	7
 **.....FF"mm15 &&s+++M jjK'P KKAB&&&&::$%%dkk99U & L L L--44T: ++-12aD / '' ##@BjjnnT***,,DD   JLjjnnT***   GF jjnnT***    FAPjjnnT***	
 , 'L  s6  OA	I I CI 'I(I ,O-I :M  O$O %O:6O0O1>O/O0O5O	 O'O( OI I AMJM O#AM8K;9M>OM	AMMMOMO 	N=)AN8,N/-N82O8N==OOO		OOOOc                   K   | j                   dk(  r~t        | j                  | j                        dz   }d}	 t	        j
                  | j                  j                         |       d{   g}|dgk(  r| j                  j                          g }nQ	 	 |j                  | j                  j                                |d    |dd }| j                  j                          nP|sn| j                   dk(  rt#        j$                  |      }| j'                  d| j(                  |j+                         d	d
i| j,                         d{   }|D ]  }| j                  j                           |t/        |t0              r | j                  j3                  |xs d       n|j4                  dk  s|j4                  dk\  r| j                  j3                  d|j4                         d| _        n	 |D ]  }|j8                  r2| j:                  j=                  |j+                                d{    n1| j:                  j?                  |j+                                d{    | j                  j                           	 | j                   dk(  r~| j                  jK                  d       y7 C# | j                  t        j                  f$ r | j                  j                  d       Y [t        j                  $ r Y ow xY w# | j                  $ r Y 2w xY w7 7 7 # t@        jB                  jD                  tF        tH        f$ r | j                  jK                  d       Y w xY ww)zhThis background task sends packages to the server as they are
        pushed to the send queue.
        rL   r   Nzpacket queue is empty, abortingr'   )r   POSTzContent-Typez
text/plain)r   r<   r   r   r   r   r   z5Write loop: WebSocket connection was closed, abortingzExiting write loop task)&r5   r  r   r   r   r  rQ   getrr   r   r3   r   r   rq   r   rp   rV   r   r   r   r   r   r   r7   r8   r4   r   r   binaryrW   
send_bytesr   r   r   r	  BrokenPipeErrorOSErrorr   )r$   r   r   r   r   r   s         r   r   zAsyncClient._write_loopr  s     jjK' $,,d.?.?@1DGG!(!1!1$**..2BG!LLM 4& 

$$&tzz'<'<'>? r{*")#2,

,,.  %%2OOG4,,DMM
+\: 00 - 2 2 # +CJJ((*+9
1c 2KK''IIK88c>QXX_KK'' )=>?hhH+/D(& /::"&''"4"4SZZ\"BBB"&''"2"23::<"@@@

,,./c jjK'z 	23m M$$g&:&:; !!"CD))   ++ 2( C@11II'2 KK$$#$ s   6M*1J0 *J-+J0 0$M*)L >BM*
LB#M*/=L! ,L-1L! L L! ?M*M*-J0 0:L*M*,L>M* LM*LM*LM*L! L! !AM'$M*&M''M*)NNz	engine.io)FN)r   )NNN)__name__
__module____qualname____doc__r   r@   rC   rJ   r   r`   rb   rf   ri   rl   rZ   r|   r   r   r   rG   r   rS   r   r   r   __classcell__)rv   s   @r   r    r    /   s    #H ;?$/-/^	&	J4>,."
(
:%xqf/(I 37,;z(3T53nA4r   r    )r   http.cookiesr   r   r0   r   r,   r   ImportError r   r   r   r   r+   setr   r   
BaseClientr    r"   r   r   <module>r     sm     %   
       
  &*D
4+(( D
4K  Gs   A A$#A$