
    i+                         U d 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m	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ d	d
lmZmZmZmZmZmZmZmZ d	dlmZmZ dZee   e d<   dZ!ee   e d<   dZ" G d d      Z#y)z%WebSocket protocol versions 13 and 8.    N)partial)FinalOptionalSetUnion   )BaseProtocol)ClientConnectionResetError)ZLibBackendZLibCompressor   )MASK_LENMSG_SIZEPACK_CLOSE_CODE	PACK_LEN1	PACK_LEN2	PACK_LEN3PACK_RANDBITSwebsocket_mask)WS_DEFLATE_TRAILING	WSMsgType   DEFAULT_LIMIT   WS_CONTROL_FRAME_OPCODEi @  c                   8   e Zd ZdZde ej                         ddddedej                  de
ded	ej                  d
ede
ddfdZ	 ddeded
ee   ddfdZdedededdfdZd
ee   defdZdeded
ee   ddfdZdeded
ee   ddfdZddedeeef   ddfdZy)WebSocketWritera3  WebSocket writer.

    The writer is responsible for sending messages to the client. It is
    created by the protocol when a connection is established. The writer
    should avoid implementing any application logic and should only be
    concerned with the low-level details of the WebSocket protocol.
    Fr   )use_masklimitrandomcompress
notakeoverprotocol	transportr   r   r    r!   r"   returnNc                   || _         || _        || _        t        |j                  d      | _        || _        || _        d| _        || _	        d| _
        d| _        t        j                         | _        t               | _        y)zInitialize a WebSocket writer.    Fr   N)r#   r$   r   r   getrandbitsget_random_bitsr!   r"   _closing_limit_output_size_compressobjasyncioLock
_send_lockset_background_tasks)selfr#   r$   r   r   r    r!   r"   s           T/var/www/html/binance/venv/lib/python3.12/site-packages/aiohttp/_websocket/writer.py__init__zWebSocketWriter.__init__1   sq     !" &v'9'92> $6:!,,.:=%    messageopcodec                   K   | j                   r|t        j                  z  st        d      |s| j                  r	|t
        k\  r| j                  ||d       nt        |      t        k  r9| j                  4 d{    | j                  |||       ddd      d{    nt        j                         }| j                  |||      }t        j                  dk\  rt        j                   ||d      }n|j#                  |      }| j$                  j'                  |       |j)                  | j$                  j*                         t        j,                  |       d{    | j.                  | j0                  kD  rAd| _        | j2                  j4                  r#| j2                  j7                          d{    yyy7 E7 %# 1 d{  7  sw Y   sxY w7 z7 $w)z<Send a frame over the websocket with message as its payload.!Cannot write to closing transportr   N)      T)loopeager_start)r*   r   CLOSEr
   r!   r   _write_websocket_framelenWEBSOCKET_MAX_SYNC_CHUNK_SIZEr0   _send_compressed_frame_syncr.   get_running_loop#_send_compressed_frame_async_lockedsysversion_infoTaskcreate_taskr2   addadd_done_callbackdiscardshieldr,   r+   r#   _paused_drain_helper)r3   r7   r8   r!   r=   coro	send_tasks          r4   
send_framezWebSocketWriter.send_frameJ   s     ==&9??":,-PQQDMMf8O.O'';\::
  L L00&(KL L L ++-D;;GVXVD7*#LLDdK	 ,,T2	""&&y1''(>(>(F(FG..+++ t{{* !D}}$$mm11333 % +=L L L L L* , 4sh   A9G;G <G?GGGCG G!AG8G9GGGGGGGrsvc                    t        |      }| j                  }|rdnd}d|z  |z  }|dk  rt        |||z        }d}	n*|dk  rt        |d|z  |      }d}	nt	        |d|z  |      }d}	| j
                  j                         rt        d	      |rkt        | j                               }
t        |      }t        |
|       | j
                  j                  ||
z   |z          | xj                  t        z  c_        n^|t        kD  r7| j
                  j                  |       | j
                  j                  |       n| j
                  j                  ||z          | xj                  |	|z   z  c_        y
)a  
        Write a websocket frame to the transport.

        This method handles frame header construction, masking, and writing to transport.
        It does not handle compression or flow control - those are the responsibility
        of the caller.
           r   ~   r   r         
   r:   N)rA   r   r   r   r   r$   
is_closingr
   r   r)   	bytearrayr   writer,   r   r   )r3   r7   r8   rS   
msg_lengthr   mask_bit
first_byteheader
header_lenmasks              r4   r@   z&WebSocketWriter._write_websocket_frame|   sP    \
==#4 CZ&(
z:+@AFJ%z3>:FFJz3>:FFJ>>$$&,-PQQ  !5!5!78D(G4)NN  $!89)("NN  (NN  )NN  '!12Z*44r6   c                     |r!t        t        j                  | t              S | j                  s0t        t        j                  | j
                   t              | _        | j                  S )zBGet or create a compressor object for the given compression level.)levelwbitsmax_sync_chunk_size)r   r   Z_BEST_SPEEDrB   r-   r!   )r3   r!   s     r4   _get_compressorzWebSocketWriter._get_compressor   s^    !!..i$A 
    .!..}}n$A!D
    r6   c                    | j                  |      }| j                  |j                  |      |j                  | j                  rt
        j                  nt
        j                        z   j                  t              |d       y)z
        Synchronous send for small compressed frames.

        This is used for small compressed payloads that compress synchronously in the event loop.
        Since there are no await points, this is inherently cancellation-safe.
        @   N)
rh   r@   compress_syncflushr"   r   Z_FULL_FLUSHZ_SYNC_FLUSHremovesuffixr   r3   r7   r8   r!   compressobjs        r4   rC   z+WebSocketWriter._send_compressed_frame_sync   ss     **84 	##))'2##  ,,$11 l./	
r6   c                   K   | j                   4 d{    | j                  |      }| j                  |j                  |       d{   |j	                  | j
                  rt        j                  nt        j                        z   j                  t              |d       ddd      d{    y7 7 l7 	# 1 d{  7  sw Y   yxY ww)a  
        Async send for large compressed frames with lock.

        Acquires the lock and compresses large payloads asynchronously in
        the executor. The lock is held for the entire operation to ensure
        the compressor state is not corrupted by concurrent sends.

        MUST be run shielded from cancellation. If cancelled after
        compression but before sending, the compressor state would be
        advanced but data not sent, corrupting subsequent frames.
        Nrj   )r0   rh   r@   r!   rl   r"   r   rm   rn   ro   r   rp   s        r4   rE   z3WebSocketWriter._send_compressed_frame_async_locked   s      ?? 	 	 ..x8K ''%..w77!''?? $00(55 ,23	 	 	 8	 	 	 	sW   CB3C1B9B5
	AB9"C-B7.C5B97C9C?C CCcodec                    K   t        |t              r|j                  d      }	 | j                  t	        |      |z   t
        j                         d{    d| _        y7 # d| _        w xY ww)z<Close the websocket, sending the specified code and message.zutf-8)r8   NT)
isinstancestrencoderR   r   r   r?   r*   )r3   rs   r7   s      r4   closezWebSocketWriter.close   si     gs#nnW-G	!//%/	 "    !DM	 !DMs.   "A00A$ A"A$ A0"A$ $	A--A0)N)i  r6   )__name__
__module____qualname____doc__r   r    Randomr	   r.   	Transportboolintr5   bytesr   rR   r@   r   rh   rC   rE   r   rv   rx    r6   r4   r   r   (   sg    " - @@ $$@
 @ @ @ @ @ 
@4 FJ0404&)045=c]04	04d05e 05S 05s 05t 05d! !. !"

&)
5=c]
	
8  &) 5=c] 	 D	! 	!U5#:5F 	!QU 	!r6   r   )$r|   r.   r    rF   	functoolsr   typingr   r   r   r   base_protocolr	   client_exceptionsr
   compression_utilsr   r   helpersr   r   r   r   r   r   r   r   modelsr   r   r   r   __annotations__r   rB   r   r   r6   r4   <module>r      sj    +   
  . . ( : ;	 	 	 3!uSz ! '( s ' !* ^! ^!r6   