a
    `g                     @   s   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 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)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eej	e
eejee
ddddZdeeee dd	d
dZeedddZdeeeef dd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
notakeoverN)protocol	transportr   r   r   r   r   returnc                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)   g/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/aiohttp/_websocket/writer.py__init__-   s    zWebSocketWriter.__init__)messageopcoder   r    c                    s  | j r|tj@ stdd}|s&| jr|dk rd}|rB| |}n| jsV| | j| _| j}||I dH || jrzt	j
nt	j t}t|}| j}|rdnd}d|B |B }	|dk rt|	||B }
d}n2|d	k rt|	d|B |}
d
}nt|	d|B |}
d}| j rtd|rZt|  }t|}t|| | j|
| |  |  jt7  _n4|tkr~| j|
 | j| n| j|
|  |  j|| 7  _| j| jkrd| _| jjr| j  I dH  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   zlib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   ZrsvcompressobjZ
msg_lengthr   Zmask_bitZ
first_byteheaderZ
header_lenmaskr)   r)   r*   
send_frameD   s^    	



zWebSocketWriter.send_frame)r   r    c                 C   s   t tj| tdS )N)levelwbitsZmax_sync_chunk_size)r
   r8   Z_BEST_SPEEDWEBSOCKET_MAX_SYNC_CHUNK_SIZE)r(   r   r)   r)   r*   r6      s
    z"WebSocketWriter._make_compress_obj      )coder,   r    c                    sJ   t |tr|d}z(| jt|| tjdI dH  W d| _nd| _0 dS )z<Close the websocket, sending the specified code and message.zutf-8)r-   NT)
isinstancestrencoderE   r   r   r5   r$   )r(   rL   r,   r)   r)   r*   close   s    

zWebSocketWriter.close)N)rJ   rK   )__name__
__module____qualname____doc__r   r   Randomr   asyncio	Transportboolintr+   bytesr   rE   r
   r6   r   rN   rP   r)   r)   r)   r*   r   $   s.    
]r   )"rT   rV   r   r8   	functoolsr   typingr   r   r   r   Zbase_protocolr   Zclient_exceptionsr	   Zcompression_utilsr
   helpersr   r   r   r   r   r   r   r   modelsr   r   r   rY   __annotations__rI   r   r)   r)   r)   r*   <module>   s   (
	