|
14 | 14 | Implementation Notes
|
15 | 15 | --------------------
|
16 | 16 |
|
17 |
| -**Hardware:** |
18 |
| -
|
19 |
| -.. todo:: Add links to any specific hardware product page(s), or category page(s). |
20 |
| - Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_" |
21 |
| -
|
22 | 17 | **Software and Dependencies:**
|
23 | 18 |
|
24 | 19 | * Adafruit CircuitPython firmware for the supported boards:
|
25 | 20 | https://circuitpython.org/downloads
|
26 | 21 |
|
27 |
| -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies |
28 |
| - based on the library's use of either. |
29 |
| -
|
30 |
| -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
31 |
| -# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
32 | 22 | """
|
33 | 23 |
|
34 | 24 | # imports
|
35 | 25 |
|
36 | 26 | __version__ = "0.0.0+auto.0"
|
37 | 27 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager.git"
|
| 28 | + |
| 29 | +import errno |
| 30 | +import sys |
| 31 | + |
| 32 | +if not sys.implementation.name == "circuitpython": |
| 33 | + from ssl import SSLContext |
| 34 | + from types import ModuleType |
| 35 | + from typing import Any, Optional, Tuple, Union |
| 36 | + |
| 37 | + try: |
| 38 | + from typing import Protocol |
| 39 | + except ImportError: |
| 40 | + from typing_extensions import Protocol |
| 41 | + |
| 42 | + # Based on https://github.com/python/typeshed/blob/master/stdlib/_socket.pyi |
| 43 | + class CommonSocketType(Protocol): |
| 44 | + """Describes the common structure every socket type must have.""" |
| 45 | + |
| 46 | + def send(self, data: bytes, flags: int = ...) -> None: |
| 47 | + """Send data to the socket. The meaning of the optional flags kwarg is |
| 48 | + implementation-specific.""" |
| 49 | + |
| 50 | + def settimeout(self, value: Optional[float]) -> None: |
| 51 | + """Set a timeout on blocking socket operations.""" |
| 52 | + |
| 53 | + def close(self) -> None: |
| 54 | + """Close the socket.""" |
| 55 | + |
| 56 | + class CommonCircuitPythonSocketType(CommonSocketType, Protocol): |
| 57 | + """Describes the common structure every CircuitPython socket type must have.""" |
| 58 | + |
| 59 | + def connect( |
| 60 | + self, |
| 61 | + address: Tuple[str, int], |
| 62 | + conntype: Optional[int] = ..., |
| 63 | + ) -> None: |
| 64 | + """Connect to a remote socket at the provided (host, port) address. The conntype |
| 65 | + kwarg optionally may indicate SSL or not, depending on the underlying interface. |
| 66 | + """ |
| 67 | + |
| 68 | + class SupportsRecvWithFlags(Protocol): |
| 69 | + """Describes a type that posseses a socket recv() method supporting the flags kwarg.""" |
| 70 | + |
| 71 | + def recv(self, bufsize: int = ..., flags: int = ...) -> bytes: |
| 72 | + """Receive data from the socket. The return value is a bytes object representing |
| 73 | + the data received. The maximum amount of data to be received at once is specified |
| 74 | + by bufsize. The meaning of the optional flags kwarg is implementation-specific. |
| 75 | + """ |
| 76 | + |
| 77 | + class SupportsRecvInto(Protocol): |
| 78 | + """Describes a type that possesses a socket recv_into() method.""" |
| 79 | + |
| 80 | + def recv_into( |
| 81 | + self, buffer: bytearray, nbytes: int = ..., flags: int = ... |
| 82 | + ) -> int: |
| 83 | + """Receive up to nbytes bytes from the socket, storing the data into the provided |
| 84 | + buffer. If nbytes is not specified (or 0), receive up to the size available in the |
| 85 | + given buffer. The meaning of the optional flags kwarg is implementation-specific. |
| 86 | + Returns the number of bytes received.""" |
| 87 | + |
| 88 | + class CircuitPythonSocketType( |
| 89 | + CommonCircuitPythonSocketType, |
| 90 | + SupportsRecvInto, |
| 91 | + SupportsRecvWithFlags, |
| 92 | + Protocol, |
| 93 | + ): |
| 94 | + """Describes the structure every modern CircuitPython socket type must have.""" |
| 95 | + |
| 96 | + class StandardPythonSocketType( |
| 97 | + CommonSocketType, SupportsRecvInto, SupportsRecvWithFlags, Protocol |
| 98 | + ): |
| 99 | + """Describes the structure every standard Python socket type must have.""" |
| 100 | + |
| 101 | + def connect(self, address: Union[Tuple[Any, ...], str, bytes]) -> None: |
| 102 | + """Connect to a remote socket at the provided address.""" |
| 103 | + |
| 104 | + SocketType = Union[ |
| 105 | + CircuitPythonSocketType, |
| 106 | + StandardPythonSocketType, |
| 107 | + ] |
| 108 | + |
| 109 | + SocketpoolModuleType = ModuleType |
| 110 | + |
| 111 | + class InterfaceType(Protocol): |
| 112 | + """Describes the structure every interface type must have.""" |
| 113 | + |
| 114 | + @property |
| 115 | + def TLS_MODE(self) -> int: # pylint: disable=invalid-name |
| 116 | + """Constant representing that a socket's connection mode is TLS.""" |
| 117 | + |
| 118 | + SSLContextType = Union[SSLContext, "_FakeSSLContext"] |
| 119 | + |
| 120 | + |
| 121 | +class SocketGetOSError(OSError): |
| 122 | + """ConnectionManager Exception class.""" |
| 123 | + |
| 124 | + |
| 125 | +class SocketGetRuntimeError(RuntimeError): |
| 126 | + """ConnectionManager Exception class.""" |
| 127 | + |
| 128 | + |
| 129 | +class SocketConnectMemoryError(OSError): |
| 130 | + """ConnectionManager Exception class.""" |
| 131 | + |
| 132 | + |
| 133 | +class SocketConnectOSError(OSError): |
| 134 | + """ConnectionManager Exception class.""" |
| 135 | + |
| 136 | + |
| 137 | +class _FakeSSLSocket: |
| 138 | + def __init__(self, socket: CircuitPythonSocketType, tls_mode: int) -> None: |
| 139 | + self._socket = socket |
| 140 | + self._mode = tls_mode |
| 141 | + self.settimeout = socket.settimeout |
| 142 | + self.send = socket.send |
| 143 | + self.recv = socket.recv |
| 144 | + self.close = socket.close |
| 145 | + self.recv_into = socket.recv_into |
| 146 | + |
| 147 | + def connect(self, address: Tuple[str, int]) -> None: |
| 148 | + """connect wrapper to add non-standard mode parameter""" |
| 149 | + try: |
| 150 | + return self._socket.connect(address, self._mode) |
| 151 | + except RuntimeError as error: |
| 152 | + raise OSError(errno.ENOMEM) from error |
| 153 | + |
| 154 | + |
| 155 | +class _FakeSSLContext: |
| 156 | + def __init__(self, iface: InterfaceType) -> None: |
| 157 | + self._iface = iface |
| 158 | + |
| 159 | + def wrap_socket( |
| 160 | + self, socket: CircuitPythonSocketType, server_hostname: Optional[str] = None |
| 161 | + ) -> _FakeSSLSocket: |
| 162 | + """Return the same socket""" |
| 163 | + # pylint: disable=unused-argument |
| 164 | + return _FakeSSLSocket(socket, self._iface.TLS_MODE) |
| 165 | + |
| 166 | + |
| 167 | +def create_fake_ssl_context( |
| 168 | + socket_pool: SocketpoolModuleType, iface: Optional[InterfaceType] = None |
| 169 | +) -> _FakeSSLContext: |
| 170 | + """Legacy API for creating a fake SSL context""" |
| 171 | + if not iface: |
| 172 | + # pylint: disable=protected-access |
| 173 | + iface = socket_pool._the_interface |
| 174 | + socket_pool.set_interface(iface) |
| 175 | + return _FakeSSLContext(iface) |
| 176 | + |
| 177 | + |
| 178 | +class ConnectionManager: |
| 179 | + """Connection manager for sharing sockets.""" |
| 180 | + |
| 181 | + def __init__( |
| 182 | + self, |
| 183 | + socket_pool: SocketpoolModuleType, |
| 184 | + ) -> None: |
| 185 | + self._socket_pool = socket_pool |
| 186 | + # Hang onto open sockets so that we can reuse them. |
| 187 | + self._open_sockets = {} |
| 188 | + self._socket_free = {} |
| 189 | + |
| 190 | + def _free_sockets(self) -> None: |
| 191 | + free_sockets = [] |
| 192 | + for socket, val in self._socket_free.items(): |
| 193 | + if val: |
| 194 | + free_sockets.append(socket) |
| 195 | + |
| 196 | + for socket in free_sockets: |
| 197 | + self.close_socket(socket) |
| 198 | + |
| 199 | + def free_socket(self, socket: SocketType) -> None: |
| 200 | + """Mark a socket as free so it can be reused if needed""" |
| 201 | + if socket not in self._open_sockets.values(): |
| 202 | + raise RuntimeError("Socket not from session") |
| 203 | + self._socket_free[socket] = True |
| 204 | + |
| 205 | + def close_socket(self, socket: SocketType) -> None: |
| 206 | + """Close a socket""" |
| 207 | + socket.close() |
| 208 | + del self._socket_free[socket] |
| 209 | + key = None |
| 210 | + for k, value in self._open_sockets.items(): |
| 211 | + if value == socket: |
| 212 | + key = k |
| 213 | + break |
| 214 | + if key: |
| 215 | + del self._open_sockets[key] |
| 216 | + |
| 217 | + # pylint: disable=too-many-locals,too-many-statements |
| 218 | + def get_socket( |
| 219 | + self, |
| 220 | + host: str, |
| 221 | + port: int, |
| 222 | + proto: str, |
| 223 | + *, |
| 224 | + timeout: float = 1, |
| 225 | + is_ssl: bool = False, |
| 226 | + ssl_context: Optional[SSLContextType] = None, |
| 227 | + max_retries: int = 5, |
| 228 | + exception_passthrough: bool = False, |
| 229 | + ) -> CircuitPythonSocketType: |
| 230 | + """Get socket and connect""" |
| 231 | + # pylint: disable=too-many-branches |
| 232 | + key = (host, port, proto) |
| 233 | + if key in self._open_sockets: |
| 234 | + socket = self._open_sockets[key] |
| 235 | + if self._socket_free[socket]: |
| 236 | + self._socket_free[socket] = False |
| 237 | + return socket |
| 238 | + |
| 239 | + if proto == "https:": |
| 240 | + is_ssl = True |
| 241 | + if is_ssl and not ssl_context: |
| 242 | + raise RuntimeError( |
| 243 | + "ssl_context must be set before using adafruit_requests for https" |
| 244 | + ) |
| 245 | + |
| 246 | + addr_info = self._socket_pool.getaddrinfo( |
| 247 | + host, port, 0, self._socket_pool.SOCK_STREAM |
| 248 | + )[0] |
| 249 | + |
| 250 | + retry_count = 0 |
| 251 | + socket = None |
| 252 | + last_exc = None |
| 253 | + last_exc_new_type = None |
| 254 | + while retry_count < max_retries and socket is None: |
| 255 | + if retry_count > 0: |
| 256 | + if any(self._socket_free.items()): |
| 257 | + self._free_sockets() |
| 258 | + else: |
| 259 | + raise RuntimeError("Sending request failed") from last_exc |
| 260 | + retry_count += 1 |
| 261 | + |
| 262 | + try: |
| 263 | + socket = self._socket_pool.socket(addr_info[0], addr_info[1]) |
| 264 | + except OSError as exc: |
| 265 | + last_exc_new_type = SocketGetOSError |
| 266 | + last_exc = exc |
| 267 | + continue |
| 268 | + except RuntimeError as exc: |
| 269 | + last_exc_new_type = SocketGetRuntimeError |
| 270 | + last_exc = exc |
| 271 | + continue |
| 272 | + |
| 273 | + connect_host = addr_info[-1][0] |
| 274 | + if is_ssl: |
| 275 | + socket = ssl_context.wrap_socket(socket, server_hostname=host) |
| 276 | + connect_host = host |
| 277 | + socket.settimeout(timeout) # socket read timeout |
| 278 | + |
| 279 | + try: |
| 280 | + socket.connect((connect_host, port)) |
| 281 | + except MemoryError as exc: |
| 282 | + last_exc_new_type = SocketConnectMemoryError |
| 283 | + last_exc = exc |
| 284 | + socket.close() |
| 285 | + socket = None |
| 286 | + except OSError as exc: |
| 287 | + last_exc_new_type = SocketConnectOSError |
| 288 | + last_exc = exc |
| 289 | + socket.close() |
| 290 | + socket = None |
| 291 | + |
| 292 | + if socket is None: |
| 293 | + if exception_passthrough: |
| 294 | + raise last_exc_new_type("Repeated socket failures") from last_exc |
| 295 | + raise RuntimeError("Repeated socket failures") from last_exc |
| 296 | + |
| 297 | + self._open_sockets[key] = socket |
| 298 | + self._socket_free[socket] = False |
| 299 | + return socket |
| 300 | + |
| 301 | + |
| 302 | +_global_connection_manager = None # pylint: disable=invalid-name |
| 303 | + |
| 304 | + |
| 305 | +def get_connection_manager(socket_pool: SocketpoolModuleType) -> None: |
| 306 | + """Get the ConnectionManager singleton""" |
| 307 | + global _global_connection_manager # pylint: disable=global-statement |
| 308 | + if _global_connection_manager is None: |
| 309 | + _global_connection_manager = ConnectionManager(socket_pool) |
| 310 | + return _global_connection_manager |
0 commit comments