o
    ]i#                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZ ddlmZmZmZmZ dd	lmZ erLdd
l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.
    )annotations)deque)Enum)TYPE_CHECKING   )BytesMessageCloseConnectionEventMessagePingPongTextMessage)CloseReasonFrameProtocolOpcodeParseFailed)LocalProtocolError)	Generator)	Extensionc                   @  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$   W/var/www/html/stock_analysis/be/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&   2   s    r&   c                   @  sL   e Zd ZdZ		ddddZedddZdddZd ddZd!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.
    N    connection_typer&   
extensionslist[Extension] | Nonetrailing_databytesreturnNonec                 C  s<   |t ju | _t | _t| j|pg | _tj| _	| 
| dS )a  
        Constructor

        :param wsproto.connection.ConnectionType connection_type: Whether this
            object is on the client- or server-side of a connection.
            To initialise as a client pass ``CLIENT`` otherwise pass ``SERVER``.
        :param list extensions: The proposed extensions.
        :param bytes trailing_data: Data that has been received, but not yet
            processed.
        N)r&   r'   clientr   _eventsr   _protor   r   _statereceive_data)selfr+   r,   r.   r$   r$   r%   __init__I   s
   zConnection.__init__r   c                 C  s   | j S )N)r5   )r7   r$   r$   r%   state_   s   zConnection.stateeventr	   c                 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 d| d| j d}t|)Nr*   zEvent z cannot be sent in state .)
isinstancer
   r9   r   r   r4   	send_datadatamessage_finishedr   pingpayloadr   pongr   r    closecodereasonr"   r5   r!   r   )r7   r:   r>   msgr$   r$   r%   sendc   s0   zConnection.sendr>   bytes | Nonec                 C  sh   |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 r1d}t|	 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)rD   zConnection already closed.)r3   appendr   r   ABNORMAL_CLOSUREr   r"   r5   r9   r   r!   r4   receive_bytesr   )r7   r>   rF   r$   r$   r%   r6   {   s   
zConnection.receive_dataGenerator[Event, None, None]c              
   c  s   | j r| j  V  | j sz| j D ]}|jtju r7|js J |js%J t	|j
ttfs/J t|j
dV  q|jtju rY|jsBJ |jsGJ t	|j
ttfsQJ t|j
dV  q|jtju rt	|j
tsgJ |j
\}}| jtju rwt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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
        )rA   )rD   rE   )r>   frame_finishedr?   N) r3   popleftr4   received_framesopcoder   PINGrM   r?   r<   rA   r/   	bytearrayr   PONGr   CLOSEtupler9   r   r!   r"   r5   r    r   TEXTstrr   BINARYr   r   rD   )r7   framerD   rE   excr$   r$   r%   events   sV   







("zConnection.events)Nr*   )r+   r&   r,   r-   r.   r/   r0   r1   )r0   r   )r:   r	   r0   r/   )r>   rH   r0   r1   )r0   rL   )
r   r   r   r   r8   propertyr9   rG   r6   r[   r$   r$   r$   r%   r)   @   s    

r)   N)!r   
__future__r   collectionsr   enumr   typingr   r[   r   r   r	   r
   r   r   r   frame_protocolr   r   r   r   	utilitiesr   collections.abcr   r,   r   r   r&   r'   r(   r)   r$   r$   r$   r%   <module>   s     $	
