
    wg)                     p    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y)	    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y)
BaseServerconnect
disconnectNc                    |}	|	j                  dd       }
|
|
|	d<   |dk(  rt        j                  | _        n$|dk(  rddlm} |j                  | _        n|| _        ||| j                  _        ||	d<   d|	d	<    | j                         di |	| _	        | j                  j                  d
| j                         | j                  j                  d| j                         | j                  j                  d| j                         i | _        i | _        i | _        t#               | _        i | _        t)        |t*              s|| _        nt.        | _        | j,                  j0                  t2        j4                  k(  r|r*| j,                  j7                  t2        j8                         n)| j,                  j7                  t2        j:                         | j,                  j=                  t3        j>                                |tA        jB                         }|| _         | j@                  jE                  |        d| _#        || _$        || _%        |xs dg| _&        | j                  jN                  | _'        y )Nengineio_loggerloggerdefault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               Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/base_server.py__init__zBaseServer.__init__   s    "*../@$G&)8X&" &D9$( . < <D *D%)D"'+V$-2)*04..0D3CDIt778It778L$"="=>"$!8 &$' DK(DK{{  GNN2KK((6KK((7&&w'<'<'>?!$__.N%%#( ,,$-((--    c                      y)NFr   r8   s    r=   is_asyncio_basedzBaseServer.is_asyncio_basedE   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', 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                 f    j                   vri j                   <   | j                      <   | S N)r"   )handlerevent	namespacer8   s    r=   set_handlerz"BaseServer.on.<locals>.set_handlerz   s5    -+-i(.5DMM)$U+Nr?   Nr   )r8   rG   rF   rH   rI   s   `` ` r=   r   zBaseServer.onH   s*    ` $		 ?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 rE   )r   __name__)rF   argsr;   r8   s    r=   rI   z%BaseServer.event.<locals>.set_handler   s+    Awtwww//A$A&A'JJr?   )lencallabler   rL   )r8   rM   r;   rI   s   ``` r=   rG   zBaseServer.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 serverN)r'   r   BaseServerNamespace
ValueErrorrB   _set_serverr#   rH   )r8   namespace_handlers     r=   register_namespacezBaseServer.register_namespace   ss     +(<<>788  "&7&H&H&JJJKK%%d+ 	 1 ; ;<r?   c                 F    |xs d}| j                   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5r?   c                 x    | j                   j                  ||xs d      }| j                  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   s4     ,,//Y5E#Fxx!!'**r?   c                 x    | j                   j                  ||xs d      }| j                  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   s4     ,,//Y5E#F||((r?   c                    d }|| j                   v r[|| j                   |   v r| j                   |   |   }n7|| j                  vr)d| j                   |   v r| j                   |   d   }|g|}|sd| 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*)r"   reserved_events)r8   rG   rH   rM   rF   s        r=   _get_event_handlerzBaseServer._get_event_handler   s    %i00--	259d2224==33--	237~~?sdmm3c**--,U3!)D)
 }	 d2224==----,S1y040}r?   c                     d }|| j                   v r| j                   |   }|#d| j                   v r| j                   d   }|g|}||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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      sn     ,/O__##FEN@E 3.j:x!F 6+)4$$$Gr?   r   )	r+   rs   r   r   r   r   	getLoggerr)   r   r   r?   r=   <module>rw      s5        """#45~G ~Gr?   