U
    œËúgG  ã                   @   sj   d Z ddlmZmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ dZG d	d
„ d
ƒZdZdS )z.
wsproto
~~~~~~~

A WebSocket implementation.
é    )Ú	GeneratorÚOptionalÚUnioné   )Ú
ConnectionÚConnectionStateÚConnectionType)ÚEvent)ÚH11Handshake)ÚHeadersz1.2.0c                   @   sˆ   e Zd ZdZeddœdd„Zeedœdd„ƒZe	e
eef d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 )ÚWSConnectionzN
    Represents the local end of a WebSocket connection to a remote peer.
    N)Úconnection_typeÚreturnc                 C   s    |t jk| _t|ƒ| _d| _dS )z­
        Constructor

        :param wsproto.connection.ConnectionType connection_type: Controls
            whether the library behaves as a client or as a server.
        N)r   ZCLIENTÚclientr
   Ú	handshakeÚ
connection)Úselfr   © r   ú4/tmp/pip-unpacked-wheel-u83us8q9/wsproto/__init__.pyÚ__init__   s    
zWSConnection.__init__)r   c                 C   s   | j dkr| jjS | j jS )z_
        :returns: Connection state
        :rtype: wsproto.connection.ConnectionState
        N)r   r   Ústate©r   r   r   r   r   !   s    
zWSConnection.state)ÚheadersÚpathr   c                 C   s   | j  ||¡ d S )N)r   Úinitiate_upgrade_connection)r   r   r   r   r   r   r   +   s    z(WSConnection.initiate_upgrade_connection)Úeventr   c                 C   s>   d}| j dkr*|| j |¡7 }| jj | _ n|| j  |¡7 }|S )a‹  
        Generate network data for the specified event.

        When you want to communicate with a WebSocket peer, you should construct
        an event and pass it to this method. This method will return the bytes
        that you should send to the peer.

        :param wsproto.events.Event event: The event to generate data for
        :returns bytes: The data to send to the peer
        ó    N)r   r   Úsend)r   r   Údatar   r   r   r   0   s    
zWSConnection.send)r   r   c                 C   s2   | j dkr"| j |¡ | jj | _ n| j  |¡ dS )zý
        Feed network data into the connection instance.

        After calling this method, you should call :meth:`events` to see if the
        received data triggered any new events.

        :param bytes data: Data received from remote peer
        N)r   r   Úreceive_data)r   r   r   r   r   r   C   s    	
zWSConnection.receive_datac                 c   s.   | j  ¡ E dH  | jdk	r*| j ¡ E dH  dS )z”
        A generator that yields pending events.

        Each event is an instance of a subclass of
        :class:`wsproto.events.Event`.
        N)r   Úeventsr   r   r   r   r   r    R   s    
zWSConnection.events)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   r   r   r   ÚbytesÚstrr   r	   r   r   r   r   r    r   r   r   r   r      s   
 
þr   )r   r   N)r$   Útypingr   r   r   r   r   r   r   r    r	   r   r
   r   Ú__version__r   Ú__all__r   r   r   r   Ú<module>   s   M