o
    ¨Ýj™-  ã                   @   sl   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 d¡Z	g Z
dd„ ZdaG dd	„ d	ƒZdS )
é    Né   )Úbase_namespace)Úpacketzsocketio.clientc                 C   s:   t dd… D ]}|j ¡  qttƒrt| |ƒS t | |¡S )z•SIGINT 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Úclient© r   úO/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/base_client.pyÚsignal_handler   s
   
r   c                   @   sš   e Zd Zg d¢ZejjZ				d&d
d„Zdd„ Zd'dd„Z	dd„ Z
dd„ Zd(dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd	S ))Ú
BaseClient)ÚconnectZconnect_errorÚ
disconnectZ__disconnect_finalTr   r   é   ç      à?FÚdefaultNc
                 K   sÊ  |	rt d u rt ¡ t ¡ krt tjt¡a || _|| _|| _	|| _
|| _|	| _|
}|	|d< | dd ¡}|d ur;||d< |dkrDtj| _n|dkrSddlm} |j| _n|| _|d urb|| j_||d< |  ¡ di |¤Ž| _| j d	| j¡ | j d
| j¡ | j d| j¡ t|tƒs�|| _n#t| _| jjt j!kr°|r¡| j "t j#¡ n| j "t j$¡ | j %t  &¡ ¡ d | _'d | _(d | _)d | _*g | _+d | _,d | _-d| _.i | _/g | _0i | _1i | _2i | _3d | _4d | _5d | _6d | _7d S )NÚhandle_sigintÚengineio_loggerÚloggerr   Úmsgpackr   )Úmsgpack_packetÚjsonr   Úmessager   Fr   )8r	   Ú	threadingÚcurrent_threadÚmain_threadr
   ÚSIGINTr   ÚreconnectionÚreconnection_attemptsÚreconnection_delayÚreconnection_delay_maxÚrandomization_factorr   Úpopr   ZPacketZpacket_classÚ r   Z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ÚStreamHandlerZconnection_urlZconnection_headersZconnection_authZconnection_transportsZconnection_namespacesZsocketio_pathZsidÚ	connectedÚ
namespacesZfailed_namespacesÚhandlersÚnamespace_handlersÚ	callbacksZ_binary_packetZ_connect_eventZ_reconnect_taskr   )Úselfr#   r$   r%   r&   r'   r   Ú
serializerr   r   ÚkwargsZengineio_optionsr   r   r   r   r   Ú__init__&   sl   ÿ



zBaseClient.__init__c                 C   s   dS )NFr   ©r@   r   r   r   Úis_asyncio_basedi   s   zBaseClient.is_asyncio_basedc                    s0   ˆpd‰‡ ‡‡fdd„}|du r|S ||ƒ dS )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                    s&   ˆˆj vr
i ˆj ˆ< | ˆj ˆ ˆ < | S ©N)r=   ©Úhandler©ÚeventÚ	namespacer@   r   r   Úset_handlerš   s   

z"BaseClient.on.<locals>.set_handlerNr   )r@   rK   rI   rL   rM   r   rJ   r   r,   l   s
   ,zBaseClient.onc                    sP   t ˆ ƒdkrt ˆƒdkrtˆ d ƒrˆ ˆ d j¡ˆ d ƒS ‡ ‡‡fd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                    s   ˆj | jgˆ ¢R i ˆ¤Ž| ƒS rG   )r,   Ú__name__rH   ©ÚargsrB   r@   r   r   rM   Â   s   z%BaseClient.event.<locals>.set_handler)Úlenr   r,   rN   )r@   rP   rB   rM   r   rO   r   rK   ¤   s   $zBaseClient.eventc                 C   sF   t |tjƒs
tdƒ‚|  ¡ | ¡ krtdƒ‚| | ¡ || j|j< dS )zõRegister 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)r0   r   ZBaseClientNamespaceÚ
ValueErrorrE   Z_set_clientr>   rL   )r@   Únamespace_handlerr   r   r   Úregister_namespaceÇ   s   ÿ
ÿzBaseClient.register_namespacec                 C   s   | j  |p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.
        rF   )r<   Úget)r@   rL   r   r   r   Úget_sid×   s   zBaseClient.get_sidc                 C   s
   | j  ¡ S )z§Return the name of the transport used by the client.

        The two possible values returned by this function are ``'polling'``
        and ``'websocket'``.
        )r+   Ú	transportrD   r   r   r   rW   å   s   
zBaseClient.transportc                 C   sâ   d }|| j v r6|| j | v r| j | | }||fS || jvr2d| j | v r2| j | d }|g|¢R }||fS d| j v rm|| j d v rS| j d | }|g|¢R }||fS || jvrmd| j d v rm| j d d }||g|¢R }||fS ©NÚ*)r=   Úreserved_events)r@   rK   rL   rP   rI   r   r   r   Ú_get_event_handlerí   s(   

ô	
ø
üzBaseClient._get_event_handlerc                 C   sH   d }|| j v r| j | }||fS d| j v r | j d }|g|¢R }||fS rX   )r>   )r@   rL   rP   rI   r   r   r   Ú_get_namespace_handler  s   


ý
z!BaseClient._get_namespace_handlerc                 C   sJ   |pd}|| j vrdt d¡i| j |< t| j | d ƒ}|| j | |< |S )z/Generate a unique identifier for an ACK packet.rF   r   r   )r?   Ú	itertoolsÚcountÚnext)r@   rL   ÚcallbackÚidr   r   r   Ú_generate_ack_id  s   
zBaseClient._generate_ack_idc                 C   ó   t ƒ ‚rG   ©ÚNotImplementedErrorrD   r   r   r   r-     ó   zBaseClient._handle_eio_connectc                 C   rc   rG   rd   )r@   Údatar   r   r   r.   !  rf   zBaseClient._handle_eio_messagec                 C   rc   rG   rd   )r@   Úreasonr   r   r   r/   $  rf   z!BaseClient._handle_eio_disconnectc                 C   rc   rG   rd   rD   r   r   r   r*   '  rf   z!BaseClient._engineio_client_class)	Tr   r   r   r   Fr   NT)NNrG   )rN   Ú
__module__Ú__qualname__rZ   ÚengineioZClientrh   rC   rE   r,   rK   rT   rV   rW   r[   r\   rb   r-   r.   r/   r*   r   r   r   r   r   !   s*    
ýC
8#
	r   )r]   r4   r
   r   rk   r)   r   r   Ú	getLoggerr2   r   r   r	   r   r   r   r   r   Ú<module>   s    
