Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 1 | """Abstract Transport class.""" |
| 2 | |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 3 | __all__ = ( |
| 4 | 'BaseTransport', 'ReadTransport', 'WriteTransport', |
| 5 | 'Transport', 'DatagramTransport', 'SubprocessTransport', |
| 6 | ) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 7 | |
| 8 | |
| 9 | class BaseTransport: |
Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 10 | """Base class for transports.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 11 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 12 | __slots__ = ('_extra',) |
| 13 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 14 | def __init__(self, extra=None): |
| 15 | if extra is None: |
| 16 | extra = {} |
| 17 | self._extra = extra |
| 18 | |
| 19 | def get_extra_info(self, name, default=None): |
| 20 | """Get optional transport information.""" |
| 21 | return self._extra.get(name, default) |
| 22 | |
Yury Selivanov | 5bb1afb | 2015-11-16 12:43:21 -0500 | [diff] [blame] | 23 | def is_closing(self): |
| 24 | """Return True if the transport is closing or closed.""" |
| 25 | raise NotImplementedError |
| 26 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 27 | def close(self): |
Antoine Pitrou | dec4338 | 2013-11-23 12:30:00 +0100 | [diff] [blame] | 28 | """Close the transport. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 29 | |
| 30 | Buffered data will be flushed asynchronously. No more data |
| 31 | will be received. After all buffered data is flushed, the |
Miss Islington (bot) | a2d00f0 | 2020-08-21 05:19:40 -0700 | [diff] [blame] | 32 | protocol's connection_lost() method will (eventually) be |
| 33 | called with None as its argument. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 34 | """ |
| 35 | raise NotImplementedError |
| 36 | |
Yury Selivanov | a05a6ef | 2016-09-11 21:11:02 -0400 | [diff] [blame] | 37 | def set_protocol(self, protocol): |
| 38 | """Set a new protocol.""" |
| 39 | raise NotImplementedError |
| 40 | |
| 41 | def get_protocol(self): |
| 42 | """Return the current protocol.""" |
| 43 | raise NotImplementedError |
| 44 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 45 | |
| 46 | class ReadTransport(BaseTransport): |
Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 47 | """Interface for read-only transports.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 48 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 49 | __slots__ = () |
| 50 | |
Yury Selivanov | d757aaf | 2017-12-18 17:03:23 -0500 | [diff] [blame] | 51 | def is_reading(self): |
| 52 | """Return True if the transport is receiving.""" |
| 53 | raise NotImplementedError |
| 54 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 55 | def pause_reading(self): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 56 | """Pause the receiving end. |
| 57 | |
| 58 | No data will be passed to the protocol's data_received() |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 59 | method until resume_reading() is called. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 60 | """ |
| 61 | raise NotImplementedError |
| 62 | |
Guido van Rossum | 57497ad | 2013-10-18 07:58:20 -0700 | [diff] [blame] | 63 | def resume_reading(self): |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 64 | """Resume the receiving end. |
| 65 | |
| 66 | Data received will once again be passed to the protocol's |
| 67 | data_received() method. |
| 68 | """ |
| 69 | raise NotImplementedError |
| 70 | |
| 71 | |
| 72 | class WriteTransport(BaseTransport): |
Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 73 | """Interface for write-only transports.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 74 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 75 | __slots__ = () |
| 76 | |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 77 | def set_write_buffer_limits(self, high=None, low=None): |
| 78 | """Set the high- and low-water limits for write flow control. |
| 79 | |
| 80 | These two values control when to call the protocol's |
| 81 | pause_writing() and resume_writing() methods. If specified, |
| 82 | the low-water limit must be less than or equal to the |
| 83 | high-water limit. Neither value can be negative. |
| 84 | |
| 85 | The defaults are implementation-specific. If only the |
Serhiy Storchaka | d65c949 | 2015-11-02 14:10:23 +0200 | [diff] [blame] | 86 | high-water limit is given, the low-water limit defaults to an |
Guido van Rossum | 355491d | 2013-10-18 15:17:11 -0700 | [diff] [blame] | 87 | implementation-specific value less than or equal to the |
| 88 | high-water limit. Setting high to zero forces low to zero as |
| 89 | well, and causes pause_writing() to be called whenever the |
| 90 | buffer becomes non-empty. Setting low to zero causes |
| 91 | resume_writing() to be called only once the buffer is empty. |
| 92 | Use of zero for either limit is generally sub-optimal as it |
| 93 | reduces opportunities for doing I/O and computation |
| 94 | concurrently. |
| 95 | """ |
| 96 | raise NotImplementedError |
| 97 | |
| 98 | def get_write_buffer_size(self): |
| 99 | """Return the current size of the write buffer.""" |
| 100 | raise NotImplementedError |
| 101 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 102 | def write(self, data): |
| 103 | """Write some data bytes to the transport. |
| 104 | |
| 105 | This does not block; it buffers the data and arranges for it |
| 106 | to be sent out asynchronously. |
| 107 | """ |
| 108 | raise NotImplementedError |
| 109 | |
| 110 | def writelines(self, list_of_data): |
| 111 | """Write a list (or any iterable) of data bytes to the transport. |
| 112 | |
Guido van Rossum | f10345e | 2013-12-02 18:36:30 -0800 | [diff] [blame] | 113 | The default implementation concatenates the arguments and |
| 114 | calls write() on the result. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 115 | """ |
INADA Naoki | 3e2ad8e | 2017-04-25 10:57:18 +0900 | [diff] [blame] | 116 | data = b''.join(list_of_data) |
Victor Stinner | 71080fc | 2015-07-25 02:23:21 +0200 | [diff] [blame] | 117 | self.write(data) |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 118 | |
| 119 | def write_eof(self): |
Antoine Pitrou | dec4338 | 2013-11-23 12:30:00 +0100 | [diff] [blame] | 120 | """Close the write end after flushing buffered data. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 121 | |
| 122 | (This is like typing ^D into a UNIX program reading from stdin.) |
| 123 | |
| 124 | Data may still be received. |
| 125 | """ |
| 126 | raise NotImplementedError |
| 127 | |
| 128 | def can_write_eof(self): |
Antoine Pitrou | dec4338 | 2013-11-23 12:30:00 +0100 | [diff] [blame] | 129 | """Return True if this transport supports write_eof(), False if not.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 130 | raise NotImplementedError |
| 131 | |
| 132 | def abort(self): |
Guido van Rossum | 488b0da | 2013-11-23 11:51:09 -0800 | [diff] [blame] | 133 | """Close the transport immediately. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 134 | |
| 135 | Buffered data will be lost. No more data will be received. |
| 136 | The protocol's connection_lost() method will (eventually) be |
| 137 | called with None as its argument. |
| 138 | """ |
| 139 | raise NotImplementedError |
| 140 | |
| 141 | |
| 142 | class Transport(ReadTransport, WriteTransport): |
Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 143 | """Interface representing a bidirectional transport. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 144 | |
| 145 | There may be several implementations, but typically, the user does |
| 146 | not implement new transports; rather, the platform provides some |
| 147 | useful transports that are implemented using the platform's best |
| 148 | practices. |
| 149 | |
| 150 | The user never instantiates a transport directly; they call a |
| 151 | utility function, passing it a protocol factory and other |
| 152 | information necessary to create the transport and protocol. (E.g. |
| 153 | EventLoop.create_connection() or EventLoop.create_server().) |
| 154 | |
| 155 | The utility function will asynchronously create a transport and a |
| 156 | protocol and hook them up by calling the protocol's |
| 157 | connection_made() method, passing it the transport. |
| 158 | |
| 159 | The implementation here raises NotImplemented for every method |
| 160 | except writelines(), which calls write() in a loop. |
| 161 | """ |
| 162 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 163 | __slots__ = () |
| 164 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 165 | |
| 166 | class DatagramTransport(BaseTransport): |
Guido van Rossum | 9204af4 | 2013-11-30 15:35:42 -0800 | [diff] [blame] | 167 | """Interface for datagram (UDP) transports.""" |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 168 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 169 | __slots__ = () |
| 170 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 171 | def sendto(self, data, addr=None): |
| 172 | """Send data to the transport. |
| 173 | |
| 174 | This does not block; it buffers the data and arranges for it |
| 175 | to be sent out asynchronously. |
| 176 | addr is target socket address. |
| 177 | If addr is None use target address pointed on transport creation. |
| 178 | """ |
| 179 | raise NotImplementedError |
| 180 | |
| 181 | def abort(self): |
Antoine Pitrou | dec4338 | 2013-11-23 12:30:00 +0100 | [diff] [blame] | 182 | """Close the transport immediately. |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 183 | |
| 184 | Buffered data will be lost. No more data will be received. |
| 185 | The protocol's connection_lost() method will (eventually) be |
| 186 | called with None as its argument. |
| 187 | """ |
| 188 | raise NotImplementedError |
| 189 | |
| 190 | |
| 191 | class SubprocessTransport(BaseTransport): |
| 192 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 193 | __slots__ = () |
| 194 | |
Guido van Rossum | 27b7c7e | 2013-10-17 13:40:50 -0700 | [diff] [blame] | 195 | def get_pid(self): |
| 196 | """Get subprocess id.""" |
| 197 | raise NotImplementedError |
| 198 | |
| 199 | def get_returncode(self): |
| 200 | """Get subprocess returncode. |
| 201 | |
| 202 | See also |
| 203 | http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode |
| 204 | """ |
| 205 | raise NotImplementedError |
| 206 | |
| 207 | def get_pipe_transport(self, fd): |
| 208 | """Get transport for pipe with number fd.""" |
| 209 | raise NotImplementedError |
| 210 | |
| 211 | def send_signal(self, signal): |
| 212 | """Send signal to subprocess. |
| 213 | |
| 214 | See also: |
| 215 | docs.python.org/3/library/subprocess#subprocess.Popen.send_signal |
| 216 | """ |
| 217 | raise NotImplementedError |
| 218 | |
| 219 | def terminate(self): |
| 220 | """Stop the subprocess. |
| 221 | |
| 222 | Alias for close() method. |
| 223 | |
| 224 | On Posix OSs the method sends SIGTERM to the subprocess. |
| 225 | On Windows the Win32 API function TerminateProcess() |
| 226 | is called to stop the subprocess. |
| 227 | |
| 228 | See also: |
| 229 | http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate |
| 230 | """ |
| 231 | raise NotImplementedError |
| 232 | |
| 233 | def kill(self): |
| 234 | """Kill the subprocess. |
| 235 | |
| 236 | On Posix OSs the function sends SIGKILL to the subprocess. |
| 237 | On Windows kill() is an alias for terminate(). |
| 238 | |
| 239 | See also: |
| 240 | http://docs.python.org/3/library/subprocess#subprocess.Popen.kill |
| 241 | """ |
| 242 | raise NotImplementedError |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 243 | |
| 244 | |
| 245 | class _FlowControlMixin(Transport): |
| 246 | """All the logic for (write) flow control in a mix-in base class. |
| 247 | |
| 248 | The subclass must implement get_write_buffer_size(). It must call |
| 249 | _maybe_pause_protocol() whenever the write buffer size increases, |
| 250 | and _maybe_resume_protocol() whenever it decreases. It may also |
| 251 | override set_write_buffer_limits() (e.g. to specify different |
| 252 | defaults). |
| 253 | |
| 254 | The subclass constructor must call super().__init__(extra). This |
| 255 | will call set_write_buffer_limits(). |
| 256 | |
| 257 | The user may call set_write_buffer_limits() and |
| 258 | get_write_buffer_size(), and their protocol's pause_writing() and |
| 259 | resume_writing() may be called. |
| 260 | """ |
| 261 | |
Andrew Svetlov | 9eb35ab | 2019-09-13 15:18:46 +0300 | [diff] [blame] | 262 | __slots__ = ('_loop', '_protocol_paused', '_high_water', '_low_water') |
| 263 | |
Victor Stinner | 004adb9 | 2014-11-05 15:27:41 +0100 | [diff] [blame] | 264 | def __init__(self, extra=None, loop=None): |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 265 | super().__init__(extra) |
Victor Stinner | 004adb9 | 2014-11-05 15:27:41 +0100 | [diff] [blame] | 266 | assert loop is not None |
| 267 | self._loop = loop |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 268 | self._protocol_paused = False |
Yury Selivanov | 1589920 | 2014-02-19 11:10:52 -0500 | [diff] [blame] | 269 | self._set_write_buffer_limits() |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 270 | |
| 271 | def _maybe_pause_protocol(self): |
| 272 | size = self.get_write_buffer_size() |
| 273 | if size <= self._high_water: |
| 274 | return |
| 275 | if not self._protocol_paused: |
| 276 | self._protocol_paused = True |
| 277 | try: |
| 278 | self._protocol.pause_writing() |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 279 | except (SystemExit, KeyboardInterrupt): |
| 280 | raise |
| 281 | except BaseException as exc: |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 282 | self._loop.call_exception_handler({ |
| 283 | 'message': 'protocol.pause_writing() failed', |
| 284 | 'exception': exc, |
| 285 | 'transport': self, |
| 286 | 'protocol': self._protocol, |
| 287 | }) |
| 288 | |
| 289 | def _maybe_resume_protocol(self): |
| 290 | if (self._protocol_paused and |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 291 | self.get_write_buffer_size() <= self._low_water): |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 292 | self._protocol_paused = False |
| 293 | try: |
| 294 | self._protocol.resume_writing() |
Yury Selivanov | 431b540 | 2019-05-27 14:45:12 +0200 | [diff] [blame] | 295 | except (SystemExit, KeyboardInterrupt): |
| 296 | raise |
| 297 | except BaseException as exc: |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 298 | self._loop.call_exception_handler({ |
| 299 | 'message': 'protocol.resume_writing() failed', |
| 300 | 'exception': exc, |
| 301 | 'transport': self, |
| 302 | 'protocol': self._protocol, |
| 303 | }) |
| 304 | |
Victor Stinner | 52bb949 | 2014-08-26 00:22:28 +0200 | [diff] [blame] | 305 | def get_write_buffer_limits(self): |
| 306 | return (self._low_water, self._high_water) |
| 307 | |
Yury Selivanov | 1589920 | 2014-02-19 11:10:52 -0500 | [diff] [blame] | 308 | def _set_write_buffer_limits(self, high=None, low=None): |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 309 | if high is None: |
| 310 | if low is None: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 311 | high = 64 * 1024 |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 312 | else: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 313 | high = 4 * low |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 314 | if low is None: |
| 315 | low = high // 4 |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 316 | |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 317 | if not high >= low >= 0: |
Yury Selivanov | 6370f34 | 2017-12-10 18:36:12 -0500 | [diff] [blame] | 318 | raise ValueError( |
| 319 | f'high ({high!r}) must be >= low ({low!r}) must be >= 0') |
| 320 | |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 321 | self._high_water = high |
| 322 | self._low_water = low |
| 323 | |
Yury Selivanov | 1589920 | 2014-02-19 11:10:52 -0500 | [diff] [blame] | 324 | def set_write_buffer_limits(self, high=None, low=None): |
| 325 | self._set_write_buffer_limits(high=high, low=low) |
| 326 | self._maybe_pause_protocol() |
| 327 | |
Yury Selivanov | 3cb9914 | 2014-02-18 18:41:13 -0500 | [diff] [blame] | 328 | def get_write_buffer_size(self): |
| 329 | raise NotImplementedError |