o
    Rh                     @   s   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZ dd	lmZ G d
d deZG dd deZejZejZG dd dZdS )zU
wsproto/connection
~~~~~~~~~~~~~~~~~~

An implementation of a WebSocket connection.
    )deque)Enum)Deque	GeneratorListOptional   )BytesMessageCloseConnectionEventMessagePingPongTextMessage)	Extension)CloseReasonFrameProtocolOpcodeParseFailed)LocalProtocolErrorc                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	ConnectionStatez1
    RFC 6455, Section 4 - Opening Handshake
    r   r               N)
__name__
__module____qualname____doc__
CONNECTINGOPENREMOTE_CLOSINGLOCAL_CLOSINGCLOSED	REJECTING r%   r%   T/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/wsproto/connection.pyr      s    r   c                   @   s   e Zd ZdZdZdZdS )ConnectionTypez#An enumeration of connection types.r   r   N)r   r   r   r   CLIENTSERVERr%   r%   r%   r&   r'   -   s    r'   c                	   @   s   e Zd ZdZ		ddedeee  deddfdd	Z	e
defd
dZdedefddZdee ddfddZdeeddf fddZdS )
Connectiona  
    A low-level WebSocket connection object.

    This wraps two other protocol objects, an HTTP/1.1 protocol object used
    to do the initial HTTP upgrade handshake and a WebSocket frame protocol
    object used to exchange messages and other control frames.

    :param conn_type: Whether this object is on the client- or server-side of
        a connection. To initialise as a client pass ``CLIENT`` otherwise
        pass ``SERVER``.
    :type conn_type: ``ConnectionType``
    N    connection_type
extensionstrailing_datareturnc                 C   s<   |t ju | _t | _t| j|pg | _tj| _	| 
| d S N)r'   r(   clientr   _eventsr   _protor   r    _statereceive_data)selfr,   r-   r.   r%   r%   r&   __init__I   s
   zConnection.__init__c                 C   s   | j S r0   )r4   )r6   r%   r%   r&   stateU   s   zConnection.stateeventc                 C   s   d}t |tr| jtjkr|| j|j|j7 }|S t |t	r0| jtjkr0|| j
|j7 }|S t |trF| jtjkrF|| j|j7 }|S t |trq| jtjtjhv rq|| j|j|j7 }| jtjkrktj| _|S tj| _|S td| d| j d)Nr+   zEvent z cannot be sent in state .)
isinstancer   r8   r   r    r3   	send_datadatamessage_finishedr   pingpayloadr   pongr
   r!   closecodereasonr#   r4   r"   r   )r6   r9   r=   r%   r%   r&   sendY   s.   zConnection.sendr=   c                 C   sd   |du r| j ttjd tj| _dS | jtj	tj
fv r%| j| dS | jtju r/td	 dS )aO  
        Pass some received data to the connection for handling.

        A list of events that the remote peer triggered by sending this data can
        be retrieved with :meth:`~wsproto.connection.Connection.events`.

        :param data: The data received from the remote peer on the network.
        :type data: ``bytes``
        N)rC   zConnection already closed.)r2   appendr
   r   ABNORMAL_CLOSUREr   r#   r4   r8   r    r"   r3   receive_bytesr   )r6   r=   r%   r%   r&   r5   p   s   zConnection.receive_datac              
   c   s   | j r| j  V  | j sz| j D ]}|jtju r5|jr!|js#J t	|j
ttfs-J t|j
dV  q|jtju rU|jrA|jsCJ t	|j
ttfsMJ t|j
dV  q|jtju rt	|j
tscJ |j
\}}| jtju rstj| _ntj| _t||dV  q|jtju rt	|j
tsJ t|j
|j|jdV  q|jtju rt	|j
ttfsJ t|j
|j|jdV  q	 qW dS  ty } zt|jt|dV  W Y d}~dS d}~ww )z
        Return a generator that provides any events that have been generated
        by protocol activity.

        :returns: generator of :class:`Event <wsproto.events.Event>` subclasses
        )r@   )rC   rD   )r=   frame_finishedr>   N) r2   popleftr3   received_framesopcoder   PINGrI   r>   r;   r@   bytes	bytearrayr   PONGr   CLOSEtupler8   r   r"   r#   r4   r!   r
   TEXTstrr   BINARYr	   r   rC   )r6   framerC   rD   excr%   r%   r&   events   sR   



&"zConnection.events)Nr+   )r   r   r   r   r'   r   r   r   rN   r7   propertyr   r8   r   rE   r5   r   rX   r%   r%   r%   r&   r*   ;   s$    

r*   N) r   collectionsr   enumr   typingr   r   r   r   rX   r	   r
   r   r   r   r   r   r-   r   frame_protocolr   r   r   r   	utilitiesr   r   r'   r(   r)   r*   r%   r%   r%   r&   <module>   s    $	
