3 \O@sdZdgZddlZddlZddlmZddlmZddlmZddlmZdd lm Z dd lm Z dd l m Z Gd d d e j e jZGdddee jZGdddee jZGdddeZGdddeee jZGdddeee jZGdddejZdS)zEvent loop using a proactor and related classes. A proactor is a "notify-on-completion" multiplexer. Currently a proactor is only implemented on Windows with IOCP. BaseProactorEventLoopN) base_events)compat) constants)futures)sslproto) transports)loggercseZdZdZdfdd ZddZddZd d Zd d Zd dZ ddZ e j rXddZ dddZddZddZddZZS)_ProactorBasePipeTransportz*Base class for pipe and socket transports.Ncstj|||j|||_||_||_d|_d|_d|_d|_ d|_ d|_ d|_ |jdk rh|jj |jj|jj||dk r|jjtj|ddS)NrF)super__init__ _set_extra_sock _protocol_server_buffer _read_fut _write_fut_pending_write _conn_lost_closing _eof_writtenZ_attach_loop call_soonZconnection_maderZ_set_result_unless_cancelled)selfloopsockprotocolwaiterextraserver) __class__//usr/lib64/python3.6/asyncio/proactor_events.pyr s$    z#_ProactorBasePipeTransport.__init__cCs|jjg}|jdkr |jdn|jr0|jd|jdk rN|jd|jj|jdk rh|jd|j|jdk r|jd|j|jrt |j}|jd||j r|jddd j |S) Nclosedclosingzfd=%szread=%szwrite=%rzwrite_bufsize=%sz EOF writtenz<%s> ) r"__name__rappendrfilenorrrlenrjoin)rinfobufsizer#r#r$__repr__/s"         z#_ProactorBasePipeTransport.__repr__cCs||jd<dS)Npipe)_extra)rrr#r#r$rBsz%_ProactorBasePipeTransport._set_extracCs ||_dS)N)r)rrr#r#r$ set_protocolEsz'_ProactorBasePipeTransport.set_protocolcCs|jS)N)r)rr#r#r$ get_protocolHsz'_ProactorBasePipeTransport.get_protocolcCs|jS)N)r)rr#r#r$ is_closingKsz%_ProactorBasePipeTransport.is_closingcCs^|jr dSd|_|jd7_|j r@|jdkr@|jj|jd|jdk rZ|jjd|_dS)NTr) rrrrrr_call_connection_lostrcancel)rr#r#r$closeNs  z _ProactorBasePipeTransport.closecCs*|jdk r&tjd|t|d|jdS)Nzunclosed transport %r)source)rwarningswarnResourceWarningr7)rr#r#r$__del__]s  z"_ProactorBasePipeTransport.__del__Fatal error on pipe transportcCsPt|tjr*|jjrBtjd||ddn|jj||||jd|j |dS)Nz%r: %sT)exc_info)message exceptionZ transportr) isinstancerZ_FATAL_ERROR_IGNOREr get_debugr debugcall_exception_handlerr _force_close)rexcr?r#r#r$ _fatal_errorcs   z'_ProactorBasePipeTransport._fatal_errorcCsj|jr dSd|_|jd7_|jr4|jjd|_|jrJ|jjd|_d|_d|_|jj|j |dS)NTrr) rrrr6rrrrrr5)rrFr#r#r$rEps  z'_ProactorBasePipeTransport._force_closec Cs^z|jj|Wdt|jdr,|jjtj|jjd|_|j}|dk rX|j d|_XdS)Nshutdown) rZconnection_losthasattrrrHsocketZ SHUT_RDWRr7rZ_detach)rrFr!r#r#r$r5s  z0_ProactorBasePipeTransport._call_connection_lostcCs"|j}|jdk r|t|j7}|S)N)rrr+)rsizer#r#r$get_write_buffer_sizes z0_ProactorBasePipeTransport.get_write_buffer_size)NNN)r=)r( __module__ __qualname____doc__r r/rr2r3r4r7rZPY34r<rGrEr5rL __classcell__r#r#)r"r$r s r cs<eZdZdZd fdd ZddZddZd d d ZZS) _ProactorReadPipeTransportzTransport for read pipes.Ncs4tj||||||d|_d|_|jj|jdS)NF)r r _paused_reschedule_on_resumerr _loop_reading)rrrrrr r!)r"r#r$r sz#_ProactorReadPipeTransport.__init__cCs0|js |jrdSd|_|jjr,tjd|dS)NTz%r pauses reading)rrRrrBr rC)rr#r#r$ pause_readings   z(_ProactorReadPipeTransport.pause_readingcCsP|js|j rdSd|_|jr6|jj|j|jd|_|jjrLtj d|dS)NFz%r resumes reading) rrRrSrrrTrrBr rC)rr#r#r$resume_readings z)_ProactorReadPipeTransport.resume_readingcCs|jrd|_dSd}z@yf|dk rN|j|ks@|jdkr<|js@td|_|j}|jr\d}dS|dkrhdS|jjj|j d|_Wnt k r}z2|js|j |dn|jj rt jdddWYdd}~Xntk r}z|j|WYdd}~Xn^tk r$}z|j |dWYdd}~Xn0tjk rD|js@YnX|jj|jWd|rl|jj|n:|dk r|jj rt jd||jj}|s|jXdS)NTiz"Fatal read error on pipe transportz*Read error on pipe transport while closing)r>z%r received EOF)rRrSrrAssertionErrorresultr _proactorrecvrConnectionAbortedErrorrGrBr rCConnectionResetErrorrEOSErrorrCancelledErroradd_done_callbackrTrZ data_receivedZ eof_receivedr7)rfutdatarFZ keep_openr#r#r$rTsL      z(_ProactorReadPipeTransport._loop_reading)NNN)N) r(rMrNrOr rUrVrTrPr#r#)r"r$rQs  rQc@s:eZdZdZddZd ddZddZd d Zd d ZdS)_ProactorBaseWritePipeTransportzTransport for write pipes.cCst|tttfs&dt|j}t||jr4td|speernamezgetpeername() failed on %r) r1Z getsocknamerJerrorAttributeErrorrrBr rjZ getpeername)rrr#r#r$rfs    z#_ProactorSocketTransport._set_extracCsdS)NTr#)rr#r#r$rtvsz&_ProactorSocketTransport.can_write_eofcCs2|js |jrdSd|_|jdkr.|jjtjdS)NT)rrrrrHrJrp)rr#r#r$ruys   z"_ProactorSocketTransport.write_eof)NNN) r(rMrNrOr rrtrurPr#r#)r"r$r\s rcseZdZfddZd-ddZd.ddddddd Zd/d d Zd0d d Zd1ddZfddZ ddZ ddZ ddZ ddZ ddZddZddZd2d d!Zd"d#Zd3d%d&Zd'd(Zd)d*Zd+d,ZZS)4rcsHtjtjd|jj||_||_d|_i|_ |j ||j dS)NzUsing proactor: %s) r r r rCr"r(rZ _selector_self_reading_future_accept_futuresZset_loop_make_self_pipe)rZproactor)r"r#r$r s  zBaseProactorEventLoop.__init__NcCst||||||S)N)r)rrrrr r!r#r#r$_make_socket_transports z,BaseProactorEventLoop._make_socket_transportF) server_sideserver_hostnamer r!c Cs<tjstdtj||||||} t||| ||d| jS)NzOProactor event loop requires Python 3.5 or newer (ssl.MemoryBIO) to support SSL)r r!)rZ_is_sslproto_availabler~Z SSLProtocolrZ_app_transport) rZrawsockr sslcontextrrrr r!Z ssl_protocolr#r#r$_make_ssl_transports  z)BaseProactorEventLoop._make_ssl_transportcCst|||||S)N)r})rrrrr r#r#r$_make_duplex_pipe_transportsz1BaseProactorEventLoop._make_duplex_pipe_transportcCst|||||S)N)rQ)rrrrr r#r#r$_make_read_pipe_transportsz/BaseProactorEventLoop._make_read_pipe_transportcCst|||||S)N)rw)rrrrr r#r#r$_make_write_pipe_transportsz0BaseProactorEventLoop._make_write_pipe_transportcsP|jrtd|jrdS|j|j|jjd|_d|_tjdS)Nz!Cannot close a running event loop) Z is_runningri is_closed_stop_accept_futures_close_self_piperZr7rr )r)r"r#r$r7s zBaseProactorEventLoop.closecCs|jj||S)N)rZr[)rrnr#r#r$ sock_recvszBaseProactorEventLoop.sock_recvcCs|jj||S)N)rZrq)rrrbr#r#r$ sock_sendallsz"BaseProactorEventLoop.sock_sendallcCs|jj||S)N)rZZconnect)rrZaddressr#r#r$ sock_connectsz"BaseProactorEventLoop.sock_connectcCs |jj|S)N)rZaccept)rrr#r#r$ sock_acceptsz!BaseProactorEventLoop.sock_acceptcCstdS)N)r~)rr#r#r$ _socketpairsz!BaseProactorEventLoop._socketpaircCsL|jdk r|jjd|_|jjd|_|jjd|_|jd8_dS)Nr)rr6_ssockr7_csock _internal_fds)rr#r#r$rs    z&BaseProactorEventLoop._close_self_pipecCsF|j\|_|_|jjd|jjd|jd7_|j|jdS)NFr)rrrZ setblockingrr_loop_self_reading)rr#r#r$rs   z%BaseProactorEventLoop._make_self_pipecCsy$|dk r|j|jj|jd}WnHtjk r:dStk rl}z|jd||dWYdd}~XnX||_|j |j dS)Niz.Error on reading from the event loop self pipe)r?r@r) rYrZr[rrr_ ExceptionrDrr`r)rrsrFr#r#r$rsz(BaseProactorEventLoop._loop_self_readingcCs|jjddS)N)rrq)rr#r#r$_write_to_selfsz$BaseProactorEventLoop._write_to_selfdcs&dfdd jdS)Ncs"y|dk rl|j\}}jr,tjd||}dk rVj||dd|idnj||d|idjrxdSjj}Wn~t k r}zDj d krj d|dj njrtjd dd WYdd}~Xn8t jk rj YnX|jj <|jdS) Nz#%r got a new connection from %r: %rTr)rr r!)r r!rzAccept failed on a socket)r?r@rJzAccept failed on socket %r)r>)rYZ_debugr rCrrrrZrr^r*rDr7rr_rr`)rsZconnZaddrrrF)rprotocol_factoryrr!rrr#r$rs>     z2BaseProactorEventLoop._start_serving..loop)N)r)rrrrr!Zbacklogr#)rrrr!rrr$_start_servings$z$BaseProactorEventLoop._start_servingcCsdS)Nr#)rZ event_listr#r#r$_process_events sz%BaseProactorEventLoop._process_eventscCs*x|jjD] }|jq W|jjdS)N)rvaluesr6clear)rZfuturer#r#r$r$s z*BaseProactorEventLoop._stop_accept_futurescCs |j|jj||jdS)N)rrZ _stop_servingr7)rrr#r#r$r)s z#BaseProactorEventLoop._stop_serving)NNN)N)NN)NN)NN)N)NNr)r(rMrNr rrrrrr7rrrrrrrrrrrrrrPr#r#)r"r$rs4          ()rO__all__rJr9rrrrrr logr Z_FlowControlMixinZ BaseTransportr Z ReadTransportrQZWriteTransportrcrwZ Transportr}rZ BaseEventLooprr#r#r#r$s2        M T  #