o
    _i                     @  s   d dl mZ d dlZd dlmZmZ d dlmZmZm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZ zdd	lmZ W n eyK   dd	lmZ Y nw G d
d de	ZdddZd ddZeZddlmZ d!ddZd"ddZdS )#    )annotationsN)	AwaitableSequence)AnyCallable
NamedTuple   )
extensionsframes)PayloadTooBigProtocolError)	BytesLikeDataLike)
apply_maskc                   @  s   e Zd ZU ded< ded< ded< dZded< dZded	< dZded
< ed%ddZd&ddZ	d'ddZ
edddd(ddZdd d)d#d$ZdS )*Frameboolfinzframes.Opcodeopcoder   dataFrsv1rsv2rsv3returnframes.Framec                 C  s    t | j| j| j| j| j| jS N)r
   r   r   r   r   r   r   r   self r   [/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/websockets/legacy/framing.py	new_frame   s   zFrame.new_framestrc                 C  s
   t | jS r   )r    r   r   r   r   r   __str__%      
zFrame.__str__Nonec                 C  s
   | j  S r   )r   checkr   r   r   r   r$   (   r"   zFrame.checkN)max_sizer	   reader!Callable[[int], Awaitable[bytes]]maskr%   
int | Noner	   %Sequence[extensions.Extension] | Nonec             
     s  |dI dH }t d|\}}|d@ rdnd}|d@ rdnd}	|d@ r&dnd}
|d	@ r.dnd}z	t|d
@ }W n tyJ } ztd|d}~ww |d@ rQdnd|krYtd|d@ }|dkrp|dI dH }t d|\}n|dkr|dI dH }t d|\}|dur||krt|||r|dI dH }||I dH }|rt||}t||||	|
|}|du rg }t	|D ]	}|j
||d}q|  | |j|j|j|j|j|jS )a@  
        Read a WebSocket frame.

        Args:
            reader: Coroutine that reads exactly the requested number of
                bytes, unless the end of file is reached.
            mask: Whether the frame should be masked i.e. whether the read
                happens on the server side.
            max_size: Maximum payload size in bytes.
            extensions: List of extensions, applied in reverse order.

        Raises:
            PayloadTooBig: If the frame exceeds ``max_size``.
            ProtocolError: If the frame contains incorrect values.

        r   Nz!BB   TF@             zinvalid opcodezincorrect masking   ~   z!H   z!Q   )r%   )structunpackr
   Opcode
ValueErrorr   r   r   r   reverseddecoder$   r   r   r   r   r   r   )clsr&   r(   r%   r	   r   head1head2r   r   r   r   r   exclength	mask_bitsr   	extensionr   r   r   read+   sV   


z
Frame.read)r	   writeCallable[[bytes], Any]c                C  s   || j j||d dS )a  
        Write a WebSocket frame.

        Args:
            frame: Frame to write.
            write: Function that writes bytes.
            mask: Whether the frame should be masked i.e. whether the write
                happens on the client side.
            extensions: List of extensions, applied in order.

        Raises:
            ProtocolError: If the frame contains incorrect values.

        )r(   r	   N)r   	serialize)r   rB   r(   r	   r   r   r   rB   z   s   zFrame.write)r   r   )r   r    )r   r#   )
r&   r'   r(   r   r%   r)   r	   r*   r   r   )rB   rC   r(   r   r	   r*   r   r#   )__name__
__module____qualname____annotations__r   r   r   propertyr   r!   r$   classmethodrA   rB   r   r   r   r   r      s"   
 


Sr   r   r   r   tuple[int, BytesLike]c                 C  s8   t | trtjj|  fS t | trtjj| fS td)a  
    Convert a string or byte-like object to an opcode and a bytes-like object.

    This function is designed for data frames.

    If ``data`` is a :class:`str`, return ``OP_TEXT`` and a :class:`bytes`
    object encoding ``data`` in UTF-8.

    If ``data`` is a bytes-like object, return ``OP_BINARY`` and a bytes-like
    object.

    Raises:
        TypeError: If ``data`` doesn't have a supported type.

    data must be str or bytes-like)	
isinstancer    r
   r6   TEXTencoder   BINARY	TypeErrorr   r   r   r   prepare_data   s
   

rS   bytesc                 C  s,   t | tr	|  S t | trt| S td)ai  
    Convert a string or byte-like object to bytes.

    This function is designed for ping and pong frames.

    If ``data`` is a :class:`str`, return a :class:`bytes` object encoding
    ``data`` in UTF-8.

    If ``data`` is a bytes-like object, return a :class:`bytes` object.

    Raises:
        TypeError: If ``data`` doesn't have a supported type.

    rL   )rM   r    rO   r   rT   rQ   rR   r   r   r   prepare_ctrl   s
   

rU   )Closetuple[int, str]c                 C  s   t | }|j|jfS )z
    Parse the payload from a close frame.

    Returns:
        Close code and reason.

    Raises:
        ProtocolError: If data is ill-formed.
        UnicodeDecodeError: If the reason isn't valid UTF-8.

    )rV   parsecodereason)r   closer   r   r   parse_close   s   
r\   rY   intrZ   r    c                 C  s   t | | S )z3
    Serialize the payload for a close frame.

    )rV   rD   )rY   rZ   r   r   r   serialize_close   s   r^   )r   r   r   rK   )r   r   r   rT   )r   rT   r   rW   )rY   r]   rZ   r    r   rT   )
__future__r   r4   collections.abcr   r   typingr   r   r    r	   r
   
exceptionsr   r   r   r   speedupsr   ImportErrorutilsr   rS   rU   encode_datarV   r\   r^   r   r   r   r   <module>   s(     


