
    DQcj8                         d Z 	 ddlZddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlT ddlmZ dgZ G d	 d
          Z G d d          Z G d de          ZdS )z

    N   )ABNF)	WebSocketgetdefaulttimeout)*)_loggingWebSocketAppc                       e Zd ZdZd Zd ZdS )
Dispatcherz
    Dispatcher
    c                 "    || _         || _        d S Nappping_timeoutselfr   r   s      .lib/python3.11/site-packages/websocket/_app.py__init__zDispatcher.__init__.       (    c                     | j         j        rVt          j        | j         j        j        fdd| j                  \  }}}|r |            sd S  |             | j         j        Td S d S N )r   keep_runningselectsockr   )r   r   read_callbackcheck_callbackrwes          r   readzDispatcher.read2   s    h# 	mX]'*BD4EG GGAq! $} EN h# 	 	 	 	 	r   N)__name__
__module____qualname____doc__r   r"   r   r   r   r   r   *   s<         ) ) )    r   r   c                   $    e Zd ZdZd Zd Zd ZdS )SSLDispatcherz
    SSLDispatcher
    c                 "    || _         || _        d S r   r   r   s      r   r   zSSLDispatcher.__init__?   r   r   c                     | j         j        r:|                                 }|r |            sd S  |             | j         j        8d S d S r   )r   r   r   )r   r   r   r   r   s        r   r"   zSSLDispatcher.readC   sh    h# 	A $} EN h# 	 	 	 	 	r   c                     | j         j        j        }|                                r|gS t          j        |fdd| j                  \  }}}|S r   )r   r   pendingr   r   )r   r   r   r    r!   s        r   r   zSSLDispatcher.selectK   sJ    x}!<<>> 	7N-"b$2CDD1ar   N)r#   r$   r%   r&   r   r"   r   r   r   r   r(   r(   ;   sK         ) ) )      r   r(   c                   p    e Zd ZdZ	 	 	 	 	 	 	 ddZej        fdZd Zd Z		 	 	 	 	 	 	 dd
Z
d Zd Zd ZdS )r	   z_
    Higher level of APIs are provided. The interface is like JavaScript WebSocket object.
    NTc                     || _         ||ng | _        || _        || _        || _        || _        || _        || _        || _        || _	        |	| _
        d| _        || _        d| _        d| _        d| _        || _        dS )a4	  
        WebSocketApp initialization

        Parameters
        ----------
        url: <type>
            websocket url.
        header: list or dict
            custom header for websocket handshake.
        on_open: <type>
            callable object which is called at opening websocket.
            this function has one argument. The argument is this class object.
        on_message: <type>
            callable object which is called when received data.
            on_message has 2 arguments.
            The 1st argument is this class object.
            The 2nd argument is utf-8 string which we get from the server.
        on_error: <type>
            callable object which is called when we get error.
            on_error has 2 arguments.
            The 1st argument is this class object.
            The 2nd argument is exception object.
        on_close: <type>
            callable object which is called when closed the connection.
            this function has one argument. The argument is this class object.
        on_cont_message: <type>
            callback object which is called when receive continued
            frame data.
            on_cont_message has 3 arguments.
            The 1st argument is this class object.
            The 2nd argument is utf-8 string which we get from the server.
            The 3rd argument is continue flag. if 0, the data continue
            to next frame data
        on_data: <type>
            callback object which is called when a message received.
            This is called before on_message or on_cont_message,
            and then on_message or on_cont_message is called.
            on_data has 4 argument.
            The 1st argument is this class object.
            The 2nd argument is utf-8 string which we get from the server.
            The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
            The 4th argument is continue flag. if 0, the data continue
        keep_running: <type>
            this parameter is obsolete and ignored.
        get_mask_key: func
            a callable to produce new mask keys,
            see the WebSocket.set_mask_key's docstring for more information
        cookie: str
            cookie value.
        subprotocols: <type>
            array of available sub protocols. default is None.
        NFr   )urlheadercookieon_open
on_messageon_dataon_erroron_closeon_pingon_pongon_cont_messager   get_mask_keyr   last_ping_tmlast_pong_tmsubprotocols)r   r/   r0   r2   r3   r5   r6   r7   r8   r9   r   r:   r1   r=   r4   s                  r   r   zWebSocketApp.__init__Y   s    v  &:ff$  .!(	(r   c                 p    | j         r| j                             ||          dk    rt          d          dS )a  
        send message

        Parameters
        ----------
        data: <type>
            Message to send. If you set opcode to OPCODE_TEXT,
            data must be utf-8 string or unicode.
        opcode: <type>
            Operation code of data. default is OPCODE_TEXT.
        r   zConnection is already closed.N)r   send"WebSocketConnectionClosedException)r   dataopcodes      r   r?   zWebSocketApp.send   sI     y 	1DINN488A= 	14/1 1 1	1 	1r   c                 X    d| _         | j        r | j        j        di | d| _        dS dS )z-
        Close websocket connection.
        FNr   )r   r   close)r   kwargss     r   rD   zWebSocketApp.close   sE     "9 	DIO%%f%%%DIII	 	r   c                 P   |                     |          st          j                    | _        | j        rZ	 | j                                         n?# t
          $ r2}t          j        d                    |                     Y d }~d S d }~ww xY w|                     |          d S d S )Nz send_ping routine terminated: {})	waittimer;   r   ping	Exceptionr   warningformat)r   intervaleventexs       r   
_send_pingzWebSocketApp._send_ping   s    **X&& 	 $	Dy INN$$$$    $%G%N%Nr%R%RSSSEEEEE **X&& 	 	 	 	 	s   A 
B'BBr   Fc                 B    dk    rdr|r|k    rt          d          |sg }|si } j        rt          d          dd _        d _        d _        d fd		 t           j        || j        du|	|rdnd           _         j                            t                                  j        
                     j         j         j        |||| j        |
|||	           |s                               }                      j                   |rYt%          j                    t%          j         j        |f
                              d                                             fd} fd}|                     j        j        ||           dS # t2          t4          t6          f$ r[}                      j        |           t;          |t6                    r               t;          |t4                     cY d}~S d}~ww xY w)a}  
        Run event loop for WebSocket framework.

        This loop is an infinite loop and is alive while websocket is available.

        Parameters
        ----------
        sockopt: tuple
            values for socket.setsockopt.
            sockopt must be tuple
            and each element is argument of sock.setsockopt.
        sslopt: dict
            optional dict object for ssl socket option.
        ping_interval: int or float
            automatically send "ping" command
            every specified period (in seconds)
            if set to 0, not send automatically.
        ping_timeout: int or float
            timeout (in seconds) if the pong message is not received.
        http_proxy_host: <type>
            http proxy host name.
        http_proxy_port: <type>
            http proxy port. If not set, set to 80.
        http_no_proxy: <type>
            host names, which doesn't use proxy.
        skip_utf8_validation: bool
            skip utf8 validation.
        host: str
            update host header.
        origin: str
            update origin header.
        dispatcher: <type>
            customize reading data from socket.
        suppress_origin: bool
            suppress outputting origin header.

        Returns
        -------
        teardown: bool
            False if caught KeyboardInterrupt, True if other exception was raised during a loop
        Nr   z#Ensure ping_interval > ping_timeoutzsocket is already openedTc                 B   r<                                 r(                                                                  d_        j        rj                                                             | r| j        nd          } j        j	        g|R   d_        dS )z
            Tears down the connection.

            If close_frame is set, we will invoke the on_close handler with the
            statusCode and reason from there.
            FN)
is_alivesetjoinr   r   rD   _get_close_argsrA   	_callbackr6   )close_frame
close_argsrN   r   threads     r   teardownz*WebSocketApp.run_forever.<locals>.teardown  s      &//++ 		 %Dy "	!!!--$/9  T; ;JDN4=6:6666DIIIr   F)sockoptssloptfire_cont_frameskip_utf8_validationenable_multithread)r0   r1   http_proxy_hosthttp_proxy_porthttp_no_proxyhttp_proxy_authr=   hostoriginsuppress_origin
proxy_type)targetargsc                  N   j         s
             S j                            d          \  } }| t          j        k    r |          S | t          j        k    r"                    j        |j                   n(| t          j	        k    r9t          j
                    _                            j        |j                   n| t          j        k    rZj        rS                    j        |j        |j        |j                                       j        |j        |j                   nu|j        }t$          j        r%| t          j        k    r|                    d          }                    j        ||j        d                               j        |           dS )NTutf-8)r   r   recv_data_framer   OPCODE_CLOSEOPCODE_PINGrW   r7   rA   OPCODE_PONGrH   r<   r8   OPCODE_CONTr9   r4   rB   finsixPY3OPCODE_TEXTdecoder3   )op_codeframerA   r   r[   s      r   r"   z&WebSocketApp.run_forever.<locals>.read7  s   ( &#8::%!%!:!:4!@!@d// :#8E??* 00 :NN4<<<<< 00 :(,	D%NN4<<<<< 00 
:T5I 
:NN4<#(<< < <NN4#7#(:uy: : : : !:Dw 47d.>#> 4#{{733NN4<u|TJJJNN4?D999tr   c                      rat          j                     j        z
  k    } j        j        z
  dk     }j        j        z
  k    }j        r| r|s|rt          d          dS )Nr   zping/pong timed outT)rH   r;   r<   WebSocketTimeoutException)has_timeout_expired$has_pong_not_arrived_after_last_pinghas_pong_arrived_too_later   r   s      r   checkz'WebSocketApp.run_forever.<locals>.checkQ  s      O*.)++8I*IL*X';?;LtO`;`cd;d8040ADDU0UXd0d-) O 3O!EOIbO 88MNNNtr   r   )WebSocketExceptionr   r   r;   r<   r   r:   r9   
settimeoutr   connectr/   r0   r1   r=   create_dispatcherrW   r2   	threadingEventThreadrP   	setDaemonstartr"   rJ   KeyboardInterrupt
SystemExitr5   
isinstance)r   r\   r]   ping_intervalr   ra   rb   rc   rd   r_   re   rf   
dispatcherrg   rh   r"   r~   r!   rN   r[   rZ   s   `   `             @@@r   run_foreverzWebSocketApp.run_forever   s   b  	 (9 	 L 	LM 	Lm|.K 	L$%JKKK 	G 	F9 	A$%?@@@ 	 	 	 	 	 	 	 	$G	8!!76 $ 4D @%9+8#C44e	E E EDI
 I  !2!4!4555IT[ / /} /d>O&/%  ' ' '  B!33LAA
NN4<((( !))")?-1GI I I  &&&     4
 
 
 
 
 
 OODIND%88888,j9 	8 	8 	8NN4=!,,,!Z(( HJJJ!!%6777777777	8s    'EF, ,HAHHHc                 |    |pd}| j                                         rt          | |          S t          | |          S )N
   )r   is_sslr(   r   )r   r   timeouts      r   r   zWebSocketApp.create_dispatcherf  sB    $"9 	0 w///$(((r   c                    t           j        dk     r9| j        r/t          t	          j        | j                  j                  dk    rg S n8| j        r/t          t	          j        | j                  j                  dk    rg S |rpt          |          dk    r]dt          j	        |dd                   z  t          j	        |dd                   z   }|dd         
                    d          }||gS ddgS )	z
        _get_close_args extracts the code, reason from the close body
        if they exists, and if the self.on_close except three arguments
        )   r   r         r   r   Nrl   )sysversion_infor6   leninspect
getargspecrj   getfullargspecrs   byte2intrv   )r   rA   codereasons       r   rV   zWebSocketApp._get_close_argsm  s     f$ 	= C(:4=(I(I(N$O$OST$T 	 = C(>t}(M(M(R$S$SWX$X 	 	"CIIN 	"d1Q3i0003<QqS	3J3JJD!""X__W--F&>!d|r   c                 *   |r	  || g|R   d S # t           $ rw}t          j        d                    ||                     t          j                    r1t          j                    \  }}}t          j        |           Y d }~d S Y d }~d S d }~ww xY wd S )Nzerror from callback {}: {})	rJ   r   errorrL   isEnabledForDebugr   exc_info	tracebackprint_tb)r   callbackrj   r!   _tbs         r   rW   zWebSocketApp._callback  s     	++%%%%%%% + + +;BB8QOOPPP-// +"|~~HAq"&r*********+ + + + + ++		+ 	+s   	 
BA&BB)NNNNNNNNTNNNN)NNr   NNNNNFNNNFN)r#   r$   r%   r&   r   r   ru   r?   rD   rP   r   r   rV   rW   r   r   r   r	   r	   T   s          $(9=6:!%>B"L) L) L) L)\ !% 0 1 1 1 1"     0426:>8<).7;6:Y8 Y8 Y8 Y8v) ) )  (	+ 	+ 	+ 	+ 	+r   )r&   r   r   r   r   rH   r   rs   _abnfr   _corer   r   _exceptions r   __all__r   r(   objectr	   r   r   r   <module>r      s;   (   



          



       / / / / / / / /           
       "       2v+ v+ v+ v+ v+6 v+ v+ v+ v+ v+r   