
    6h                      % S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  SSKJ	r	  SSK
Jr  \R                  (       a  S SKrSSKJrJrJrJrJr  SSKJrJr  \R                  (       a  S S	KJr  \\\\\\   4   4   rS
rSrSrSrSr \\\ S.r!S\"S'   Sr#Sr$Sr%Sr&Sr'Sr(S
r)Sr*\#\$\%\&\'\(\)\*S.r+S\"S'   Sr,Sr-Sr.S
r/\,\-\.\/S.r0S\"S'   Sr1Sr2\1\2S.r3S\"S'   Sr4Sr5\4\5S.r6S\"S'   Sr7Sr8\7\8S.r9S\"S'   Sr:Sr;\:\;S.r<S\"S'   \#\0\$\3\%\6\&0 \'0 \(0 \)\9\*\<0r=S\"S '   \R|                  " S!5      r?SbS" jr@\R                  " S#S$9 " S% S&5      5       rB\R                  " S#S'S(9 " S) S*5      5       rC              ScS+ jrDSdS, jrESeS- jrFSfS. jrGS\S4       SgS0 jjrH      ShS2 jrI\4       SiS3 jjrJ      SjS4 jrK Sk       SlS5 jjrL          SmS6 jrM\4       SgS7 jjrN " S8 S95      rO " S: S;\5      rP " S< S=\5      rQ " S> S?\P5      rR " S@ SA\Q5      rS " SB SC\P5      rT " SD SE\Q5      rU " SF SG\P5      rV " SH SI\V5      rW " SJ SK\Q5      rX " SL SM\X5      rY\R                  (       a  \\R\T-  \W-     rZ\\S\U-  \Y-     r[0 r\SN\"SO'   / r]SP\"SQ'   SnSR jr^ " SS S/5      r_ " ST SU\_5      r` " SV S1\_5      raSoSW jrbSXSY/0rcSZ\"S['    " S\ S]5      rd " S^ S_5      re " S` Sa5      rfg)p    )annotationsN)ProtocolType   )_)attr)encodingerrorpycompatutilwireprototypes)cborutil
stringutil)Iterator   i         )s   stream-begins
   stream-ends   encodeddict[bytes, int]STREAM_FLAGS            	   )s   command-requests   command-datas   command-responses   error-responses   text-outputs   progresss   sender-protocol-settingss   stream-settingsFRAME_TYPES)s   new   continuations   mores	   have-dataFLAGS_COMMAND_REQUEST)r      eosFLAGS_COMMAND_DATAFLAGS_COMMAND_RESPONSEFLAGS_SENDER_PROTOCOL_SETTINGSFLAGS_STREAM_ENCODING_SETTINGSzdict[int, dict[bytes, int]]FRAME_TYPE_FLAGSz<HHc                    U R                  5        VVs0 s H  u  p#X2_M	     nnn/ nSnX:  a9  X-  (       a$  UR                  UR                  USU-  5      5        US-  nX:  a  M9  SR                  U5      $ s  snnf )zFConvert a numeric flags value to a human value, using a mapping table.r      <unknown 0x%02x>   |)itemsappendgetjoin)mappingvaluekvnamemapflagsvals          </usr/lib/python3/dist-packages/mercurial/wireprotoframing.py
humanflagsr3      sw     '0qtG0E
C
,;LLS*=*CDE	 ,
 99U 1s   A8T)slotsc                      \ rS rSrSr\R                  " \S9r\R                  " \S9r	\R                  " \S9r
\R                  " \S9r\R                  " \S9r\R                  " \S9rSrg)frameheader   z&Represents the data in a frame header.type N)__name__
__module____qualname____firstlineno____doc__r   ibintlength	requestidstreamidstreamflagstypeidr0   __static_attributes__r:       r2   r6   r6      sY    0WW#FS!IwwC H''s#KWW#FGGErH   r6   F)r4   reprc                      \ rS rSrSr\R                  " \S9r\R                  " \S9r	\R                  " \S9r
\R                  " \S9r\R                  " \S9r\R                  " \S9r\R                   S 5       rSrg)frame   zRepresents a parsed frame.r8   c                x   SU R                   -  n[        R                  5        H  u  p#X0R                   :X  d  M  Un  O   S[        U R                  5      U R
                  U R                  [        [        U R                  5      U[        [        R                  U R                   0 5      U R                  5      4-  $ )Nr%   sH   frame(size=%d; request=%d; stream=%d; streamflags=%s; type=%s; flags=%s))rF   r   r'   lenpayloadrC   rD   r3   r   rE   r#   r)   r0   )selftypenamenamer,   s       r2   __repr__frame.__repr__   s    &4&,,.KD# /" DLL!<)9)9:+//R@$**M		
rH   r:   N)r;   r<   r=   r>   r?   r   r@   rA   rC   rD   rE   rF   r0   bytesrO   r	   	strmethodrS   rG   r:   rH   r2   rK   rK      sq    $S!IwwC H''s#KWW#FGGEgg5!G
 
rH   rK   c                    [        [        [        U5      -   5      n[        R                  " S[        U5      5      nUSS USS& [        R
                  " SUSXU5        US-  U-  US'   XVSS& U$ )	z#Assemble a frame into a byte array.z<Ir   r   <HBBr   r   r   N)	bytearrayFRAME_HEADER_SIZErN   structpack	pack_into)rC   rD   rE   rF   r0   rO   rK   ls           r2   	makeframer_      su     '#g,67E 	D#g,'A1QE!AJ
VUAyKH!u$E!H!"ILrH   c           	        U R                  SS5      nUu  p#pEpg[        U5      n[        U5      nSnUR                  S5       H)  n	U	[        ;   a  U[        U	   -  nM  U[        U	5      -  nM+     U[        ;   a
  [        U   nO[        U5      nSn
[        U   nUR                  S5       H  n	X;   a	  XU	   -  n
M  U
[        U	5      -  n
M!     UR                  S5      (       a=  SR                  [        R                  " [        R                  " USS 5      5      5      nO[        R                  " U5      n[        UUUUU
US9$ )	a  Create a frame from a human readable string

Strings have the form:

    <request-id> <stream-id> <stream-flags> <type> <flags> <payload>

This can be used by user-facing applications and tests for creating
frames easily without having to type out a bunch of constants.

Request ID and stream IDs are integers.

Stream flags, frame type, and flags can be specified by integer or
named constant.

Flags can be delimited by `|` to bitwise OR them together.

If the payload begins with ``cbor:``, the following string will be
evaluated as Python literal and the resulting object will be fed into
a CBOR encoder. Otherwise, the payload is interpreted as a Python
byte string literal.
    r   r   r&   s   cbor:rH   N)rC   rD   rE   rF   r0   rO   )splitrA   r   r   r#   
startswithr*   r   streamencoder   evalpythonliteralunescapestrr_   )sfieldsrC   rD   rE   	frametype
frameflagsrO   finalstreamflagsflag
finalflags
validflagss               r2   makeframefromhumanstringro      sG   , WWT1FGMDIII8}H!!$'<T 22D	)	 ( K	*		N	J!),J  &T**J#d)#J	 ' (##((!!*">">wqr{"KL

 ((1$ rH   c                    U S   SU S   -  -   SU S   -  -   n[         R                  " SU S5      u  p#nU S   nUS	-  S
-	  nUS-  n[        XX4Xg5      $ )zParse a unified framing protocol frame header from a buffer.

The header is expected to be in the buffer at offset 0 and the
buffer is expected to be large enough to hold a full header.
r      r   i @  r   rX   r   r      r      )r[   unpack_fromr6   )dataframelengthrC   rD   rE   	typeflagsri   rj   s           r2   parseheaderrx   '  sv     q'C$q'M)EDGO;K'-'9'9&$'J$IQIT!a'IT!Jy rH   c                   [        [        5      nU R                  U5      nUS:X  a  gU[        :w  a#  [        R                  " [        S5      X!4-  5      e[        U5      nU R                  UR                  5      n[        U5      UR                  :w  a7  [        R                  " [        S5      UR                  [        U5      4-  5      e[        UR                  UR                  UR                  UR                  UR                  U5      $ )zRead a unified framing protocol frame from a file object.

Returns a 3-tuple of (type, flags, payload) for the decoded frame or
None if no frame is available. May raise if a malformed frame is
seen.
r   Ns+   received incomplete frame: got %d bytes: %ss'   frame length error: expected %d; got %d)rY   rZ   readintor
   Abortr   rx   readrB   rN   rK   rC   rD   rE   rF   r0   )fhheader	readcounthrO   s        r2   	readframer   @  s     ()FF#IA~%%kk<=!"
 	

 	FAggahhG
7|qxxkk89xxW&'
 	

 	QZZ!''7 rH   streamc              #    #    SU0nU(       a  X7S'   U(       a  XgS'   SR                  [        R                  " U5      5      nSn Sn	U(       d
  U	[        -  n	O	U	[        -  n	U(       a	  U	[
        -  n	XxX-    n
U[        U
5      -  n[        U
5      U:X  a  U[        U5      :  a	  U	[        -  n	U R                  U[        U	U
S9v   U	[        -  (       d  OM  U(       as   UR                  [        5      nSn[        U5      [        :X  a  [        n	O[        n	UR                  S	5      S:X  d   eSnU R                  U[        U	US9v   U(       a  g
Mr  g
7f)zCreate frames necessary to transmit a request to run a command.

This is a generator of bytearrays. Each item represents a frame
ready to be sent over the wire to a peer.
   name   args   redirectrH   r   TrC   rF   r0   rO   Fr   N)r*   r   rd   FLAG_COMMAND_REQUEST_NEW!FLAG_COMMAND_REQUEST_CONTINUATION FLAG_COMMAND_REQUEST_EXPECT_DATArN    FLAG_COMMAND_REQUEST_MORE_FRAMESr_   FRAME_TYPE_COMMAND_REQUESTr|   DEFAULT_MAX_FRAME_SIZEFLAG_COMMAND_DATA_CONTINUATIONFLAG_COMMAND_DATA_EOSFRAME_TYPE_COMMAND_DATA)r   rC   cmdargsdatafhmaxframesizeredirectru   offsetr0   rO   dones               r2   createcommandframesr   b  sn     S>DW$[88H))$/0DF
 --E66E 55E 56#g,w<<'FSY,>55E-	  
 	
 887 : ;;56DD4y226-{{1~,,,""#.	 #   '  s   EEoutputstreamc                    SR                  [        R                  " SS05      5      nU R                  (       a  U R	                  U5      nSnU(       d  g OSnU R                  U[        [        UUS9$ )NrH      statuss   okTFrC   rF   r0   rO   encoded)r*   r   rd   streamsettingssentencoder_   FRAME_TYPE_COMMAND_RESPONSE"FLAG_COMMAND_RESPONSE_CONTINUATION)r   rC   overallr   s       r2   createcommandresponseokframer     sw     hhx,,i-?@AG  --(  *0   rH   c              #     #    U R                  5       nSn X4XB-    nU[        U5      -  nU[        U5      :H  nU(       a  [        nO[        nU R	                  U[
        UUUS:g  S9v   U(       a  gM\  7f)zACreate an empty payload frame representing command end-of-stream.r   rH   r   N)flushrN   FLAG_COMMAND_RESPONSE_EOSr   r_   r   )r   rC   r   rO   r   chunkr   r0   s           r2   createcommandresponseeosframesr     s      llnGF
!67#e*W%-E6E.sN  
 	
 ) s   A0A2c                p   SUR                   UR                  S.S.nS H.  n[        X$5      nUc  M  XSS   [        R                  " U5      '   M0     SR                  [        R                  " U5      5      nU R                  (       a  U R                  U5      nSnOSnU R                  U[        [        UUS	9$ )
Nr   )s   urls	   mediatype)r      location)size
fullhashesfullhashseedserverdercertsservercadercertsr   rH   TFr   )url	mediatypegetattrr   bytestrr*   r   rd   r   r   r_   r   r   )r   rC   locationru   ar,   rO   r   s           r2   $createalternatelocationresponseframer     s     LL",,
D $5:h..q12 hhx,,T23G  --(*0   rH   c              #     #    SSU0S.nU(       a  X4S   S'   SR                  [        R                  " U5      5      nU R                  U[        [
        US9v   g 7f)N   error   message)r   r   r   rH   r   )r*   r   rd   r_   r   r   )r   rC   messager   mr   s         r2   createcommanderrorresponser     si      
	A #(Ghhx,,Q/0G


*'	   s   AAc              #     #    [        U5      [        ::  d   eSR                  [        R                  " USU0/S.5      5      nU R                  U[        SUS9v   g 7f)NrH      msg)   typer   r   r   )rN   r   r*   r   rd   r_   FRAME_TYPE_ERROR_RESPONSE)r   rC   msgerrtyperO   s        r2   createerrorframer   (  sm      s8----hh $c]O	
G 

(	   s   AAc              #  *  #    / nU GH%  u  pVn[        U[        5      (       d  [        S5      eU H#  n[        U[        5      (       a  M  [        S5      e   U H#  n	[        U	[        5      (       a  M  [        S5      e   UR                  SS5      R	                  S5      nU V
s/ s H#  oR                  SS5      R	                  S5      PM%     nn
U Vs/ s H#  oR                  SS5      R	                  S5      PM%     nnSU0nU(       a  XlS	'   U(       a  X|S
'   UR                  U5        GM(     SR                  [        R                  " U5      5      n[        U5      U:  a  [        S5      eU R                  U[        SUS9v   gs  sn
f s  snf 7f)ar  Create a text output frame to render text to people.

``atoms`` is a 3-tuple of (formatting string, args, labels).

The formatting string contains ``%s`` tokens to be replaced by the
corresponding indexed entry in ``args``. ``labels`` is an iterable of
formatters to be applied at rendering time. In terms of the ``ui``
class, each atom corresponds to a ``ui.write()``.
s!   must use bytes formatting stringss   must use bytes for argumentss   must use bytes for labelsasciireplacezutf-8strictr   r   s   labelsrH   s$   cannot encode data in a single framer   r   N)
isinstancerU   
ValueErrordecoder   r(   r*   r   rd   rN   r_   FRAME_TYPE_TEXT_OUTPUT)r   rC   atomsr   	atomdicts
formattingr   labelsarglabelr   r^   atomrO   s                 r2   createtextoutputframer   ?  s     I$) 
& *e,,ABBCc5)) !@AA  EeU++ !=>> 
  &&w	:AA'J
 GKKd),33G<dK HNNv!((7H-44W=vN
# M$O7 %*: hhx,,Y78G
7|l"@AA


%	   % L Os+   AF
%F32F%*F	F*F?BFc                  >    \ rS rSrSr\4     SS jjrS rS rSr	g)	bufferingcommandresponseemitteriz  a  Helper object to emit command response frames intelligently.

Raw command response data is likely emitted in chunks much smaller
than what can fit in a single frame. This class exists to buffer
chunks until enough data is available to fit in a single frame.

TODO we'll need something like this when compression is supported.
So it might make sense to implement this functionality at the stream
level.
c                D    Xl         X l        X0l        / U l        SU l        g Nr   )_stream
_requestid_maxsize_chunks_chunkssize)rP   r   rC   r   s       r2   __init__(bufferingcommandresponseemitter.__init__  s"     #$rH   c              #  
  #    Uc  U R                  5        Sh  vN   gU R                  R                  U5      nU(       d  g[        U5      U R                  :  a|  U R                  5        Sh  vN   Sn XX R                  -    nU[        U5      -  nU R                  R                  U R                  [        [        USS9v   U[        U5      :X  a  gMa  [        U5      U R                  -   U R                  :  a:  U R                  R                  U5        U =R                  [        U5      -  sl	        gU R                  5        Sh  vN   U R                  R                  U5        [        U5      U l	        g GNb GN N67f)zSend new data for emission.

Is a generator of new frames that were derived from the new input.

If the special input ``None`` is received, flushes all buffered
data to frames.
Nr   TrF   r0   rO   r   )_flushr   r   rN   r   r_   r   r   r   r   r   r(   )rP   ru   r   r   s       r2   send$bufferingcommandresponseemitter.send  sM     <{{}$$||""4( t9t}}${{}$$ Ff}}&<=#e*$ll,,OO6<!  -   SY& " t9t'''$--7LL%D	)
 ;;=  D!t9k %* %: 	!s5   FE;AF.E>/CF
F1F>FFc              #    #    SR                  U R                  5      n[        U5      U R                  ::  d   e/ U R                  S S & SU l        U(       d  g U R
                  R                  U R                  [        [        USS9v   g 7f)NrH   r   Tr   )
r*   r   rN   r   r   r   r_   r   r   r   )rP   rO   s     r2   r   &bufferingcommandresponseemitter._flush  sw     ((4<<(7|t}},,,Qll$$OO.4 % 
 	
s   BB	)r   r   r   r   r   N)rC   rA   r   rA   returnNone)
r;   r<   r=   r>   r?   r   r   r   r   rG   r:   rH   r2   r   r   z  s5    	 ;Q!$47	?%B
rH   r   c                      \ rS rSrSr\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r	Sr
g)	Encoderi  9A protocol class for the various encoder implementations.c                    [         eNNotImplementedErrorrP   ru   s     r2   r   Encoder.encode      !!rH   c                    [         er   r   rP   s    r2   r   Encoder.flush  r   rH   c                    [         er   r   r   s    r2   finishEncoder.finish  r   rH   r:   Nr   rU   )r;   r<   r=   r>   r?   abcabstractmethodr   r   r   rG   r:   rH   r2   r   r     sQ    C" " 	" " 	" "rH   r   c                  @    \ rS rSrSr\R                  SS j5       rSrg)Decoderi  r   c                    [         er   r   r   s     r2   r   Decoder.decode  r   rH   r:   Nr   )	r;   r<   r=   r>   r?   r   r   r   rG   r:   rH   r2   r   r     s    C" "rH   r   c                  @    \ rS rSrSrS	S jrS
S jrS
S jrS
S jrSr	g)identityencoderi  z3Encoder for the "identity" stream encoding profile.c                    g r   r:   )rP   uis     r2   r   identityencoder.__init__  s    rH   c                    U$ r   r:   r   s     r2   r   identityencoder.encode      rH   c                    gNrH   r:   r   s    r2   r   identityencoder.flush      rH   c                    gr  r:   r   s    r2   r   identityencoder.finish  r
  rH   r:   Nr   r   r   )
r;   r<   r=   r>   r?   r   r   r   r   rG   r:   rH   r2   r   r     s    =rH   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	identitydecoderi  z3Decoder for the "identity" stream encoding profile.c                P    U(       a  [         R                  " [        S5      5      eg )Ns6   identity decoder received unexpected additional values)r
   r{   r   )rP   r  	extraobjss      r2   r   identitydecoder.__init__  s$    ++KL  rH   c                    U$ r   r:   r   s     r2   r   identitydecoder.decode  r  rH   r:   Nr  r   )r;   r<   r=   r>   r?   r   r   rG   r:   rH   r2   r  r    s    =rH   r  c                  <    \ rS rSrSS jrS	S jrS	S jrS	S jrSrg)
zlibencoderi  c                B    SS K nX l        UR                  5       U l        g r   )zlib_zlibcompressobj_compressor)rP   r  r  s      r2   r   zlibencoder.__init__  s    
++-rH   c                8    U R                   R                  U5      $ r   r  compressr   s     r2   r   zlibencoder.encode#      ((..rH   c                `    U R                   R                  U R                  R                  5      $ r   )r  r   r  Z_SYNC_FLUSHr   s    r2   r   zlibencoder.flush&  s%     %%djj&=&=>>rH   c                r    U R                   R                  U R                  R                  5      nS U l         U$ r   )r  r   r  Z_FINISHrP   ress     r2   r   zlibencoder.finish+  s/    $$TZZ%8%89
rH   )r  r  Nr  r   	r;   r<   r=   r>   r   r   r   r   rG   r:   rH   r2   r  r    s    ./?
rH   r  c                  (    \ rS rSrSS jrSS jrSrg)zlibdecoderi1  c                    SS K nU(       a  [        R                  " [        S5      5      eUR	                  5       U l        g )Nr   s2   zlib decoder received unexpected additional values)r  r
   r{   r   decompressobj_decompressor)rP   r  r  r  s       r2   r   zlibdecoder.__init__2  s5    ++GH  "//1rH   c                8    U R                   R                  U5      $ r   r/  
decompressr   s     r2   r   zlibdecoder.decode<      !!,,T22rH   r/  Nr  r   r;   r<   r=   r>   r   r   rG   r:   rH   r2   r,  r,  1  s    23rH   r,  c                  <    \ rS rSrSS jrS	S jrS	S jrS	S jrSrg)
zstdbaseencoderi@  c                d    SSK Jn  X l        UR                  US9nUR	                  5       U l        g )Nr   zstd)level) r<  _zstdZstdCompressorr  r  )rP   r=  r<  cctxs       r2   r   zstdbaseencoder.__init__A  s.    
"""/++-rH   c                8    U R                   R                  U5      $ r   r  r   s     r2   r   zstdbaseencoder.encodeH  r!  rH   c                `    U R                   R                  U R                  R                  5      $ r   )r  r   r?  COMPRESSOBJ_FLUSH_BLOCKr   s    r2   r   zstdbaseencoder.flushK  s%     %%djj&H&HIIrH   c                r    U R                   R                  U R                  R                  5      nS U l         U$ r   )r  r   r?  COMPRESSOBJ_FLUSH_FINISHr'  s     r2   r   zstdbaseencoder.finishQ  s/    $$TZZ%H%HI
rH   )r  r?  N)r=  rA   r   r   r   r*  r:   rH   r2   r9  r9  @  s    ./JrH   r9  c                  ,   ^  \ rS rSrSU 4S jjrSrU =r$ )zstd8mbencoderiW  c                $   > [         TU ]  S5        g )Nr   )superr   )rP   r  	__class__s     r2   r   zstd8mbencoder.__init__X  s    rH   r:   r  r;   r<   r=   r>   r   rG   __classcell__rO  s   @r2   rL  rL  W  s     rH   rL  c                  (    \ rS rSrSS jrSS jrSrg)zstdbasedecoderi\  c                X    SSK Jn  UR                  US9nUR                  5       U l        g )Nr   r;  )max_window_size)r>  r<  ZstdDecompressorr.  r/  )rP   maxwindowsizer<  dctxs       r2   r   zstdbasedecoder.__init__]  s)    $$]$C!//1rH   c                8    U R                   R                  U5      $ r   r2  r   s     r2   r   zstdbasedecoder.decodec  r5  rH   r6  N)rY  rA   r   r   r   r7  r:   rH   r2   rU  rU  \  s    23rH   rU  c                  ,   ^  \ rS rSrSU 4S jjrSrU =r$ )zstd8mbdecoderig  c                l   > U(       a  [         R                  " [        S5      5      e[        TU ]  SS9  g )Ns5   zstd8mb decoder received unexpected additional valuesi   )rY  )r
   r{   r   rN  r   )rP   r  r  rO  s      r2   r   zstd8mbdecoder.__init__h  s2    ++JK  	{3rH   r:   r  rQ  rS  s   @r2   r_  r_  g  s    4 4rH   r_  z&dict[bytes, tuple[EncoderT, DecoderT]]STREAM_ENCODERSzlist[bytes]STREAM_ENCODERS_ORDERc                 d   [         (       a  g  SSKJn   U R                    U (       a(  [
        [        4[         S'   [        R                  S5        [        [        4[         S'   [        R                  S5        [        [        4[         S'   [        R                  S5        g ! [         a    S n  Nf = f)Nr   r;  s   zstd-8mbs   zlib   identity)rb  r>  r<  __version__ImportErrorrL  r_  rc  r(   r  r,  r   r  r;  s    r2   populatestreamencodersrh  }  s    
 (6'G$$$[1 +[9OG  )$3_#EOK   -  s   B   B/.B/c                  Z    \ rS rSr% SrS\S'   S\S'   SSS jjr          SS jrS	rg
)r   i  z5Represents a logical unidirectional series of frames.rA   rD   bool_activec                    Xl         X l        g r   )rD   rk  )rP   rD   actives      r2   r   stream.__init__  s     rH   c                v    SnU R                   (       d  U[        -  nSU l         [        XR                  XRX45      $ )mCreate a frame to be sent out over this stream.

Only returns the frame instance. Does not actually send it.
r   T)rk  STREAM_FLAG_BEGIN_STREAMr_   rD   )rP   rC   rF   r0   rO   rE   s         r2   r_   stream.makeframe  s:     ||33KDL}}k5
 	
rH   )rk  rD   NFrD   rA   rm  rj  r   r   )
rC   rA   rF   rA   r0   rA   rO   rU   r   rY   )	r;   r<   r=   r>   r?   __annotations__r   r_   rG   r:   rH   r2   r   r     sA    ?MM

&)
25
@E
	
rH   c                  ^   ^  \ rS rSr% SrS\S'   S
SU 4S jjjrSS jrSS jrSS jr	S	r
U =r$ )inputstreami  z,Represents a stream used for receiving data.zDecoder | None_decoderc                .   > [         TU ]  XS9  S U l        g )Nrm  )rN  r   rx  rP   rD   rm  rO  s      r2   r   inputstream.__init__  s    1rH   c                    U[         ;  a"  [        R                  " [        S5      U-  5      e[         U   S   " X5      U l        g)zSet the decoder for this stream.

Receives the stream profile name and any additional CBOR objects
decoded from the stream encoding settings frame payloads.
s   unknown stream decoder: %sr   N)rb  r
   r{   r   rx  )rP   r  rR   r  s       r2   
setdecoderinputstream.setdecoder  s<     &++a =>EFF'-a0?rH   c                ^    U R                   (       d  U$ U R                   R                  U5      $ r   )rx  r   r   s     r2   r   inputstream.decode  s%     }}K}}##D))rH   c                Z    U R                   (       d  gU R                   R                  5       $ r  )rx  r   r   s    r2   r   inputstream.flush  s!    }} }}""$$rH   )rx  rs  rt  rR   rU   r   r   r   )r;   r<   r=   r>   r?   ru  r   r~  r   r   rG   rR  rS  s   @r2   rw  rw    s+    6 	@*% %rH   rw  c                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   SSU 4S	 jjjrSS
 jrSS jrSS jr	S r
 S           SS jjrSS jrSrU =r$ )r   i  z*Represents a stream used for sending data.rj  r   zEncoder | None_encoderzbytes | None_encodernamec                J   > [         TU ]  XS9  SU l        S U l        S U l        g )Nrz  F)rN  r   r   r  r  r{  s      r2   r   outputstream.__init__  s*    1"' rH   c                    U[         ;  a"  [        R                  " [        S5      U-  5      e[         U   S   " U5      U l        X l        g)zDSet the encoder for this stream.

Receives the stream profile name.
s   unknown stream encoder: %sr   N)rb  r
   r{   r   r  r  )rP   r  rR   s      r2   
setencoderoutputstream.setencoder  sB    
 &++a =>EFF'-a04 rH   c                ^    U R                   (       d  U$ U R                   R                  U5      $ r   )r  r   r   s     r2   r   outputstream.encode  s#    }}K}}##D))rH   c                Z    U R                   (       d  gU R                   R                  5       $ r  )r  r   r   s    r2   r   outputstream.flush  s    }}}}""$$rH   c                \    U R                   (       d  gU R                   R                  5         g r  )r  r   r   s    r2   r   outputstream.finish  s    }}rH   c                "   SnU R                   (       d  U[        -  nSU l         U(       a0  U R                  (       d  [        R                  " S5      eU[
        -  nU[        :X  a  U[        -  (       a  SU l        [        XR                  XbX45      $ )rp  r   Ts@   attempting to send encoded frame without sending stream settings)
rk  rq  r   r
   ProgrammingErrorSTREAM_FLAG_ENCODING_APPLIEDFRAME_TYPE_STREAM_SETTINGS!FLAG_STREAM_ENCODING_SETTINGS_EOSr_   rD   )rP   rC   rF   r0   rO   r   rE   s          r2   r_   outputstream.makeframe  s     ||33KDL**,,  
 77K 0099&*D#}}k5
 	
rH   c                    U R                   (       a  U R                  (       a  gSR                  [        R                  " U R
                  5      5      nU R                  U[        [        U5      $ )zCreate a stream settings frame for this stream.

Returns frame data or None if no stream settings frame is needed or has
already been sent.
NrH   )	r  r   r*   r   rd   r  r_   r  r  )rP   rC   rO   s      r2   makestreamsettingsframe$outputstream.makestreamsettingsframe#  sR     }} 7 7((8001B1BCD~~&-	
 	
rH   )rk  r  r  r   rs  rt  r  r   )rC   rA   rF   rA   r0   rA   rO   rU   r   rj  r   rY   )rC   rA   r   bytearray | None)r;   r<   r=   r>   r?   ru  r   r  r   r   r   r_   r  rG   rR  rS  s   @r2   r   r     s    4! !	!*% "
"
 "
 	"

 "
 "
 
"
H
 
rH   c                r    U R                   S-  (       a#  [        R                  " SU R                   -  5      eg )Nr   sA   server should only write to even numbered streams; %d is not even)rD   r
   r  )r   s    r2   ensureserverstreamr  5  s8    $$028//B
 	
 rH      contentencodingsre  zdict[bytes, list[bytes]]DEFAULT_PROTOCOL_SETTINGSc                     \ rS rSr% SrS\S'   SSS jjrSS jr    SS jrSS jr	    SS	 jr
        SS
 jr S        S!S jjrS"S jrS#S jrS$S jrS%S jrSS jrSS jrSS jrSS jrSS jrSS jrSS jrSrg)&serverreactoriB  a~
  Holds state of a server handling frame-based protocol requests.

This class is the "brain" of the unified frame-based protocol server
component. While the protocol is stateless from the perspective of
requests/commands, something needs to track which frames have been
received, what frames to expect, etc. This class is that thing.

Instances are modeled as a state machine of sorts. Instances are also
reactionary to external events. The point of this class is to encapsulate
the state of the connection and the exchange of frames, not to perform
work. Instead, callers tell this class when something occurs, like a
frame arriving. If that activity is worthy of a follow-up action (say
*run a command*), the return value of that handler will say so.

I/O and CPU intensive operations are purposefully delegated outside of
this class.

Consumers are expected to tell instances when events occur. They do so by
calling the various ``on*`` methods. These methods return a 2-tuple
describing any follow-up action(s) to take. The first element is the
name of an action to perform. The second is a data structure (usually
a dict) specific to that action that contains more information. e.g.
if the server wants to send frames back to the client, the data structure
will contain a reference to those frames.

Valid actions that consumers can be instructed to take are:

sendframes
   Indicates that frames should be sent to the client. The ``framegen``
   key contains a generator of frames that should be sent. The server
   assumes that all frames are sent to the client.

error
   Indicates that an error occurred. Consumer should probably abort.

runcommand
   Indicates that the consumer should run a wire protocol command. Details
   of the command to run are given in the data structure.

wantframe
   Indicates that nothing of interest happened and the server is waiting on
   more frames from the client before anything interesting can be done.

noop
   Indicates no additional action is required.

Known Issues
------------

There are no limits to the number of partially received commands or their
size. A malicious client could stream command request data and exhaust the
server's memory.

Partially received commands are not acted upon when end of input is
reached. Should the server error if it receives a partial request?
Should the client send a message to abort a partially transmitted request
to facilitate graceful shutdown?

Active requests that haven't been responded to aren't tracked. This means
that if we receive a command and instruct its dispatch, another command
with its request ID can come in over the wire and there will be a race
between who responds to what.
zlist[Iterator[bytearray]]_bufferedframegensc                    Xl         X l        SU l        SU l        / U l        0 U l        0 U l        0 U l        [        5       U l	        SU l
        [        [        5      U l        [        5         g)a  Construct a new server reactor.

``deferoutput`` can be used to indicate that no output frames should be
instructed to be sent until input has been exhausted. In this mode,
events that would normally generate output frames (such as a command
response being ready) will instead defer instructing the consumer to
send those frames. This is useful for half-duplex transports where the
sender cannot receive until all data has been transmitted.
   initialr   N)_ui_deferoutput_state_nextoutgoingstreamidr  _incomingstreams_outgoingstreams_receivingcommandsset_activecommands_protocolsettingsdecoderdictr  _sendersettingsrh  )rP   r  deferoutputs      r2   r   serverreactor.__init__  sh     ' %&""$ " ""$  #u(,%  $$=> rH   c                Z   UR                   S-  (       d.  SU l        U R                  [        S5      UR                   -  5      $ UR                   U R                  ;  ae  UR
                  [        -  (       d!  SU l        U R                  [        S5      5      $ [        UR                   5      U R                  UR                   '   UR
                  [        -  (       a  SU l        [        R                  " S5      eUR
                  [        -  (       a  U R                  UR                   	 U R                  U R                  U R                  U R                  U R                   S.nUR#                  U R                  5      nU(       d#  [        R                  " SU R                  -  5      eU" U5      $ )zProcess a frame that has been received off the wire.

Returns a dict with an ``action`` key that details what action,
if any, the consumer should take next.
r      erroreds/   received frame with even numbered stream ID: %dsN   received frame on unknown inactive stream without beginning of stream flag sets8   support for decoding stream payloads not yet implemented)r     protocol-settings-receiving   idle   command-receivingr  s   unhandled state: %s)rD   r  _makeerrorresultr   r  rE   rq  rw  r  r
   r  STREAM_FLAG_END_STREAM_onframeinitial_onframeprotocolsettings_onframeidle_onframecommandreceiving_onframeerroredr)   )rP   rK   handlersmeths       r2   onframerecvserverreactor.onframerecv  sj    ~~!$DK((DE..! 
 >>!6!66$$'??(,,8  5@4OD!!%..1;;$DK((K  55%%enn5 ,,,0,I,I&&"&"?"?,,
 ||DKK((()?$++)MNNE{rH   c                   ^ ^^^ [        T5        [        T[        5      (       a  [        T5      mUUU U4S jnT R	                  U" 5       5      $ )zSignal that objects are ready to be sent to the client.

``objs`` is an iterable of objects (typically a generator) that will
be encoded via CBOR and added to frames, which will be sent to the
client.
c            	   3    >#    Sn Sn[        T
T5      n  [        T5      n [        U[        R                   5      (       aN  U (       a  [
        R"                  " S5      eT
R%                  T5      nU(       a  Uv   ['        T
TU5      v   SnSn M{  U(       a  [
        R"                  " S5      eU (       d5  T
R%                  T5      nU(       a  Uv   [)        T
T5      nU(       a  Uv   Sn [        U[        R*                  5      (       a'  UR                  UR,                  5       H  nUv   M	     O[        U[        R.                  5      (       aD  [0        R2                  " UR4                  5       H  nUR                  U5       H  nUv   M	     M!     O:[0        R6                  " U5       H  nUR                  U5       H  nUv   M	     M!      GM  ! [         a=    UR                  S 5       H  nUv   M	     U (       a  [	        T
T5       H  nUv   M	      O[
        R                   a7  n[        T
TUR                  UR                  5       H  nUv   M	      S nAOyS nAf[         a8  n[        T
TS[        R                  " U5      -  SS9 H  nUv   M	      S nAO9S nAff = f! [         a$  n[        T
TSU-  SS9 H  nUv   M	      S nAOS nAff = fT	R8                  R;                  T5        g 7f)NFTs   %s   serverr   s:   alternatelocationresponse seen after initial output objects(   object follows alternatelocationresponse)r   nextStopIterationr   r   r
   WireprotoCommandErrorr   r   messageargs	Exceptionr   r   forcebytestrr   r   alternatelocationresponser  r  r   r   encodedresponseru   indefinitebytestringresponser   streamencodebytestringfromiterchunksrd   r  remove)emittedalternatelocationsentemitterorK   er   objsrC   rP   r   s          r2   
sendframes?serverreactor.oncommandresponsereadyobjects.<locals>.sendframes  s    G$)!5fiHGT
A<G "!^%M%MNN""'"8"8!1# 
 !' > >y I "'KB"Iq  15-"& ,#44G  # & > >y I "'K !=VY O "'K"& "!^%C%CDD%,\\!&&%9E"'K &: $>FF  &.%L%LHH&E *1e)<&+ *=& &.%:%:1%=E)0e)<&+ *= &>{  % 	!(d!3# "4 %C"I&E #(K& 22 !;	199amm" $"   	!1!
 7 7 :: )	" $" 	X ! !1	519i" $"
    ''	2s   K.G A+J! K.BJ! -K..A"J! K.9J! 
K.AJK.J'-IK.J&.JK.JK.!
K+K
K.
KK.)r  r   listiter_handlesendframes)rP   r   rC   r  r  s   ```` r2   oncommandresponsereadyobjects+serverreactor.oncommandresponsereadyobjects  sG     	6" dD!!:Dm	3 m	3^ %%jl33rH   c                r   ^  T R                   (       a  T R                  (       d  S0 4$ U 4S jnSSU" 5       04$ )zySignals that end of input has been received.

No more frames will be received. All pending activity should be
completed.
   noopc               3  L   >#    TR                    H  n U  S h  vN   M     g  N	7fr   )r  )genrP   s    r2   makegen)serverreactor.oninputeof.<locals>.makegeno  s      .. /s   $"
$
   sendframes   framegen)r  r  )rP   r  s   ` r2   
oninputeofserverreactor.oninputeofc  s@       (?(?B;	 
 
 	
rH   c                n    U R                   (       a  U R                  R                  U5        S0 4$ SSU04$ )Nr  r  r  )r  r  r(   )rP   framegens     r2   r  serverreactor._handlesendframesw  sB     ##**84B; X#  rH   c                ^   ^ ^^^ [        T5        UUU U4S jnT R                  U" 5       5      $ )Nc               3  p   >#    [        TTT SS9 S h  vN   TR                  R                  T5        g  N 7f)Nr  r  )r   r  r  )r   rC   rP   r   s   r2   r  /serverreactor.onservererror.<locals>.sendframes  s<     '	3	     ''	2	s   64!6r  r  )rP   r   rC   r   r  s   ```` r2   onservererrorserverreactor.onservererror  s,     	6"	3 	3 %%jl33rH   Nc                b   ^ ^^^^ [        T5        UUUU U4S jnT R                  U" 5       5      $ )zACalled when a command encountered an error before sending output.c               3  t   >#    [        TTTT 5       S h  vN   TR                  R                  T5        g  N 7fr   )r   r  r  )r   r   rC   rP   r   s   r2   r  0serverreactor.oncommanderror.<locals>.sendframes  s<     1	7D     ''	2	s   86!8r  )rP   r   rC   r   r   r  s   ````` r2   oncommanderrorserverreactor.oncommanderror  s,     	6"	3 	3 %%jl33rH   c                    U R                   nU =R                   S-  sl         [        U5      nX R                  U'   [         H4  nX0R                  S   ;   d  M  UR                  U R                  U5          U$    U$ )zCreate a stream to be used for sending data to the client.

If this is called before protocol settings frames are received, we
don't know what stream encodings are supported by the client and
we will default to identity.
r   r  )r  r   r  rc  r  r  r  )rP   rD   rg   rR   s       r2   makeoutputstreamserverreactor.makeoutputstream  sw     --""a'""*+h' *D++,?@@TXXt, *
 rH   c                    SSU04$ )Nr   r   r:   )rP   r   s     r2   r  serverreactor._makeerrorresult  s    
 
 	
rH   c                ~   U R                   U   nUS   (       d  SU l        [        R                  " S5      eU R                   U	 U R                   (       a  SU l        OSU l        US   R	                  S5        [
        R                  " US   R                  5       5      S   nSU;  a!  SU l        U R                  [        S	5      5      $ S
U;  a  0 US
'   XR                  ;  d   eU R                  R                  U5        SUUS   US
   UR                  S5      US   (       a  US   R                  5       S.4$ S S.4$ )N   requestdoner  s,   should not be called without requestdone setr  r     payloadr   r   s$   command request missing "name" fieldr   s
   runcommandr      data)s	   requestids   commandr   r   r  )r  r  r
   r  seekr   	decodeallgetvaluer  r   r  addr)   )rP   rC   entryrequests       r2   _makeruncommandresult#serverreactor._makeruncommandresult  sZ   ''	2^$$DK((?  ##I."".DK!DK 	jq!$$U:%6%?%?%AB1E'!$DK((9:  '!!GG 4 4444  + '#G, )$[[56;Gnw002	
 		
 KO	
 		
rH   c                "    SSU R                   04$ )Ns	   wantframes   state)r  r   s    r2   _makewantframeresult"serverreactor._makewantframeresult  s    dkk
 
 	
rH   c                   UR                   [        -  nUR                   [        -  nU(       a(  U(       a!  SU l        U R	                  [        S5      5      $ U(       d)  U(       d!  SU l        U R	                  [        S5      5      $ g g )Nr  sG   received command request frame with both new and continuation flags setsJ   received command request frame with neither new nor continuation flags set)r0   r   r   r  r  r   )rP   rK   newcontinuations       r2   _validatecommandrequestframe*serverreactor._validatecommandrequestframe  s    kk44{{%FF<$DK((.  <$DK((.  (srH   c                B   UR                   [        :X  a2  SU l        [        R                  " 5       U l        U R                  U5      $ UR                   [        :X  a  SU l        U R                  U5      $ SU l        U R                  [        S5      UR                   -  5      $ )Nr  r  r  sB   expected sender protocol settings or command request frame; got %d)rF   #FRAME_TYPE_SENDER_PROTOCOL_SETTINGSr  r   bufferingdecoderr  r  r   r  r  r   rP   rK   s     r2   r  serverreactor._onframeinitial   s    <<>>8DK,4,E,E,GD)0077\\77!DK$$U++ %DK((% ,,	 rH   c                T   U R                   S:X  d   eU R                  c   eUR                  [        :w  a.  SU l         U R	                  [        S5      UR                  -  5      $ UR                  [        -  nUR                  [        -  nU(       a(  U(       a!  SU l         U R	                  [        S5      5      $ U(       d(  U(       d!  SU l         U R	                  [        S5      5      $  U R                  R                  UR                  5        U(       a  U R                  5       $ U(       d   eU R                  R                  5       nS U l        U(       d!  SU l         U R	                  [        S5      5      $ [!        U5      S:  a!  SU l         U R	                  [        S	5      5      $ US
   nSU;   a  US   U R"                  S'   SU l         U R                  5       $ ! [         aB  nSU l         U R	                  [        S5      [        R                  " U5      -  5      s S nA$ S nAff = f)Nr  r  s/   expected sender protocol settings frame; got %dsX   sender protocol settings frame cannot have both continuation and end of stream flags setsO   sender protocol settings frame must have continuation or end of stream flag sets;   error decoding CBOR from sender protocol settings frame: %ss8   sender protocol settings frame did not contain CBOR datar   s=   sender protocol settings frame contained multiple CBOR valuesr   r  r  )r  r  rF   r  r  r   r0   *FLAG_SENDER_PROTOCOL_SETTINGS_CONTINUATION!FLAG_SENDER_PROTOCOL_SETTINGS_EOSr   rO   r  r   r  r  getavailablerN   r  )rP   rK   moreeosr  decodedds          r2   r  &serverreactor._onframeprotocolsettings  s   {{<<<<,,888<<>>$DK((DE,, 
 {{GGkk==C$DK((@  C$DK((. 		))00? ,,..
s//<<>(,%$DK((MN  \A$DK((  AJ!#89:M8ND  !45((**M  	$DK((R ))!,- 	s   )%G 
H'%7H"H'"H'c                   UR                   [        :w  a.  SU l        U R                  [	        S5      UR                   -  5      $ U R                  U5      nU(       a  U$ UR                  U R                  ;   a.  SU l        U R                  [	        S5      UR                  -  5      $ UR                  U R                  ;   a.  SU l        U R                  [	        S5      UR                  -  5      $ UR                  [        -  nUR                  [        -  nUR                  [        -  nU(       d!  SU l        U R                  [	        S5      5      $ [        R                  " 5       nUR                  UR                   5        US U(       + [#        U5      S.U R                  UR                  '   U(       d"  U(       d  U R%                  UR                  5      $ U(       d	  U(       d   eSU l        U R'                  5       $ )Nr  s&   expected command request frame; got %ds#   request with ID %d already receiveds$   request with ID %d is already actives3   received command request frame without new flag set)r   r  r     expectingdatar  )rF   r   r  r  r   r  rC   r  r  r0   r   r   r   r   bytesiowriterO   rj  r  r  )rP   rK   r(  r  
moreframesexpectingdatarO   s          r2   r  serverreactor._onframeidlea  s    <<55$DK((;<u||K  //6J??d555$DK((89EOOK  ??d222$DK((9:U__L  kk44[[#CC
&FF$DK((HI  ,,.emm$   *N"=1	4
0 ---eoo>>]***((**rH   c                H   UR                   [        :X  aC  UR                  [        -  (       a  U R	                  U5      $ U R                  U5      nU(       a  U$ UR                  U R                  ;   a.  SU l        U R                  [        S5      UR                  -  5      $ UR                  U R                  ;  a.  SU l        U R                  [        S5      UR                  -  5      $ U R                  UR                     nUR                   [        :X  a  UR                  [        -  n[        UR                  [        -  5      nUS   (       a!  SU l        U R                  [        S5      5      $ XSS   :w  a!  SU l        U R                  [        S5      5      $ US   R                  UR                   5        U(       d  S	US'   U(       d"  U(       d  U R#                  UR                  5      $ U R%                  5       $ UR                   [&        :X  ag  US   (       d.  SU l        U R                  [        S
5      UR                  -  5      $ US   c  [(        R*                  " 5       US'   U R-                  X5      $ SU l        U R                  [        S5      UR                   -  5      $ )Nr  s3   received frame for request that is still active: %ds4   received frame for request that is not receiving: %dr  sG   received command request frame when request frames were supposedly doner!  s4   mismatch between expect data flag and previous framer   TsF   received command data frame for request that is not expecting data: %dr  s"   received unexpected frame type: %d)rF   r   r0   r   r  r  rC   r  r  r  r   r  r   rj  r   r#  rO   r  r  r   r   r"  _handlecommanddataframe)rP   rK   r(  r  r$  r%  s         r2   r  &serverreactor._onframecommandreceiving  sc   <<55{{55((//33E:C
 ??d222$DK((HI//" 
 ??$"9"99$DK((IJ//" 
 ''8<<55'GGJ /O!OPM^$(,,0  &6 77(,,MN  *##EMM2(,n%m11%//BB,,..\\44)*(,,. oo	&  W~%!%g//==$DK((785<<G rH   c                   UR                   [        :X  d   eUS   R                  UR                  5        UR                  [
        -  (       a  U R                  5       $ UR                  [        -  (       a/  US   R                  S5        U R                  UR                  5      $ SU l        U R                  [        S5      5      $ )Nr  r   r  s    command data frame without flags)rF   r   r#  rO   r0   r   r  r   r  r  rC   r  r  r   )rP   rK   r  s      r2   r(  %serverreactor._handlecommanddataframe  s    ||6666 	gU]]+;;77,,..[[00'N"--eoo>>$DK((+N)OPPrH   c                6    U R                  [        S5      5      $ )Ns   server already errored)r  r   r  s     r2   r  serverreactor._onframeerrored  s    $$Q'@%ABBrH   )r  r  r  r  r  r  r  r  r  r  r  rs  )r  rj  r   r   rK   rK   )rC   rA   r   HandleSendFramesReturnTr   z.tuple[bytes, dict[bytes, Iterator[bytearray]]])r  Iterator[bytearray]r   r/  )r   r   rC   rA   r   rU   r   r/  r   )r   r   rC   rA   r   rU   r   r/  )r   r   )r   rU   r    tuple[bytes, dict[bytes, bytes]])rC   rA   )r   r2  )r;   r<   r=   r>   r?   ru  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r(  r  rG   r:   rH   r2   r  r  B  s    >@ 21!@/bK4!$K4	 K4Z
(	+		 	44),4384	 4 DH44),47<4	 4,

)
V

,*J+X3+jL\QCrH   r  c                  :    \ rS rSr% SrS\S'    S   S	S jjrSrg)
commandrequesti  z&Represents a request to run a command.rU   stateNc                N    Xl         X l        X0l        X@l        XPl        SU l        g )Ns   pending)rC   rR   r   r   r   r5  )rP   rC   rR   r   r   r   s         r2   r   commandrequest.__init__  s$     #		 
rH   )r   r   rR   r   rC   r5  NN)rC   rA   r   r   )r;   r<   r=   r>   r?   ru  r   rG   r:   rH   r2   r4  r4    s+    0L AE  	   rH   r4  c                      \ rS rSr% SrS\S'   S\S'      S     SS jjrSS jrSS	 jr    SS
 jr	SS jr
SS jrSS jrSS jrSrg)clientreactori  a!  Holds state of a client issuing frame-based protocol requests.

This is like ``serverreactor`` but for client-side state.

Each instance is bound to the lifetime of a connection. For persistent
connection transports using e.g. TCP sockets and speaking the raw
framing protocol, there will be a single instance for the lifetime of
the TCP socket. For transports where there are multiple discrete
interactions (say tunneled within in HTTP request), there will be a
separate instance for each distinct interaction.

Consumers are expected to tell instances when events occur by calling
various methods. These methods return a 2-tuple describing any follow-up
action(s) to take. The first element is the name of an action to
perform. The second is a data structure (usually a dict) specific to
that action that contains more information. e.g. if the reactor wants
to send frames to the server, the data structure will contain a reference
to those frames.

Valid actions that consumers can be instructed to take are:

noop
   Indicates no additional action is required.

sendframes
   Indicates that frames should be sent to the server. The ``framegen``
   key contains a generator of frames that should be sent. The reactor
   assumes that all frames in this generator are sent to the server.

error
   Indicates that an error occurred. The ``message`` key contains an
   error message describing the failure.

responsedata
   Indicates a response to a previously-issued command was received.

   The ``request`` key contains the ``commandrequest`` instance that
   represents the request this data is for.

   The ``data`` key contains the decoded data from the server.

   ``expectmore`` and ``eos`` evaluate to True when more response data
   is expected to follow or we're at the end of the response stream,
   respectively.
rj  _hasmultiplesend_buffersendsNc                    Xl         X l        X0l        X@l        SU l        SU l        SU l        SU l        [        S5      U l	        [        R                  " 5       U l        0 U l        0 U l        0 U l        [!        5         g)aB  Create a new instance.

``hasmultiplesend`` indicates whether multiple sends are supported
by the transport. When True, it is possible to send commands immediately
instead of buffering until the caller signals an intent to finish a
send operation.

``buffercommands`` indicates whether sends should be buffered until the
last request has been issued.

``clientcontentencoders`` is an iterable of content encoders the client
will advertise to the server and that the server can use for encoding
data. If not defined, the client will not advertise content encoders
to the server.
TFr   N)r  r;  r<  _clientcontentencoders_canissuecommands_cansend_protocolsettingssent_nextrequestidr   _outgoingstreamcollectionsdeque_pendingrequests_activerequestsr  _streamsettingsdecodersrh  )rP   r  hasmultiplesendbuffersendsclientcontentencoderss        r2   r   clientreactor.__init__9  su    ,  /'&;#!%%*"+A + 1 1 3! "')$ rH   c                   U R                   (       d  [        R                  " S5      eU R                  nU =R                  S-  sl        [	        XQX#US9nU R
                  (       a   U R                  R                  U5        US0 4$ U R                  (       d  [        R                  " S5      eU R                  (       d  SU l        SU l         USSU R                  U5      04$ )	zRequest that a command be executed.

Receives the command name, a dict of arguments to pass to the command,
and an optional file object containing the raw data for the command.

Returns a 3-tuple of (request, action, action data).
s   cannot issue new commandsr   r   r   r  *   sends cannot be performed on this instanceFr  r  )r?  r
   r  rB  r4  r<  rF  r(   r@  r;  _makecommandframes)rP   rR   r   r   r   rC   r  s          r2   callcommandclientreactor.callcommandb  s     %%(()EFF''	q  T8
 !!((1GR''==,,A  (( %).& !8!8!A rH   c                   ^  T R                   (       d  S0 4$ T R                  (       d  [        R                  " S5      eT R                  (       d  ST l        ST l        U 4S jnSSU" 5       04$ )a/  Request that all queued commands be sent.

If any commands are buffered, this will instruct the caller to send
them over the wire. If no commands are buffered it instructs the client
to no-op.

If instances aren't configured for multiple sends, no new command
requests are allowed after this is called.
r  rO  Fc               3     >#    TR                   (       aG  TR                   R                  5       n TR                  U 5       S h  vN   TR                   (       a  MF  g g  N7fr   )rF  popleftrP  )r  rP   s    r2   
makeframes/clientreactor.flushcommands.<locals>.makeframes  sH     ''//779227;;; ''';s   A AAAAr  r  )rF  r@  r
   r  r;  r?  )rP   rV  s   ` r2   flushcommandsclientreactor.flushcommands  so     $$B;}}((=  $$%*D"!DM	<
 
 
 	
rH   c           	   #    #    XR                   UR                  '   SUl        U R                  (       dy  U R                  (       ah  SU l        SR                  [        R                  " SU R                  05      5      nU R                  R                  UR                  [        [        US9v   [        U R                  UR                  UR                  UR                  UR                  UR                   S9nU Sh  vN   SUl        g N7f)	zsEmit frames to issue a command request.

As a side-effect, update request accounting to reflect its changed
state.
s   sendingTrH   r  r   rN  Ns   sent)rG  rC   r5  rA  r>  r*   r   rd   rC  r_   r  r  r   rR   r   r   r   )rP   r  rO   r(  s       r2   rP   clientreactor._makecommandframes  s      3:W../"))d.I.I)-D&hh%%+T-H-HG &&00!++:7	 1   "  LLLL>>%%
  	s   C9D
;D<D
c                   UR                   S-  (       a  SS[        S5      UR                   -  04$ UR                   U R                  ;  aS  UR                  [        -  (       d  SS[        S5      04$ [        UR                   5      U R                  UR                   '   U R                  UR                      nUR                  [        -  (       a   UR                  UR                  5      Ul        UR                  [        -  (       a  U R                  UR                   	 UR                  [        :X  a  U R                  U5      $ UR                  U R                  ;  a  SS[        S5      UR                  -  04$ U R                  UR                     nSUl        [         U R"                  [$        U R&                  0nUR)                  UR                  5      nU(       d#  [*        R,                  " SUR                  -  5      eU" X15      $ )	zProcess a frame that has been received off the wire.

Returns a 2-tuple of (action, meta) describing further action the
caller needs to take as a result of receiving this frame.
r   r   r   s.   received frame with odd numbered stream ID: %dsE   received frame on unknown stream without beginning of stream flag sets*   received frame for inactive request ID: %ds	   receivings   unhandled frame type: %d)rD   r   r  rE   rq  rw  r  r   rO   r  rF   r  _onstreamsettingsframerC   rG  r5  r   _oncommandresponseframer   _onerrorresponseframer)   r
   r  )rP   rK   r   r  r  r  s         r2   r  clientreactor.onframerecv  s    >>AKL..)  >>!6!66$$'??"AD%  5@4OD!!%..1&&u~~6
 ;;"MM%--8EM55%%enn5<<55..u55??$"6"66GH//*  &&u7$ ()E)E%t'A'A

 ||ELL)((+ell:  G##rH   c                   UR                   [        :X  d   eUR                  [        -  nUR                  [        -  nU(       a  U(       a  SS[        S5      04$ U(       d  U(       d  SS[        S5      04$ UR                  U R                  ;  a-  [        R                  " 5       nX@R                  UR                  '   U R                  UR                     n UR                  UR                  5        U(       a  S0 4$ U(       d   eUR                  5       nU R                  UR                  	 U(       d  SS[        S5      04$  U R                   UR                     R#                  U R$                  US   US	S  5        S0 4$ ! [         a0  nSS[        S5      [        R                  " U5      -  04s S nA$ S nAff = f! [         a0  nSS[        S
5      [        R                  " U5      -  04s S nA$ S nAff = f)Nr   r   sX   stream encoding settings frame cannot have both continuation and end of stream flags setsO   stream encoding settings frame must have continuation or end of stream flag sets;   error decoding CBOR from stream encoding settings frame: %sr  s8   stream encoding settings frame did not contain CBOR datar   r   s    error setting stream decoder: %s)rF   r  r0   *FLAG_STREAM_ENCODING_SETTINGS_CONTINUATIONr  r   rD   rH  r   r  r   rO   r  r   r  r  r  r~  r  )rP   rK   r  r  decoderr  r  s          r2   r]  $clientreactor._onstreamsettingsframe  s   ||9999{{GGkk==CH
 
 CB!  >>!=!==//1G;B((8..u~~>	NN5==) B;
s&&(((8%! 	!!%..1<<'!*gabk {a  	2 %11!4	5 	J  		=>$11!45 		s<   F :G  
F=%F82F=8F= 
G:
%G5/G:5G:c                    UR                   [        -  (       a  SUl        U R                  UR                  	 SUUR                   [
        -  UR                   [        -  UR                  S.4$ )Ns   receiveds   responsedata)   requests
   expectmorer   r  )r0   r   r5  rG  rC   r   rO   )rP   r  rK   s      r2   r^  %clientreactor._oncommandresponseframet  sb    ;;22'GM$$W%6%67 #${{-OO&??	
 	
rH   c                    SUl         U R                  UR                  	 [        R                  " UR
                  5      S   nSUUS   US   S.4$ )Nr  r   r   r   r   )rf  r   r   )r5  rG  rC   r   r  rO   )rP   r  rK   r   s       r2   r_  #clientreactor._onerrorresponseframe  s]    "  !2!23 u}}-a0 #7jM
 	
rH   )rG  r<  r?  r@  r>  r;  r  rB  rC  rF  rA  rH  r  )FTN)rI  rj  rJ  rj  r   r   r8  r0  )r  r4  r   r1  r.  )r  r4  rK   rK   )r;   r<   r=   r>   r?   ru  r   rQ  rX  rP  r  r]  r^  r_  rG   r:   rH   r2   r:  r:    s    ,\ 
 !& "'! '! 	'! 
'!R%N
B( %( 	( TF$PVp

rH   r:  )r+   r   r,   rA   r   rU   )rC   rA   rD   rA   rE   rA   rF   rA   r0   rA   rO   rU   r   rY   )rg   rU   r   rY   )ru   rU   r   r6   )r   zframe | None)r   r   rC   rA   r   rA   r   r1  )r   r   rC   rA   r   r  )r   r   rC   rA   r   rA   r   r1  )r   r   rC   rA   r   rY   r   )r   r   rC   rA   r   rU   r   r1  )
r   r   rC   rA   r   rU   r   rU   r   r1  r  )r   r   r   r   )g
__future__r   r   rD  r[   typingr   r   i18nr   
thirdpartyr   TYPE_CHECKINGr>  r	   r
   r   r   r   utilsr   r   r   tuplerU   r  rY   r/  rZ   r   rq  r  r  r   ru  r   r   r   r   r   FRAME_TYPE_PROGRESSr  r  r   r   r   r   r   r   r   r   r   r   r   r    r  r  r!   rb  r  r"   r#   StructARGUMENT_RECORD_HEADERr3   rg   r6   rK   r_   ro   rx   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r,  r9  rL  rU  r_  EncoderTDecoderTrb  rc  rh  r   rw  r   r  r  r  r4  r:  r:   rH   r2   <module>rv     s2   # 
   
   
 
 
 $E4x	7J0J+K$KL    #  .),"  "  "     &* #!  3,40*$!D2	! 	   $( !#'  #'   %6-2	+ '  "&   4!( $ 
 &* "   8%, ( 
 .2 *$( ! @-4  0 
 .2 *$( ! @-4  0   5/!7rB')G >	1 - 	  u- 
 d   d
 
  
@  	
   8?D2N .JJJ J JZ%(4 /  	@&&%(&&T :>"-22").9>6 /	888 	8
 8vf
 f
Z"h " "h "g  
g 
' *3' 3g ._ 
3g 34_ 4 
Ok1NBCHOk1NBCH ;=7 <%' { '..
 
6"%& "%J^
6 ^
B
 +7 3 
r
C r
Cj    J
 J
rH   