| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | """Event loop using a selector and related classes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A selector is a "notify-when-ready" multiplexer.  For a subclass which | 
					
						
							|  |  |  | also includes support for signal handling, see the unix_events sub-module. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  | __all__ = 'BaseSelectorEventLoop', | 
					
						
							| 
									
										
										
										
											2014-01-25 15:32:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import collections | 
					
						
							| 
									
										
										
										
											2013-11-01 14:12:50 -07:00
										 |  |  | import errno | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  | import functools | 
					
						
							| 
									
										
										
										
											2017-11-28 15:19:56 +01:00
										 |  |  | import selectors | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | import socket | 
					
						
							| 
									
										
										
										
											2015-01-29 17:50:58 +01:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  | import weakref | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | try: | 
					
						
							|  |  |  |     import ssl | 
					
						
							|  |  |  | except ImportError:  # pragma: no cover | 
					
						
							|  |  |  |     ssl = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from . import base_events | 
					
						
							|  |  |  | from . import constants | 
					
						
							|  |  |  | from . import events | 
					
						
							|  |  |  | from . import futures | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | from . import protocols | 
					
						
							| 
									
										
										
										
											2015-01-14 00:19:09 +01:00
										 |  |  | from . import sslproto | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | from . import transports | 
					
						
							| 
									
										
										
										
											2019-05-27 15:57:20 +02:00
										 |  |  | from . import trsock | 
					
						
							| 
									
										
										
										
											2013-10-17 15:39:45 -07:00
										 |  |  | from .log import logger | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  | def _test_selector_event(selector, fd, event): | 
					
						
							|  |  |  |     # Test if the selector is monitoring 'event' events | 
					
						
							|  |  |  |     # for the file descriptor 'fd'. | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         key = selector.get_key(fd) | 
					
						
							|  |  |  |     except KeyError: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return bool(key.events & event) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  | def _check_ssl_socket(sock): | 
					
						
							|  |  |  |     if ssl is not None and isinstance(sock, ssl.SSLSocket): | 
					
						
							|  |  |  |         raise TypeError("Socket cannot be of type SSLSocket") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | class BaseSelectorEventLoop(base_events.BaseEventLoop): | 
					
						
							|  |  |  |     """Selector event loop.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     See events.EventLoop for API specification. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, selector=None): | 
					
						
							|  |  |  |         super().__init__() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if selector is None: | 
					
						
							|  |  |  |             selector = selectors.DefaultSelector() | 
					
						
							| 
									
										
										
										
											2013-10-17 15:39:45 -07:00
										 |  |  |         logger.debug('Using selector: %s', selector.__class__.__name__) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._selector = selector | 
					
						
							|  |  |  |         self._make_self_pipe() | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._transports = weakref.WeakValueDictionary() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _make_socket_transport(self, sock, protocol, waiter=None, *, | 
					
						
							|  |  |  |                                extra=None, server=None): | 
					
						
							|  |  |  |         return _SelectorSocketTransport(self, sock, protocol, waiter, | 
					
						
							|  |  |  |                                         extra, server) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |     def _make_ssl_transport( | 
					
						
							|  |  |  |             self, rawsock, protocol, sslcontext, waiter=None, | 
					
						
							|  |  |  |             *, server_side=False, server_hostname=None, | 
					
						
							|  |  |  |             extra=None, server=None, | 
					
						
							|  |  |  |             ssl_handshake_timeout=constants.SSL_HANDSHAKE_TIMEOUT): | 
					
						
							|  |  |  |         ssl_protocol = sslproto.SSLProtocol( | 
					
						
							|  |  |  |                 self, protocol, sslcontext, waiter, | 
					
						
							|  |  |  |                 server_side, server_hostname, | 
					
						
							|  |  |  |                 ssl_handshake_timeout=ssl_handshake_timeout) | 
					
						
							| 
									
										
										
										
											2015-01-14 00:19:09 +01:00
										 |  |  |         _SelectorSocketTransport(self, rawsock, ssl_protocol, | 
					
						
							|  |  |  |                                  extra=extra, server=server) | 
					
						
							|  |  |  |         return ssl_protocol._app_transport | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def _make_datagram_transport(self, sock, protocol, | 
					
						
							| 
									
										
										
										
											2014-07-08 23:57:31 +02:00
										 |  |  |                                  address=None, waiter=None, extra=None): | 
					
						
							|  |  |  |         return _SelectorDatagramTransport(self, sock, protocol, | 
					
						
							|  |  |  |                                           address, waiter, extra) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							| 
									
										
										
										
											2014-12-26 21:07:52 +01:00
										 |  |  |         if self.is_running(): | 
					
						
							| 
									
										
										
										
											2014-11-21 00:23:27 +01:00
										 |  |  |             raise RuntimeError("Cannot close a running event loop") | 
					
						
							| 
									
										
										
										
											2014-06-10 10:23:10 +02:00
										 |  |  |         if self.is_closed(): | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self._close_self_pipe() | 
					
						
							| 
									
										
										
										
											2014-11-21 00:23:27 +01:00
										 |  |  |         super().close() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if self._selector is not None: | 
					
						
							|  |  |  |             self._selector.close() | 
					
						
							|  |  |  |             self._selector = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _close_self_pipe(self): | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._remove_reader(self._ssock.fileno()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._ssock.close() | 
					
						
							|  |  |  |         self._ssock = None | 
					
						
							|  |  |  |         self._csock.close() | 
					
						
							|  |  |  |         self._csock = None | 
					
						
							|  |  |  |         self._internal_fds -= 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _make_self_pipe(self): | 
					
						
							|  |  |  |         # A self-socket, really. :-) | 
					
						
							| 
									
										
										
										
											2017-11-28 11:15:26 +01:00
										 |  |  |         self._ssock, self._csock = socket.socketpair() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._ssock.setblocking(False) | 
					
						
							|  |  |  |         self._csock.setblocking(False) | 
					
						
							|  |  |  |         self._internal_fds += 1 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._add_reader(self._ssock.fileno(), self._read_from_self) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-17 22:43:40 +02:00
										 |  |  |     def _process_self_data(self, data): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def _read_from_self(self): | 
					
						
							| 
									
										
										
										
											2014-06-19 12:59:15 +02:00
										 |  |  |         while True: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 data = self._ssock.recv(4096) | 
					
						
							|  |  |  |                 if not data: | 
					
						
							|  |  |  |                     break | 
					
						
							| 
									
										
										
										
											2014-07-17 22:43:40 +02:00
										 |  |  |                 self._process_self_data(data) | 
					
						
							| 
									
										
										
										
											2014-06-19 12:59:15 +02:00
										 |  |  |             except InterruptedError: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             except BlockingIOError: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _write_to_self(self): | 
					
						
							| 
									
										
										
										
											2014-05-06 14:42:40 -07:00
										 |  |  |         # This may be called from a different thread, possibly after | 
					
						
							|  |  |  |         # _close_self_pipe() has been called or even while it is | 
					
						
							|  |  |  |         # running.  Guard for self._csock being None or closed.  When | 
					
						
							|  |  |  |         # a socket is closed, send() raises OSError (with errno set to | 
					
						
							|  |  |  |         # EBADF, but let's not rely on the exact error code). | 
					
						
							|  |  |  |         csock = self._csock | 
					
						
							| 
									
										
										
										
											2020-09-12 08:50:18 +02:00
										 |  |  |         if csock is None: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             csock.send(b'\0') | 
					
						
							|  |  |  |         except OSError: | 
					
						
							|  |  |  |             if self._debug: | 
					
						
							|  |  |  |                 logger.debug("Fail to write a null byte into the " | 
					
						
							|  |  |  |                              "self-pipe socket", | 
					
						
							|  |  |  |                              exc_info=True) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 14:22:30 -07:00
										 |  |  |     def _start_serving(self, protocol_factory, sock, | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |                        sslcontext=None, server=None, backlog=100, | 
					
						
							|  |  |  |                        ssl_handshake_timeout=constants.SSL_HANDSHAKE_TIMEOUT): | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._add_reader(sock.fileno(), self._accept_connection, | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |                          protocol_factory, sock, sslcontext, server, backlog, | 
					
						
							|  |  |  |                          ssl_handshake_timeout) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |     def _accept_connection( | 
					
						
							|  |  |  |             self, protocol_factory, sock, | 
					
						
							|  |  |  |             sslcontext=None, server=None, backlog=100, | 
					
						
							|  |  |  |             ssl_handshake_timeout=constants.SSL_HANDSHAKE_TIMEOUT): | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |         # This method is only called once for each event loop tick where the | 
					
						
							|  |  |  |         # listening socket has triggered an EVENT_READ. There may be multiple | 
					
						
							|  |  |  |         # connections waiting for an .accept() so it is called in a loop. | 
					
						
							|  |  |  |         # See https://bugs.python.org/issue27906 for more details. | 
					
						
							|  |  |  |         for _ in range(backlog): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 conn, addr = sock.accept() | 
					
						
							|  |  |  |                 if self._debug: | 
					
						
							|  |  |  |                     logger.debug("%r got a new connection from %r: %r", | 
					
						
							|  |  |  |                                  server, addr, conn) | 
					
						
							|  |  |  |                 conn.setblocking(False) | 
					
						
							|  |  |  |             except (BlockingIOError, InterruptedError, ConnectionAbortedError): | 
					
						
							|  |  |  |                 # Early exit because the socket accept buffer is empty. | 
					
						
							|  |  |  |                 return None | 
					
						
							|  |  |  |             except OSError as exc: | 
					
						
							|  |  |  |                 # There's nowhere to send the error, so just log it. | 
					
						
							|  |  |  |                 if exc.errno in (errno.EMFILE, errno.ENFILE, | 
					
						
							|  |  |  |                                  errno.ENOBUFS, errno.ENOMEM): | 
					
						
							|  |  |  |                     # Some platforms (e.g. Linux keep reporting the FD as | 
					
						
							|  |  |  |                     # ready, so we remove the read handler temporarily. | 
					
						
							|  |  |  |                     # We'll try again in a while. | 
					
						
							|  |  |  |                     self.call_exception_handler({ | 
					
						
							|  |  |  |                         'message': 'socket.accept() out of system resource', | 
					
						
							|  |  |  |                         'exception': exc, | 
					
						
							| 
									
										
										
										
											2019-05-27 15:57:20 +02:00
										 |  |  |                         'socket': trsock.TransportSocket(sock), | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |                     }) | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |                     self._remove_reader(sock.fileno()) | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |                     self.call_later(constants.ACCEPT_RETRY_DELAY, | 
					
						
							|  |  |  |                                     self._start_serving, | 
					
						
							|  |  |  |                                     protocol_factory, sock, sslcontext, server, | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |                                     backlog, ssl_handshake_timeout) | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     raise  # The event loop will catch, log and ignore it. | 
					
						
							| 
									
										
										
										
											2013-11-01 14:12:50 -07:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |                 extra = {'peername': addr} | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                 accept = self._accept_connection2( | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |                     protocol_factory, conn, extra, sslcontext, server, | 
					
						
							|  |  |  |                     ssl_handshake_timeout) | 
					
						
							| 
									
										
										
										
											2016-09-15 14:13:15 -04:00
										 |  |  |                 self.create_task(accept) | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |     async def _accept_connection2( | 
					
						
							|  |  |  |             self, protocol_factory, conn, extra, | 
					
						
							|  |  |  |             sslcontext=None, server=None, | 
					
						
							|  |  |  |             ssl_handshake_timeout=constants.SSL_HANDSHAKE_TIMEOUT): | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |         protocol = None | 
					
						
							|  |  |  |         transport = None | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2015-01-15 00:04:21 +01:00
										 |  |  |             protocol = protocol_factory() | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |             waiter = self.create_future() | 
					
						
							| 
									
										
										
										
											2013-11-01 14:22:30 -07:00
										 |  |  |             if sslcontext: | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |                 transport = self._make_ssl_transport( | 
					
						
							|  |  |  |                     conn, protocol, sslcontext, waiter=waiter, | 
					
						
							| 
									
										
										
										
											2017-12-19 19:45:42 +00:00
										 |  |  |                     server_side=True, extra=extra, server=server, | 
					
						
							|  |  |  |                     ssl_handshake_timeout=ssl_handshake_timeout) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |                 transport = self._make_socket_transport( | 
					
						
							|  |  |  |                     conn, protocol, waiter=waiter, extra=extra, | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                     server=server) | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await waiter | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |             except BaseException: | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |                 transport.close() | 
					
						
							|  |  |  |                 raise | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |                 # It's now up to the protocol to handle the connection. | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2015-02-04 14:50:59 +01:00
										 |  |  |             if self._debug: | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |                 context = { | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                     'message': | 
					
						
							|  |  |  |                         'Error on transport creation for incoming connection', | 
					
						
							| 
									
										
										
										
											2015-01-29 14:15:19 +01:00
										 |  |  |                     'exception': exc, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if protocol is not None: | 
					
						
							|  |  |  |                     context['protocol'] = protocol | 
					
						
							|  |  |  |                 if transport is not None: | 
					
						
							|  |  |  |                     context['transport'] = transport | 
					
						
							|  |  |  |                 self.call_exception_handler(context) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |     def _ensure_fd_no_transport(self, fd): | 
					
						
							| 
									
										
										
										
											2017-11-13 13:38:22 -05:00
										 |  |  |         fileno = fd | 
					
						
							|  |  |  |         if not isinstance(fileno, int): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 fileno = int(fileno.fileno()) | 
					
						
							|  |  |  |             except (AttributeError, TypeError, ValueError): | 
					
						
							|  |  |  |                 # This code matches selectors._fileobj_to_fd function. | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                 raise ValueError(f"Invalid file object: {fd!r}") from None | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2017-11-13 13:38:22 -05:00
										 |  |  |             transport = self._transports[fileno] | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         except KeyError: | 
					
						
							|  |  |  |             pass | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if not transport.is_closing(): | 
					
						
							|  |  |  |                 raise RuntimeError( | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                     f'File descriptor {fd!r} is used by transport ' | 
					
						
							|  |  |  |                     f'{transport!r}') | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _add_reader(self, fd, callback, *args): | 
					
						
							| 
									
										
										
										
											2014-06-10 10:23:10 +02:00
										 |  |  |         self._check_closed() | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         handle = events.Handle(callback, args, self, None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             key = self._selector.get_key(fd) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             self._selector.register(fd, selectors.EVENT_READ, | 
					
						
							|  |  |  |                                     (handle, None)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             mask, (reader, writer) = key.events, key.data | 
					
						
							|  |  |  |             self._selector.modify(fd, mask | selectors.EVENT_READ, | 
					
						
							|  |  |  |                                   (handle, writer)) | 
					
						
							|  |  |  |             if reader is not None: | 
					
						
							|  |  |  |                 reader.cancel() | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         return handle | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |     def _remove_reader(self, fd): | 
					
						
							| 
									
										
										
										
											2014-06-10 10:23:10 +02:00
										 |  |  |         if self.is_closed(): | 
					
						
							| 
									
										
										
										
											2014-03-06 00:52:53 +01:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             key = self._selector.get_key(fd) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             mask, (reader, writer) = key.events, key.data | 
					
						
							|  |  |  |             mask &= ~selectors.EVENT_READ | 
					
						
							|  |  |  |             if not mask: | 
					
						
							|  |  |  |                 self._selector.unregister(fd) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self._selector.modify(fd, mask, (None, writer)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if reader is not None: | 
					
						
							|  |  |  |                 reader.cancel() | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |     def _add_writer(self, fd, callback, *args): | 
					
						
							| 
									
										
										
										
											2014-06-10 10:23:10 +02:00
										 |  |  |         self._check_closed() | 
					
						
							| 
									
										
										
										
											2018-01-22 19:11:18 -05:00
										 |  |  |         handle = events.Handle(callback, args, self, None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             key = self._selector.get_key(fd) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             self._selector.register(fd, selectors.EVENT_WRITE, | 
					
						
							|  |  |  |                                     (None, handle)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             mask, (reader, writer) = key.events, key.data | 
					
						
							|  |  |  |             self._selector.modify(fd, mask | selectors.EVENT_WRITE, | 
					
						
							|  |  |  |                                   (reader, handle)) | 
					
						
							|  |  |  |             if writer is not None: | 
					
						
							|  |  |  |                 writer.cancel() | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         return handle | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |     def _remove_writer(self, fd): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """Remove a writer callback.""" | 
					
						
							| 
									
										
										
										
											2014-06-10 10:23:10 +02:00
										 |  |  |         if self.is_closed(): | 
					
						
							| 
									
										
										
										
											2014-03-06 00:52:53 +01:00
										 |  |  |             return False | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             key = self._selector.get_key(fd) | 
					
						
							|  |  |  |         except KeyError: | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             mask, (reader, writer) = key.events, key.data | 
					
						
							|  |  |  |             # Remove both writer and connector. | 
					
						
							|  |  |  |             mask &= ~selectors.EVENT_WRITE | 
					
						
							|  |  |  |             if not mask: | 
					
						
							|  |  |  |                 self._selector.unregister(fd) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self._selector.modify(fd, mask, (reader, None)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if writer is not None: | 
					
						
							|  |  |  |                 writer.cancel() | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |     def add_reader(self, fd, callback, *args): | 
					
						
							|  |  |  |         """Add a reader callback.""" | 
					
						
							|  |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         self._add_reader(fd, callback, *args) | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def remove_reader(self, fd): | 
					
						
							|  |  |  |         """Remove a reader callback.""" | 
					
						
							|  |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |         return self._remove_reader(fd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def add_writer(self, fd, callback, *args): | 
					
						
							|  |  |  |         """Add a writer callback..""" | 
					
						
							|  |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         self._add_writer(fd, callback, *args) | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def remove_writer(self, fd): | 
					
						
							|  |  |  |         """Remove a writer callback.""" | 
					
						
							|  |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |         return self._remove_writer(fd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |     async def sock_recv(self, sock, n): | 
					
						
							| 
									
										
										
										
											2014-06-19 17:11:49 +02:00
										 |  |  |         """Receive data from the socket.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The return value is a bytes object representing the data received. | 
					
						
							|  |  |  |         The maximum amount of data to be received at once is specified by | 
					
						
							|  |  |  |         nbytes. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  |         _check_ssl_socket(sock) | 
					
						
							| 
									
										
										
										
											2015-02-04 14:50:59 +01:00
										 |  |  |         if self._debug and sock.gettimeout() != 0: | 
					
						
							| 
									
										
										
										
											2014-07-29 23:08:17 +02:00
										 |  |  |             raise ValueError("the socket must be non-blocking") | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             return sock.recv(n) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |         fut = self.create_future() | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         fd = sock.fileno() | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |         handle = self._add_reader(fd, self._sock_recv, fut, sock, n) | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         fut.add_done_callback( | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |             functools.partial(self._sock_read_done, fd, handle=handle)) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |         return await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |     def _sock_read_done(self, fd, fut, handle=None): | 
					
						
							|  |  |  |         if handle is None or not handle.cancelled(): | 
					
						
							|  |  |  |             self.remove_reader(fd) | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _sock_recv(self, fut, sock, n): | 
					
						
							| 
									
										
										
										
											2014-02-13 09:24:37 +01:00
										 |  |  |         # _sock_recv() can add itself as an I/O callback if the operation can't | 
					
						
							| 
									
										
										
										
											2014-02-18 22:27:48 -05:00
										 |  |  |         # be done immediately. Don't use it directly, call sock_recv(). | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         if fut.done(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             data = sock.recv(n) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |             return  # try again next time | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fut.set_result(data) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |     async def sock_recv_into(self, sock, buf): | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |         """Receive data from the socket.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The received data is written into *buf* (a writable buffer). | 
					
						
							|  |  |  |         The return value is the number of bytes written. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  |         _check_ssl_socket(sock) | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |         if self._debug and sock.gettimeout() != 0: | 
					
						
							|  |  |  |             raise ValueError("the socket must be non-blocking") | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             return sock.recv_into(buf) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |         fut = self.create_future() | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         fd = sock.fileno() | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |         handle = self._add_reader(fd, self._sock_recv_into, fut, sock, buf) | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         fut.add_done_callback( | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |             functools.partial(self._sock_read_done, fd, handle=handle)) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |         return await fut | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |     def _sock_recv_into(self, fut, sock, buf): | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |         # _sock_recv_into() can add itself as an I/O callback if the operation | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |         # can't be done immediately. Don't use it directly, call | 
					
						
							|  |  |  |         # sock_recv_into(). | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         if fut.done(): | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |             return | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             nbytes = sock.recv_into(buf) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |             return  # try again next time | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2017-10-19 21:46:40 +02:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fut.set_result(nbytes) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |     async def sock_sendall(self, sock, data): | 
					
						
							| 
									
										
										
										
											2014-06-19 17:11:49 +02:00
										 |  |  |         """Send data to the socket.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The socket must be connected to a remote socket. This method continues | 
					
						
							|  |  |  |         to send data from data until either all data has been sent or an | 
					
						
							|  |  |  |         error occurs. None is returned on success. On error, an exception is | 
					
						
							|  |  |  |         raised, and there is no way to determine how much data, if any, was | 
					
						
							|  |  |  |         successfully processed by the receiving end of the connection. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  |         _check_ssl_socket(sock) | 
					
						
							| 
									
										
										
										
											2015-02-04 14:50:59 +01:00
										 |  |  |         if self._debug and sock.gettimeout() != 0: | 
					
						
							| 
									
										
										
										
											2014-07-29 23:08:17 +02:00
										 |  |  |             raise ValueError("the socket must be non-blocking") | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             n = sock.send(data) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             n = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if n == len(data): | 
					
						
							|  |  |  |             # all data sent | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         fut = self.create_future() | 
					
						
							|  |  |  |         fd = sock.fileno() | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         self._ensure_fd_no_transport(fd) | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |         # use a trick with a list in closure to store a mutable state | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |         handle = self._add_writer(fd, self._sock_sendall, fut, sock, | 
					
						
							|  |  |  |                                   memoryview(data), [n]) | 
					
						
							|  |  |  |         fut.add_done_callback( | 
					
						
							|  |  |  |             functools.partial(self._sock_write_done, fd, handle=handle)) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |         return await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |     def _sock_sendall(self, fut, sock, view, pos): | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         if fut.done(): | 
					
						
							|  |  |  |             # Future cancellation can be scheduled on previous loop iteration | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |         start = pos[0] | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |             n = sock.send(view[start:]) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |         start += n | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if start == len(view): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             fut.set_result(None) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-16 16:30:16 +03:00
										 |  |  |             pos[0] = start | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def sock_connect(self, sock, address): | 
					
						
							| 
									
										
										
										
											2014-06-19 17:11:49 +02:00
										 |  |  |         """Connect to a remote socket at address.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This method is a coroutine. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  |         _check_ssl_socket(sock) | 
					
						
							| 
									
										
										
										
											2015-02-04 14:50:59 +01:00
										 |  |  |         if self._debug and sock.gettimeout() != 0: | 
					
						
							| 
									
										
										
										
											2014-07-29 23:08:17 +02:00
										 |  |  |             raise ValueError("the socket must be non-blocking") | 
					
						
							| 
									
										
										
										
											2016-06-08 12:33:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:56:36 -04:00
										 |  |  |         if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX: | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |             resolved = await self._ensure_resolved( | 
					
						
							| 
									
										
										
										
											2016-06-28 11:00:22 -04:00
										 |  |  |                 address, family=sock.family, proto=sock.proto, loop=self) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |             _, _, _, _, address = resolved[0] | 
					
						
							| 
									
										
										
										
											2016-09-15 17:56:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         fut = self.create_future() | 
					
						
							|  |  |  |         self._sock_connect(fut, sock, address) | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |         return await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  |     def _sock_connect(self, fut, sock, address): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         fd = sock.fileno() | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2015-04-07 21:38:04 +02:00
										 |  |  |             sock.connect(address) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             # Issue #23618: When the C function connect() fails with EINTR, the | 
					
						
							|  |  |  |             # connection runs in background. We have to wait until the socket | 
					
						
							|  |  |  |             # becomes writable to be notified when the connection succeed or | 
					
						
							|  |  |  |             # fails. | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |             self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |             handle = self._add_writer( | 
					
						
							|  |  |  |                 fd, self._sock_connect_cb, fut, sock, address) | 
					
						
							| 
									
										
										
										
											2016-09-15 17:56:36 -04:00
										 |  |  |             fut.add_done_callback( | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |                 functools.partial(self._sock_write_done, fd, handle=handle)) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fut.set_result(None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 14:47:30 -05:00
										 |  |  |     def _sock_write_done(self, fd, fut, handle=None): | 
					
						
							|  |  |  |         if handle is None or not handle.cancelled(): | 
					
						
							|  |  |  |             self.remove_writer(fd) | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _sock_connect_cb(self, fut, sock, address): | 
					
						
							| 
									
										
										
										
											2018-11-12 19:00:22 +02:00
										 |  |  |         if fut.done(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  |             err = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) | 
					
						
							|  |  |  |             if err != 0: | 
					
						
							|  |  |  |                 # Jump to any except clause below. | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                 raise OSError(err, f'Connect call failed {address}') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2014-08-31 15:07:57 +02:00
										 |  |  |             # socket is still registered, the callback will be retried later | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fut.set_result(None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |     async def sock_accept(self, sock): | 
					
						
							| 
									
										
										
										
											2014-06-19 17:11:49 +02:00
										 |  |  |         """Accept a connection.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         The socket must be bound to an address and listening for connections. | 
					
						
							|  |  |  |         The return value is a pair (conn, address) where conn is a new socket | 
					
						
							|  |  |  |         object usable to send and receive data on the connection, and address | 
					
						
							|  |  |  |         is the address bound to the socket on the other end of the connection. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-12-09 15:02:03 +01:00
										 |  |  |         _check_ssl_socket(sock) | 
					
						
							| 
									
										
										
										
											2015-02-04 14:50:59 +01:00
										 |  |  |         if self._debug and sock.gettimeout() != 0: | 
					
						
							| 
									
										
										
										
											2014-07-29 23:08:17 +02:00
										 |  |  |             raise ValueError("the socket must be non-blocking") | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |         fut = self.create_future() | 
					
						
							| 
									
										
										
										
											2020-07-23 22:45:08 +03:00
										 |  |  |         self._sock_accept(fut, sock) | 
					
						
							| 
									
										
										
										
											2017-12-14 20:53:26 -05:00
										 |  |  |         return await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 22:45:08 +03:00
										 |  |  |     def _sock_accept(self, fut, sock): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         fd = sock.fileno() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             conn, address = sock.accept() | 
					
						
							|  |  |  |             conn.setblocking(False) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2020-07-23 22:45:08 +03:00
										 |  |  |             self._ensure_fd_no_transport(fd) | 
					
						
							|  |  |  |             handle = self._add_reader(fd, self._sock_accept, fut, sock) | 
					
						
							|  |  |  |             fut.add_done_callback( | 
					
						
							|  |  |  |                 functools.partial(self._sock_read_done, fd, handle=handle)) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             fut.set_exception(exc) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fut.set_result((conn, address)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |     async def _sendfile_native(self, transp, file, offset, count): | 
					
						
							|  |  |  |         del self._transports[transp._sock_fd] | 
					
						
							|  |  |  |         resume_reading = transp.is_reading() | 
					
						
							|  |  |  |         transp.pause_reading() | 
					
						
							|  |  |  |         await transp._make_empty_waiter() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return await self.sock_sendfile(transp._sock, file, offset, count, | 
					
						
							|  |  |  |                                             fallback=False) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             transp._reset_empty_waiter() | 
					
						
							|  |  |  |             if resume_reading: | 
					
						
							|  |  |  |                 transp.resume_reading() | 
					
						
							|  |  |  |             self._transports[transp._sock_fd] = transp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def _process_events(self, event_list): | 
					
						
							|  |  |  |         for key, mask in event_list: | 
					
						
							|  |  |  |             fileobj, (reader, writer) = key.fileobj, key.data | 
					
						
							|  |  |  |             if mask & selectors.EVENT_READ and reader is not None: | 
					
						
							|  |  |  |                 if reader._cancelled: | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |                     self._remove_reader(fileobj) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     self._add_callback(reader) | 
					
						
							|  |  |  |             if mask & selectors.EVENT_WRITE and writer is not None: | 
					
						
							|  |  |  |                 if writer._cancelled: | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |                     self._remove_writer(fileobj) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     self._add_callback(writer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _stop_serving(self, sock): | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._remove_reader(sock.fileno()) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         sock.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-18 18:41:13 -05:00
										 |  |  | class _SelectorTransport(transports._FlowControlMixin, | 
					
						
							|  |  |  |                          transports.Transport): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     max_size = 256 * 1024  # Buffer size passed to recv(). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |     _buffer_factory = bytearray  # Constructs initial value for self._buffer. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 17:50:58 +01:00
										 |  |  |     # Attribute used in the destructor: it must be set even if the constructor | 
					
						
							|  |  |  |     # is not called (see _SelectorSslTransport which may start by raising an | 
					
						
							|  |  |  |     # exception) | 
					
						
							|  |  |  |     _sock = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 02:56:05 +01:00
										 |  |  |     def __init__(self, loop, sock, protocol, extra=None, server=None): | 
					
						
							| 
									
										
										
										
											2014-11-05 15:27:41 +01:00
										 |  |  |         super().__init__(extra, loop) | 
					
						
							| 
									
										
										
										
											2019-05-27 15:57:20 +02:00
										 |  |  |         self._extra['socket'] = trsock.TransportSocket(sock) | 
					
						
							| 
									
										
										
										
											2019-05-07 19:18:49 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self._extra['sockname'] = sock.getsockname() | 
					
						
							|  |  |  |         except OSError: | 
					
						
							|  |  |  |             self._extra['sockname'] = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if 'peername' not in self._extra: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 self._extra['peername'] = sock.getpeername() | 
					
						
							|  |  |  |             except socket.error: | 
					
						
							|  |  |  |                 self._extra['peername'] = None | 
					
						
							|  |  |  |         self._sock = sock | 
					
						
							|  |  |  |         self._sock_fd = sock.fileno() | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self._protocol_connected = False | 
					
						
							|  |  |  |         self.set_protocol(protocol) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._server = server | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         self._buffer = self._buffer_factory() | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |         self._conn_lost = 0  # Set when call to connection_lost scheduled. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._closing = False  # Set when close() called. | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         if self._server is not None: | 
					
						
							| 
									
										
										
										
											2014-07-11 22:52:21 +02:00
										 |  |  |             self._server._attach() | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         loop._transports[self._sock_fd] = self | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2014-10-12 09:52:11 +02:00
										 |  |  |         info = [self.__class__.__name__] | 
					
						
							|  |  |  |         if self._sock is None: | 
					
						
							|  |  |  |             info.append('closed') | 
					
						
							|  |  |  |         elif self._closing: | 
					
						
							|  |  |  |             info.append('closing') | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |         info.append(f'fd={self._sock_fd}') | 
					
						
							| 
									
										
										
										
											2014-08-25 23:20:52 +02:00
										 |  |  |         # test if the transport was closed | 
					
						
							| 
									
										
										
										
											2015-03-27 15:20:08 +01:00
										 |  |  |         if self._loop is not None and not self._loop.is_closed(): | 
					
						
							| 
									
										
										
										
											2014-08-25 23:20:52 +02:00
										 |  |  |             polling = _test_selector_event(self._loop._selector, | 
					
						
							|  |  |  |                                            self._sock_fd, selectors.EVENT_READ) | 
					
						
							|  |  |  |             if polling: | 
					
						
							|  |  |  |                 info.append('read=polling') | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 info.append('read=idle') | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-25 23:20:52 +02:00
										 |  |  |             polling = _test_selector_event(self._loop._selector, | 
					
						
							| 
									
										
										
										
											2015-01-09 00:09:10 +01:00
										 |  |  |                                            self._sock_fd, | 
					
						
							|  |  |  |                                            selectors.EVENT_WRITE) | 
					
						
							| 
									
										
										
										
											2014-08-25 23:20:52 +02:00
										 |  |  |             if polling: | 
					
						
							|  |  |  |                 state = 'polling' | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 state = 'idle' | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-25 23:20:52 +02:00
										 |  |  |             bufsize = self.get_write_buffer_size() | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             info.append(f'write=<{state}, bufsize={bufsize}>') | 
					
						
							|  |  |  |         return '<{}>'.format(' '.join(info)) | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def abort(self): | 
					
						
							|  |  |  |         self._force_close(None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 21:11:02 -04:00
										 |  |  |     def set_protocol(self, protocol): | 
					
						
							|  |  |  |         self._protocol = protocol | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |         self._protocol_connected = True | 
					
						
							| 
									
										
										
										
											2016-09-11 21:11:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_protocol(self): | 
					
						
							|  |  |  |         return self._protocol | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-16 12:43:21 -05:00
										 |  |  |     def is_closing(self): | 
					
						
							|  |  |  |         return self._closing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def close(self): | 
					
						
							|  |  |  |         if self._closing: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self._closing = True | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._loop._remove_reader(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if not self._buffer: | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |             self._conn_lost += 1 | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._remove_writer(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._loop.call_soon(self._call_connection_lost, None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 11:24:40 +01:00
										 |  |  |     def __del__(self, _warn=warnings.warn): | 
					
						
							| 
									
										
										
										
											2017-04-25 10:57:18 +09:00
										 |  |  |         if self._sock is not None: | 
					
						
							| 
									
										
										
										
											2019-01-10 11:24:40 +01:00
										 |  |  |             _warn(f"unclosed transport {self!r}", ResourceWarning, source=self) | 
					
						
							| 
									
										
										
										
											2017-04-25 10:57:18 +09:00
										 |  |  |             self._sock.close() | 
					
						
							| 
									
										
										
										
											2015-01-29 17:50:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |     def _fatal_error(self, exc, message='Fatal error on transport'): | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |         # Should be called from exception handler only. | 
					
						
							| 
									
										
										
										
											2019-05-27 16:28:34 +03:00
										 |  |  |         if isinstance(exc, OSError): | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  |             if self._loop.get_debug(): | 
					
						
							|  |  |  |                 logger.debug("%r: %s", self, message, exc_info=True) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2014-02-18 18:02:19 -05:00
										 |  |  |             self._loop.call_exception_handler({ | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |                 'message': message, | 
					
						
							| 
									
										
										
										
											2014-02-18 18:02:19 -05:00
										 |  |  |                 'exception': exc, | 
					
						
							|  |  |  |                 'transport': self, | 
					
						
							|  |  |  |                 'protocol': self._protocol, | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._force_close(exc) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _force_close(self, exc): | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if self._buffer: | 
					
						
							|  |  |  |             self._buffer.clear() | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._remove_writer(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |         if not self._closing: | 
					
						
							|  |  |  |             self._closing = True | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._remove_reader(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._conn_lost += 1 | 
					
						
							|  |  |  |         self._loop.call_soon(self._call_connection_lost, exc) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _call_connection_lost(self, exc): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2015-01-29 02:56:05 +01:00
										 |  |  |             if self._protocol_connected: | 
					
						
							|  |  |  |                 self._protocol.connection_lost(exc) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         finally: | 
					
						
							|  |  |  |             self._sock.close() | 
					
						
							|  |  |  |             self._sock = None | 
					
						
							|  |  |  |             self._protocol = None | 
					
						
							|  |  |  |             self._loop = None | 
					
						
							|  |  |  |             server = self._server | 
					
						
							|  |  |  |             if server is not None: | 
					
						
							| 
									
										
										
										
											2014-07-11 22:52:21 +02:00
										 |  |  |                 server._detach() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 self._server = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |     def get_write_buffer_size(self): | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         return len(self._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-21 11:13:45 +03:00
										 |  |  |     def _add_reader(self, fd, callback, *args): | 
					
						
							|  |  |  |         if self._closing: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._loop._add_reader(fd, callback, *args) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class _SelectorSocketTransport(_SelectorTransport): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 00:35:36 -05:00
										 |  |  |     _start_tls_compatible = True | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |     _sendfile_compatible = constants._SendfileMode.TRY_NATIVE | 
					
						
							| 
									
										
										
										
											2017-12-30 00:35:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def __init__(self, loop, sock, protocol, waiter=None, | 
					
						
							|  |  |  |                  extra=None, server=None): | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |         self._read_ready_cb = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         super().__init__(loop, sock, protocol, extra, server) | 
					
						
							|  |  |  |         self._eof = False | 
					
						
							|  |  |  |         self._paused = False | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |         self._empty_waiter = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 21:39:31 -04:00
										 |  |  |         # Disable the Nagle algorithm -- small writes will be | 
					
						
							|  |  |  |         # sent without waiting for the TCP ACK.  This generally | 
					
						
							|  |  |  |         # decreases the latency (in some cases significantly.) | 
					
						
							| 
									
										
										
										
											2018-12-03 21:08:13 +02:00
										 |  |  |         base_events._set_nodelay(self._sock) | 
					
						
							| 
									
										
										
										
											2016-09-11 21:39:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._loop.call_soon(self._protocol.connection_made, self) | 
					
						
							| 
									
										
										
										
											2015-01-29 00:36:51 +01:00
										 |  |  |         # only start reading when connection_made() has been called | 
					
						
							| 
									
										
										
										
											2018-05-21 11:13:45 +03:00
										 |  |  |         self._loop.call_soon(self._add_reader, | 
					
						
							| 
									
										
										
										
											2015-01-29 00:36:51 +01:00
										 |  |  |                              self._sock_fd, self._read_ready) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if waiter is not None: | 
					
						
							| 
									
										
										
										
											2015-01-29 00:36:35 +01:00
										 |  |  |             # only wake up the waiter when connection_made() has been called | 
					
						
							| 
									
										
										
										
											2015-11-17 12:19:41 -05:00
										 |  |  |             self._loop.call_soon(futures._set_result_unless_cancelled, | 
					
						
							|  |  |  |                                  waiter, None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |     def set_protocol(self, protocol): | 
					
						
							|  |  |  |         if isinstance(protocol, protocols.BufferedProtocol): | 
					
						
							|  |  |  |             self._read_ready_cb = self._read_ready__get_buffer | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._read_ready_cb = self._read_ready__data_received | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         super().set_protocol(protocol) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  |     def is_reading(self): | 
					
						
							|  |  |  |         return not self._paused and not self._closing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 07:58:20 -07:00
										 |  |  |     def pause_reading(self): | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  |         if self._closing or self._paused: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._paused = True | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |         self._loop._remove_reader(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  |         if self._loop.get_debug(): | 
					
						
							|  |  |  |             logger.debug("%r pauses reading", self) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 07:58:20 -07:00
										 |  |  |     def resume_reading(self): | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  |         if self._closing or not self._paused: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-12-18 17:03:23 -05:00
										 |  |  |         self._paused = False | 
					
						
							| 
									
										
										
										
											2018-05-21 11:13:45 +03:00
										 |  |  |         self._add_reader(self._sock_fd, self._read_ready) | 
					
						
							| 
									
										
										
										
											2014-07-12 03:11:53 +02:00
										 |  |  |         if self._loop.get_debug(): | 
					
						
							|  |  |  |             logger.debug("%r resumes reading", self) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |     def _read_ready(self): | 
					
						
							|  |  |  |         self._read_ready_cb() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |     def _read_ready__get_buffer(self): | 
					
						
							|  |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2018-05-28 14:31:28 -04:00
										 |  |  |             buf = self._protocol.get_buffer(-1) | 
					
						
							|  |  |  |             if not len(buf): | 
					
						
							|  |  |  |                 raise RuntimeError('get_buffer() returned an empty buffer') | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self._fatal_error( | 
					
						
							|  |  |  |                 exc, 'Fatal error: protocol.get_buffer() call failed.') | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             nbytes = self._sock.recv_into(buf) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self._fatal_error(exc, 'Fatal read error on socket transport') | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not nbytes: | 
					
						
							|  |  |  |             self._read_ready__on_eof() | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self._protocol.buffer_updated(nbytes) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self._fatal_error( | 
					
						
							|  |  |  |                 exc, 'Fatal error: protocol.buffer_updated() call failed.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _read_ready__data_received(self): | 
					
						
							| 
									
										
										
										
											2016-06-11 11:19:47 -04:00
										 |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             data = self._sock.recv(self.max_size) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |             self._fatal_error(exc, 'Fatal read error on socket transport') | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not data: | 
					
						
							|  |  |  |             self._read_ready__on_eof() | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self._protocol.data_received(data) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self._fatal_error( | 
					
						
							|  |  |  |                 exc, 'Fatal error: protocol.data_received() call failed.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _read_ready__on_eof(self): | 
					
						
							|  |  |  |         if self._loop.get_debug(): | 
					
						
							|  |  |  |             logger.debug("%r received EOF", self) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             keep_open = self._protocol.eof_received() | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self._fatal_error( | 
					
						
							|  |  |  |                 exc, 'Fatal error: protocol.eof_received() call failed.') | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if keep_open: | 
					
						
							|  |  |  |             # We're keeping the connection open so the | 
					
						
							|  |  |  |             # protocol can write more, but we still can't | 
					
						
							|  |  |  |             # receive more, so remove the reader callback. | 
					
						
							|  |  |  |             self._loop._remove_reader(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2018-01-28 16:30:26 -05:00
										 |  |  |             self.close() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def write(self, data): | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         if not isinstance(data, (bytes, bytearray, memoryview)): | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             raise TypeError(f'data argument must be a bytes-like object, ' | 
					
						
							|  |  |  |                             f'not {type(data).__name__!r}') | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         if self._eof: | 
					
						
							|  |  |  |             raise RuntimeError('Cannot call write() after write_eof()') | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |         if self._empty_waiter is not None: | 
					
						
							|  |  |  |             raise RuntimeError('unable to write; sendfile is in progress') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if not data: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES: | 
					
						
							| 
									
										
										
										
											2013-10-17 15:39:45 -07:00
										 |  |  |                 logger.warning('socket.send() raised exception.') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._conn_lost += 1 | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not self._buffer: | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |             # Optimization: try to send now. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 n = self._sock.send(data) | 
					
						
							|  |  |  |             except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |                 pass | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |             except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |             except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |                 self._fatal_error(exc, 'Fatal write error on socket transport') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 data = data[n:] | 
					
						
							|  |  |  |                 if not data: | 
					
						
							|  |  |  |                     return | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |             # Not all was written; register write handler. | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._add_writer(self._sock_fd, self._write_ready) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         # Add it to the buffer. | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         self._buffer.extend(data) | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         self._maybe_pause_protocol() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _write_ready(self): | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         assert self._buffer, 'Data should not be empty' | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 11:19:47 -04:00
										 |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |             n = self._sock.send(self._buffer) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |             pass | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._remove_writer(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |             self._buffer.clear() | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |             self._fatal_error(exc, 'Fatal write error on socket transport') | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |             if self._empty_waiter is not None: | 
					
						
							|  |  |  |                 self._empty_waiter.set_exception(exc) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |             if n: | 
					
						
							|  |  |  |                 del self._buffer[:n] | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |             self._maybe_resume_protocol()  # May append to buffer. | 
					
						
							|  |  |  |             if not self._buffer: | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |                 self._loop._remove_writer(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |                 if self._empty_waiter is not None: | 
					
						
							|  |  |  |                     self._empty_waiter.set_result(None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 if self._closing: | 
					
						
							|  |  |  |                     self._call_connection_lost(None) | 
					
						
							|  |  |  |                 elif self._eof: | 
					
						
							|  |  |  |                     self._sock.shutdown(socket.SHUT_WR) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def write_eof(self): | 
					
						
							| 
									
										
										
										
											2018-05-28 23:16:45 +08:00
										 |  |  |         if self._closing or self._eof: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return | 
					
						
							|  |  |  |         self._eof = True | 
					
						
							|  |  |  |         if not self._buffer: | 
					
						
							|  |  |  |             self._sock.shutdown(socket.SHUT_WR) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def can_write_eof(self): | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 21:22:47 +02:00
										 |  |  |     def _call_connection_lost(self, exc): | 
					
						
							|  |  |  |         super()._call_connection_lost(exc) | 
					
						
							|  |  |  |         if self._empty_waiter is not None: | 
					
						
							|  |  |  |             self._empty_waiter.set_exception( | 
					
						
							|  |  |  |                 ConnectionError("Connection is closed by peer")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _make_empty_waiter(self): | 
					
						
							|  |  |  |         if self._empty_waiter is not None: | 
					
						
							|  |  |  |             raise RuntimeError("Empty waiter is already set") | 
					
						
							|  |  |  |         self._empty_waiter = self._loop.create_future() | 
					
						
							|  |  |  |         if not self._buffer: | 
					
						
							|  |  |  |             self._empty_waiter.set_result(None) | 
					
						
							|  |  |  |         return self._empty_waiter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _reset_empty_waiter(self): | 
					
						
							|  |  |  |         self._empty_waiter = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class _SelectorDatagramTransport(_SelectorTransport): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |     _buffer_factory = collections.deque | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-08 23:57:31 +02:00
										 |  |  |     def __init__(self, loop, sock, protocol, address=None, | 
					
						
							|  |  |  |                  waiter=None, extra=None): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         super().__init__(loop, sock, protocol, extra) | 
					
						
							|  |  |  |         self._address = address | 
					
						
							|  |  |  |         self._loop.call_soon(self._protocol.connection_made, self) | 
					
						
							| 
									
										
										
										
											2015-01-29 02:56:05 +01:00
										 |  |  |         # only start reading when connection_made() has been called | 
					
						
							| 
									
										
										
										
											2018-05-21 11:13:45 +03:00
										 |  |  |         self._loop.call_soon(self._add_reader, | 
					
						
							| 
									
										
										
										
											2015-01-29 02:56:05 +01:00
										 |  |  |                              self._sock_fd, self._read_ready) | 
					
						
							| 
									
										
										
										
											2014-07-08 23:57:31 +02:00
										 |  |  |         if waiter is not None: | 
					
						
							| 
									
										
										
										
											2015-01-29 00:36:35 +01:00
										 |  |  |             # only wake up the waiter when connection_made() has been called | 
					
						
							| 
									
										
										
										
											2015-11-17 12:19:41 -05:00
										 |  |  |             self._loop.call_soon(futures._set_result_unless_cancelled, | 
					
						
							|  |  |  |                                  waiter, None) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |     def get_write_buffer_size(self): | 
					
						
							|  |  |  |         return sum(len(data) for data, _ in self._buffer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def _read_ready(self): | 
					
						
							| 
									
										
										
										
											2016-06-11 11:19:47 -04:00
										 |  |  |         if self._conn_lost: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             data, addr = self._sock.recvfrom(self.max_size) | 
					
						
							|  |  |  |         except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2013-11-15 16:51:48 -08:00
										 |  |  |         except OSError as exc: | 
					
						
							|  |  |  |             self._protocol.error_received(exc) | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |         except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2014-02-19 01:40:41 +01:00
										 |  |  |             self._fatal_error(exc, 'Fatal read error on datagram transport') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self._protocol.datagram_received(data, addr) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def sendto(self, data, addr=None): | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         if not isinstance(data, (bytes, bytearray, memoryview)): | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             raise TypeError(f'data argument must be a bytes-like object, ' | 
					
						
							|  |  |  |                             f'not {type(data).__name__!r}') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if not data: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 19:18:49 +02:00
										 |  |  |         if self._address: | 
					
						
							|  |  |  |             if addr not in (None, self._address): | 
					
						
							|  |  |  |                 raise ValueError( | 
					
						
							|  |  |  |                     f'Invalid address: must be None or {self._address}') | 
					
						
							|  |  |  |             addr = self._address | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if self._conn_lost and self._address: | 
					
						
							|  |  |  |             if self._conn_lost >= constants.LOG_THRESHOLD_FOR_CONNLOST_WRITES: | 
					
						
							| 
									
										
										
										
											2013-10-17 15:39:45 -07:00
										 |  |  |                 logger.warning('socket.send() raised exception.') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._conn_lost += 1 | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not self._buffer: | 
					
						
							|  |  |  |             # Attempt to send it right away first. | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2019-05-07 19:18:49 +02:00
										 |  |  |                 if self._extra['peername']: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                     self._sock.send(data) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self._sock.sendto(data, addr) | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |             except (BlockingIOError, InterruptedError): | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |                 self._loop._add_writer(self._sock_fd, self._sendto_ready) | 
					
						
							| 
									
										
										
										
											2013-11-15 16:51:48 -08:00
										 |  |  |             except OSError as exc: | 
					
						
							|  |  |  |                 self._protocol.error_received(exc) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |             except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |             except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                 self._fatal_error( | 
					
						
							|  |  |  |                     exc, 'Fatal write error on datagram transport') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 14:12:48 -08:00
										 |  |  |         # Ensure that what we buffer is immutable. | 
					
						
							|  |  |  |         self._buffer.append((bytes(data), addr)) | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         self._maybe_pause_protocol() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _sendto_ready(self): | 
					
						
							|  |  |  |         while self._buffer: | 
					
						
							|  |  |  |             data, addr = self._buffer.popleft() | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2019-05-07 19:18:49 +02:00
										 |  |  |                 if self._extra['peername']: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                     self._sock.send(data) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     self._sock.sendto(data, addr) | 
					
						
							| 
									
										
										
										
											2013-10-18 10:10:36 -07:00
										 |  |  |             except (BlockingIOError, InterruptedError): | 
					
						
							|  |  |  |                 self._buffer.appendleft((data, addr))  # Try again later. | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2013-11-15 16:51:48 -08:00
										 |  |  |             except OSError as exc: | 
					
						
							|  |  |  |                 self._protocol.error_received(exc) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2019-05-27 14:45:12 +02:00
										 |  |  |             except (SystemExit, KeyboardInterrupt): | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |             except BaseException as exc: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |                 self._fatal_error( | 
					
						
							|  |  |  |                     exc, 'Fatal write error on datagram transport') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 15:17:11 -07:00
										 |  |  |         self._maybe_resume_protocol()  # May append to buffer. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if not self._buffer: | 
					
						
							| 
									
										
										
										
											2016-10-05 17:48:59 -04:00
										 |  |  |             self._loop._remove_writer(self._sock_fd) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             if self._closing: | 
					
						
							|  |  |  |                 self._call_connection_lost(None) |