U
    g                     @   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#   6/tmp/pip-unpacked-wheel-u83us8q9/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eeee  eddddZ	e
eddd	Zeed
ddZee ddddZeeddf 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.

    :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k| _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__)r-   c                 C   s   | j S r.   )r2   )r4   r#   r#   r$   stateU   s    zConnection.state)eventr-   c                 C   s   d}t |tr2| jtjkr2|| j|j|j7 }nt |t	r\| jtjkr\|| j
|j7 }nt |tr| jtjkr|| j|j7 }njt |tr| jtjtjhkr|| j|j|j7 }| jtjkrtj| _qtj| _ntd| d| j d|S )Nr)   zEvent z cannot be sent in state .)
isinstancer   r6   r   r   r1   Z	send_datadatamessage_finishedr   Zpingpayloadr   Zpongr
   r    closecodereasonr"   r2   r!   r   )r4   r7   r:   r#   r#   r$   sendY   s&    

zConnection.send)r:   r-   c                 C   sb   |dkr(| j ttjd tj| _dS | jtj	tj
fkrH| j| n| jtjkr^tdn 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  q z\| j D ]J}|jtjkrh|jr@|jsDt	t
|jttfsXt	t|jdV  q"|jtjkr|jr|jst	t
|jttfst	t|jdV  q"|jtjkrt
|jtst	|j\}}| jtjkrtj| _ntj| _t||dV  q"|jtjkr4t
|jtst	t|j|j|jdV  q"|jtjkr"t
|jttfsVt	t|j|j|jdV  q"q"W n8 tk
r } zt|j t|dV  W 5 d}~X Y nX dS )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PINGrB   r;   AssertionErrorr9   r<   bytes	bytearrayr   ZPONGr   ZCLOSEtupler6   r   r!   r"   r2   r    r
   ZTEXTstrr   ZBINARYr	   r   r>   )r4   framer>   r?   excr#   r#   r$   events   sH    



zConnection.events)Nr)   )r   r   r   r   r%   r   r   r   rF   r5   propertyr   r6   r   r@   r3   r   rL   r#   r#   r#   r$   r(   ;   s     
r(   N) r   collectionsr   enumr   typingr   r   r   r   rL   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   $	
