
    i)                     t    d dl Z d dlZddlmZ ddlmZ ddlmZ  e j        d          Z G d d          ZdS )	    N   )manager)base_namespace)packetzsocketio.serverc                       e Zd ZddgZej        j        Z	 	 	 ddZd Zdd	Z	d
 Z
d ZddZddZddZd Zd Zd Zd Zd Zd ZdS )
BaseServerconnect
disconnectNFdefaultTc                 ~   |}	|	                     dd           }
|
|
|	d<   |dk    rt          j        | _        n |dk    rddlm} |j        | _        n|| _        ||| j        _        ||	d<   d|	d	<    |                                 di |	| _	        | j	        
                    d
| j                   | j	        
                    d| j                   | j	        
                    d| j                   i | _        i | _        i | _        t#                      | _        i | _        t)          |t*                    s|| _        nt.          | _        | j        j        t2          j        k    rv|r%| j                            t2          j                   n$| j                            t2          j                   | j                            t3          j                               |tA          j!                    }|| _         | j         "                    |            d| _#        || _$        || _%        |pdg| _&        | j	        j'        | _'        d S )Nengineio_loggerloggerr   msgpackr   )msgpack_packetjsonFasync_handlersr	   messager
   / )(popr   Packetpacket_class r   MsgPackPacketr   _engineio_server_classeioon_handle_eio_connect_handle_eio_message_handle_eio_disconnectenvironhandlersnamespace_handlersobjectnot_handled_binary_packet
isinstanceboolr   default_loggerlevelloggingNOTSETsetLevelINFOERROR
addHandlerStreamHandlerr   Manager
set_servermanager_initializedr   always_connect
namespaces
async_mode)selfclient_managerr   
serializerr   r   r5   r6   kwargsengineio_optionsr   r   s               ?/usr/local/lib/python3.11/dist-packages/socketio/base_server.py__init__zBaseServer.__init__   s2    "*../@$GG&)8X&"" &D9$$(((((( . <D *D%)D"'+V$-2)*04..00DD3CDDIt7888It7888L$"=>>>"$!88 &$'' 		@ DKK(DK{ GN22 8K((6666K((777&&w'<'>'>???!$_..N%%%%#( ,,$-(-    c                     dS )NFr   r8   s    r=   is_asyncio_basedzBaseServer.is_asyncio_basedE   s    ur?   c                 @     pd fd}||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', namespace='/chat')
            def connect_handler(sid, environ):
                print('Connection request')
                if environ['REMOTE_ADDR'] in blacklisted:
                    return False  # reject

            # as a method:
            def message_handler(sid, msg):
                print('Received message: ', msg)
                sio.send(sid, 'response')
            socket_io.on('message', namespace='/chat', handler=message_handler)

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

        - The ``'connect'`` event handler receives the ``sid`` (session ID) for
          the client and the WSGI environment dictionary as arguments.
        - The ``'disconnect'`` handler receives the ``sid`` for the client as
          only argument.
        - The ``'message'`` handler and handlers for custom event names receive
          the ``sid`` for the client and the message payload as arguments. 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.
        r   c                 N    j         vr
i j         <   | j                  <   | S N)r"   )handlerevent	namespacer8   s    r=   set_handlerz"BaseServer.on.<locals>.set_handlerz   s2    --+-i(.5DM)$U+Nr?   Nr   )r8   rG   rF   rH   rI   s   `` ` r=   r   zBaseServer.onH   sX    ` $		 	 	 	 	 	 	 ?Gr?   c                      t                    dk    rWt                    dk    rDt          d                   r/                      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                 B      j         | j        gR i |           S rE   )r   __name__)rF   argsr;   r8   s    r=   rI   z%BaseServer.event.<locals>.set_handler   s2    Awtww/A$AAA&AA'JJJr?   )lencallabler   rL   )r8   rM   r;   rI   s   ``` r=   rG   zBaseServer.event   s    0 t99>>c&kkQ..8DG3D3D. -47747+,,T!W555K K K K K K K r?   c                    t          |t          j                  st          d          |                                 |                                k    rt          d          |                    |            || j        |j        <   dS )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 serverN)r'   r   BaseServerNamespace
ValueErrorrB   _set_serverr#   rH   )r8   namespace_handlers     r=   register_namespacezBaseServer.register_namespace   s     +(<> > 	97888  ""&7&H&H&J&JJJJKKK%%d+++ 	 1 ;<<<r?   c                 @    |pd}| j                             ||          S )zReturn the rooms a client is in.

        :param sid: Session ID of the client.
        :param namespace: The Socket.IO namespace for the event. If this
                          argument is omitted the default namespace is used.
        r   )r   	get_rooms)r8   sidrH   s      r=   roomszBaseServer.rooms   s&     $	|%%c9555r?   c                 p    | j                             ||pd          }| j                            |          S )a_  Return the name of the transport used by the client.

        The two possible values returned by this function are ``'polling'``
        and ``'websocket'``.

        :param sid: The session of the client.
        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the default namespace is used.
        r   )r   eio_sid_from_sidr   	transportr8   rX   rH   eio_sids       r=   r\   zBaseServer.transport   s6     ,//Y5E#FFx!!'***r?   c                 p    | j                             ||pd          }| j                            |          S )zReturn the WSGI environ dictionary for a client.

        :param sid: The session of the client.
        :param namespace: The Socket.IO namespace. If this argument is omitted
                          the default namespace is used.
        r   )r   r[   r!   getr]   s       r=   get_environzBaseServer.get_environ   s6     ,//Y5E#FF|(((r?   c                    d }|| j         v rT|| j         |         v r| j         |         |         }n1|| j        vr(d| j         |         v r| j         |         d         }|g|R }|dd| j         v r[|| j         d         v r| j         d         |         }|g|R }n2|| j        vr)d| j         d         v r| j         d         d         }||g|R }||fS N*)r"   reserved_events)r8   rG   rH   rM   rF   s        r=   _get_event_handlerzBaseServer._get_event_handler   s    %%i000-	259d2224=333-	237~~~?sdm33c***-,U3!)D))d2224=----,S1y0400}r?   c                 v    d }|| j         v r| j         |         }|d| j         v r| j         d         }|g|R }||fS rc   )r#   )r8   rH   rM   rF   s       r=   _get_namespace_handlerz!BaseServer._get_namespace_handler   s]     ///-i8G?sd&===-c2G%%%D}r?   c                     t                      rE   NotImplementedErrorrA   s    r=   r   zBaseServer._handle_eio_connect       !###r?   c                     t                      rE   rj   )r8   datas     r=   r   zBaseServer._handle_eio_message  rl   r?   c                     t                      rE   rj   rA   s    r=   r    z!BaseServer._handle_eio_disconnect  rl   r?   c                      t          d          )Nz!Must be implemented in subclassesrj   rA   s    r=   r   z!BaseServer._engineio_server_class	  s    !"EFFFr?   )NFr   NTFN)NNrE   )rL   
__module____qualname__re   engineioServerreasonr>   rB   r   rG   rU   rY   r\   ra   rf   rh   r   r   r    r   r   r?   r=   r   r      s$        ,/O_#FEN@E 3. 3. 3. 3.j  : : : :x! ! !F   6 6 6 6+ + + +) ) ) )  4  $ $ $$ $ $$ $ $G G G G Gr?   r   )	r+   rs   r   r   r   r   	getLoggerr)   r   r   r?   r=   <module>rw      s                       ""#455~G ~G ~G ~G ~G ~G ~G ~G ~G ~Gr?   