-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadafruit_connection_manager.py
325 lines (248 loc) · 10.3 KB
/
adafruit_connection_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_connection_manager`
================================================================================
A urllib3.poolmanager/urllib3.connectionpool-like library for managing sockets and connections
* Author(s): Justin Myers
Implementation Notes
--------------------
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://circuitpython.org/downloads
"""
# imports
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager.git"
import errno
import sys
WIZNET5K_SSL_SUPPORT_VERSION = (9, 1)
# typing
if not sys.implementation.name == "circuitpython":
from typing import Optional, Tuple
from circuitpython_typing.socket import (
CircuitPythonSocketType,
InterfaceType,
SocketpoolModuleType,
SocketType,
SSLContextType,
)
# ssl and pool helpers
class _FakeSSLSocket:
def __init__(self, socket: CircuitPythonSocketType, tls_mode: int) -> None:
self._socket = socket
self._mode = tls_mode
self.settimeout = socket.settimeout
self.send = socket.send
self.recv = socket.recv
self.close = socket.close
self.recv_into = socket.recv_into
if hasattr(socket, "_interface"):
self._interface = socket._interface
if hasattr(socket, "_socket_pool"):
self._socket_pool = socket._socket_pool
def connect(self, address: Tuple[str, int]) -> None:
"""Connect wrapper to add non-standard mode parameter"""
try:
return self._socket.connect(address, self._mode)
except RuntimeError as error:
raise OSError(errno.ENOMEM) from error
class _FakeSSLContext:
def __init__(self, iface: InterfaceType) -> None:
self._iface = iface
# pylint: disable=unused-argument
def wrap_socket(
self, socket: CircuitPythonSocketType, server_hostname: Optional[str] = None
) -> _FakeSSLSocket:
"""Return the same socket"""
if hasattr(self._iface, "TLS_MODE"):
return _FakeSSLSocket(socket, self._iface.TLS_MODE)
raise AttributeError("This radio does not support TLS/HTTPS")
def create_fake_ssl_context(
socket_pool: SocketpoolModuleType, iface: InterfaceType
) -> _FakeSSLContext:
"""Method to return a fake SSL context for when ssl isn't available to import
For example when using a:
* `Adafruit Ethernet FeatherWing <https://www.adafruit.com/product/3201>`_
* `Adafruit AirLift – ESP32 WiFi Co-Processor Breakout Board
<https://www.adafruit.com/product/4201>`_
* `Adafruit AirLift FeatherWing – ESP32 WiFi Co-Processor
<https://www.adafruit.com/product/4264>`_
"""
if hasattr(socket_pool, "set_interface"):
# this is to manually support legacy hardware like the fona
socket_pool.set_interface(iface)
return _FakeSSLContext(iface)
_global_socketpool = {}
_global_ssl_contexts = {}
def get_radio_socketpool(radio):
"""Helper to get a socket pool for common boards
Currently supported:
* Boards with onboard WiFi (ESP32S2, ESP32S3, Pico W, etc)
* Using the ESP32 WiFi Co-Processor (like the Adafruit AirLift)
* Using a WIZ5500 (Like the Adafruit Ethernet FeatherWing)
"""
class_name = radio.__class__.__name__
if class_name not in _global_socketpool:
if class_name == "Radio":
import ssl # pylint: disable=import-outside-toplevel
import socketpool # pylint: disable=import-outside-toplevel
pool = socketpool.SocketPool(radio)
ssl_context = ssl.create_default_context()
elif class_name == "ESP_SPIcontrol":
import adafruit_esp32spi.adafruit_esp32spi_socketpool as socketpool # pylint: disable=import-outside-toplevel
pool = socketpool.SocketPool(radio)
ssl_context = create_fake_ssl_context(pool, radio)
elif class_name == "WIZNET5K":
import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool # pylint: disable=import-outside-toplevel
pool = socketpool.SocketPool(radio)
# Note: At this time, SSL/TLS connections are not supported by older
# versions of the Wiznet5k library or on boards withouut the ssl module
# see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html
ssl_context = None
cp_version = sys.implementation[1]
if pool.SOCK_STREAM == 1 and cp_version >= WIZNET5K_SSL_SUPPORT_VERSION:
try:
import ssl # pylint: disable=import-outside-toplevel
ssl_context = ssl.create_default_context()
except ImportError:
# if SSL not on board, default to fake_ssl_context
pass
if ssl_context is None:
ssl_context = create_fake_ssl_context(pool, radio)
else:
raise AttributeError(f"Unsupported radio class: {class_name}")
_global_socketpool[class_name] = pool
_global_ssl_contexts[class_name] = ssl_context
return _global_socketpool[class_name]
def get_radio_ssl_context(radio):
"""Helper to get ssl_contexts for common boards
Currently supported:
* Boards with onboard WiFi (ESP32S2, ESP32S3, Pico W, etc)
* Using the ESP32 WiFi Co-Processor (like the Adafruit AirLift)
* Using a WIZ5500 (Like the Adafruit Ethernet FeatherWing)
"""
class_name = radio.__class__.__name__
get_radio_socketpool(radio)
return _global_ssl_contexts[class_name]
# main class
class ConnectionManager:
"""Connection manager for sharing open sockets (aka connections)."""
def __init__(
self,
socket_pool: SocketpoolModuleType,
) -> None:
self._socket_pool = socket_pool
# Hang onto open sockets so that we can reuse them.
self._available_socket = {}
self._open_sockets = {}
def _free_sockets(self) -> None:
available_sockets = []
for socket, free in self._available_socket.items():
if free:
available_sockets.append(socket)
for socket in available_sockets:
self.close_socket(socket)
def _get_key_for_socket(self, socket):
try:
return next(
key for key, value in self._open_sockets.items() if value == socket
)
except StopIteration:
return None
def close_socket(self, socket: SocketType) -> None:
"""Close a previously opened socket."""
if socket not in self._open_sockets.values():
raise RuntimeError("Socket not managed")
key = self._get_key_for_socket(socket)
socket.close()
del self._available_socket[socket]
del self._open_sockets[key]
def free_socket(self, socket: SocketType) -> None:
"""Mark a previously opened socket as available so it can be reused if needed."""
if socket not in self._open_sockets.values():
raise RuntimeError("Socket not managed")
self._available_socket[socket] = True
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
def get_socket(
self,
host: str,
port: int,
proto: str,
session_id: Optional[str] = None,
*,
timeout: float = 1,
is_ssl: bool = False,
ssl_context: Optional[SSLContextType] = None,
) -> CircuitPythonSocketType:
"""Get a new socket and connect"""
if session_id:
session_id = str(session_id)
key = (host, port, proto, session_id)
if key in self._open_sockets:
socket = self._open_sockets[key]
if self._available_socket[socket]:
self._available_socket[socket] = False
return socket
raise RuntimeError(f"Socket already connected to {proto}//{host}:{port}")
if proto == "https:":
is_ssl = True
if is_ssl and not ssl_context:
raise AttributeError(
"ssl_context must be set before using adafruit_requests for https"
)
addr_info = self._socket_pool.getaddrinfo(
host, port, 0, self._socket_pool.SOCK_STREAM
)[0]
try_count = 0
socket = None
last_exc = None
while try_count < 2 and socket is None:
try_count += 1
if try_count > 1:
if any(
socket
for socket, free in self._available_socket.items()
if free is True
):
self._free_sockets()
else:
break
try:
socket = self._socket_pool.socket(addr_info[0], addr_info[1])
except OSError as exc:
last_exc = exc
continue
except RuntimeError as exc:
last_exc = exc
continue
if is_ssl:
socket = ssl_context.wrap_socket(socket, server_hostname=host)
connect_host = host
else:
connect_host = addr_info[-1][0]
socket.settimeout(timeout) # socket read timeout
try:
socket.connect((connect_host, port))
except MemoryError as exc:
last_exc = exc
socket.close()
socket = None
except OSError as exc:
last_exc = exc
socket.close()
socket = None
if socket is None:
raise RuntimeError(f"Error connecting socket: {last_exc}") from last_exc
self._available_socket[socket] = False
self._open_sockets[key] = socket
return socket
# global helpers
_global_connection_manager = {}
def get_connection_manager(socket_pool: SocketpoolModuleType) -> ConnectionManager:
"""Get the ConnectionManager singleton for the given pool"""
if socket_pool not in _global_connection_manager:
_global_connection_manager[socket_pool] = ConnectionManager(socket_pool)
return _global_connection_manager[socket_pool]