
    i,                    @   d dl mZ d dlZd dlZd dlZd dlmZmZmZm	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 dd
lmZ ddlm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$ g dZ% G d de"          Z&dddddddddedddddddd=d9Z'	 	 d>d?d<Z(dS )@    )annotationsN)AnyOptionalSequenceType   )ClientProtocol)HeadersLike)ClientExtensionFactory) enable_client_permessage_deflate)validate_subprotocols)
USER_AGENT)Response)
CONNECTINGOPENEvent)
LoggerLikeOriginSubprotocol)	parse_uri   )
Connection)Deadline)connectunix_connectClientConnectionc                  R     e Zd ZdZddd fdZdedfddZd fdZd fdZ xZ	S )r   a  
    Threaded implementation of a WebSocket client connection.

    :class:`ClientConnection` provides :meth:`recv` and :meth:`send` methods for
    receiving and sending messages.

    It supports iteration to receive messages::

        for message in websocket:
            process(message)

    The iterator exits normally when the connection is closed with close code
    1000 (OK) or 1001 (going away) or without a close code. It raises a
    :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
    closed with any other code.

    Args:
        socket: Socket connected to a WebSocket server.
        protocol: Sans-I/O connection.
        close_timeout: Timeout for closing the connection in seconds.

    
   close_timeoutsocketsocket.socketprotocolr	   r    Optional[float]returnNonec                   |  t          j                    | _        t                                          |||           d S )Nr   )	threadingr   response_rcvdsuper__init__)selfr!   r#   r    	__class__s       \/var/www/html/crypto-bot/backend/venv/lib/python3.11/site-packages/websockets/sync/client.pyr+   zClientConnection.__init__1   sO     	%&_..' 	 	
 	
 	
 	
 	
    Nadditional_headersOptional[HeadersLike]user_agent_headerOptional[str]timeoutc                j   |                      t                    5  | j                                        | _        || j        j                            |           ||| j        j        d<   | j                            | j                   ddd           n# 1 swxY w Y   | j        	                    |          s<| 
                                 | j                                         t          d          | j        <| 
                                 | j                                         t          d          | j        j        t"          urL| j                            | j                   | 
                                 | j                                         | j        j        | j        j        dS )z1
        Perform the opening handshake.

        )expected_stateNz
User-Agentztimed out during handshakez"connection closed during handshake)send_contextr   r#   r   requestheadersupdatesend_requestr)   waitclose_socketrecv_events_threadjoinTimeoutErrorresponseConnectionErrorstater   r    handshake_exc)r,   r0   r2   r4   s       r.   	handshakezClientConnection.handshake@   s    j99 	5 	5=0022DL!-$++,>??? ,5F$\2M&&t|444	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 !&&w// 	=#((***;<<<= #((***!"FGGG=d**#(();<<<#((***=&2--- 32s   A0BBBeventr   c                    | j         9t          |t                    sJ || _         | j                                         dS t                                          |           dS )z.
        Process one incoming event.

        N)rA   
isinstancer   r)   setr*   process_event)r,   rF   r-   s     r.   rJ   zClientConnection.process_eventd   se     = eX.....!DM""$$$$$ GG!!%(((((r/   c                    	 t                                                       | j                                         dS # | j                                         w xY w)zI
        Read incoming data from the socket and process events.

        N)r*   recv_eventsr)   rI   )r,   r-   s    r.   rL   zClientConnection.recv_eventsr   sV    
	%GG!!! ""$$$$$D""$$$$s	    > A)r!   r"   r#   r	   r    r$   r%   r&   )r0   r1   r2   r3   r4   r$   r%   r&   )rF   r   r%   r&   )r%   r&   )
__name__
__module____qualname____doc__r+   r   rE   rJ   rL   __classcell__)r-   s   @r.   r   r      s         8 *,
 
 
 
 
 
 
 
" 59+5#'	". ". ". ". ".H) ) ) ) ) )	% 	% 	% 	% 	% 	% 	% 	% 	% 	%r/   r   Fdeflater   i   )sockssl_contextserver_hostnameunixpathorigin
extensionssubprotocolsr0   r2   compressionopen_timeoutr    max_sizeloggercreate_connectionuristrrS   Optional[socket.socket]rT   Optional[ssl.SSLContext]rU   r3   rV   boolrW   rX   Optional[Origin]rY   *Optional[Sequence[ClientExtensionFactory]]rZ   Optional[Sequence[Subprotocol]]r0   r1   r2   r[   r\   r$   r    r]   Optional[int]r^   Optional[LoggerLike]r_    Optional[Type[ClientConnection]]r%   c          	        t          |           }|j        s|t          d          |r'||t          d          ||t          d          n|J |t          |           |dk    rt	          |          }n|t          d|           t          |          }|t          }	 ||rjt          j        t          j	        t          j
                  }|                    |                                           |J |                    |           n3t          j        |j        |j        f|                                          }|                    d           |s+|                    t          j        t          j        d           |j        rq|t)          j                    }||j        }|                    |                                           |                    ||          }|                    d           t/          ||||t0          ||	          } ||||
          }|                    |	|
|                                           n%# t4          $ r ||                                  w xY w|S )a)
  
    Connect to the WebSocket server at ``uri``.

    This function returns a :class:`ClientConnection` instance, which you can
    use to send and receive messages.

    :func:`connect` may be used as a context manager::

        async with websockets.sync.client.connect(...) as websocket:
            ...

    The connection is closed automatically when exiting the context.

    Args:
        uri: URI of the WebSocket server.
        sock: Preexisting TCP socket. ``sock`` overrides the host and port
            from ``uri``. You may call :func:`socket.create_connection` to
            create a suitable TCP socket.
        ssl_context: Configuration for enabling TLS on the connection.
        server_hostname: Host name for the TLS handshake. ``server_hostname``
            overrides the host name from ``uri``.
        origin: Value of the ``Origin`` header, for servers that require it.
        extensions: List of supported extensions, in order in which they
            should be negotiated and run.
        subprotocols: List of supported subprotocols, in order of decreasing
            preference.
        additional_headers (HeadersLike | None): Arbitrary HTTP headers to add
            to the handshake request.
        user_agent_header: Value of  the ``User-Agent`` request header.
            It defaults to ``"Python/x.y.z websockets/X.Y"``.
            Setting it to :obj:`None` removes the header.
        compression: The "permessage-deflate" extension is enabled by default.
            Set ``compression`` to :obj:`None` to disable it. See the
            :doc:`compression guide <../../topics/compression>` for details.
        open_timeout: Timeout for opening the connection in seconds.
            :obj:`None` disables the timeout.
        close_timeout: Timeout for closing the connection in seconds.
            :obj:`None` disables the timeout.
        max_size: Maximum size of incoming messages in bytes.
            :obj:`None` disables the limit.
        logger: Logger for this client.
            It defaults to ``logging.getLogger("websockets.client")``.
            See the :doc:`logging guide <../../topics/logging>` for details.
        create_connection: Factory for the :class:`ClientConnection` managing
            the connection. Set it to a wrapper or a subclass to customize
            connection handling.

    Raises:
        InvalidURI: If ``uri`` isn't a valid WebSocket URI.
        OSError: If the TCP connection fails.
        InvalidHandshake: If the opening handshake fails.
        TimeoutError: If the opening handshake times out.

    Nz5ssl_context argument is incompatible with a ws:// URIzmissing path argumentz(path and sock arguments are incompatiblerR   zunsupported compression: T)rU   )rX   rY   rZ   rC   r]   r^   r   )r   secure	TypeErrorr   r   
ValueErrorr   r   r!   AF_UNIXSOCK_STREAM
settimeoutr4   r   r_   hostport
setsockoptIPPROTO_TCPTCP_NODELAYsslcreate_default_contextwrap_socketr	   r   rE   	Exceptionclose)r`   rS   rT   rU   rV   rW   rX   rY   rZ   r0   r2   r[   r\   r    r]   r^   r_   wsurideadliner#   
connections                        r.   r   r   ~   s   f cNNE< QK3OPPP <DL3444$"2FGGG|||l+++i5jAA

		 B[BBCCC
 %%H ,= < 	}V^V5GHH 0 0 2 2333'''T""""/Z,$$&&  OOD!!!  	JOOF.0BDIII < 	""!8::&"'*OOH,,..///**4*QQDOOD!!! "!%
 
 
 '&'
 
 

 		
 	
 	
 	
    JJLLL
 s   )F-I "I9kwargsr   c                Z    ||                     d          d}nd}t          d|d| d|S )a  
    Connect to a WebSocket server listening on a Unix socket.

    This function is identical to :func:`connect`, except for the additional
    ``path`` argument. It's only available on Unix.

    It's mainly useful for debugging servers listening on Unix sockets.

    Args:
        path: File system path to the Unix socket.
        uri: URI of the WebSocket server. ``uri`` defaults to
            ``ws://localhost/`` or, when a ``ssl_context`` is provided, to
            ``wss://localhost/``.

    NrT   zws://localhost/zwss://localhost/T)r`   rV   rW    )getr   )rW   r`   r   s      r.   r   r   /  sD    ( {::m$$,#CC$C;sD;;F;;;r/   )$r`   ra   rS   rb   rT   rc   rU   r3   rV   rd   rW   r3   rX   re   rY   rf   rZ   rg   r0   r1   r2   r3   r[   r3   r\   r$   r    r$   r]   rh   r^   ri   r_   rj   r%   r   )NN)rW   r3   r`   r3   r   r   r%   r   ))
__future__r   r!   rw   r(   typingr   r   r   r   clientr	   datastructuresr
   extensions.baser   extensions.permessage_deflater   r9   r   httpr   http11r   r#   r   r   r   r   r   r   r`   r   r~   r   utilsr   __all__r   r   r   r   r/   r.   <module>r      s   " " " " " "  



     0 0 0 0 0 0 0 0 0 0 0 0 # # # # # # ( ( ( ( ( ( 4 4 4 4 4 4 L L L L L L + + + + + +             . . . . . . . . . . 4 4 4 4 4 4 4 4 4 4       " " " " " "       :
9
9b% b% b% b% b%z b% b% b%R %),0%)#=A4804'1!*$&%'##':>1n n n n n nd < < < < < < <r/   