o
    US`c                     @   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__Z
CONNECTINGOPENREMOTE_CLOSINGLOCAL_CLOSINGCLOSEDZ	REJECTING r#   r#   =/usr/local/lib/python3.10/dist-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 r.   )r2   )r4   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   r6   r   r   r1   Z	send_datadatamessage_finishedr   Zpingpayloadr   pongr
   r    closecodereasonr"   r2   r!   r   )r4   r7   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)r?   zConnection already closed.)r0   appendr
   r   ZABNORMAL_CLOSUREr   r"   r2   r6   r   r!   r1   Zreceive_bytesr   )r4   r:   r#   r#   r$   r3   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<   )r?   r@   )r:   frame_finishedr;   N) r0   popleftr1   Zreceived_framesopcoder   ZPINGrC   r;   r9   r<   bytes	bytearrayr   ZPONGr   ZCLOSEtupler6   r   r!   r"   r2   r    r
   ZTEXTstrr   BINARYr	   r   r?   )r4   framer?   r@   excr#   r#   r$   events   sR   



&"zConnection.events)Nr)   )r   r   r   r   r%   r   r   r   rF   r5   propertyr   r6   r   rA   r3   r   rM   r#   r#   r#   r$   r(   ;   s$    

r(   N) r   collectionsr   enumr   typingr   r   r   r   rM   r	   r
   r   r   r   r   r   r+   r   Zframe_protocolr   r   r   r   Z	utilitiesr   r   r%   r&   r'   r(   r#   r#   r#   r$   <module>   s    $	
