
    aDg                         d 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 ddlmZmZmZmZ ddlmZ  G d d	e      Z G d
 de      Zej*                  Zej,                  Z G d de      Zy)zU
wsproto/connection
~~~~~~~~~~~~~~~~~~

An implementation of a WebSocket connection.
    )deque)Enum   )BytesMessageCloseConnectionMessagePingPongTextMessage)CloseReasonFrameProtocolOpcodeParseFailed)LocalProtocolErrorc                   (    e Zd ZdZdZdZdZdZdZdZ	y)	ConnectionStatez1
    RFC 6455, Section 4 - Opening Handshake
    r   r               N)
__name__
__module____qualname____doc__
CONNECTINGOPENREMOTE_CLOSINGLOCAL_CLOSINGCLOSED	REJECTING     R/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/wsproto/connection.pyr   r      s'     JDNMFIr"   r   c                       e Zd ZdZdZy)ConnectionTyper   r   N)r   r   r   CLIENTSERVERr!   r"   r#   r%   r%      s    F Fr"   r%   c                   :    e Zd ZdZddZed        Zd Zd Zd Z	y)	
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``
    Nc                     |t         j                  u | _        t               | _        t        | j                  |xs g       | _        t        j                  | _	        | j                  |       y N)r%   r&   clientr   _eventsr   _protor   r   _statereceive_data)selfconnection_type
extensionstrailing_datas       r#   __init__zConnection.__init__8   sO    %)>)>>w#DKK1ArB%**-(r"   c                     | j                   S r+   )r/   )r1   s    r#   statezConnection.state@   s     {{r"   c                 P   d}t        |t              r5|| j                  j                  |j                  |j
                        z  }|S t        |t              r*|| j                  j                  |j                        z  }|S t        |t              r*|| j                  j                  |j                        z  }|S t        |t              r| j                  t        j                  t        j                  hvrt!        d| j                  z        || j                  j#                  |j$                  |j&                        z  }| j                  t        j                  k(  rt        j(                  | _        |S t        j,                  | _        |S t!        dj/                  |            )Nr"   z'Connection cannot be closed in state %szEvent {} cannot be sent.)
isinstancer   r.   	send_datadatamessage_finishedr	   pingpayloadr
   pongr   r7   r   r   r   r   closecodereasonr   r/   r   format)r1   eventr;   s      r#   sendzConnection.sendE   sY   eW%DKK))%**e6L6LMMD" ! t$DKK$$U]]33D  t$DKK$$U]]33D  /zz/"6"68V8V!WW(=

J  DKK%%ejj%,,??Dzz_;;;-44
  .;;  %%?%F%Fu%MNNr"   c                 x   |I| j                   j                  t        t        j                               t
        j                  | _        y| j                  t
        j                  t
        j                  fv r| j                  j                  |       y| j                  t
        j                  u rt        d      y)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)rA   zConnection already closed.)r-   appendr   r   ABNORMAL_CLOSUREr   r   r/   r7   r   r   r.   receive_bytesr   )r1   r;   s     r#   r0   zConnection.receive_data\   s     <
 LL[5Q5Q RS)00DK::/..0M0MNNKK%%d+ZZ?111$%ABB 2r"   c              #     K   | j                   r)| j                   j                          | j                   r)	 | j                  j                         D ]  }|j                  t
        j                  u r3|j                  r|j                  sJ t        |j                         S|j                  t
        j                  u r3|j                  r|j                  sJ t        |j                         |j                  t
        j                  u rg|j                  \  }}| j                  t        j                   u rt        j"                  | _        nt        j&                  | _        t)        ||       %|j                  t
        j*                  u r0t-        |j                  |j                  |j                         q|j                  t
        j.                  u st1        |j                  |j                  |j                          y# t2        $ r,}t)        |j4                  t7        |             Y d}~yd}~ww xY 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>   )rA   rB   )r;   frame_finishedr<   N)r-   popleftr.   received_framesopcoder   PINGrK   r<   r	   r>   PONGr
   CLOSEr7   r   r   r   r/   r   r   TEXTr   BINARYr   r   rA   str)r1   framerA   rB   excs        r#   eventszConnection.eventsv   s     ll,,&&(( ll 	B446 <<6;;. //E4J4JJJu}}55\\V[[0 //E4J4JJJu}}55\\V\\1#(==LD&zz_%B%BB&5&<&<&5&D&D)tFCC\\V[[0%"]]',';';).)?)?  \\V]]2&"]]',';';).)?)? 3<  	B!sxxCAA	Bs5   5IF'H "1H I	I	"I?II		I)Nr"   )
r   r   r   r   r5   propertyr7   rE   r0   rW   r!   r"   r#   r)   r)   *   s1    )  .C4+Br"   r)   N)r   collectionsr   enumr   rW   r   r   r   r	   r
   r   frame_protocolr   r   r   r   	utilitiesr   r   r%   r&   r'   objectr)   r!   r"   r#   <module>r^      sa      S S K K )
d 
T  
					wB wBr"   