
    wgu-                         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  ej                  d      Z	g Z
d Zda G d d      Zy)	    N   )base_namespace)packetzsocketio.clientc                     t         dd D ]  }|j                  j                           t        t              rt	        | |      S t        j                  | |      S )zSIGINT handler.

    Notify any clients that are in a reconnect loop to abort. Other
    disconnection tasks are handled at the engine.io level.
    N)reconnecting_clients_reconnect_abortsetcallableoriginal_signal_handlersignaldefault_int_handler)sigframeclients      Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/base_client.pysignal_handlerr      sU     'q) &##%&'(&sE22 ))#u55    c                       e Zd Zg dZej
                  j                  Z	 	 	 	 ddZd ZddZ	d Z
d ZddZd	 Zd
 Zd Zd Zd Zd Zd Zd Zy)
BaseClient)connectconnect_error
disconnect__disconnect_finalNc
                    |	rWt         Qt        j                         t        j                         k(  r(t	        j                  t        j
                  t              a || _        || _        || _	        || _
        || _        |	| _        |
}|	|d<   |j                  dd       }|||d<   |dk(  rt        j                  | _        n$|dk(  rddlm} |j&                  | _        n|| _        ||| j                   _        ||d<    | j+                         di || _        | j,                  j/                  d	| j0                         | j,                  j/                  d
| j2                         | j,                  j/                  d| j4                         t7        |t8              s|| _        nt<        | _        | j:                  j>                  t@        jB                  k(  r|r*| j:                  jE                  t@        jF                         n)| j:                  jE                  t@        jH                         | j:                  jK                  tA        jL                                d | _'        d | _(        d | _)        d | _*        g | _+        d | _,        d | _-        d| _.        i | _/        i | _0        i | _1        i | _2        d | _3        d | _4        d | _5        d | _6        y )Nhandle_sigintengineio_loggerloggerdefaultmsgpackr   )msgpack_packetjsonr   messager   F )7r   	threadingcurrent_threadmain_threadr   SIGINTr   reconnectionreconnection_attemptsreconnection_delayreconnection_delay_maxrandomization_factorr   popr   Packetpacket_class r    MsgPackPacketr!   _engineio_client_classeioon_handle_eio_connect_handle_eio_message_handle_eio_disconnect
isinstanceboolr   default_loggerlevelloggingNOTSETsetLevelINFOERROR
addHandlerStreamHandlerconnection_urlconnection_headersconnection_authconnection_transportsconnection_namespacessocketio_pathsid	connected
namespaceshandlersnamespace_handlers	callbacks_binary_packet_connect_event_reconnect_taskr   )selfr(   r)   r*   r+   r,   r   
serializerr!   r   kwargsengineio_optionsr   r    s                 r   __init__zBaseClient.__init__&   sO   
 4<((*i.C.C.EE&,mmFMM4B'D#(%:""4&<#$8!*!,9)*../@$G&)8X&" &D9$( . < <D *D%)D"'+V$04..0D3CDIt778It778L$"="=>&$' DK(DK{{  GNN2KK((6KK((7&&w'<'<'>?""&#%)"%'"!"$""# $r   c                      y)NFr#   rR   s    r   is_asyncio_basedzBaseClient.is_asyncio_basedh   s    r   c                 <     xs d fd}||S  ||       y)a  Register an event handler.

        :param event: The event name. It can be any string. The event names
                      ``'connect'``, ``'message'`` and ``'disconnect'`` are
                      reserved and should not be used. The ``'*'`` event name
                      can be used to define a catch-all event handler.
        :param handler: The function that should be invoked to handle the
                        event. When this parameter is not given, the method
                        acts as a decorator for the handler function.
        :param namespace: The Socket.IO namespace for the event. If this
                          argument is omitted the handler is associated with
                          the default namespace. A catch-all namespace can be
                          defined by passing ``'*'`` as the namespace.

        Example usage::

            # as a decorator:
            @sio.on('connect')
            def connect_handler():
                print('Connected!')

            # as a method:
            def message_handler(msg):
                print('Received message: ', msg)
                sio.send( 'response')
            sio.on('message', message_handler)

        The arguments passed to the handler function depend on the event type:

        - The ``'connect'`` event handler does not take arguments.
        - The ``'disconnect'`` event handler does not take arguments.
        - The ``'message'`` handler and handlers for custom event names receive
          the message payload as only argument. Any values returned from a
          message handler will be passed to the client's acknowledgement
          callback function if it exists.
        - A catch-all event handler receives the event name as first argument,
          followed by any arguments specific to the event.
        - A catch-all namespace event handler receives the namespace as first
          argument, followed by any arguments specific to the event.
        - A combined catch-all namespace and catch-all event handler receives
          the event name as first argument and the namespace as second
          argument, followed by any arguments specific to the event.
        /c                 f    j                   vri j                   <   | j                      <   | S N)rL   )handlerevent	namespacerR   s    r   set_handlerz"BaseClient.on.<locals>.set_handler   s5    -+-i(.5DMM)$U+Nr   Nr#   )rR   r_   r^   r`   ra   s   `` ` r   r4   zBaseClient.onk   s*    X $		 ?Gr   c                      t              dk(  rCt              dk(  r5t        d         r'  j                  d   j                        d         S  fd}|S )a  Decorator to register an event handler.

        This is a simplified version of the ``on()`` method that takes the
        event name from the decorated function.

        Example usage::

            @sio.event
            def my_event(data):
                print('Received data: ', data)

        The above example is equivalent to::

            @sio.on('my_event')
            def my_event(data):
                print('Received data: ', data)

        A custom namespace can be given as an argument to the decorator::

            @sio.event(namespace='/test')
            def my_event(data):
                print('Received data: ', data)
        r   r   c                 P      j                   | j                  gi |       S r]   )r4   __name__)r^   argsrT   rR   s    r   ra   z%BaseClient.event.<locals>.set_handler   s+    Awtwww//A$A&A'JJr   )lenr
   r4   rd   )rR   re   rT   ra   s   ``` r   r_   zBaseClient.event   sZ    0 t9>c&kQ.8DG3D -47747++,T!W55K r   c                     t        |t        j                        st        d      | j	                         |j	                         k7  rt        d      |j                  |        || j                  |j                  <   y)zRegister a namespace handler object.

        :param namespace_handler: An instance of a :class:`Namespace`
                                  subclass that handles all the event traffic
                                  for a namespace.
        zNot a namespace instancez+Not a valid namespace class for this clientN)r8   r   BaseClientNamespace
ValueErrorrY   _set_clientrM   r`   )rR   namespace_handlers     r   register_namespacezBaseClient.register_namespace   ss     +(<<>788  "&7&H&H&JJJKK%%d+ 	 1 ; ;<r   c                 @    | j                   j                  |xs d      S )a   Return the ``sid`` associated with a connection.

        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the handler is associated with the default
                          namespace. Note that unlike previous versions, the
                          current version of the Socket.IO protocol uses
                          different ``sid`` values per namespace.

        This method returns the ``sid`` for the requested namespace as a
        string.
        r[   )rK   get)rR   r`   s     r   get_sidzBaseClient.get_sid   s     ""9#344r   c                 6    | j                   j                         S )zReturn the name of the transport used by the client.

        The two possible values returned by this function are ``'polling'``
        and ``'websocket'``.
        )r3   	transportrX   s    r   rq   zBaseClient.transport   s     xx!!##r   c                    d }|| j                   v rb|| j                   |   v r| j                   |   |   }||fS || j                  vr)d| j                   |   v r| j                   |   d   }|g|}||fS d| j                   v re|| j                   d   v r| j                   d   |   }|g|}||fS || j                  vr*d| j                   d   v r| j                   d   d   }||g|}||fS N*)rL   reserved_events)rR   r_   r`   re   r^   s        r   _get_event_handlerzBaseClient._get_event_handler   s(    %i00--	259 } d2224==33--	237~~ } DMM!c**--,U3!)D)
 }	 d2224==----,S1y040}r   c                     d }|| j                   v r| j                   |   }||fS d| j                   v r| j                   d   }|g|}||fS rs   )rM   )rR   r`   re   r^   s       r   _get_namespace_handlerz!BaseClient._get_namespace_handler  sj     ///--i8G } D+++--c2G%%D}r   c                     |xs d}|| j                   vr$dt        j                  d      i| j                   |<   t        | j                   |   d         }|| j                   |   |<   |S )z/Generate a unique identifier for an ACK packet.r[   r   r   )rN   	itertoolscountnext)rR   r`   callbackids       r   _generate_ack_idzBaseClient._generate_ack_id  sd    $	DNN*)*IOOA,>(?DNN9%$..+A./(0y!"%	r   c                     t               r]   NotImplementedErrorrX   s    r   r5   zBaseClient._handle_eio_connect      !##r   c                     t               r]   r   )rR   datas     r   r6   zBaseClient._handle_eio_message   r   r   c                     t               r]   r   )rR   reasons     r   r7   z!BaseClient._handle_eio_disconnect#  r   r   c                     t               r]   r   rX   s    r   r2   z!BaseClient._engineio_client_class&  r   r   )	Tr   r      g      ?Fr   NT)NNr]   )rd   
__module____qualname__ru   engineioClientr   rV   rY   r4   r_   rl   ro   rq   rv   rx   r   r5   r6   r7   r2   r#   r   r   r   r   !   sn    -O__##F@A>?DM*.@%D6p!F 5$4$$$$r   r   )rz   r<   r   r$   r   r0   r   r   	getLoggerr:   r   r   r   r   r#   r   r   <module>r      sL          """#45 6  F$ F$r   