o
    `^hQ&                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ dgZed	ZG d
d dZdS )    )annotationsN)Iteratorcast   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8c                   @  sD   e Zd ZdZdddZddd
dZdddZdddZdddZdS )r   z)
    Assemble messages from frames.

    returnNonec                 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   V/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/websockets/sync/messages.py__init__   s   


	
zAssembler.__init__Ntimeoutfloat | Noner   c                 C  s  | j  | jrtd| jrtdd| _W d   n1 sw   Y  | j|}| j P d| _|s<td|dd| jrCtd| j sJJ | j	  | j
du rVd	nd
}|| j}g | _| jdu shJ | j roJ | j  |W  d   S 1 sw   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.
            ConcurrencyError: If two threads run :meth:`get` or :meth:`get_iter`
                concurrently.
            TimeoutError: If a timeout is provided and elapses before a
                complete message is received.

        stream of frames ended&get() or get_iter() is already runningTNFztimed out in z.1fs     )r   r   EOFErrorr   r   r   waitTimeoutErroris_setclearr   joinr   r   r   set)r   r    	completedjoinermessager   r   r   get>   s0   

$zAssembler.getIterator[Data]c                 c  s:   | j 2 | jrtd| jrtd| j}g | _tdt | _	| j
 r,| j	d d| _W d   n1 s9w   Y  |D ]}|V  q@| j	  }dur[|V  | j	  }dusO| j 5 d| _| jritd| j
 spJ | j
  | jg ks|J d| _	| j rJ | j  W d   dS 1 sw   Y  dS )a~  
        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:`ConcurrencyError` 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.
            ConcurrencyError: If two threads run :meth:`get` or :meth:`get_iter`
                concurrently.

        r"   r#   zqueue.SimpleQueue[Data | None]NTF)r   r   r'   r   r   r   r   queueSimpleQueuer   r   r*   putr1   r+   r   r-   )r   r   chunkr   r   r   get_iter|   s@   
	
"zAssembler.get_iterframer
   c                 C  s  | j  | jrtd| jrtd|jtu rtdd| _n|jt	u r'd| _n|jt
u s.J | jdur=| j|j|j}n|j}| jdu rL| j| n| j| |js^	 W d   dS | j reJ | j  | jduru| jd | j r|J d| _W d   n1 sw   Y  | j  | j " d| _| jrtd| j sJ | j  d| _W d   dS 1 sw   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, which can be achieved 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.
            ConcurrencyError: If two threads run :meth:`put` concurrently.

        r"   zput is already runningstrict)errorsNTF)r   r   r'   r   r   opcoder	   UTF8Decoderr   r   r   decodedatafinr   r   appendr5   r   r*   r-   r   r(   r+   )r   r8   r>   r   r   r   r5      sF   






*
"zAssembler.putc                 C  s   | j ; | jr	 W d   dS d| _| jr&| j  | jdur&| jd | jr6| j  W d   dS W d   dS 1 sAw   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   r5   r   r   r   r   r   r   close  s   

"zAssembler.close)r   r   )N)r    r!   r   r   )r   r2   )r8   r
   r   r   )	__name__
__module____qualname____doc__r   r1   r7   r5   rA   r   r   r   r   r      s    
&
>
AH)
__future__r   codecsr3   r   typingr   r   
exceptionsr   framesr   r   r	   r
   r   __all__getincrementaldecoderr<   r   r   r   r   r   <module>   s    
