a
    یxd%                     @  sz   d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	 ddl
mZmZ ddlmZ dgZedZG d	d dZdS )
    )annotationsN)IteratorListOptionalcast   )FrameOpcode)Data	Assemblerzutf-8c                   @  s\   e Zd ZdZddddZdddd	d
dZddddZdddddZddddZdS )r   z)
    Assemble messages from frames.

    None)returnc                 C  sF   t  | _t  | _t  | _d| _d| _d | _g | _	d | _
d| _d S )NF)	threadingLockmutexEventmessage_completemessage_fetchedget_in_progressput_in_progressdecoderchunkschunks_queueclosedself r   P/var/www/html/Ranjet/env/lib/python3.9/site-packages/websockets/sync/messages.py__init__   s    



zAssembler.__init__NzOptional[float]r
   )timeoutr   c                 C  s  | j 2 | jrtd| jr$tdd| _W d   n1 s>0    Y  | j|}| j  d| _|sxtd|dd| jrtd| j sJ | j	  | j
du rd	nd
}|| j}| j rJ | j  g | _| jdu sJ |W  d   S 1 s0    Y  dS )a  
        Read the next message.

        :meth:`get` returns a single :class:`str` or :class:`bytes`.

        If the message is fragmented, :meth:`get` waits until the last frame is
        received, then it reassembles the message and returns it. To receive
        messages frame by frame, use :meth:`get_iter` instead.

        Args:
            timeout: If a timeout is provided and elapses before a complete
                message is received, :meth:`get` raises :exc:`TimeoutError`.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
                concurrently.

        stream of frames ended"get or get_iter is already runningTNFztimed out in z.1fs     )r   r   EOFErrorr   RuntimeErrorr   waitTimeoutErroris_setclearr   joinr   r   setr   )r   r   	completedjoinermessager   r   r   get;   s,    $

zAssembler.getzIterator[Data]c                 c  s$  | j d | jrtd| jr$td| j}g | _tdt | _	| j
 rV| j	d d| _W d   n1 sp0    Y  |E dH  | j	 }|du rq|V  q| j h d| _| j
 sJ | j
  | jrtd| j rJ | j  | jg ksJ d| _	W d   n1 s0    Y  dS )aw  
        Stream the next message.

        Iterating the return value of :meth:`get_iter` yields a :class:`str` or
        :class:`bytes` for each frame in the message.

        The iterator must be fully consumed before calling :meth:`get_iter` or
        :meth:`get` again. Else, :exc:`RuntimeError` is raised.

        This method only makes sense for fragmented messages. If messages aren't
        fragmented, use :meth:`get` instead.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
                concurrently.

        r    r!   z!queue.SimpleQueue[Optional[Data]]NTF)r   r   r%   r   r&   r   r   queueSimpleQueuer   r   r)   putr0   r*   r   r,   )r   r   chunkr   r   r   get_iterw   s:    	
$



zAssembler.get_iterr   )framer   c                 C  s  | j  | jrtd| jr&td|jtju r@tdd| _	n2|jtj
u rTd| _	n|jtju rbnW d   dS | j	dur| j	|j|j}n|j}| jdu r| j| n| j| |jsW d   dS | j rJ | j  | jdur| jd | j rJ d| _W d   n1 s*0    Y  | j  | j F d| _| j s\J | j  | jrvtdd| _	W d   n1 s0    Y  dS )a  
        Add ``frame`` to the next message.

        When ``frame`` is the final frame in a message, :meth:`put` waits until
        the message is fetched, either by calling :meth:`get` or by fully
        consuming the return value of :meth:`get_iter`.

        :meth:`put` assumes that the stream of frames respects the protocol. If
        it doesn't, the behavior is undefined.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`put` concurrently.

        r    zput is already runningstrict)errorsNTF)r   r   r%   r   r&   opcoder	   ZTEXTUTF8Decoderr   BINARYZCONTdecodedataZfinr   r   appendr3   r   r)   r,   r   r'   r*   )r   r6   r=   r   r   r   r3      sD    




&

zAssembler.putc                 C  s|   | j b | jrW d   dS d| _| jrJ| j  | jdurJ| jd | jrZ| j  W d   n1 sn0    Y  dS )z
        End the stream of frames.

        Callling :meth:`close` concurrently with :meth:`get`, :meth:`get_iter`,
        or :meth:`put` is safe. They will raise :exc:`EOFError`.

        NT)	r   r   r   r   r,   r   r3   r   r   r   r   r   r   close  s    

zAssembler.close)N)	__name__
__module____qualname____doc__r   r0   r5   r3   r?   r   r   r   r   r      s   $<BJ)
__future__r   codecsr1   r   typingr   r   r   r   framesr   r	   r
   __all__getincrementaldecoderr:   r   r   r   r   r   <module>   s   
