B -_ `@sdZddlZeedred7ZddlmZddlmZddlmZdd lmZdd l m Z d Z Gd d d e Z GdddeZd!de dddZd"de dddZeedrd#de dddZd$de dddZGdddejZGdddeejZGdddZGdd d ZdS)%) StreamReader StreamWriterStreamReaderProtocolopen_connection start_serverIncompleteReadErrorLimitOverrunErrorNZAF_UNIX)open_unix_connectionstart_unix_server) coroutines)events) protocols)logger)sleepics(eZdZdZfddZddZZS)rz Incomplete read error. Attributes: - partial: read bytes string before the end of stream was reached - expected: total number of expected bytes (or None if unknown) cs,tt|d|d||_||_dS)Nz bytes read on a total of z expected bytes)super__init__lenpartialexpected)selfrr) __class__%/usr/lib/python3.7/asyncio/streams.pyrszIncompleteReadError.__init__cCst||j|jffS)N)typerr)rrrr __reduce__#szIncompleteReadError.__reduce__)__name__ __module__ __qualname____doc__rr __classcell__rr)rrrs rcs(eZdZdZfddZddZZS)rzReached the buffer limit while looking for a separator. Attributes: - consumed: total number of to be consumed bytes. cst|||_dS)N)rrconsumed)rmessager!)rrrr-s zLimitOverrunError.__init__cCst||jd|jffS)Nr)rargsr!)rrrrr1szLimitOverrunError.__reduce__)rrrrrrr rr)rrr's r)looplimitc sb|dkrt}t||d}t||d|jfdd||f|IdH\}}t|||}||fS)aA wrapper for create_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to create_connection() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). (If you want to customize the StreamReader and/or StreamReaderProtocol classes, just copy the code -- there's really nothing special here except some convenience.) N)r%r$)r$csS)Nrr)protocolrrMz!open_connection..)r get_event_looprrZcreate_connectionr) hostportr$r%kwdsreader transport_writerr)r&rr5s   rcs8dkrtfdd}j|||f|IdHS)aStart a socket server, call back for each client connected. The first parameter, `client_connected_cb`, takes two parameters: client_reader, client_writer. client_reader is a StreamReader object, while client_writer is a StreamWriter object. This parameter can either be a plain callback function or a coroutine; if it is a coroutine, it will be automatically converted into a Task. The rest of the arguments are all the usual arguments to loop.create_server() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. The return value is the same as loop.create_server(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). The return value is the same as loop.create_server(), i.e. a Server object which can be used to stop the service. Ncstd}t|d}|S)N)r%r$)r$)rr)r-r&)client_connected_cbr%r$rrfactoryls zstart_server..factory)r r)Z create_server)r1r*r+r$r%r,r2r)r1r%r$rrRsrcs`|dkrt}t||d}t||d|jfdd|f|IdH\}}t|||}||fS)z@Similar to `open_connection` but works with UNIX Domain Sockets.N)r%r$)r$csS)Nrr)r&rrr'r(z&open_unix_connection..)r r)rrZcreate_unix_connectionr)pathr$r%r,r-r.r/r0r)r&rr xs  r cs6dkrtfdd}j||f|IdHS)z=Similar to `start_server` but works with UNIX Domain Sockets.Ncstd}t|d}|S)N)r%r$)r$)rr)r-r&)r1r%r$rrr2s z"start_unix_server..factory)r r)Zcreate_unix_server)r1r3r$r%r,r2r)r1r%r$rr sr c@s:eZdZdZd ddZddZddZd d Zd d ZdS)FlowControlMixina)Reusable flow control logic for StreamWriter.drain(). This implements the protocol methods pause_writing(), resume_writing() and connection_lost(). If the subclass overrides these it must call the super methods. StreamWriter.drain() must wait for _drain_helper() coroutine. NcCs0|dkrt|_n||_d|_d|_d|_dS)NF)r r)_loop_paused _drain_waiter_connection_lost)rr$rrrrs  zFlowControlMixin.__init__cCs*|jr td|_|jr&td|dS)NTz%r pauses writing)r6AssertionErrorr5 get_debugrdebug)rrrr pause_writings  zFlowControlMixin.pause_writingcCsP|js td|_|jr&td||j}|dk rLd|_|sL|ddS)NFz%r resumes writing) r6r9r5r:rr;r7done set_result)rwaiterrrrresume_writings   zFlowControlMixin.resume_writingcCsVd|_|jsdS|j}|dkr"dSd|_|r4dS|dkrH|dn ||dS)NT)r8r6r7r=r> set_exception)rexcr?rrrconnection_losts z FlowControlMixin.connection_lostcsP|jrtd|jsdS|j}|dks2|s2t|j}||_|IdHdS)NzConnection lost)r8ConnectionResetErrorr6r7 cancelledr9r5 create_future)rr?rrr _drain_helpers zFlowControlMixin._drain_helper)N) rrrrrr<r@rCrGrrrrr4s   r4csNeZdZdZdfdd ZddZfddZd d Zd d Zd dZ Z S)ra=Helper class to adapt between Protocol and StreamReader. (This is a helper class instead of making StreamReader itself a Protocol subclass, because the StreamReader has other potential uses, and to prevent the user of the StreamReader to accidentally call inappropriate methods of the protocol.) Ncs6tj|d||_d|_||_d|_|j|_dS)N)r$F) rr_stream_reader_stream_writer_client_connected_cb _over_sslr5rF_closed)rZ stream_readerr1r$)rrrrs zStreamReaderProtocol.__init__cCsd|j||ddk |_|jdk r`t|||j|j|_||j|j}t |r`|j |dS)NZ sslcontext) rH set_transportget_extra_inforKrJrr5rIr Z iscoroutineZ create_task)rr.Zresrrrconnection_mades    z$StreamReaderProtocol.connection_madecsr|jdk r*|dkr|jn |j||jsV|dkrJ|jdn |j|t|d|_d|_dS)N) rHfeed_eofrArLr=r>rrCrI)rrB)rrrrCs      z$StreamReaderProtocol.connection_lostcCs|j|dS)N)rH feed_data)rdatarrr data_receivedsz"StreamReaderProtocol.data_receivedcCs|j|jrdSdS)NFT)rHrPrK)rrrr eof_receiveds z!StreamReaderProtocol.eof_receivedcCs"|j}|r|s|dS)N)rLr=rE exception)rclosedrrr__del__ szStreamReaderProtocol.__del__)NN) rrrrrrOrCrSrTrWr rr)rrrs  rc@sveZdZdZddZddZeddZdd Zd d Z d d Z ddZ ddZ ddZ ddZdddZddZdS)ra'Wraps a Transport. This exposes write(), writelines(), [can_]write_eof(), get_extra_info() and close(). It adds drain() which returns an optional Future on which you can wait for flow control. It also adds a transport property which references the Transport directly. cCs2||_||_|dks"t|ts"t||_||_dS)N) _transport _protocol isinstancerr9_readerr5)rr.r&r-r$rrrrs zStreamWriter.__init__cCs@|jjd|jg}|jdk r0|d|jdd|S)Nz transport=zreader=z<{}> )rrrXr[appendformatjoin)rinforrr__repr__&s zStreamWriter.__repr__cCs|jS)N)rX)rrrrr.,szStreamWriter.transportcCs|j|dS)N)rXwrite)rrRrrrrb0szStreamWriter.writecCs|j|dS)N)rX writelines)rrRrrrrc3szStreamWriter.writelinescCs |jS)N)rX write_eof)rrrrrd6szStreamWriter.write_eofcCs |jS)N)rX can_write_eof)rrrrre9szStreamWriter.can_write_eofcCs |jS)N)rXclose)rrrrrf<szStreamWriter.closecCs |jS)N)rX is_closing)rrrrrg?szStreamWriter.is_closingcs|jjIdHdS)N)rYrL)rrrr wait_closedBszStreamWriter.wait_closedNcCs|j||S)N)rXrN)rnamedefaultrrrrNEszStreamWriter.get_extra_infocsR|jdk r |j}|dk r ||jr>td|jdIdH|jIdHdS)zyFlush the write buffer. The intended use is to write w.write(data) await w.drain() Nr)r$)r[rUrXrgrr5rYrG)rrBrrrdrainHs   zStreamWriter.drain)N)rrrrrrapropertyr.rbrcrdrerfrgrhrNrkrrrrrs  rc@seZdZedfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZd&ddZd'ddZd d!Zd"d#Zd$d%ZdS)(rNcCsZ|dkrtd||_|dkr*t|_n||_t|_d|_d|_d|_ d|_ d|_ dS)NrzLimit cannot be <= 0F) ValueError_limitr r)r5 bytearray_buffer_eof_waiter _exceptionrXr6)rr%r$rrrras zStreamReader.__init__cCsdg}|jr"|t|jd|jr2|d|jtkrN|d|j|jrf|d|j|jr~|d|j|jr|d|j|j r|dd d |S) Nrz bytesZeofzlimit=zwaiter=z exception=z transport=Zpausedz<{}>r\) rpr]rrqrn_DEFAULT_LIMITrrrsrXr6r^r_)rr`rrrrats    zStreamReader.__repr__cCs|jS)N)rs)rrrrrUszStreamReader.exceptioncCs0||_|j}|dk r,d|_|s,||dS)N)rsrrrErA)rrBr?rrrrAs zStreamReader.set_exceptioncCs*|j}|dk r&d|_|s&|ddS)z1Wakeup read*() functions waiting for data or EOF.N)rrrEr>)rr?rrr_wakeup_waiters zStreamReader._wakeup_waitercCs|jdkstd||_dS)NzTransport already set)rXr9)rr.rrrrMszStreamReader.set_transportcCs*|jr&t|j|jkr&d|_|jdS)NF)r6rrprnrXresume_reading)rrrr_maybe_resume_transportsz$StreamReader._maybe_resume_transportcCsd|_|dS)NT)rqru)rrrrrPszStreamReader.feed_eofcCs|jo |j S)z=Return True if the buffer is empty and 'feed_eof' was called.)rqrp)rrrrat_eofszStreamReader.at_eofcCs|jrtd|sdS|j|||jdk r~|js~t|jd|jkr~y|j Wnt k rvd|_YnXd|_dS)Nzfeed_data after feed_eofT) rqr9rpextendrurXr6rrnZ pause_readingNotImplementedError)rrRrrrrQs   zStreamReader.feed_datacsf|jdk rt|d|jr&td|jrs0      "  A@K