K igddlmZddlZddlZddlZddlmZddlmZddl m Z m Z m Z m Z mZmZddlmZddlmZmZmZmZmZmZmZmZmZmZdd lmZmZdd l m!Z!dd lm"Z"m#Z#m$Z$gd Z%eeeefZ& Gd dejNZ(e(jRZ)e(jTZ*GddejNZ+e+jXZ,e+jZZ-e+j\Z.e+j^Z/dZ0 GddZ1y)) annotationsN) Generator)Union)ConnectionClosedConnectionClosedErrorConnectionClosedOK InvalidState PayloadTooBig ProtocolError) Extension) OK_CLOSE_CODES OP_BINARYOP_CLOSEOP_CONTOP_PINGOP_PONGOP_TEXTClose CloseCodeFrame)RequestResponse) StreamReader) LoggerLikeOrigin Subprotocol)ProtocolSideStateSEND_EOFc&eZdZdZed\ZZy)rz6A WebSocket connection is either a server or a client.N)__name__ __module__ __qualname____doc__rangeSERVERCLIENTY/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/websockets/protocol.pyrr/s@1XNFFr,rc*eZdZdZed\ZZZZy)r z6A WebSocket connection is in one of these four states.N) r$r%r&r'r( CONNECTINGOPENCLOSINGCLOSEDr+r,r-r r 9s@(-a%Jgvr,r r,c2eZdZdZeddd ddZeddZejddZeddZ ed d Z ed!d Z d"d Z d#d Z d$d Zd%d$dZd%d$dZd&d'dZd"dZd"dZd(d)dZd*dZd+dZd,dZd-dZd-dZd.dZd.dZd#dZy)/ra6 Sans-I/O implementation of a WebSocket connection. Args: side: :attr:`~Side.CLIENT` or :attr:`~Side.SERVER`. state: Initial state of the WebSocket connection. max_size: Maximum size of incoming messages in bytes; :obj:`None` disables the limit. logger: Logger for this connection; depending on ``side``, defaults to ``logging.getLogger("websockets.client")`` or ``logging.getLogger("websockets.server")``; see the :doc:`logging guide <../../topics/logging>` for details. iN)statemax_sizeloggercHtj|_ |0tjd|j j }||_ |jtj|_ ||_ ||_ ||_ d|_d|_d|_g|_d|_d|_d|_d|_d|_ d|_t1|_g|_g|_|j9|_t=|j:d|_y)Nz websockets.F) uuiduuid4idlogging getLoggernamelowerr7 isEnabledForDEBUGdebugsider5r6cur_sizeexpect_continuation_frameorigin extensions subprotocol close_rcvd close_sentclose_rcvd_then_sent handshake_exceof_sentrreadereventswritesparseparsernext parser_exc)selfrCr5r6r7s r-__init__zProtocol.__init__Ys"ZZ\B >&&TYY__5F4G'HIF"( )((7   ! %) */&&* +-/3)-(,15!04  #n #% #% jjl  T[[,0r,c|jS)a State of the WebSocket connection. Defined in 4.1_, 4.2_, 7.1.3_, and 7.1.4_ of :rfc:`6455`. .. _4.1: https://datatracker.ietf.org/doc/html/rfc6455#section-4.1 .. _4.2: https://datatracker.ietf.org/doc/html/rfc6455#section-4.2 .. _7.1.3: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.3 .. _7.1.4: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.4 )_staterUs r-r5zProtocol.states{{r,cv|jr&|jjd|j||_y)Nz= connection is %s)rBr7r>rX)rUr5s r-r5zProtocol.states) :: KK  2EJJ ? r,c|jtury|jtjS|jj S)z WebSocket close code received from the remote endpoint. Defined in 7.1.5_ of :rfc:`6455`. .. _7.1.5: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5 :obj:`None` if the connection isn't closed yet. N)r5r3rIrABNORMAL_CLOSUREcoderYs r- close_codezProtocol.close_codes9 ::V # __ $-- -??'' 'r,cn|jtury|jy|jjS)a WebSocket close reason received from the remote endpoint. Defined in 7.1.6_ of :rfc:`6455`. .. _7.1.6: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6 :obj:`None` if the connection isn't closed yet. N)r5r3rIreasonrYs r- close_reasonzProtocol.close_reasons0 ::V # __ $??)) )r,cd|jtusJd|jK|j?|jjt vr#|jjt vrt }nt}||j|j|j}|j|_ |S)a Exception to raise when trying to interact with a closed connection. Don't raise this exception while the connection :attr:`state` is :attr:`~websockets.protocol.State.CLOSING`; wait until it's :attr:`~websockets.protocol.State.CLOSED`. Indeed, the exception includes the close code and reason, which are known only once the connection is closed. Raises: AssertionError: If the connection isn't closed yet. zconnection isn't closed yet) r5r3rIrJr]rr rrKrT __cause__)rUexc_typeexcs r- close_exczProtocol.close_excs zzV#B%BB# OO '+$$6$$6)H,H ( OO OO  % %!    r,cd|jj|t|jy)aH Receive data from the network. After calling this method: - You must call :meth:`data_to_send` and send this data to the network. - You should call :meth:`events_received` and process resulting events. Raises: EOFError: If :meth:`receive_eof` was called earlier. N)rN feed_datarSrRrUdatas r- receive_datazProtocol.receive_datas" d# T[[r,c|jjry|jjt|jy)a Receive the end of the data stream from the network. After calling this method: - You must call :meth:`data_to_send` and send this data to the network; it will return ``[b""]``, signaling the end of the stream, or ``[]``. - You aren't expected to call :meth:`events_received`; it won't return any new events. :meth:`receive_eof` is idempotent. N)rNeoffeed_eofrSrRrYs r- receive_eofzProtocol.receive_eofs. ;;??   T[[r,c|js td|jtur0t d|j j j| |_|jtt||y)a Send a `Continuation frame`_. .. _Continuation frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 Parameters: data: payload containing the same kind of data as the initial frame. fin: FIN bit; set it to :obj:`True` if this is the last frame of a fragmented message and to :obj:`False` otherwise. Raises: ProtocolError: If a fragmented message isn't in progress. unexpected continuation frameconnection is N) rEr rXr1r r5r>r? send_framerrrUrkfins r-send_continuationzProtocol.send_continuation#sh"-- ?@ @ ;;d " 0E0E0G/HIJ J-0& gtS12r,c|jr td|jtur0t d|j j j| |_|jtt||y)a Send a `Text frame`_. .. _Text frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 Parameters: data: payload containing text encoded with UTF-8. fin: FIN bit; set it to :obj:`False` if this is the first frame of a fragmented message. Raises: ProtocolError: If a fragmented message is in progress. expected a continuation framersN) rEr rXr1r r5r>r?rtrrrus r- send_textzProtocol.send_text;sh  ) ) ?@ @ ;;d " 0E0E0G/HIJ J-0& gtS12r,c|jr td|jtur0t d|j j j| |_|jtt||y)a Send a `Binary frame`_. .. _Binary frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.6 Parameters: data: payload containing arbitrary binary data. fin: FIN bit; set it to :obj:`False` if this is the first frame of a fragmented message. Raises: ProtocolError: If a fragmented message is in progress. ryrsN) rEr rXr1r r5r>r?rtrrrus r- send_binaryzProtocol.send_binaryRsh  ) ) ?@ @ ;;d " 0E0E0G/HIJ J-0& is34r,c|jtur0td|jjj |-|dk7r t dttjd}d}nt||}|j}|jtt||jJ||_t |_y)a_ Send a `Close frame`_. .. _Close frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1 Parameters: code: close code. reason: close reason. Raises: ProtocolError: If the code isn't valid or if a reason is provided without a code. rsNr`z#cannot send a reason without a coder,)rXr1r r5r>r?r rrNO_STATUS_RCVD serializertrrrIrJr2rUr]racloserks r- send_closezProtocol.send_closeis$ ;;d " 0E0E0G/HIJ J <|#$IJJ)22B7ED$'E??$D h-.&&& r,c|jturB|jtur0td|jj j |jtt|y)z Send a `Ping frame`_. .. _Ping frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.2 Parameters: data: payload containing arbitrary binary data. rsN) rXr1r2r r5r>r?rtrrrjs r- send_pingzProtocol.send_pingR ;;d "t{{''A 0E0E0G/HIJ J gt,-r,c|jturB|jtur0td|jj j |jtt|y)z Send a `Pong frame`_. .. _Pong frame: https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.3 Parameters: data: payload containing arbitrary binary data. rsN) rXr1r2r r5r>r?rtrrrjs r- send_pongzProtocol.send_pongrr,c|jturs|tjk7r`t ||}|j }|j tt|||_ |jd|_ t|_|jtur|js|j!|j#|_t'|j$y)a? `Fail the WebSocket connection`_. .. _Fail the WebSocket connection: https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.7 Parameters: code: close code reason: close reason Raises: ProtocolError: If the code isn't valid. NT)r5r1rr\rrrtrrrJrIrKr2rCr)rMsend_eofdiscardrRrSrs r-failz Protocol.fails& :: y111dF+(h 56"' ??.04D-$ 99 t}} MMO lln  T[[r,c.|jgc}|_|S)a* Fetch events generated from data received from the network. Call this method immediately after any of the ``receive_*()`` methods. Process resulting events, likely by passing them to the application. Returns: Events read from the connection. )rO)rUrOs r-events_receivedzProtocol.events_receiveds#kk2  r,c.|jgc}|_|S)a Obtain data to send to the network. Call this method immediately after any of the ``receive_*()``, ``send_*()``, or :meth:`fail` methods. Write resulting data to the connection. The empty bytestring :data:`~websockets.protocol.SEND_EOF` signals the end of the data stream. When you receive it, half-close the TCP connection. Returns: Data to write to the connection. )rP)rUrPs r- data_to_sendzProtocol.data_to_sends"#kk2  r,c|jtury|jtury|jtury|jtusJ|j S)a Tell if the TCP connection is expected to close soon. Call this method immediately after any of the ``receive_*()``, ``send_close()``, or :meth:`fail` methods. If it returns :obj:`True`, schedule closing the TCP connection after a short timeout if the other side hasn't already closed it. Returns: Whether the TCP connection is expected to close soon. FT)r5r1r2r3r0rMrYs r-close_expectedzProtocol.close_expectedsND ::  ::  :: zzZ'''}}r,c#K |jjEd{r2|jr|jjdt d|j d}n2|j |j }n|j |j z }tj|jj|jtu||jEd{}|jr|jjd||j|77B#t$r;}|jt j"t%|||_Yd}~nKd}~wt$r;}|jt j(t%|||_Yd}~n d}~wt*$rJ}|jt j,|j.d|j0||_Yd}~nd}~wt2$rU}|j5|j |jt j6t%|||_Yd}~n\d}~wt8$rM}|jj;dd |jt j<||_Yd}~nd}~wwxYwdt?d w) a- Parse incoming data into frames. :meth:`receive_data` and :meth:`receive_eof` run this generator coroutine until it needs more data or reaches EOF. :meth:`parse` never raises an exception. Instead, it sets the :attr:`parser_exc` and yields control. TN< EOFzunexpected end of stream)maskr6rGz< %sz at position z parser failed)exc_infoz"parse() shouldn't step after error) rNat_eofrBr7EOFErrorr6rDrrQ read_exactrCr)rG recv_framer rrPROTOCOL_ERRORstrrTr\UnicodeDecodeError INVALID_DATArastartr set_current_sizeMESSAGE_TOO_BIG ExceptionerrorINTERNAL_ERRORAssertionError)rUr6framerfs r-rQzProtocol.parse/s 6 "#{{11333zz ))'2 ##=>>==(#H]]*#}}H#}}t}} %s)rrG)rBr7rPrrrCr*rG)rUrs r-rtzProtocol.send_framesS :: KK  fe ,  OOYY&(??   r,c|jrJd|_|jr|jjd|jj t y)NTz> EOF)rMrBr7rPrr!rYs r-rzProtocol.send_eofsA==   :: KK  g & 8$r,) rCrr5r r6 int | Noner7zLoggerLike | NonereturnNone)rr )r5r rr)rr)rz str | None)rr)rkbytesrr)rr)rkrrvboolrr)T)Nr`)r]rrarrr)r`)r]intrarrr)rz list[Event])rz list[bytes])rr)rzGenerator[None])rrrr)r$r%r&r'r1rVpropertyr5setterr^rbrgrlrprwrzr|rrrrrrrrQrrrtrr+r,r-rrIs &$$( B1B1 B1  B1 " B1 B1H   \\ (($**$!!J *303.5."H. . -b  ()ZFCPCrs"  %"   &!33  gx&'?4<<  1DLL1     zz --  Em %m %r,