U e5d hã@s&dZddlZddlZddlZddlZeedƒr6ed7ZddlmZddlmZddlm Z dd lm Z dd lm Z dd l m Z dd lmZd Zddedœdd„Zd dedœdd„Zeedƒràd!dedœdd„Zd"dedœdd„ZGdd„de jƒZGdd„dee jƒZGdd„dƒZGdd„dƒZdS)#)Ú StreamReaderÚ StreamWriterÚStreamReaderProtocolÚopen_connectionÚ start_serveréNZAF_UNIX)Úopen_unix_connectionÚstart_unix_serveré)Ú coroutines)Úevents)Ú exceptions)Úformat_helpers)Ú protocols)Úlogger)Úsleepi)ÚloopÚlimitc ‹st|dkrt ¡}ntjdtdd�t||d�}t||d�‰|j‡fdd„||f|ŽIdH\}}t|ˆ||ƒ}||fS) aÂA 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ú[The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10.é©Ú stacklevel©rr©rcsˆS©N©r©Úprotocolrú'/usr/lib64/python3.8/asyncio/streams.pyÚ5óz!open_connection..) r Úget_event_loopÚwarningsÚwarnÚDeprecationWarningrrZcreate_connectionr) ÚhostÚportrrÚkwdsÚreaderÚ transportÚ_Úwriterrrrrs" þ   ÿÿrc‹sJˆdkrt ¡‰ntjdtdd�‡‡‡fdd„}ˆj|||f|ŽIdHS)aÉStart 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. Nrrrcstˆˆd�}t|ˆˆd�}|S©Nrr©rr©r'r©Úclient_connected_cbrrrrÚfactoryXs  ÿzstart_server..factory)r r r!r"r#Z create_server)r/r$r%rrr&r0rr.rr:s þrc‹sr|dkrt ¡}ntjdtdd�t||d�}t||d�‰|j‡fdd„|f|ŽIdH\}}t|ˆ||ƒ}||fS) z@Similar to `open_connection` but works with UNIX Domain Sockets.NrrrrrcsˆSrrrrrrrprz&open_unix_connection..) r r r!r"r#rrZcreate_unix_connectionr)Úpathrrr&r'r(r)r*rrrrds  þ   ÿÿrc‹sHˆdkrt ¡‰ntjdtdd�‡‡‡fdd„}ˆj||f|ŽIdHS)z=Similar to `start_server` but works with UNIX Domain Sockets.Nrrrcstˆˆd�}t|ˆˆd�}|Sr+r,r-r.rrr0~s  ÿz"start_unix_server..factory)r r r!r"r#Zcreate_unix_server)r/r1rrr&r0rr.rrts þrc@sBeZdZdZddd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)Ú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)ÚselfrrrrÚ__init__‘s  zFlowControlMixin.__init__cCs*|jr t‚d|_|j ¡r&t d|¡dS)NTz%r pauses writing)r5ÚAssertionErrorr4Ú get_debugrÚdebug©r8rrrÚ pause_writingšs  zFlowControlMixin.pause_writingcCsP|js t‚d|_|j ¡r&t d|¡|j}|dk rLd|_| ¡sL| d¡dS)NFz%r resumes writing) r5r:r4r;rr<r6ÚdoneÚ set_result©r8ÚwaiterrrrÚresume_writing s   zFlowControlMixin.resume_writingcCsVd|_|jsdS|j}|dkr"dSd|_| ¡r4dS|dkrH| d¡n | |¡dS©NT)r7r5r6r?r@Ú set_exception©r8ÚexcrBrrrÚconnection_lost¬s z FlowControlMixin.connection_lostcÃsP|jrtdƒ‚|jsdS|j}|dks2| ¡s2t‚|j ¡}||_|IdHdS)NzConnection lost)r7ÚConnectionResetErrorr5r6Ú cancelledr:r4Ú create_futurerArrrÚ _drain_helper¼s zFlowControlMixin._drain_helpercCst‚dSr)ÚNotImplementedError©r8ÚstreamrrrÚ_get_close_waiterÇsz"FlowControlMixin._get_close_waiter)N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r9r>rCrHrLrPrrrrr2‡s   r2csfeZdZdZdZd‡fdd„ Zedd„ƒZdd„Z‡fd d „Z d d „Z d d„Z dd„Z dd„Z ‡ZS)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.) Ncsntƒj|d�|dk r,t |¡|_|j|_nd|_|dk r@||_d|_d|_d|_ ||_ d|_ |j   ¡|_dS)NrF)Úsuperr9ÚweakrefÚrefÚ_stream_reader_wrÚ_source_tracebackÚ_strong_readerÚ_reject_connectionÚ_stream_writerÚ _transportÚ_client_connected_cbÚ _over_sslr4rKÚ_closed)r8Z stream_readerr/r©Ú __class__rrr9Ös  zStreamReaderProtocol.__init__cCs|jdkrdS| ¡Sr)rXr=rrrÚ_stream_readerés z#StreamReaderProtocol._stream_readercCs®|jr6ddi}|jr|j|d<|j |¡| ¡dS||_|j}|dk rT| |¡| d¡dk |_ |j dk rªt ||||jƒ|_ |  ||j ¡}t  |¡r¤|j |¡d|_dS)NÚmessagezpAn open stream was garbage collected prior to establishing network connection; call "stream.close()" explicitly.Zsource_tracebackZ sslcontext)r[rYr4Zcall_exception_handlerÚabortr]rcÚ set_transportÚget_extra_infor_r^rr\r Z iscoroutineZ create_taskrZ)r8r(Úcontextr'ÚresrrrÚconnection_madeïs2ÿ    þÿ  z$StreamReaderProtocol.connection_madecsx|j}|dk r*|dkr | ¡n | |¡|j ¡sV|dkrJ|j d¡n |j |¡tƒ |¡d|_d|_ d|_ dSr) rcÚfeed_eofrEr`r?r@rUrHrXr\r])r8rGr'rarrrH s     z$StreamReaderProtocol.connection_lostcCs|j}|dk r| |¡dSr)rcÚ feed_data)r8Údatar'rrrÚ data_receivedsz"StreamReaderProtocol.data_receivedcCs$|j}|dk r| ¡|jr dSdS)NFT)rcrkr_)r8r'rrrÚ eof_received s z!StreamReaderProtocol.eof_receivedcCs|jSr)r`rNrrrrP+sz&StreamReaderProtocol._get_close_waitercCs"|j}| ¡r| ¡s| ¡dSr)r`r?rJÚ exception)r8ÚclosedrrrÚ__del__.szStreamReaderProtocol.__del__)NN)rQrRrSrTrYr9ÚpropertyrcrjrHrnrorPrrÚ __classcell__rrrarrËs   rc@sveZdZdZdd„Zdd„Zedd„ƒZdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zddd„Zdd„ZdS)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. cCsJ||_||_|dks"t|tƒs"t‚||_||_|j ¡|_|j  d¡dSr) r]Ú _protocolÚ isinstancerr:Ú_readerr4rKZ _complete_futr@)r8r(rr'rrrrr9@s zStreamWriter.__init__cCs@|jjd|j›�g}|jdk r0| d|j›�¡d d |¡¡S)Nú transport=zreader=ú<{}>ú )rbrQr]rwÚappendÚformatÚjoin©r8ÚinforrrÚ__repr__Js zStreamWriter.__repr__cCs|jSr)r]r=rrrr(PszStreamWriter.transportcCs|j |¡dSr)r]Úwrite©r8rmrrrr�TszStreamWriter.writecCs|j |¡dSr)r]Ú writelinesr‚rrrrƒWszStreamWriter.writelinescCs |j ¡Sr)r]Ú write_eofr=rrrr„ZszStreamWriter.write_eofcCs |j ¡Sr)r]Ú can_write_eofr=rrrr…]szStreamWriter.can_write_eofcCs |j ¡Sr)r]Úcloser=rrrr†`szStreamWriter.closecCs |j ¡Sr)r]Ú is_closingr=rrrr‡cszStreamWriter.is_closingcÃs|j |¡IdHdSr)rurPr=rrrÚ wait_closedfszStreamWriter.wait_closedNcCs|j ||¡Sr)r]rg)r8ÚnameÚdefaultrrrrgiszStreamWriter.get_extra_infocÃsL|jdk r |j ¡}|dk r |‚|j ¡r8tdƒIdH|j ¡IdHdS)zyFlush the write buffer. The intended use is to write w.write(data) await w.drain() Nr)rwrpr]r‡rrurL)r8rGrrrÚdrainls   zStreamWriter.drain)N)rQrRrSrTr9r€rsr(r�rƒr„r…r†r‡rˆrgr‹rrrrr6s    rc@s¢eZdZdZedfdd„Zdd„Zdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd&dd„Zd'dd„Zd d!„Zd"d#„Zd$d%„ZdS)(rNcCsv|dkrtdƒ‚||_|dkr*t ¡|_n||_tƒ|_d|_d|_d|_ d|_ d|_ |j  ¡rrt  t d¡¡|_dS)NrzLimit cannot be <= 0Fr )Ú ValueErrorÚ_limitr r r4Ú bytearrayÚ_bufferÚ_eofÚ_waiterÚ _exceptionr]r5r;r Ú extract_stackÚsysÚ _getframerY)r8rrrrrr9Šs   ÿzStreamReader.__init__cCs¶dg}|jr"| t|jƒ›d�¡|jr2| d¡|jtkrN| d|j›�¡|jrf| d|j›�¡|jr~| d|j›�¡|jr–| d|j›�¡|j r¦| d¡d   d   |¡¡S) Nrz bytesÚeofzlimit=zwaiter=z exception=rxZpausedryrz) r�r{Úlenr�r�Ú_DEFAULT_LIMITr‘r’r]r5r|r}r~rrrr€ s    zStreamReader.__repr__cCs|jSr)r’r=rrrrp²szStreamReader.exceptioncCs0||_|j}|dk r,d|_| ¡s,| |¡dSr)r’r‘rJrErFrrrrEµs zStreamReader.set_exceptioncCs*|j}|dk r&d|_| ¡s&| d¡dS)z1Wakeup read*() functions waiting for data or EOF.N)r‘rJr@rArrrÚ_wakeup_waiter¾s zStreamReader._wakeup_waitercCs|jdkstdƒ‚||_dS)NzTransport already set)r]r:)r8r(rrrrfÆszStreamReader.set_transportcCs*|jr&t|jƒ|jkr&d|_|j ¡dSr3)r5r—r�r�r]Úresume_readingr=rrrÚ_maybe_resume_transportÊsz$StreamReader._maybe_resume_transportcCsd|_| ¡dSrD)r�r™r=rrrrkÏszStreamReader.feed_eofcCs|jo |j S)z=Return True if the buffer is empty and 'feed_eof' was called.)r�r�r=rrrÚat_eofÓszStreamReader.at_eofcCs‚|jrtdƒ‚|sdS|j |¡| ¡|jdk r~|js~t|jƒd|jkr~z|j  ¡Wnt k rvd|_YnXd|_dS)Nzfeed_data after feed_eofrT) r�r:r�Úextendr™r]r5r—r�Z pause_readingrMr‚rrrrl×s  ÿþ zStreamReader.feed_datacÃsf|jdk rt|›d�ƒ‚|jr&tdƒ‚|jr     þ    ÿzStreamReader.readuntilr®cÃsœ|jdk r|j‚|dkrdS|dkrVg}| |j¡IdH}|s@qL| |¡q(d |¡S|jsr|jsr| d¡IdHt|jd|…ƒ}|jd|…=|  ¡|S)aÚRead up to `n` bytes from the stream. If n is not provided, or set to -1, read until EOF and return all read bytes. If the EOF was received and the internal buffer is empty, return an empty bytes object. If n is zero, return empty bytes object immediately. If n is positive, this function try to read `n` bytes, and may return less or equal bytes than requested, but at least one byte. If EOF was received before any byte is read, this function returns empty byte object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. NrrÚread) r’r³r�r{r}r�r�rŸr°r›)r8ÚnZblocksÚblockrmrrrr³ƒs"     zStreamReader.readcÃsÀ|dkrtdƒ‚|jdk r |j‚|dkr,dSt|jƒ|krr|jr`t|jƒ}|j ¡t ||¡‚|  d¡IdHq,t|jƒ|kr–t|jƒ}|j ¡nt|jd|…ƒ}|jd|…=|  ¡|S)aÏRead exactly `n` bytes. Raise an IncompleteReadError if EOF is reached before `n` bytes can be read. The IncompleteReadError.partial attribute of the exception will contain the partial read bytes. if n is zero, return empty bytes object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. rz*readexactly size can not be less than zeroNrÚ readexactly) rŒr’r—r�r�r°r§r r¢rŸr›)r8r´Z incompletermrrrr¶µs&       zStreamReader.readexactlycCs|Srrr=rrrÚ __aiter__ÞszStreamReader.__aiter__cÃs| ¡IdH}|dkrt‚|S)Nr)r­ÚStopAsyncIteration)r8ÚvalrrrÚ __anext__ászStreamReader.__anext__)r )r®)rQrRrSrYr˜r9r€rprEr™rfr›rkrœrlrŸr­r¡r³r¶r·rºrrrrr†s$  [ 2)r)NN)NN)N)N)Ú__all__Zsocketr”r!rVÚhasattrÚr r r r rÚlogrZtasksrr˜rrrrZProtocolr2rrrrrrrÚsF        ÿ !ÿ ' ÿ ÿ DkP