
    wgs                         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   )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      Z/home/mcse/projects/flask/flask-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 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_basedR   s    r   c                   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 || _        | j'                         | _         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create_queuequeuegetattr)r#   urlheadersr8   engineio_pathvalid_transports	transports          r   connectzAsyncClient.connectU   sK    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   <E6D 59E.E;AEE	E$E>E E	E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#   rG   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%   )r4   rH   r   rI   CLOSEr:   put_trigger_eventreasonCLIENT_DISCONNECTcurrent_transportwscloserC   r   r   remover5   _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                 X    t        j                         }t         j                  |_        |S )zCreate a queue object.)r   Queue
QueueEmptyEmpty)r#   qs     r   r9   zAsyncClient.create_queue   s    MMO$$r   c                 *    t        j                         S )zCreate an event object.)r   Eventr"   s    r   create_eventzAsyncClient.create_event   s    }}r   c                    K   t         |           | j                  sG| j                  r:| j                  j                  s#| j                  j                          d {    y y y y 7 wN)superrZ   external_httphttpclosedrX   )r#   	__class__s    r   rZ   zAsyncClient._reset   sP     !!yy!1!1iioo''' "2y "'s   AA(A&	A(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rm   )rp   rq   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=rM   rA   FrO   r   r'   )/aiohttpr2   error_get_engineio_urlbase_urlinfo_send_request_get_url_timestamprequest_timeoutr6   r7   rZ   r   ConnectionErrorstatusjsonClientErrorformatr   Payloadreaddecoder5   packetspacket_typer   OPENrG   r   r   intping_intervalping_timeoutrV   r4   r   r   appendrS   _receive_packetr8   _connect_websocketr`   _write_loopwrite_loop_task_read_loop_pollingrC   )	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           
      "  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                         | _        i }|j                         D ]U  \  }}|j                         d	k(  st        |j                  d
      D 	cg c]  }	|	j                  dd       c}	      }||=  n | j                  j                  j                  |       d| j                   i}
| j"                  s5t%        j&                         }d|_        t$        j*                  |_        ||
d<   |j/                  | j0                  j3                  di              ||
d<   |
j/                  | j0                         	  | j                  j4                  || j7                         z   fi |
 d{   }|r=tG        jH                  tF        jJ                  d      jM                         }	 |jO                  |       d{    	 |jU                          d{   jV                  }tG        jH                  |      }|jX                  tF        jZ                  k7  s|jV                  dk7  r| j                  jA                  d       ytG        jH                  tF        j\                        jM                         }	 |jO                  |       d{    d| _/        | j                  j                  d       nL	 |jU                          d{   jV                  }tG        jH                  |      }|jX                  tF        j`                  k7  rtC        jD                  d      | j                  j                  dtS        |jV                        z          |jV                  d   | _        |jV                  d   | _1        te        |jV                  d         dz  | _3        te        |jV                  d          dz  | _4        d| _/        d!| _5        tl        jn                  jq                  |        | js                  d"d#       d{    || _:        | jw                  | jx                        | _=        | jw                  | j|                        | _?        yc c}	w 7 # t         j8                  j:                  t         j8                  j<                  t         j8                  j>                  f$ r5 |r| j                  jA                  d       Y ytC        jD                  d      w xY w7 # tP        $ r/}| j                  jA                  dtS        |             Y d}~yd}~ww xY w7 ># tP        $ r/}| j                  jA                  dtS        |             Y d}~yd}~ww xY w7 # tP        $ r/}| j                  jA                  dtS        |             Y d}~yd}~ww xY w7 # tP        $ r&}tC        jD                  dtS        |      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 cookiez; =r   r{   sslr=   z*WebSocket upgrade failed: connection errorzConnection errorproberF   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   rM   rA   rO   )@r   r2   r   r   r   r   r   rp   rq   ClientSessionitemslowerdictsplit
cookie_jarupdate_cookiesr   
ssl_verifyr   create_default_contextcheck_hostname	CERT_NONEverify_modeupdatewebsocket_extra_optionspop
ws_connectr   client_exceptionsWSServerHandshakeErrorServerConnectionErrorClientConnectionErrorr3   r   r   r   rI   PINGencodesend_str	Exceptionr7   receiverG   r   PONGUPGRADErV   r   r   r   r   r   r4   r   r   r   rS   rW   r`   r   r   _read_loop_websocketrC   )r#   r<   r=   r>   websocket_urlupgradecookiesheadervaluer   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||~)8=D8IJfV\\#q)JLFO	 			++G4"D$8$89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 K&L))@@))??))??A 	E ##@B 001CDD	E % ##MF 	 ( ##MF 	 % ##MF 	 ( < 001CF:< <<  BsC  DZZT
6CZ1T 4T5T 96Z0V  VV  
W WW ,BZ<X XX $Z:Y YY D$Z ZAZT A-VZVZV   	W)%WZWZW 	X'%XZXZX 	Y%%Y
ZYZY 	Z	#!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>messageTrO   N)r[   rT   z%Received unexpected packet of type %s)r   lenr   packet_namesr2   r   r6   rG   bytesrJ   rS   r   rH   rI   r   rQ   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.rM   NzSending packet %s data %sr   )r4   r:   rR   r2   r   r   r   r   r6   rG   r   )r#   r   s     r   rH   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=   rG   r{   r   )r=   rG   r{   z+HTTP %s request to %s failed with error %s.)rp   rq   r   r   r;   r   r   ClientTimeoutr   r   TimeoutErrorr2   r   r7   )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.rP   FNTr   r   z async handler errorrA   c                  0   K    j                        S wrm   )handlers)r^   eventr#   s   r   async_handlerz1AsyncClient._trigger_event.<locals>.async_handler  s     3t}}U3T::s   z handler error)r   r   r   iscoroutinefunctionr`   task_reference_holderaddadd_done_callbackdiscard	TypeErrorr   CancelledErrorr2   	exception)r#   r   r^   r_   rP   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.rM   zSending polling GET request to rz      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   FrO   Exiting read loop task)r4   r   r2   r   r   r   r   maxr   r   r6   r7   r3   r:   rR   r   r   r   r   r   r5   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.rM   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   FrO   r   )%r4   r   wait_forrW   r   r   r   r6   rG   r7   r   r2   r3   typer   	WSMsgTyperQ   CLOSINGr:   rR   r   r   ServerDisconnectedErrorr   r   r   rI   r   r   rS   rT   r   r   r   rY   r5   rZ   )r#   r   r   r   s       r   r   z AsyncClient._read_loop_websocket0  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                  j                  t        j                  f$ r | j                  j                  d       Y et        j                  $ r Y yw xY w# | j                  j                  $ r Y Fw 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.
        rM   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)&r4   r   r   r   r   r   r:   getrg   r   r2   r   r   	task_doner   
get_nowaitrV   r   r   r   r   r   r   r6   r7   r3   r   r   binaryrW   
send_bytesr   r   r   r  BrokenPipeErrorOSErrorr   )r#   r{   r   r   r   r   s         r   r   zAsyncClient._write_loopg  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JJ$$g&:&:; !!"CD))   ::++ 2( C@11II'2 KK$$#$ s   6M?1J0 *J-+J0 0$M?)L >BM?
L.B#M?/=L6 ,L1-1L6 L4 L6 ?M?M?-J0 0AL4M?6LM?
LM?L+'M?*L++M?1L6 4L6 6AM<9M?;M<<M?)NNz	engine.io)FN)r   )NNN)__name__
__module____qualname____doc__r   rA   rD   rK   r   r`   rb   r9   rk   rZ   rx   r   r   r   rH   r   rS   r   r   r   __classcell__)rr   s   @r   r   r   -   s    #H ;?$/./`	&	J4>,(
:%xqf/(I 37,;z(3T53nA4r   r   )r   r/   r   r+   r   ImportError r   r   r   r   r*   setr   r   
BaseClientr   r!   r   r   <module>r     sg      
       
  &*{	4+(( {	4K  Gs   A AA