
    i                       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rdd
l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          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                  *    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     =/usr/local/lib/python3.11/dist-packages/wsproto/connection.pyr   r      s8         
 JDNMFIIIr'   r   c                      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"        -- F FFFr'   r*   c                  P    e Zd ZdZ	 	 dddZedd            ZddZddZ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.
    Nr'   connection_typer*   
extensionslist[Extension] | Nonetrailing_databytesreturnNonec                    |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/   r0   r2   s       r(   __init__zConnection.__init__I   sZ      &)>>%*WW#DK1ArBB%*-(((((r'   r   c                    | j         S )N)r:   )r<   s    r(   statezConnection.state_   s
    {r'   eventr
   c                J   d}t          |t                    r?| j        t          j        k    r*|| j                            |j        |j                  z  }nLt          |t                    r8| j        t          j        k    r#|| j        
                    |j                  z  }nt          |t                    r8| j        t          j        k    r#|| j                            |j                  z  }nt          |t                    r| j        t          j        t          j        hv ra|| j                            |j        |j                  z  }| j        t          j        k    rt          j        | _        n/t          j        | _        nd| d| j         d}t-          |          |S )Nr'   zEvent z cannot be sent in state .)
isinstancer   r?   r   r!   r9   	send_datadatamessage_finishedr   pingpayloadr   pongr	   r"   closecodereasonr$   r:   r#   r   )r<   r@   rE   msgs       r(   sendzConnection.sendc   s{   eW%% 	$*8L*L*LDK))%*e6LMMMDDt$$ 	7K)K)KDK$$U]333DDt$$ 	7K)K)KDK$$U]333DD// 	DJ *C
 5
 5
 DK%%ej%,???Dz_;;;-4-;H5HH4:HHHC$   r'   rE   bytes | Nonec                R   |E| j                             t          t          j                             t
          j        | _        dS | j        t
          j	        t
          j
        fv r| j                            |           dS | j        t
          j        u rd}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)rK   zConnection already closed.)r8   appendr	   r   ABNORMAL_CLOSUREr   r$   r:   r?   r!   r#   r9   receive_bytesr   )r<   rE   rM   s      r(   r;   zConnection.receive_data{   s     <
 L[5Q R R RSSS)0DKF:/.0MNNNK%%d+++++Z?111.C$S)))Dr'   Generator[Event, None, None]c              #    K   | j         r"| j                                         V  | j         "	 | j                                        D ]}|j        t
          j        u rM|j        sJ |j        sJ t          |j
        t          t          f          sJ t          |j
                  V  c|j        t
          j        u rM|j        sJ |j        sJ t          |j
        t          t          f          sJ t          |j
                  V  |j        t
          j        u rqt          |j
        t"                    sJ |j
        \  }}| j        t&          j        u rt&          j        | _        nt&          j        | _        t1          ||          V  G|j        t
          j        u rAt          |j
        t4                    sJ t7          |j
        |j        |j                  V  |j        t
          j        u rHt          |j
        t          t          f          sJ t;          |j
        |j        |j                  V  dS # t<          $ r0}t1          |j        t5          |                    V  Y d}~dS d}~ww xY 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
        )rH   )rK   rL   )rE   frame_finishedrF   N) r8   popleftr9   received_framesopcoder   PINGrV   rF   rC   rH   r3   	bytearrayr   PONGr   CLOSEtupler?   r   r#   r$   r:   r"   r	   TEXTstrr   BINARYr   r   rK   )r<   framerK   rL   excs        r(   eventszConnection.events   s      l 	),&&((((( l 	)*	B4466 ' '<6;.. //// 1111%emeY5GHHHHHu}5555555\V[00 //// 1111%emeY5GHHHHHu}5555555\V\11%emU;;;;;#(=LD&z_%BBB&5&<&5&D)tFCCCCCCC\V[00%emS99999%"]',';).)?       \V]22%emeY5GHHHHH&"]',';).)?       O' 'P  	B 	B 	B!sxCAAAAAAAAAAAA	Bs   HI   
I:
%I55I:)Nr'   )r/   r*   r0   r1   r2   r3   r4   r5   )r4   r   )r@   r
   r4   r3   )rE   rO   r4   r5   )r4   rT   )
r   r   r   r   r=   propertyr?   rN   r;   rd   r&   r'   r(   r.   r.   @   s          .2"	) ) ) ) ),    X   0   64B 4B 4B 4B 4B 4Br'   r.   N)!r   
__future__r   collectionsr   enumr   typingr   rd   r   r	   r
   r   r   r   r   frame_protocolr   r   r   r   	utilitiesr   collections.abcr   r0   r   r   r*   r+   r,   r.   r&   r'   r(   <module>rm      s    # " " " " "                                          L K K K K K K K K K K K ) ) ) ) ) ) &))))))%%%%%%    d   &    T    
			JB JB JB JB JB JB JB JB JB JBr'   