
    wg                         d dl Z d dlZ	 d dlm Z d dlmZ ddlm	Z	  G d de	      Z
y# e$ r 	 d dlZd dlmZ n# e$ r dZdZY nw xY wY 4w xY w)    N)asyncio)
RedisError   )AsyncPubSubManagerc                   D     e Zd ZdZdZ	 	 d fd	Zd Zd Zd Zd Z	 xZ
S )	AsyncRedisManagera  Redis based client manager for asyncio servers.

    This class implements a Redis backend for event sharing across multiple
    processes.

    To use a Redis backend, initialize the :class:`AsyncServer` instance as
    follows::

        url = 'redis://hostname:port/0'
        server = socketio.AsyncServer(
            client_manager=socketio.AsyncRedisManager(url))

    :param url: The connection URL for the Redis server. For a default Redis
                store running on the same host, use ``redis://``.  To use an
                SSL connection, use ``rediss://``.
    :param channel: The channel name on which the server sends and receives
                    notifications. Must be the same in all the servers.
    :param write_only: If set to ``True``, only initialize to emit events. The
                       default of ``False`` initializes the class for emitting
                       and receiving.
    :param redis_options: additional keyword arguments to be passed to
                          ``aioredis.from_url()``.
    aioredisc                     t         t        d      t        t         j                  d      st        d      || _        |xs i | _        | j                          t        | !  |||       y )NzLRedis package is not installed (Run "pip install redis" in your virtualenv).from_urlz*Version 2 of aioredis package is required.)channel
write_onlylogger)	r	   RuntimeErrorhasattrRedis	redis_urlredis_options_redis_connectsuper__init__)selfurlr   r   r   r   	__class__s         a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/async_redis_manager.pyr   zAsyncRedisManager.__init__,   sl      O P Px~~z2KLL*0bZO    c                     t        j                  j                  | j                  fi | j                  | _        | j
                  j                  d      | _        y )NT)ignore_subscribe_messages)r	   r   r   r   r   redispubsub)r   s    r   r   z AsyncRedisManager._redis_connect8   sG    ^^,,T^^ C/3/A/AC
jj''$'Gr   c                 f  K   d}	 	 |s| j                          | j                  j                  | j                  t	        j
                  |             d {   S 7 # t        $ rH |r"| j                         j                  d       d}n!| j                         j                  d       Y y Y nw xY ww)NTz#Cannot publish to redis... retryingFz$Cannot publish to redis... giving up)	r   r   publishr   pickledumpsr   _get_loggererror)r   dataretrys      r   _publishzAsyncRedisManager._publish=   s     '')!ZZ//LL&,,t"46 6 6 6 $$&,, .8 9!E$$&,, .9 :	 	 s<   B1AA AA B1A AB-(B1,B--B1c                  K   d}d}	 	 |r?| j                          | j                  j                  | j                         d {    d}| j                  j	                         2 3 d {   }| 7 /7 6 ni# t
        $ r] | j                         j                  dj                  |             d}t        j                  |       d {  7   |dz  }|dkD  rd}Y nw xY wۭw)Nr   FTz0Cannot receive from redis... retrying in {} secs   <   )r   r   	subscriber   listenr   r$   r%   formatr   sleep)r   retry_sleepconnectmessages       r   _redis_listen_with_retriesz,AsyncRedisManager._redis_listen_with_retriesO   s     %'')++//==="#K%)[[%7%7%9 " "'!M >"%9 %  "(( *3396+3FH mmK000q #"$K% se   C#:A9 A3 A9 $A7(A5)A7,A9 5A77A9 8C#9AC
CCC#CC#c                t  K   | j                   j                  d      }| j                  j                  | j                          d {    | j	                         2 3 d {   }|d   |k(  s|d   dk(  sd|v s |d    )7 =7 &6 | j                  j                  | j                          d {  7   y w)Nzutf-8r   typer2   r&   )r   encoder   r,   r3   unsubscribe)r   r   r2   s      r   _listenzAsyncRedisManager._listend   s     ,,%%g.kk##DLL111!<<> 	& 	&'y!W,FOy0Vw5Ffo%	 	2	&> kk%%dll333sT   AB8BB8BB B#	B8-B86B8;
B8B)B81B42B8)zredis://localhost:6379/0socketioFNN)__name__
__module____qualname____doc__namer   r   r(   r3   r8   __classcell__)r   s   @r   r   r      s0    . D?I>B
PH
$%*4r   r   )r   r"   r   r	   redis.exceptionsr   ImportErroraioredis.exceptionsasync_pubsub_managerr   r    r   r   <module>rE      s_     	)+ 5Y4* Y4  2 
	s/   ( A
9A	AAAA
A