o
    f>h                     @   s   U d 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 G dd dZ!dS )z%WebSocket protocol versions 13 and 8.    N)partial)AnyFinalOptional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_LIMITi   c                   @   s   e Zd ZdZdee ddddedej	de
ded	ejd
ede
ddfddZ	ddeded
ee ddfddZd
edefddZddedeeef ddfddZdS )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                C   sH   || _ || _|| _t|jd| _|| _|| _d| _|| _	d| _
d| _dS )zInitialize a WebSocket writer.    Fr   N)r   r    r   r   getrandbitsget_random_bitsr   r   _closing_limit_output_size_compressobj)selfr   r    r   r   r   r   r    r*   L/var/www/html/venv/lib/python3.10/site-packages/aiohttp/_websocket/writer.py__init__,   s   
zWebSocketWriter.__init__messageopcodec                    s  | j r|tj@ stdd}|s| jrF|dk rFd}|r"| |}n| js,| | j| _| j}||I dH || jr>t	j
nt	j t}t|}| j}|rQdnd}d|B |B }	|dk rgt|	||B }
d}n|d	k rvt|	d|B |}
d
}n
t|	d|B |}
d}| j rtd|rt|  }t|}t|| | j|
| |  |  jt7  _n|tkr| j|
 | j| n| j|
|  |  j|| 7  _| j| jkrd| _| jjr| j  I dH  dS dS dS )z<Send a frame over the websocket with message as its payload.z!Cannot write to closing transportr      @   N   ~   r   r         
   )!r%   r   CLOSEr	   r   _make_compress_objr(   flushr   r
   Z_FULL_FLUSHZ_SYNC_FLUSHremovesuffixr   lenr   r   r   r   r    
is_closingr   r$   	bytearrayr   writer'   r   r   r&   r   _paused_drain_helper)r)   r-   r.   r   rsvcompressobj
msg_lengthr   mask_bit
first_byteheader
header_lenmaskr*   r*   r+   
send_frameC   sf   
	

zWebSocketWriter.send_framec                 C   s   t tj| tdS )N)levelwbitsmax_sync_chunk_size)r   r
   Z_BEST_SPEEDWEBSOCKET_MAX_SYNC_CHUNK_SIZE)r)   r   r*   r*   r+   r7      s
   z"WebSocketWriter._make_compress_obj      codec                    sJ   t |tr|d}z| jt|| tjdI dH  W d| _dS d| _w )z<Close the websocket, sending the specified code and message.zutf-8)r.   NT)
isinstancestrencoderJ   r   r   r6   r%   )r)   rR   r-   r*   r*   r+   close   s   

zWebSocketWriter.close)N)rP   rQ   )__name__
__module____qualname____doc__r   r   Randomr   asyncio	Transportboolintr,   bytesr   rJ   r   r7   r   rT   rV   r*   r*   r*   r+   r   #   sH    	


_$r   )"rZ   r\   r   	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__rO   r   r*   r*   r*   r+   <module>   s    (
	