
    h                        S r SSKrSSKrSSKrSSKrSSKrSSK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  SSKJrJrJr  SSKJr  SSKrSSKJrJrJrJrJrJrJrJ r J!r!  SS	K"J#r#  \RH                  (       a
  SS
KJ%r%J&r&J'r'  \ " SSS9r(\RR                  \RT                  \RV                  \RX                  4r-\." \S5      (       a&  \-\R^                  \R`                  \Rb                  4-  r-\	Rd                  S:X  a  \-\Rf                  4-  r-\	Rd                  Ri                  S5      r5 " S S\65      r7 " S S\85      r9 " S S\85      r: " S S5      r; " S S5      r< " S S\<5      r= " S S\=5      r> " S S\<5      r?S \4S! jr@g)"aB  Utility classes to write to and read from non-blocking files and sockets.

Contents:

* `BaseIOStream`: Generic interface for reading and writing.
* `IOStream`: Implementation of BaseIOStream using non-blocking sockets.
* `SSLIOStream`: SSL-aware version of IOStream.
* `PipeIOStream`: Pipe-based IOStream implementation.
    N)Future"future_set_result_unless_cancelled)ioloop)gen_log)ssl_wrap_socket_client_ssl_defaults_server_ssl_defaults)errno_from_exception)	UnionOptional	AwaitableCallablePatternAnyDictTypeVarTuple)TracebackType)DequeListType_IOStreamTypeIOStream)boundWSAECONNRESETdarwinwinc                   B   ^  \ rS rSrSrSS\\   SS4U 4S jjjrSrU =r	$ )	StreamClosedErrorT   a  Exception raised by `IOStream` methods when the stream is closed.

Note that the close callback is scheduled to run *after* other
callbacks on the stream (to allow for buffered data to be processed),
so you may see this error before you see the close callback.

The ``real_error`` attribute contains the underlying error that caused
the stream to close (if any).

.. versionchanged:: 4.3
   Added the ``real_error`` attribute.
N
real_errorreturnc                 0   > [         TU ]  S5        Xl        g )NzStream is closed)super__init__r!   )selfr!   	__class__s     cC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\tornado/iostream.pyr%   StreamClosedError.__init__b   s    +,$    r!   N)
__name__
__module____qualname____firstlineno____doc__r   BaseExceptionr%   __static_attributes____classcell__r'   s   @r(   r   r   T   s%    %8M#: %d % %r*   r   c                       \ rS rSrSrSrg)UnsatisfiableReadErrorg   zException raised when a read cannot be satisfied.

Raised by ``read_until`` and ``read_until_regex`` with a ``max_bytes``
argument.
 Nr-   r.   r/   r0   r1   r3   r9   r*   r(   r7   r7   g   s     	r*   r7   c                       \ rS rSrSrSrg)StreamBufferFullErrorq   z?Exception raised by `IOStream` methods when the buffer is full.r9   Nr:   r9   r*   r(   r<   r<   q   s    Ir*   r<   c                   v    \ rS rSrSrSS jrS\4S jrSrS\	\
\\4   SS4S	 jrS
\S\4S jrS
\SS4S jrSrg)_StreamBufferu   z\
A specialized buffer that tries to avoid copies when large pieces
of data are encountered.
r"   Nc                 T    [         R                  " 5       U l        SU l        SU l        g Nr   )collectionsdeque_buffers
_first_pos_sizer&   s    r(   r%   _StreamBuffer.__init__{   s'      	 
r*   c                     U R                   $ r,   )rG   rH   s    r(   __len___StreamBuffer.__len__   s    zzr*   i   datac                    [        U5      nX R                  :  a>  [        U[        5      (       d  [        U5      nU R                  R                  SU45        OUS:  ay  U R                  (       a3  U R                  S   u  p4U=(       d    [        U5      U R                  :  nOSnU(       a'  U R                  R                  S[        U5      45        OWU-  nU =R                  U-  sl        g)zH
Append the given piece of data (should be a buffer-compatible object).
Tr   FN)len_large_buf_threshold
isinstance
memoryviewrE   append	bytearrayrG   )r&   rM   size
is_memviewbnew_bufs         r(   rT   _StreamBuffer.append   s     4y+++dJ//!$'MM  $.AX}} $b 1
$KA$2K2K(K$$eYt_%=>T	

d
r*   rV   c                     US:  d   e U R                   S   u  p#U R                  nU(       a  [        R
                  " [        X4XA-    5      $ [        U5      XDU-    $ ! [         a    [        S5      s $ f = f)zY
Get a view over at most ``size`` bytes (possibly fewer) at the
current buffer position.
r   r*   )rE   
IndexErrorrS   rF   typingcast)r&   rV   rW   rX   poss        r(   peek_StreamBuffer.peek   sx    
 axx	# MM!,MJ oo;;z13:+>??a=Tz22  	#c?"	#s   A A54A5c                    SUs=:  a  U R                   ::  d   e   eU =R                   U-  sl         U R                  nU R                  nU(       a  US:  a  US   u  pE[        U5      U-
  U-
  nUS::  a$  UR	                  5         U[        U5      U-
  -  nSnO4U(       a  X!-  nSnO&X!-  n[
        R                  " [        U5      SU2	 SnSnU(       a  US:  a  M  US:X  d   eX l        g)z8
Advance the current buffer position by ``size`` bytes.
r   N)rG   rF   rE   rP   popleftr]   r^   rU   )r&   rV   r_   buffersis_largerX   b_remains          r(   advance_StreamBuffer.advance   s     4%4::%%%%%

d
oo--$(!!*KH1v}s*H1}!A$KK	1-dsd3 $(  qyyr*   )rE   rF   rG   r"   N)r-   r.   r/   r0   r1   r%   intrK   rQ   r   bytesrU   rS   rT   r`   rg   r3   r9   r*   r(   r?   r?   u   se    
 
  5	:!=> 4 ,3 3 3"C D r*   r?   c                   f   \ rS rSrSr   S@S\\   S\\   S\\   SS4S jjrS\\\	R                  4   4S	 jrSAS
 jrS\S\4S jrS\\\4   S\\   4S jrS\\   4S jr SBS\S\\   S\\   4S jjr SBS\S\\   S\\   4S jjrSCS\S\S\\   4S jjrSCS\S\S\\   4S jjrS\\   4S jrS\\\4   SS4S jrS\\/ S4      SS4S jr SCS\S\\\S\\   \\    4   4   SS4S  jjr!SAS! jr"S\4S" jr#S\4S# jr$S\4S$ jr%S%\SS4S& jr&SAS' jr'S(\\\	R                  4   S)\SS4S* jr(S\\   4S+ jr)SAS, jr*S\+4S- jr,S.\SS4S/ jr-SAS0 jr.S\\   4S1 jr/S2\SS4S3 jr0S\\   4S4 jr1S\\\24   S.\SS4S5 jr3SAS6 jr4S7\S\4S8 jr5SAS9 jr6SAS: jr7S;\SS4S< jr8S=\S\4S> jr9S?r:g)DBaseIOStream   a  A utility class to write to and read from a non-blocking file or socket.

We support a non-blocking ``write()`` and a family of ``read_*()``
methods. When the operation completes, the ``Awaitable`` will resolve
with the data read (or ``None`` for ``write()``). All outstanding
``Awaitables`` will resolve with a `StreamClosedError` when the
stream is closed; `.BaseIOStream.set_close_callback` can also be used
to be notified of a closed stream.

When a stream is closed due to an error, the IOStream's ``error``
attribute contains the exception object.

Subclasses must implement `fileno`, `close_fd`, `write_to_fd`,
`read_from_fd`, and optionally `get_fd_error`.

Nmax_buffer_sizeread_chunk_sizemax_write_buffer_sizer"   c                 >   [         R                  R                  5       U l        U=(       d    SU l        [        U=(       d    SU R                  S-  5      U l        X0l        SU l        [        5       U l
        SU l        SU l        SU l        [        5       U l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        SU l        [2        R4                  " 5       U l        SU l        SU l        SU l        SU l        SU l         SU l!        g)a  `BaseIOStream` constructor.

:arg max_buffer_size: Maximum amount of incoming data to buffer;
    defaults to 100MB.
:arg read_chunk_size: Amount of data to read at one time from the
    underlying transport; defaults to 64KB.
:arg max_write_buffer_size: Amount of outgoing data to buffer;
    defaults to unlimited.

.. versionchanged:: 4.0
   Add the ``max_write_buffer_size`` parameter.  Changed default
   ``read_chunk_size`` to 64KB.
.. versionchanged:: 5.0
   The ``io_loop`` argument (deprecated since version 4.1) has been
   removed.
i  @i      Nr   F)"r   IOLoopcurrentio_loopro   minrp   rq   errorrU   _read_buffer_read_buffer_size_user_read_buffer_after_user_read_bufferr?   _write_buffer_total_write_index_total_write_done_index_read_delimiter_read_regex_read_max_bytes_read_bytes_read_partial_read_until_close_read_futurerC   rD   _write_futures_close_callback_connect_future_ssl_connect_future_connecting_state_closed)r&   ro   rp   rq   s       r(   r%   BaseIOStream.__init__   s   , }},,..;)  #?#;eT=Q=QUV=VW%:"
%K!"!&'+$*_"#'($##"!&  	  $#
 $(  r*   c                     [        5       e)z,Returns the file descriptor for this stream.NotImplementedErrorrH   s    r(   filenoBaseIOStream.fileno  s    !##r*   c                     [        5       e)zCloses the file underlying this stream.

``close_fd`` is called by `BaseIOStream` and should not be called
elsewhere; other users should call `close` instead.
r   rH   s    r(   close_fdBaseIOStream.close_fd  s     "##r*   rM   c                     [        5       e)zYAttempts to write ``data`` to the underlying file.

Returns the number of bytes written.
r   r&   rM   s     r(   write_to_fdBaseIOStream.write_to_fd&  s    
 "##r*   bufc                     [        5       e)a  Attempts to read from the underlying file.

Reads up to ``len(buf)`` bytes, storing them in the buffer.
Returns the number of bytes read. Returns None if there was
nothing to read (the socket returned `~errno.EWOULDBLOCK` or
equivalent), and zero on EOF.

.. versionchanged:: 5.0

   Interface redesigned to take a buffer and return a number
   of bytes instead of a freshly-allocated object.
r   r&   r   s     r(   read_from_fdBaseIOStream.read_from_fd-  s     "##r*   c                     g)a  Returns information about any error on the underlying file.

This method is called after the `.IOLoop` has signaled an error on the
file descriptor, and should return an Exception (such as `socket.error`
with additional information, or None if no such information is
available.
Nr9   rH   s    r(   get_fd_errorBaseIOStream.get_fd_error<  s     r*   regex	max_bytesc                 2   U R                  5       n[        R                  " U5      U l        X l         U R                  5         U$ ! [         a4  n[        R                  " SU-  5        U R                  US9  Us SnA$ SnAf  UR                  S 5        e = f)a,  Asynchronously read until we have matched the given regex.

The result includes the data that matches the regex and anything
that came before it.

If ``max_bytes`` is not None, the connection will be closed
if more than ``max_bytes`` bytes have been read and the regex is
not satisfied.

.. versionchanged:: 4.0
    Added the ``max_bytes`` argument.  The ``callback`` argument is
    now optional and a `.Future` will be returned if it is omitted.

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.

*Unsatisfiable read, closing connection: %sexc_infoNc                 "    U R                  5       $ r,   	exceptionfs    r(   <lambda>/BaseIOStream.read_until_regex.<locals>.<lambda>i  
    q{{}r*   )_start_readrecompiler   r   _try_inline_readr7   r   infocloseadd_done_callback)r&   r   r   futurees        r(   read_until_regexBaseIOStream.read_until_regexF  s    , !!#::e,(	!!#  & 	LLEIJJJJ"M	 $$%<=s   A 
B)A>8B>B	delimiterc                    U R                  5       nXl        X l         U R                  5         U$ ! [         a4  n[
        R                  " SU-  5        U R                  US9  Us SnA$ SnAf  UR                  S 5        e = f)a  Asynchronously read until we have found the given delimiter.

The result includes all the data read including the delimiter.

If ``max_bytes`` is not None, the connection will be closed
if more than ``max_bytes`` bytes have been read and the delimiter
is not found.

.. versionchanged:: 4.0
    Added the ``max_bytes`` argument.  The ``callback`` argument is
    now optional and a `.Future` will be returned if it is omitted.

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.
r   r   Nc                 "    U R                  5       $ r,   r   r   s    r(   r   )BaseIOStream.read_until.<locals>.<lambda>  r   r*   )	r   r   r   r   r7   r   r   r   r   )r&   r   r   r   r   s        r(   
read_untilBaseIOStream.read_untilm  s    ( !!#((		!!#  & 	LLEIJJJJ"M	$$%<=s   0 
B)A)#B)B	num_bytespartialc                     U R                  5       n[        U[        R                  5      (       d   eXl        X l         U R                  5         U$ !   UR                  S 5        e = f)a  Asynchronously read a number of bytes.

If ``partial`` is true, data is returned as soon as we have
any bytes to return (but never more than ``num_bytes``)

.. versionchanged:: 4.0
    Added the ``partial`` argument.  The callback argument is now
    optional and a `.Future` will be returned if it is omitted.

.. versionchanged:: 6.0

   The ``callback`` and ``streaming_callback`` arguments have
   been removed. Use the returned `.Future` (and
   ``partial=True`` for ``streaming_callback``) instead.

c                 "    U R                  5       $ r,   r   r   s    r(   r   )BaseIOStream.read_bytes.<locals>.<lambda>  r   r*   )r   rR   numbersIntegralr   r   r   r   )r&   r   r   r   s       r(   
read_bytesBaseIOStream.read_bytes  sh    " !!#)W%5%56666$$	!!# 	$$%<=s   A A&c                    U R                  5       nU R                  n[        U5      nXE:  a<  [        U R                  5      SU USS& U R                  SU2	 U R                  U l        O!US:  a  [        U R                  5      SS USU& SU l        Xl        X@l        XPl        X l         U R                  5         U$ !   UR                  S 5        e = f)a  Asynchronously read a number of bytes.

``buf`` must be a writable buffer into which data will be read.

If ``partial`` is true, the callback is run as soon as any bytes
have been read.  Otherwise, it is run when the ``buf`` has been
entirely filled with read data.

.. versionadded:: 5.0

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.

Nr   Tc                 "    U R                  5       $ r,   r   r   s    r(   r   (BaseIOStream.read_into.<locals>.<lambda>  r   r*   )r   rz   rP   rS   ry   r|   r{   r   r   r   r   )r&   r   r   r   available_bytesns         r(   	read_intoBaseIOStream.read_into  s    " !!# 00H 1 122A6CF!!"1"%+/+<+<D(q $.t/@/@$A!$DC !
 "&!0$	!!# 	$$%<=s   *B< <Cc                     U R                  5       nU R                  5       (       a  U R                  U R                  5        U$ SU l         U R                  5         U$ !   UR                  S 5        e = f)aK  Asynchronously reads all data from the socket until it is closed.

This will buffer all available data until ``max_buffer_size``
is reached. If flow control or cancellation are desired, use a
loop with `read_bytes(partial=True) <.read_bytes>` instead.

.. versionchanged:: 4.0
    The callback argument is now optional and a `.Future` will
    be returned if it is omitted.

.. versionchanged:: 6.0

   The ``callback`` and ``streaming_callback`` arguments have
   been removed. Use the returned `.Future` (and `read_bytes`
   with ``partial=True`` for ``streaming_callback``) instead.

Tc                 "    U R                  5       $ r,   r   r   s    r(   r   /BaseIOStream.read_until_close.<locals>.<lambda>  r   r*   )r   closed_finish_readrz   r   r   r   r&   r   s     r(   read_until_closeBaseIOStream.read_until_close  so    $ !!#;;==d445M!%	!!# 	$$%<=s   A A2zFuture[None]c                    U R                  5         U(       a  [        U[        5      (       a  [        U5      R                  S5      nU R                  b:  [        U R                  5      [        U5      -   U R                  :  a  [        S5      eU R                  R                  U5        U =R                  [        U5      -  sl	        [        5       nUR                  S 5        U R                  R                  U R                  U45        U R                  (       dV  U R                  5         U R                  (       a%  U R                  U R                   R"                  5        U R%                  5         U$ )a  Asynchronously write the given data to this stream.

This method returns a `.Future` that resolves (with a result
of ``None``) when the write has been completed.

The ``data`` argument may be of type `bytes` or `memoryview`.

.. versionchanged:: 4.0
    Now returns a `.Future` if no callback is given.

.. versionchanged:: 4.5
    Added support for `memoryview` arguments.

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.

Bz!Reached maximum write buffer sizec                 "    U R                  5       $ r,   r   r   s    r(   r   $BaseIOStream.write.<locals>.<lambda>  s
    1;;=r*   )_check_closedrR   rS   r^   rq   rP   r}   r<   rT   r~   r   r   r   r   _handle_write_add_io_staterv   WRITE_maybe_add_error_listener)r&   rM   r   s      r(   writeBaseIOStream.write  s   ( 	$
++!$',,S1**6**+c$i7$:T:TT+,OPP%%d+##s4y0#  !89""D$;$;V#DE !!""4<<#5#56**,r*   callbackc                 0    Xl         U R                  5         g)a  Call the given callback when the stream is closed.

This mostly is not necessary for applications that use the
`.Future` interface; all outstanding ``Futures`` will resolve
with a `StreamClosedError` when the stream is closed. However,
it is still useful as a way to signal that the stream has been
closed while no other read or write is in progress.

Unlike other callback-based interfaces, ``set_close_callback``
was not removed in Tornado 6.0.
N)r   r   )r&   r   s     r(   set_close_callbackBaseIOStream.set_close_callback!  s      (&&(r*   r   zOptional[Type[BaseException]]c                    U R                  5       (       Gd,  U(       ak  [        U[        5      (       a  US   U l        OK[        U[        5      (       a  Xl        O/[
        R                  " 5       n[        U5      (       a
  US   U l        U R                  (       a#  SU l        U R                  U R                  5        O2U R                  b%   U R                  5       nUb  U R                  U5        U R                  b0  U R                   R#                  U R%                  5       5        SU l        U R'                  5         SU l        U R+                  5         g! [         a     Nqf = f)zClose this stream.

If ``exc_info`` is true, set the ``error`` attribute to the current
exception from `sys.exc_info` (or if ``exc_info`` is a tuple,
use that instead of `sys.exc_info`).
   FNT)r   rR   tuplerx   r2   sysr   anyr   r   rz   r   _find_read_pos_read_from_bufferr7   r   rv   remove_handlerr   r   r   _signal_closed)r&   r   r_   s      r(   r   BaseIOStream.close0  s   & {{}}h..!)!DJ-88!)J"||~H8}}%-a[
%%).&!!$"8"89"".4--/C ..s3{{&++DKKM:"MMODL . s   E 
E! E!c                    / nU R                   b"  UR                  U R                   5        S U l         XR                   VVs/ s H  u  p#UPM	     snn-  nU R                  R                  5         U R                  b"  UR                  U R                  5        S U l        U HK  nUR                  5       (       d"  UR                  [        U R                  S95         UR                  5         MM     U R                  b  U R                  R                  5       (       dV  U R                  b&  U R                  R                  U R                  5        O#U R                  R                  [        5       5        U R                  R                  5         S U l        U R                  b.  U R                  nS U l        U R                  R                  U5        S U l        g s  snnf ! [        R                   a     GMS  f = fNr+   )r   rT   r   clearr   doneset_exceptionr   rx   r   asyncioCancelledErrorr   r   rv   add_callbackr}   )r&   futures_r   cbs        r(   r   BaseIOStream._signal_closed`  s   (NN4,,- $D,?,?@,?yqF,?@@!!#+NN4//0#'D F;;==$$%6$**%MN  "  ##/ ++0022::),,::4::F,,::;L;NO$$..0'+D$+%%B#'D LL%%b) "A A )) s   GGG43G4c                     U R                   SL$ )z=Returns ``True`` if we are currently reading from the stream.N)r   rH   s    r(   readingBaseIOStream.reading  s      ,,r*   c                 ,    [        U R                  5      $ )z;Returns ``True`` if we are currently writing to the stream.)boolr}   rH   s    r(   writingBaseIOStream.writing  s    D&&''r*   c                     U R                   $ )z/Returns ``True`` if the stream has been closed.)r   rH   s    r(   r   BaseIOStream.closed  s    ||r*   valuec                     g)a  Sets the no-delay flag for this stream.

By default, data written to TCP streams may be held for a time
to make the most efficient use of bandwidth (according to
Nagle's algorithm).  The no-delay flag requests that data be
written as soon as possible, even if doing so would consume
additional bandwidth.

This flag is currently defined only for TCP-based ``IOStreams``.

.. versionadded:: 3.1
Nr9   )r&   r  s     r(   set_nodelayBaseIOStream.set_nodelay  s     	r*   c                     [        5       er,   r   rH   s    r(   _handle_connectBaseIOStream._handle_connect  s    !##r*   fdeventsc                    U R                  5       (       a  [        R                  " SU5        g  U R                  (       a  U R	                  5         U R                  5       (       a  g X R
                  R                  -  (       a  U R                  5         U R                  5       (       a  g X R
                  R                  -  (       a  U R                  5         U R                  5       (       a  g X R
                  R                  -  (       a;  U R                  5       U l        U R
                  R                  U R                  5        g U R
                  R                  nU R                  5       (       a  X0R
                  R                  -  nU R!                  5       (       a  X0R
                  R                  -  nX0R
                  R                  :X  a(  U R"                  S:X  a  X0R
                  R                  -  nX0R$                  :w  aO  U R$                  c   S5       eX0l        U R
                  R'                  U R)                  5       U R$                  5        g g ! [*         a2  n[        R,                  " SU-  5        U R                  US9   S nAg S nAf[.         a*  n[        R                  " SSS9  U R                  US9  e S nAff = f)NzGot events for closed stream %sr   z4shouldn't happen: _handle_events without self._stater   r   z'Uncaught exception, closing connection.T)r   r   warningr   r  rv   READ_handle_readr   r   ERRORr   rx   r   r   r   r  rz   r   update_handlerr   r7   r   	Exception)r&   r  r  stater   s        r(   _handle_eventsBaseIOStream._handle_events  s   ;;==OO=rB/	
 $$&{{}})))!!#{{}}***""${{}}***!..0
 ))$**5LL&&E||~~***||~~+++***t/E/E/J
 ***#KK+JIJ+#++DKKM4;;G $ & 	#LLEIJJJJ" 	MMCdSJJJ"	s?   6I &AI )AI ,AI DI 
K(JK%J==Kc                    U R                   b  U R                   nO4U R                  b  U R                  nOU R                  5       (       a  S nOSnSnU R                  5       (       d  U R	                  5       S:X  a   U R                  5       $ Ub!  U R
                  U:  a   U R                  5       $ U R
                  U:  a$  U R                  5       nUb  U$ U R
                  S-  nU R                  5       (       d  M  U R                  5       $ )Nr   rs   )r   r   r   r   _read_to_bufferrz   r   )r&   target_bytesnext_find_posr_   s       r(   _read_to_buffer_loop!BaseIOStream._read_to_buffer_loop  s    '++L!!-//L\\^^  LL++-- ##%*$ ""$$ 'D,B,Bl,R ""$$ %%6))+?J $ 6 6 :1 ++--2 ""$$r*   c                 
    U R                  5       nUb  U R                  U5        g g ! [         a    e [        R                   a    e [         a2  n[
        R                  " SU-  5        U R                  US9   S nAg S nAff = f)Nzerror on read: %sr   )	r  r7   r   r   r  r   r  r   r   )r&   r_   r   s      r(   r  BaseIOStream._handle_read  s    		++-C ?""3'  & 	%% 	 	OO/!34JJJ"	s   ( (B(A==Bc                     U R                   b$  U R                  5         U R                   b   S5       e[        5       U l         U R                   $ )NzAlready reading)r   r   r   rH   s    r(   r   BaseIOStream._start_read  sJ    ("  $$,?.??,"H   r*   rV   c                 X   U R                   (       aM  U R                  =(       d
    [        5       U l        S U l        [	        U R                  5      U l        SU l         UnOU R                  U5      nU R                  b  U R                  nS U l        [        X25        U R                  5         g NF)
r{   r|   rU   ry   rP   rz   _consumer   r   r   )r&   rV   resultr   s       r(   r   BaseIOStream._finish_read(  s    !! $ < < K	D+/D(%():):%;D"%*D"F]]4(F(&&F $D.v>&&(r*   c                 6   U R                  5       nUb  U R                  U5        gU R                  5         U R                  5       nUb  U R                  U5        gU R	                  5       (       d*  U R                  [        R                  R                  5        gg)zAttempt to complete the current read operation from buffered data.

If the read can be completed without blocking, schedules the
read callback on the next IOLoop iteration; otherwise starts
listening for reads on the socket.
N)	r   r   r   r  r   r   r   rt   r  r&   r_   s     r(   r   BaseIOStream._try_inline_read7  s     !!#?""3''')?""3' {{}}v}}112 r*   c                       U R                   (       a#  [        U R                  5      U R                  S nO[	        U R
                  5      nU R                  U5      n Uc   AgUS:X  a  U R                  5          AgU R                   (       d!  U =R                  [        U5      SU -  sl        U =R                  U-  sl        AU R                  U R                  :  a1  [        R                  " S5        U R                  5         [        S5      eU$ ! [         a@  nU R                  U5      (       a  U R                  US9   SnAAgU R                  US9  e SnAff = f! Af = f)zReads from the socket and appends the result to the read buffer.

Returns the number of bytes read.  Returns 0 if there is nothing
to read (i.e. the read returns EWOULDBLOCK or equivalent).  On
error closes the socket and raises an exception.
Nr   r   z Reached maximum read buffer size)r{   rS   ry   rz   rU   rp   r   OSError_is_connresetr   ro   r   rx   r<   )r&   r   
bytes_readr   s       r(   r  BaseIOStream._read_to_bufferM  sU    	--():):; 224 ((<(<=!%!2!23!7J !  q

  ))!!Z_[j%AA!""j0" !!D$8$88MM<=JJL'(JKK7  	))!,, 

A
.#  JJJ*	, sH   E  AD E  %E  >AE  
E%EE  EEE   E#r_   c                 ^    S=U l         =U l        U l        SU l        U R	                  U5        g)zAttempts to complete the currently-pending read from the buffer.

The argument is either a position in the read buffer or None,
as returned by _find_read_pos.
NF)r   r   r   r   r   r+  s     r(   r   BaseIOStream._read_from_buffer{  s4     FJII4/$2B"#r*   c                 Z   U R                   b]  U R                  U R                   :  d!  U R                  (       a2  U R                  S:  a"  [        U R                   U R                  5      nU$ U R                  b  U R
                  (       a  U R
                  R                  U R                  5      nUS:w  a7  [        U R                  5      nU R                  U R                  X#-   5        X#-   $ U R                  U R                  U R                  5        gU R                  b  U R
                  (       a|  U R                  R                  U R
                  5      nUb.  UR                  5       nU R                  U R                  U5        U$ U R                  U R                  U R                  5        g)zAttempts to find a position in the read buffer that satisfies
the currently-pending read.

Returns a position in the buffer if the current read can be satisfied,
or None if it cannot.
Nr   rO   )r   rz   r   rw   r   ry   findrP   _check_max_bytesr   searchend)r&   r   locdelimiter_lenms        r(   r   BaseIOStream._find_read_pos  sb    '""d&6&66""t'='='AD,,d.D.DEI!!-   '',,T-A-AB"9$'(<(<$=M))$*>*>@ST..%%d&:&:D<R<RS  )  $$++D,=,=>=%%'C))$*:*:C@J%%d&6&68N8NOr*   c                 p    U R                   b)  X R                   :  a  [        SXR                   4-  5      eg g )Nz&delimiter %r not found within %d bytes)r   r7   )r&   r   rV   s      r(   r6  BaseIOStream._check_max_bytes  sC    +7K7K0K(82234  1L+r*   c                     [        U R                  5      nU(       d  OyUS:  d   e [        (       a  SnU R                  U R                  R	                  U5      5      nUS:X  a  O2U R                  R                  U5        U =R                  U-  sl        M  U R                  (       a[  U R                  S   u  pEX@R                  :  a  g U R                  R                  5         [!        US 5        U R                  (       a  MZ  g g ! [         a     Mz  [         aU  nU R                  U5      (       d&  [        R                  " SU R                  5       U5        U R                  US9   S nAg S nAff = f)Nr   i   zWrite error on %s: %sr   )rP   r}   _WINDOWSr   r`   rg   r   BlockingIOErrorr.  r/  r   r  r   r   r   rc   r   )r&   rV   r   r   indexr   s         r(   r   BaseIOStream._handle_write  s:   t))*D!8O88 &D ,,T-?-?-D-DT-JK	>""**95,,	9,% < !! //2ME333'').vt< !!! #  ))!,, OO$;T[[]AN

A
&s$   =D &0D 
E0	E0AE++E0r9  c                     US:X  a  gXR                   ::  d   e[        U R                  5      S U R                  5       nU =R                   U-  sl         U R                  S U2	 U$ )Nr   r*   )rz   rS   ry   tobytes)r&   r9  rX   s      r(   r'  BaseIOStream._consume  sf    !8,,,,,))*4C099;#%dsd#r*   c                 T    U R                  5       (       a  [        U R                  S9eg r   )r   r   rx   rH   s    r(   r   BaseIOStream._check_closed  s!    ;;==#tzz:: r*   c                 ,   U R                   b(  U R                   [        R                  R                  :X  a_  U R	                  5       (       dI  U R
                  S:X  a8  U R                  b*  U R                  [        R                  R                  5        g g g g g rB   )	r   r   rt   r  r   rz   r   r   r  rH   s    r(   r   &BaseIOStream._maybe_add_error_listener  su     ;;$++1D1D"DKKMM**a/((4""6==#5#56 5 0 " #Er*   r  c                    U R                  5       (       a  gU R                  cb  [        R                  R                  U-  U l        U R
                  R                  U R                  5       U R                  U R                  5        gU R                  U-  (       dI  U R                  U-  U l        U R
                  R                  U R                  5       U R                  5        gg)a  Adds `state` (IOLoop.{READ,WRITE} flags) to our event handler.

Implementation notes: Reads and writes have a fast path and a
slow path.  The fast path reads synchronously from socket
buffers, while the slow path uses `_add_io_state` to schedule
an IOLoop callback.

To detect closed connections, we must have called
`_add_io_state` at some point, but we want to delay this as
much as possible so we don't have to set an `IOLoop.ERROR`
listener that will be overwritten by the next slow-path
operation. If a sequence of fast-path ops do not end in a
slow-path op, (e.g. for an @asynchronous long-poll request),
we must add the error handler.

TODO: reevaluate this now that callbacks are gone.

N)
r   r   r   rt   r  rv   add_handlerr   r  r  )r&   r  s     r(   r   BaseIOStream._add_io_state  s    & ;;==;; ----5DKLL$$T[[]D4G4GUu$++-DKLL''t{{C %r*   excc                 t    [        U[        R                  [        45      =(       a    [	        U5      [
        ;   $ )zVReturn ``True`` if exc is ECONNRESET or equivalent.

May be overridden in subclasses.
)rR   socketrx   IOErrorr
   _ERRNO_CONNRESET)r&   rN  s     r(   r/  BaseIOStream._is_connreset  s/     sV\\734 >$S)-==	
r*   )r|   r   r   r   r   ry   rz   r   r   r   r   r   r   r   r   r   r   r~   r{   r}   r   rx   rv   ro   rq   rp   )NNNri   r,   )F);r-   r.   r/   r0   r1   r   rj   r%   r   r   _Selectabler   r   rS   r   rU   r   r  r   rk   r   r   r   r  r   r   r   r   r   r   r2   r   r   r   r   r   r  r   r
  r  r  r  r  r   r   r   r   r  r   r   r   r6  r   r'  r   r   r   r/  r3   r9   r*   r(   rm   rm      s3   & *.)-/3	7!#7 "#7  (}	7
 
7r$c6#5#556 $$$
 $s $$i&; < $# $hy1  8<%%'/}%	5	%P <@!!+3C=!	5	!FC $ 9UCS 8+Y + +)C. +Z)E"2 <(%z 12 (~ (T)8HRX4F+G )D )4 ./'')	
. 
.`%"N- -( (  $ $3sF,>,>'>!? 3 3QU 3j'%hsm '%R(!V !.) ) )3,,# ,\S T % %N%w*? s t $=L	C 	E 	;7D3 D4 D:
 
4 
r*   rm   c            
       p  ^  \ rS rSrSrS\R
                  S\S\SS4U 4S jjrS\\	\
R                  4   4S	 jrSS
 jrS\\   4S jrS\\\4   S\\	   4S jrS\S\	4S jr SS\S\S\\   SS4S jjr  SS\S\\\\\4   \R6                  4      S\\   S\S   4S jjrSS jrS\SS4S jrSr U =r!$ ) r   i  a  Socket-based `IOStream` implementation.

This class supports the read and write methods from `BaseIOStream`
plus a `connect` method.

The ``socket`` parameter may either be connected or unconnected.
For server operations the socket is the result of calling
`socket.accept <socket.socket.accept>`.  For client operations the
socket is created with `socket.socket`, and may either be
connected before passing it to the `IOStream` or connected with
`IOStream.connect`.

A very simple (and broken) HTTP client using this class:

.. testcode::

    import socket
    import tornado

    async def main():
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        stream = tornado.iostream.IOStream(s)
        await stream.connect(("friendfeed.com", 80))
        await stream.write(b"GET / HTTP/1.0\r\nHost: friendfeed.com\r\n\r\n")
        header_data = await stream.read_until(b"\r\n\r\n")
        headers = {}
        for line in header_data.split(b"\r\n"):
            parts = line.split(b":")
            if len(parts) == 2:
                headers[parts[0].strip()] = parts[1].strip()
        body_data = await stream.read_bytes(int(headers[b"Content-Length"]))
        print(body_data)
        stream.close()

    if __name__ == '__main__':
        asyncio.run(main())

rP  argskwargsr"   Nc                 h   > Xl         U R                   R                  S5        [        TU ]  " U0 UD6  g r&  )rP  setblockingr$   r%   )r&   rP  rV  rW  r'   s       r(   r%   IOStream.__init__G  s,    &$)&)r*   c                     U R                   $ r,   )rP  rH   s    r(   r   IOStream.filenoL  s    {{r*   c                 F    U R                   R                  5         S U l         g r,   )rP  r   rH   s    r(   r   IOStream.close_fdO  s    r*   c                     U R                   R                  [         R                  [         R                  5      n[         R                  " U[
        R                  " U5      5      $ r,   )rP  
getsockopt
SOL_SOCKETSO_ERRORrx   osstrerror)r&   errnos     r(   r   IOStream.get_fd_errorS  s<    &&v'8'8&//J||E2;;u#566r*   r   c                 ~     U R                   R                  U[        U5      5      A$ ! [         a     Ag f = f! Af = fr,   )rP  	recv_intorP   rA  r   s     r(   r   IOStream.read_from_fdW  sA    	;;((c#h7   		 s   $( 
69 69 <rM   c                 H     U R                   R                  U5      A$ ! Af = fr,   )rP  sendr   s     r(   r   IOStream.write_to_fd_  s"    	;;##D) s    !r&   addressserver_hostnamezFuture[_IOStreamType]c                    SU l         [        5       n[        R                  " SU5      U l         U R
                  R                  U5        U R                  U R                  R                  5        U$ ! [         a     N3[         aN  nUc0  [        R                  " SU R
                  R                  5       U5        U R                  US9  Us SnA$ SnAff = f)a}  Connects the socket to a remote address without blocking.

May only be called if the socket passed to the constructor was
not previously connected.  The address parameter is in the
same format as for `socket.connect <socket.socket.connect>` for
the type of socket passed to the IOStream constructor,
e.g. an ``(ip, port)`` tuple.  Hostnames are accepted here,
but will be resolved synchronously and block the IOLoop.
If you have a hostname instead of an IP address, the `.TCPClient`
class is recommended instead of calling this method directly.
`.TCPClient` will do asynchronous DNS resolution and handle
both IPv4 and IPv6.

If ``callback`` is specified, it will be called with no
arguments when the connection is completed; if not this method
returns a `.Future` (whose result after a successful
connection will be the stream itself).

In SSL mode, the ``server_hostname`` parameter will be used
for certificate validation (unless disabled in the
``ssl_options``) and SNI.

Note that it is safe to call `IOStream.write
<BaseIOStream.write>` while the connection is pending, in
which case the data will be written as soon as the connection
is ready.  Calling `IOStream` read methods before the socket is
connected works on some platforms but is non-portable.

.. versionchanged:: 4.0
    If no callback is given, returns a `.Future`.

.. versionchanged:: 4.2
   SSL certificates are validated by default; pass
   ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
   suitably-configured `ssl.SSLContext` to the
   `SSLIOStream` constructor to disable.

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.

TzFuture[IOStream]NConnect error on fd %s: %sr   )r   r   r]   r^   r   rP  connectrA  r.  r   r  r   r   r   rv   r   )r&   rm  rn  r   r   s        r(   rq  IOStream.connectg  s    \  %{{+=vF	KK( 	4<<--.  	  	
 ~ <dkk>P>P>RTUVJJJ"M	s$   A1 1
C=	CAC	CCserver_sidessl_optionsSSLIOStreamc                 J   U R                   (       dU  U R                  (       dD  U R                  (       d3  U R                  (       d"  U R                  (       d  U R
                  (       a  [        S5      eUc  U(       a  [        nO[        nU R                  nU R                  R                  U5        SU l	        [        UUUUSS9nU R                  nSU l        [        5       n[        XBS9nUR!                  U5        Xgl        U R$                  Ul        U R&                  Ul        U$ )a  Convert this `IOStream` to an `SSLIOStream`.

This enables protocols that begin in clear-text mode and
switch to SSL after some initial negotiation (such as the
``STARTTLS`` extension to SMTP and IMAP).

This method cannot be used if there are outstanding reads
or writes on the stream, or if there is any data in the
IOStream's buffer (data in the operating system's socket
buffer is allowed).  This means it must generally be used
immediately after reading or writing the last clear-text
data.  It can also be used immediately after connecting,
before any reads or writes.

The ``ssl_options`` argument may be either an `ssl.SSLContext`
object or a dictionary of keyword arguments for the
`ssl.SSLContext.wrap_socket` function.  The ``server_hostname`` argument
will be used for certificate validation unless disabled
in the ``ssl_options``.

This method returns a `.Future` whose result is the new
`SSLIOStream`.  After this method has been called,
any other operation on the original stream is undefined.

If a close callback is defined on this stream, it will be
transferred to the new stream.

.. versionadded:: 4.0

.. versionchanged:: 4.2
   SSL certificates are validated by default; pass
   ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
   suitably-configured `ssl.SSLContext` to disable.
z+IOStream is not idle; cannot convert to SSLNF)rn  rs  do_handshake_on_connect)rt  )r   r   r   r   ry   r}   
ValueErrorr	   r   rP  rv   r   r   r   r   ru  r   r   ro   rp   )r&   rs  rt  rn  rP  orig_close_callbackr   
ssl_streams           r(   	start_tlsIOStream.start_tls  s    R ""##||  !!JKK22##F+ +#$)
 #22# A
%%&9:)/&%)%9%9
"%)%9%9
"r*   c                 p    U R                   R                  [         R                  [         R                  5      nWS:w  a  [         R                  " U[        R                  " U5      5      U l        U R                  cA  [        R                  " SU R                   R                  5       [        R                  U   5        U R!                  5         g U R                  b  U R                  nS U l        [#        X05        SU l        g ! [         a)  n[        U5      [        R                  :X  a  Sn S nANS nAff = f)Nr   rp  F)rP  r`  ra  rb  r.  r
   re  ENOPROTOOPTrx   rc  rd  r   r   r  r   	errorcoder   r   r   )r&   errr   r   s       r(   r  IOStream._handle_connect  s    	++(():):FOOLC !8c2;;s+;<DJ
 ##+0KK&&(OOC(
 JJL+))F#'D .v< /  	 $A&%*;*;;		s   8D 
D5D00D5r  c                    U R                   b}  U R                   R                  [         R                  [         R                  4;   aD   U R                   R	                  [         R
                  [         R                  U(       a  SOS5        g g g ! [         aD  nUR                  [        R                  :w  a  U R                  U5      (       d  e  S nAg  S nAg S nAff = f)Nr   r   )rP  familyAF_INETAF_INET6
setsockoptIPPROTO_TCPTCP_NODELAYr.  re  EINVALr/  )r&   r  r   s      r(   r
  IOStream.set_nodelay  s    ;;"t{{'9'9NNOO>
 (
	&&&&(:(:AA(
"   77ell*43E3Ea3H3H 4I*	s   AB 
C5CC)r   r   r   rx   rP  ri   r,   )NN)"r-   r.   r/   r0   r1   rP  r   r%   r   rj   r   rT  r   r   r   r  r   rU   rS   r   r   r   strrq  r  r   ssl
SSLContextr   r{  r  r
  r3   r4   r5   s   @r(   r   r     sF   %N*v}} *S *C *D *
c6#5#556 7hy1 7i&; < # 
 s  MQAA&)A<DSMA	 AL HL)-	JJ eDcNCNN$BCDJ "#	J
 
=	!JX!8 $  r*   c                   *  ^  \ rS rSrSrSrS\S\SS4U 4S jjrS\4U 4S jjr	S\4U 4S	 jjr
SS
 jrSS jrSU 4S jjrSU 4S jjr SS\S\\   SS4U 4S jjjrSU 4S jjrSS jrS\S\4S jrS\\\4   S\\   4S jrS\S\4U 4S jjrSrU =r$ )ru  i#  af  A utility class to write to and read from a non-blocking SSL socket.

If the socket passed to the constructor is already connected,
it should be wrapped with::

    ssl.SSLContext(...).wrap_socket(sock, do_handshake_on_connect=False, **kwargs)

before constructing the `SSLIOStream`.  Unconnected sockets will be
wrapped when `IOStream.connect` is finished.
NrV  rW  r"   c                 4  > UR                  S[        5      U l        [        TU ]  " U0 UD6  SU l        SU l        SU l        SU l         U R                  R                  5         U R                  U R                  R                  5        g! [         a     gf = f)zThe ``ssl_options`` keyword argument may either be an
`ssl.SSLContext` object or a dictionary of keywords arguments
for `ssl.SSLContext.wrap_socket`
rt  TFN)popr   _ssl_optionsr$   r%   _ssl_accepting_handshake_reading_handshake_writing_server_hostnamerP  getpeernamer   rv   r   r.  )r&   rV  rW  r'   s      r(   r%   SSLIOStream.__init__1  s    
 #JJ}6JK$)&)""'"' $	3KK##% t||112  		s   
B
 

BBc                 F   > U R                   =(       d    [        TU ]	  5       $ r,   )r  r$   r   r&   r'   s    r(   r   SSLIOStream.readingH      &&;%'/*;;r*   c                 F   > U R                   =(       d    [        TU ]	  5       $ r,   )r  r$   r  r  s    r(   r  SSLIOStream.writingK  r  r*   c                     SU l         SU l        U R                  R                  5         SU l        [
        R                  (       d   eU R                  5         g ! [
        R                   GaH  nUR                  S   [
        R                  :X  a  SU l          S nAg UR                  S   [
        R                  :X  a  SU l         S nAg UR                  S   [
        R                  [
        R                  4;   a  U R                  US9s S nA$ UR                  S   [
        R                  [
        R                   4;   ar   U R                  R#                  5       nO! [$         a    Sn Of = f[&        R(                  " SU R                  R+                  5       X!5        U R                  US9s S nA$ e S nAf[,         ab  nU R/                  U5      (       d2  UR                  S   S[0        R2                  [0        R4                  4;   a  U R                  US9s S nA$ e S nAf[6         a  nU R                  US9s S nA$ S nAff = f)NFr   Tr   z(not connected)zSSL Error on %s %s: %s)r  r  rP  do_handshaker  r  HAS_SNI_finish_ssl_connectSSLErrorrV  SSL_ERROR_WANT_READSSL_ERROR_WANT_WRITESSL_ERROR_EOFSSL_ERROR_ZERO_RETURNr   SSL_ERROR_SSLSSL_ERROR_SYSCALLr  r  r   r  r   r.  r/  re  EBADFENOTCONNAttributeError)r&   r  peers      r(   _do_ssl_handshakeSSLIOStream._do_ssl_handshakeN  s   /	'&+D#&+D#KK$$&P #(D ;;;$$&W || 	xx{c555*.'! 8 88*.'!!2!2C4M4M NNzz3z//!!2!2C4I4I JJ-;;224D  -,D-,dkk.@.@.BD zz3z// 	 !!#&&#((1+: +
 zz3z// 	, ::s:++		,s   (A I.(F1(F1?F1I1F1?EF1E)&F1(E))AF1*I0F11I>AHIHI(H<6I<Ic                 \    U R                   b  U R                   nS U l         [        X5        g g r,   )r   r   r   s     r(   r  SSLIOStream._finish_ssl_connect  s.    ##/--F'+D$.v< 0r*   c                 f   > U R                   (       a  U R                  5         g [        TU ]  5         g r,   )r  r  r$   r  r  s    r(   r  SSLIOStream._handle_read  s&    ""$r*   c                 f   > U R                   (       a  U R                  5         g [        TU ]  5         g r,   )r  r  r$   r   r  s    r(   r   SSLIOStream._handle_write  s&    ""$r*   rm  rn  Future[SSLIOStream]c                 r   > X l         [        TU ]	  U5      nUR                  S 5        U R	                  5       $ )Nc                 "    U R                  5       $ r,   r   r   s    r(   r   %SSLIOStream.connect.<locals>.<lambda>  s
    r*   )r  r$   rq  r   wait_for_handshake)r&   rm  rn  futr'   s       r(   rq  SSLIOStream.connect  s9     !0 gog&56&&((r*   c                 J  > [         TU ]  5         U R                  5       (       a  g U R                  R	                  U R
                  5        U R                  nUc   eS U l        [        U R
                  U R                  U R                  SSS9U l        U R                  U5        g )NF)rn  rw  rs  )r$   r  r   rv   r   rP  r   r   r  r  r   )r&   	old_stater'   s     r(   r  SSLIOStream._handle_connect  s    !;;== 	##DKK0KK	$$$%KK 11$)
 	9%r*   c                     U R                   b  [        S5      e[        5       =ol         U R                  (       d  U R	                  5         U$ )a  Wait for the initial SSL handshake to complete.

If a ``callback`` is given, it will be called with no
arguments once the handshake is complete; otherwise this
method returns a `.Future` which will resolve to the
stream itself after the handshake is complete.

Once the handshake is complete, information such as
the peer's certificate and NPN/ALPN selections may be
accessed on ``self.socket``.

This method is intended for use on server-side streams
or after using `IOStream.start_tls`; it should not be used
with `IOStream.connect` (which already waits for the
handshake to complete). It may only be called once per stream.

.. versionadded:: 4.2

.. versionchanged:: 6.0

   The ``callback`` argument was removed. Use the returned
   `.Future` instead.

zAlready waiting)r   RuntimeErrorr   r  r  r   s     r(   r  SSLIOStream.wait_for_handshake  sC    2 ##/011,2H4)""$$&r*   rM   c                    [        U5      S-	  (       a  [        U5      S S n U R                  R                  U5      A$ ! [        R
                   a-  nUR                  S   [        R                  :X  a   S nAAge S nAff = f! Af = fN   i   @r   )rP   rS   rP  rk  r  r  rV  r  )r&   rM   r   s      r(   r   SSLIOStream.write_to_fd  s}     t9?d#Ig.D	;;##D)  || 		vvayC444 
 	 		 s-   ? B !A;4B :A;;B  B Br   c                 h    U R                   (       a   Ag [        U5      S-	  (       a  [        U5      S S n U R                  R	                  U[        U5      5      A$ ! [
        R                   a-  nUR                  S   [
        R                  :X  a   S nAAg e S nAf[         a     Ag f = f! Af = fr  )
r  rP   rS   rP  rh  r  r  rV  r  rA  r&   r   r   s      r(   r   SSLIOStream.read_from_fd  s    	"" $  3x2~ oi0
{{,,S#c(;  <<  66!9 7 77 	 "  sE   B. !B. $A B+2!BB. BB+'B. *B++B. .B1r   c                    > [        U[        R                  5      (       a"  UR                  S   [        R                  :X  a  g[
        TU ]  U5      $ )Nr   T)rR   r  r  rV  r  r$   r/  )r&   r   r'   s     r(   r/  SSLIOStream._is_connreset  s<    a&&166!98I8I+Iw$Q''r*   )r  r  r  r  r   r  r   rP  ri   r,   )r"   r  )r-   r.   r/   r0   r1   rP  r   r%   r  r   r  r  r  r  r   r   r   r  rq  r  r  rS   rj   r   r   rU   r   r2   r/  r3   r4   r5   s   @r(   ru  ru  #  s    	 F3c 3S 3T 3.< << <1'f=  @D))/7})	) )&&8@
 s .i&; < # 4(} ( ( (r*   ru  c                      ^  \ rS rSrSrS\S\S\SS4U 4S jjrS\4S	 jrSS
 jr	S\
S\4S jrS\\\
4   S\\   4S jrSrU =r$ )PipeIOStreami  a=  Pipe-based `IOStream` implementation.

The constructor takes an integer file descriptor (such as one returned
by `os.pipe`) rather than an open file object.  Pipes are generally
one-way, so a `PipeIOStream` can be used for reading or writing but not
both.

``PipeIOStream`` is only available on Unix-based platforms.
r  rV  rW  r"   Nc                    > Xl         [        R                  " U R                   S5      U l        [        R
                  S:X  a  [        S5      e[        R                  " US5        [        TU ](  " U0 UD6  g )Nzr+win32z(PipeIOStream is not supported on WindowsF)r  ioFileIO_fior   platformAssertionErrorrc  set_blockingr$   r%   )r&   r  rV  rW  r'   s       r(   r%   PipeIOStream.__init__$  sX    IIdggt,	<<7" !!KLL
E"$)&)r*   c                     U R                   $ r,   )r  rH   s    r(   r   PipeIOStream.fileno1  s    wwr*   c                 8    U R                   R                  5         g r,   )r  r   rH   s    r(   r   PipeIOStream.close_fd4  s    		r*   rM   c                 T     [         R                  " U R                  U5      A$ ! Af = fr,   )rc  r   r  r   s     r(   r   PipeIOStream.write_to_fd7  s"    	88DGGT* s    $ 'r   c                      U R                   R                  U5      A$ ! [         a8  n[        U5      [        R
                  :X  a  U R                  US9   S nAAg e S nAff = f! Af = f)Nr   )r  readintor.  r
   re  r  r   r  s      r(   r   PipeIOStream.read_from_fd?  sd    	99%%c*   	#A&%++5 

A
&  	 s,    
A ,AA# AA  A# #A&)r  r  ri   )r-   r.   r/   r0   r1   rj   r   r%   r   r   rS   r   r   rU   r   r   r3   r4   r5   s   @r(   r  r    ss    *3 *s *c *d * 
 s i&; < #  r*   r  r"   c                  *    SS K n U R                  5       $ rB   )doctestDocTestSuite)r  s    r(   doctestsr  N  s    !!r*   )Ar1   r   rC   re  r  r   rc  rP  r  r   r   tornado.concurrentr   r   tornador   tornado.logr   tornado.netutilr   r   r	   tornado.utilr
   r]   r   r   r   r   r   r   r   r   r   typesr   TYPE_CHECKINGr   r   r   r   
ECONNRESETECONNABORTEDEPIPE	ETIMEDOUTrR  hasattrr   WSAECONNABORTEDWSAETIMEDOUTr  
EPROTOTYPE
startswithr@  rQ  r   r  r7   r<   r?   rm   r   ru  r  r  r9   r*   r(   <module>r     sz       	  	  
 
 	 I   W W - 
 
 
  	((z: $$e&8&8%++uW 
5/""  <<8 ))++<<""5)% %&	Y 	JI JW WtM
 M
`A| AHs(( s(l2< 2j"# "r*   