
    iu                         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n# e$ r dZY nw xY w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dS )    N)SimpleCookie   )base_client)
exceptions)packet)payloadFc                  D    d } t          j         |                        dS )z>SIGINT handler.

    Disconnect all active async clients.
    c                  x  K   t           j        d d          D ]0} |                                 r|                                  d {V  1d t	          j                    D             }|D ]}|                                 t	          j        |ddi d {V  t	          j                    	                                 d S )Nc                 <    g | ]}|t          j                    u|S  )asynciocurrent_task).0tasks     @/usr/local/lib/python3.11/dist-packages/engineio/async_client.py
<listcomp>z:async_signal_handler.<locals>._handler.<locals>.<listcomp>%   s<     ) ) )$%'':( :( :( :( :(    return_exceptionsT)
r   connected_clientsis_asyncio_based
disconnectr   	all_taskscancelgatherget_running_loopstop)ctasksr   s      r   _handlerz&async_signal_handler.<locals>._handler   s      .qqq1 	% 	%A!!## %llnn$$$$$$$) )'"3"5"5 ) ) ) 	 	DKKMMMMne<t<<<<<<<<< ""'')))))r   Nr   ensure_future)r   s    r   async_signal_handlerr"      s0    
* * * ((**%%%%%r   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 )AsyncClientaH	  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. This is a process-wide setting, all instantiated
                 servers and clients must use the same JSON module.
    :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                     dS )NTr   selfs    r   r   zAsyncClient.is_asyncio_basedU   s    tr   N	engine.ioc                 ^  K   | j         rt          st          j                    t          j                    k    rb	 t          j                                        t          j	        t                     n*# t          $ r | j                            d           Y nw xY wda| j        dk    rt          d          ddg|7t!          |t"                    r|g}fd|D             }|st          d	          |p| _         t'          | d
| j        d         z             ||pi |           d{V S )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	websocketNc                     g | ]}|v |	S r   r   )r   	transportvalid_transportss     r   r   z'AsyncClient.connect.<locals>.<listcomp>   s.     < < <	&*::: $:::r   z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headersr@   engineio_pathr/   s        @r   connectzAsyncClient.connectX   s     0  	E&> 	E(**i.C.E.EEEE(**==M#79 9 9 9& E E E##$CDDDDDE#' :''DEEE%{3!*c** *(\
< < < <Z < < <J A !?@@@$8(8DWT;1C#CDDB/ / / / / / / / 	/s   6A2 2$BBc                 6   K   | j         r| j          d{V  dS dS )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>        	&%%%%%%%%%%	& 	&r   c                 |   K   |                      t          j        t          j        |                     d{V  dS )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'   rK   s     r   sendzAsyncClient.send   sE       fn4 H H HIIIIIIIIIIIr   Fc                 F  K   | j         dk    r|                     t          j        t          j                             d{V  | j                            d           d{V  d| _         |                     d|p| j        j	        d           d{V  | j
        dk    r| j                                         d{V  |s| j         d{V  d| _         	 t          j                            |            n# t"          $ r Y nw xY w|                                  d{V  dS )	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*   )r<   rL   r   rM   CLOSEqueueput_trigger_eventreasonCLIENT_DISCONNECTcurrent_transportwscloserG   r   r   remover=   _reset)r'   abortrY   s      r   r   zAsyncClient.disconnect   s      :$$##FM&,$?$?@@@@@@@@@*..&&&&&&&&&(DJ%%l&,&M0M05 & 7 7 7 7 7 7 7 7 7 %44gmmoo%%%%%%% *))))))))'DJ-44T::::   kkmms   C7 7
DDc                 6    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$     $VVT%<V%<%<===r   r   c                 :   K   t          j        |           d{V S )z[Sleep for the requested amount of time.

        Note: this method is a coroutine.
        N)r   sleep)r'   secondss     r   rg   zAsyncClient.sleep   s*      
 ]7+++++++++r   c                 $    t          j        |i |S )zCreate a queue object.)r   Queue)r'   rc   rd   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                 V  K   t                                                       	 	 | j                                         | j                                         n# | j        $ r Y nw xY wE| j        s4| j        r/| j        j        s%| j        	                                 d {V  d S d S d S d S N)
superr_   rV   
get_nowait	task_donequeue_emptyexternal_httphttpclosedr]   )r'   	__class__s    r   r_   zAsyncClient._reset   s      	
%%'''
$$&&&&#   		 ! 	(y (!1 (ioo'''''''''''	( 	(( ( ( (s   2A 
A&%A&c                 H   | j         r| j         j        s	 t          j                    }|                                r.|                    | j                                                    d S |                    | j                                                    d S #  Y d S xY wd S d S rs   )ry   rz   r   get_event_loop
is_runningr!   r]   run_until_complete)r'   loops     r   __del__zAsyncClient.__del__   s    9 	TY- 	-//??$$ ?&&ty'8'899999++DIOO,=,=>>>>>	 	 	 	s   AB *,B Bc                 V  K   t           | j                            d           dS |                     ||d          | _        | j                            d| j        z              |                     d| j        |                                 z   || j                   d{V }|t          |t                    r0|                                  d{V  t          j        |pd          |j        dk     s|j        d	k    rz|                                  d{V  	 |                                 d{V }n# t           j        $ r d}Y nw xY wt          j        d
                    |j                  |          	 t%          j        |                                 d{V                     d                    }n## t,          $ r t          j        d          dw xY w|j        d         }|j        t2          j        k    rt          j        d          | j                            dt          |j                  z              |j        d         | _        |j        d         | _        t=          |j        d                   dz  | _        t=          |j        d                   dz  | _         d| _!        | xj        d| j        z   z  c_        d| _"        tF          j$        %                    |            | &                    dd           d{V  |j        dd         D ]}| '                    |           d{V  d| j        v r(d| j(        v r| )                    |||           d{V rdS | *                    | j+                  | _,        | *                    | j-                  | _.        dS )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)rC   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=rQ   rE   FrS   r   r,   )/aiohttpr:   error_get_engineio_urlbase_urlinfo_send_request_get_url_timestamprequest_timeoutr>   r?   r_   r   ConnectionErrorstatusjsonClientErrorformatr   Payloadreaddecoder=   packetspacket_typer   OPENrK   r   r   intping_intervalping_timeoutr[   r<   r   r   appendrX   _receive_packetr@   _connect_websocketre   _write_loopwrite_loop_task_read_loop_pollingrG   )	r'   rB   rC   rD   rargpopen_packetpkts	            r   _connect_pollingzAsyncClient._connect_polling   s'     ?K * + + +F..sM9MM<t}LMMM$$4=4#:#:#<#<<g( % * * * * * * * * 9
1c**9++--,779 9 98c>>QX__++--FFHHnnnnnn&   ,>EEH "$ $ $	=qvvxx0G0G1 1   AA 	= 	= 	=,13 38<=	= il"fk11,46 6 6/#k6F2G2GG	I 	I 	I#E*#(4 !1.!ABBVK 0 ?@@6I!*48++ 
%,,T222!!)u!=========9QRR= 	, 	,C&&s++++++++++$-''K4?,J,J,,S'=IIIIIIII #99$:JKK"88#% %s   D5 5E	E	:A F; ; Gc                 l  K   t           | j                            d           dS |                     ||d          }| j        r-| j                            d|z              d}|d| j        z   z  }n&d}|| _        | j                            d|z              | j        | j        j        rt          j	                    | _        |
                                D ]r\  }}|                                d	k    rUt          ||                   }| j        j                            d
 |
                                D                        ||=  nsdt          j        | j                  i}	| j        s0t%          j                    }
d|
_        t$          j        |
_        |
|	d<   |                    | j                            di                      ||	d<   |	                    | j                   	  | j        j        ||                                 z   fi |	 d{V }nl# t           j        j        t           j        j        t           j        j        f$ r4 |r| j                             d           Y dS tC          j"        d          w xY w|rtG          j$        tF          j%        d          &                                }	 |'                    |           d{V  n@# tP          $ r3}| j                             dtS          |                     Y d}~dS d}~ww xY w	 |*                                 d{V j+        }n@# tP          $ r3}| j                             dtS          |                     Y d}~dS d}~ww xY wtG          j$        |          }|j,        tF          j-        k    s|j+        dk    r| j                             d           dS tG          j$        tF          j.                  &                                }	 |'                    |           d{V  n@# tP          $ r3}| j                             dtS          |                     Y d}~dS d}~ww xY wd| _/        | j                            d           nv	 |*                                 d{V j+        }n6# tP          $ r)}tC          j"        dtS          |          z             d}~ww xY wtG          j$        |          }|j,        tF          j0        k    rtC          j"        d          | j                            dtS          |j+                  z              |j+        d         | _        |j+        d         | _1        te          |j+        d                   dz  | _3        te          |j+        d                   dz  | _4        d| _/        d | _5        tl          j7        8                    |            | 9                    d!d"           d{V  || _:        | ;                    | j<                  | _=        | ;                    | j>                  | _?        dS )#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 cookiec                 $    i | ]\  }}||j         S r   )value)r   kms      r   
<dictcomp>z2AsyncClient._connect_websocket.<locals>.<dictcomp>I  s     777DAqQ777r   r   )ws_closesslrC   z*WebSocket upgrade failed: connection errorzConnection errorproberJ   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   rQ   rE   rS   )@r   r:   r   r   r   r   r   ry   rz   ClientSessionitemslowerr   
cookie_jarupdate_cookiesClientWSTimeoutr   
ssl_verifyr   create_default_contextcheck_hostname	CERT_NONEverify_modeupdatewebsocket_extra_optionspop
ws_connectr   client_exceptionsWSServerHandshakeErrorServerConnectionErrorClientConnectionErrorr;   r   r   r   rM   PINGencodesend_str	Exceptionr?   receiverK   r   PONGUPGRADEr[   r   r   r   r   r   r<   r   r   r   rX   r\   re   r   r   _read_loop_websocketrG   )r'   rB   rC   rD   websocket_urlupgradeheaderr   ckextra_optionsssl_contextr\   r   er   r   s                   r   r   zAsyncClient._connect_websocket.  s     ?K=>>>5..sM/:< <8 		GK2]BD D DGWtx//MMG)DMK5EG G G 9	 0-//DI %]]__ 	 	MFE||~~))!'&/22	$3377BHHJJ7779 9 9FO * w.8LMMMO 	/466K).K&&)mK##.M%  	t377	2FFGGG#*i T9:::	E+ty+ 7 7 9 99L L=JL L L L L L L LBB)@)?)?A 	E 	E 	E  E##@B B Buu 01CDDD	E  2	Bfk888??AAAkk!nn$$$$$$$$   ##MFF   uuuuu	
::<<''''''-   ##MFF   uuuuu	
 -q111C&+--W1D1D##>@ @ @ufn--4466Akk!nn$$$$$$$$   ##MFF   uuuuu	
 &1D"K?@@@@<::<<''''''- < < < 01CFF:< < << !-q999K&&+55 01ABBBK5K<L8M8MMO O O"'.DH',Z8DM!$[%5n%E!F!F!OD #K$4]$C D Dv MD%0D"$DJ)00666%%i5%AAAAAAAAA#99$:JKK"88%' 'tss   *.H AJ-J;K 
L!(LLL8 8
M5(M00M5:P 
Q (QQ:R 
S$$SSc                    K   |j         t          t          j                  k     rt          j        |j                  nd}| j                            d|t          |j        t                    s|j        nd           |j         t          j	        k    r%| 
                    d|j        d           d{V  dS |j         t          j        k    r?|                     t          j        t          j        |j                             d{V  dS |j         t          j        k    r)|                     d| j        j                   d{V  dS |j         t          j        k    rdS | j                            d	|j                    dS )
z(Handle incoming packets from the server.UNKNOWNzReceived packet %s data %s<binary>messageTrS   N)r`   rY   z%Received unexpected packet of type %s)r   lenr   packet_namesr:   r   r>   rK   bytesrN   rX   r   rL   rM   r   rU   r   rY   SERVER_DISCONNECTNOOPr   )r'   r   packet_names      r   r   zAsyncClient._receive_packet  s      V%8!9!999 )#/::?H 	(+&sx77GCHHZ	I 	I 	I ?fn,,%%iT%JJJJJJJJJJJ_++##FM&+sx$H$HIIIIIIIIIII_,,//)-)F " H H H H H H H H H H H_++DKE!o/ / / / /r   c                   K   | j         dk    rdS | j                            |           d{V  | j                            dt
          j        |j                 t          |j	        t                    s|j	        nd           dS )z(Queue a packet to be sent to the server.rQ   NzSending packet %s data %sr   )r<   rV   rW   r:   r   r   r   r   r>   rK   r   )r'   r   s     r   rL   zAsyncClient._send_packet  s      :$$FjnnS!!!!!!!!!'0&sx77GCHHZ	I 	I 	I 	I 	Ir   c                   K   | j         | j         j        rt          j                    | _         t	          | j         |                                          }	 | j        s) ||||t          j        |          d           d {V S  ||||t          j        |                     d {V S # t          j        t          j
        f$ r6}| j                            d|||           t          |          cY d }~S d }~ww xY w)N)totalF)rC   rK   r   r   )rC   rK   r   z+HTTP %s request to %s failed with error %s.)ry   rz   r   r   rA   r   r   ClientTimeoutr   r   TimeoutErrorr:   r   r?   )r'   methodrB   rC   bodyr   http_methodexcs           r   r   zAsyncClient._send_request  sn      9	 0-//DIdi88	? B([t#1@@@eM M M M M M M M M M )[t#1@@@B B B B B B B B B B #W%9: 	 	 	KJ#S#/ / /s88OOOOOO	s$   /B. 'B. .C?	+C:4C?:C?c                 2   K   |                     dd          }d} j        v rt          j         j                           du r|rT  j         j                 gR  }t
                              |           |                    t
          j                   |S 	 	   j                   d{V }nE# t          $ r8 dk    r0t                    dk    r  j                              d{V cY S  w xY wn# t          j        $ r Y n  j                            dz              dk    rY dS Y nxY w|rW fd	}                     |          }t
                              |           |                    t
          j                   |S 	 	   j                  }n@# t          $ r3 dk    r)t                    dk    r  j                             }n Y nw xY wn-#   j                            d
z              dk    rY dS Y nxY w|S )zInvoke an event handler.rT   FNTr   r   z async handler errorrE   c                  (   K    j                    S rs   )handlers)rc   eventr'   s   r   async_handlerz1AsyncClient._trigger_event.<locals>.async_handler  s      3t}U3T::r   z handler error)r   r   inspectiscoroutinefunctionre   task_reference_holderaddadd_done_callbackdiscard	TypeErrorr   r   CancelledErrorr:   	exception)r'   r   rc   rd   rT   retr   r   s   ```     r   rX   zAsyncClient._trigger_event  s     JJ{E22	DM!!*4=+?@@DHH )545dmE6J =7;= = =D)--d333**+@+HIIIK)	&(<e(<d(C"C"C"C"C"C"CCC( & & &$44$'IINN .BT]5-A-C-C'C'C'C'C'C'C C C C %&   #1   )--e6L.LMMM I-- $)55 .-
  ); ; ; ; ; ; ;  55mDDD)--d333**+@+HIIIK)	&"6$-"6"=CC( & & &$44$'IINN ';dmE&:&<&< % !$&)--e6F.FGGG I-- $)55 .- 
sZ   !B8 7C? 8>C:6C? 8C::C? ?D:$D:F) (G* ):G&#G* %G&&G* *%Hc                   K   | j         dk    r| j        r| j                            d| j        z              |                     d| j        |                                 z   t          | j        | j	                  dz              d{V }|t          |t                    r>| j                            |pd           | j                            d           d{V  n|j        dk     s|j        d	k    rA| j                            d
|j                   | j                            d           d{V  n	 t!          j        |                                 d{V                     d                    }nJ# t(          $ r= | j                            d           | j                            d           d{V  Y n<w xY w|j        D ]}|                     |           d{V  | j         dk    r| j        | j        r'| j                            d           | j         d{V  | j         dk    rs|                     d| j        j        d           d{V  	 t4          j                            |            n# t(          $ r Y nw xY w|                                  d{V  | j                            d           dS )z-Read packets by polling the Engine.IO server.rQ   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   FrS   Exiting read loop task)r<   r   r:   r   r   r   r   maxr   r   r>   r?   r;   rV   rW   r   r   r   r   r   r=   r   r   rX   rY   TRANSPORT_ERRORr   r   r^   r_   )r'   r   r   r   s       r   r   zAsyncClient._read_loop_polling  s     jK''D,@'K1DMAC C C((t}t'>'>'@'@@D.0ABBQF ) H H H H H H H HA yJq#..y##EEG G GjnnT*********x#~~S## %9:;(D D DjnnT*********O16688^^^^^^4K4K5 5      ##=? ? ?jnnT*********	
 y 0 0**3//////////3 jK''D,@'6  	'KABBB&&&&&&&&:$$%%dk9U & L L L L L L L L L-44T::::   ++--122222s&   9A E: :AG GI> >
J
Jc                   K   | j         dk    rd}	 t          j        | j                                        | j        | j        z              d{V }t          |j        t          t          f          s| j                            d|j        t          j        j        t          j        j        fv rdnt          |j                  t          |j                             | j                            d           d{V  n|j        }n# t          j        $ r> | j                            d           | j                            d           d{V  Y nJt          j        j        $ r= | j                            d           | j                            d           d{V  Y nt0          $ rR}| j                            dt          |                     | j                            d           d{V  Y d}~nd}~ww xY w	 t3          j        |	          }n_# t0          $ rR}| j                            d
t          |                     | j                            d           d{V  Y d}~n/d}~ww xY w|                     |           d{V  | j         dk    | j        r'| j                            d           | j         d{V  | j         dk    rs|                     d| j        j        d           d{V  	 t@          j!        "                    |            n# tF          $ r Y nw xY w| $                                 d{V  | j                            d           dS )z5Read packets from the Engine.IO WebSocket connection.rQ   Nr  z'Server sent %s packet data %s, abortingr]   z*Server has stopped communicating, abortingz4Read loop: WebSocket connection was closed, abortingz1Unexpected error receiving packet: "%s", abortingr   z0Unexpected error decoding packet: "%s", abortingr  r   FrS   r  )%r<   r   wait_forr\   r   r   r   r>   rK   r?   r   r:   r;   typer   	WSMsgTyperU   CLOSINGrV   rW   r   r   ServerDisconnectedErrorr   r   r   rM   r   r   rX   rY   r
  r   r   r^   r=   r_   )r'   r   r   r   s       r   r   z AsyncClient._read_loop_websocket<  sA     jK''A!*GOO%% .1BBD D D D D D D D D "!&3,77 K''A#$6g.?.E.5.?.G.I $I $I [[#af++	7 7 7
 *...........F'   ##@B B BjnnT*********,D     JL L LjnnT*********     GFF   jnnT*********m1555     FAP P PjnnT*********	
 &&s+++++++++M jK''P  	'KABBB&&&&&&&&:$$%%dk9U & L L L L L L L L L-44T::::   ++--122222sX   C,D  D A	G>AG>#	G>,AG99G>H 
I4"AI//I4 L   
L-,L-c                 F  K   | j         dk    rt          | j        | j                  dz   }d}	 t	          j        | j                                        |           d{V g}nH# | j        t          j	        f$ r | j
                            d           Y nqt          j        $ r Y n`w xY w|dgk    r| j                                         g }nl	 	 |                    | j                                                   n# | j        $ r Y n1w xY w|d         $|dd         }| j                                         nk|sn| j        dk    rt#          j        |          }|                     d	| j        |                                d
di| j                   d{V }|D ]}| j                                         |t/          |t0                    r| j
                            |pd           n|j        dk     s|j        dk    r(| j
                            d|j                   d| _        nn	 |D ]}|j        r3| j                            |                                           d{V  n2| j                            |                                           d{V  | j                                         nA# t@          j!        j"        tF          tH          f$ r | j
        %                    d           Y nw xY w| j         dk    | j
        %                    d           dS )zhThis background task sends packages to the server as they are
        pushed to the send queue.
        rQ   r  Nzpacket queue is empty, abortingTr+   )r   POSTzContent-Typez
text/plain)r   rC   r   r  r   r   r  z5Write loop: WebSocket connection was closed, abortingzExiting write loop task)&r<   r	  r   r   r   r  rV   getrw   r   r:   r   r   rv   r   ru   r[   r   r   r   r   r   r   r>   r?   r;   r   r   binaryr\   
send_bytesr   r   r   r  BrokenPipeErrorOSErrorr   )r'   r   r   r   r   r   s         r   r   zAsyncClient._write_loops  s      jK'' $,d.?@@1DGG!(!1$*..2B2BG!L!LLLLLLLM$g&:;   !!"CDDD)   4&  
$$&&&tz'<'<'>'>????+   r{*")#2#,
,,...  %22OG444,,DM

+\: 0 - 2 2 2 2 2 2 2 2 # + +CJ((****9
1c 2 29K''IIK K K8c>>QX__K'' )=>?hH H H+/D(	 &5& / /: A"&'"4"4SZZ\\"B"BBBBBBBBB"&'"2"23::<<"@"@@@@@@@@
,,..../  1I'2   K$$#$ $ $ Eo jK''z 	233333s<   3A# #0B(B('B(,C= =
D
	D
/B
J: :;K87K8)NNr(   )FN)r   )NNN)__name__
__module____qualname____doc__r   rE   rH   rO   r   re   rg   rk   rn   rq   r_   r   r   r   r   rL   r   rX   r   r   r   __classcell__)r{   s   @r   r$   r$   /   s       $ $J   ;?$/-/ -/ -/ -/^	& 	& 	&	J 	J 	J   4> > >, , , ,. . ." " "  
( 
( 
( 
( 
(
 
 
:% :% :%xq q qf/ / /(I I I 37   ,; ; ;z(3 (3 (3T53 53 53nA4 A4 A4 A4 A4 A4 A4r   r$   )r   http.cookiesr   r   r7   r   r3   r   ImportError r   r   r   r   r2   setr   r"   
BaseClientr$   r   r   r   <module>r#     s)    % % % % % %   



    NNNN   GGG                          
  & & &*E
4 E
4 E
4 E
4 E
4+( E
4 E
4 E
4 E
4 E
4s   ! ++