o
    ¨Ýj  ã                   @  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__Z
CONNECTINGÚOPENÚREMOTE_CLOSINGÚLOCAL_CLOSINGÚCLOSEDZ	REJECTING© r"   r"   úM/var/www/html/chatgpt/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)r3   )r5   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
   r7   r   r   r2   Z	send_dataÚdataÚmessage_finishedr   ZpingÚpayloadr   Úpongr   r   ÚcloseÚcodeÚreasonr!   r3   r    r   )r5   r8   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)r@   zConnection already closed.)r1   Úappendr   r   ZABNORMAL_CLOSUREr   r!   r3   r7   r   r    r2   Zreceive_bytesr   )r5   r;   rB   r"   r"   r#   r4   {   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 rƒt	|j
tƒsgJ ‚|j
\}}| jtju rwtj| _ntj| _t||d�V  q|jtju r�t	|j
tƒs‘J ‚t|j
|j|jd�V  q|jtju r¹t	|j
ttfƒs­J ‚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@   rA   )r;   Úframe_finishedr<   N) r1   Úpopleftr2   Zreceived_framesÚopcoder   ZPINGrG   r<   r:   r=   r-   Ú	bytearrayr   ZPONGr   ZCLOSEÚtupler7   r   r    r!   r3   r   r   ZTEXTÚstrr   ZBINARYr   r   r@   )r5   Úframer@   rA   Úexcr"   r"   r#   Úevents–   sV   €ÿ






ý
ýÙ("€ÿzConnection.events)Nr(   )r)   r$   r*   r+   r,   r-   r.   r/   )r.   r   )r8   r	   r.   r-   )r;   rD   r.   r/   )r.   rF   )
r   r   r   r   r6   Úpropertyr7   rC   r4   rO   r"   r"   r"   r#   r'   @   s    ü

r'   N)!r   Ú
__future__r   Úcollectionsr   Úenumr   Útypingr   rO   r   r   r	   r
   r   r   r   Zframe_protocolr   r   r   r   Z	utilitiesr   Úcollections.abcr   r*   r   r   r$   r%   r&   r'   r"   r"   r"   r#   Ú<module>   s     $	
