@@ -84,7 +84,7 @@ def is_ipv4(host):
84
84
return True
85
85
86
86
87
- # pylint: disable=invalid-name
87
+ # pylint: disable=invalid-name, too-many-public-methods
88
88
class socket :
89
89
"""A simplified implementation of the Python 'socket' class
90
90
for connecting to a Wiznet5k module.
@@ -112,11 +112,12 @@ def __enter__(self):
112
112
return self
113
113
114
114
def __exit__ (self , exc_type , exc_val , exc_tb ):
115
- self .disconnect ()
116
- stamp = time .monotonic ()
117
- while self .status == adafruit_wiznet5k .SNSR_SOCK_FIN_WAIT :
118
- if time .monotonic () - stamp > 1000 :
119
- raise RuntimeError ("Failed to disconnect socket" )
115
+ if self ._sock_type == SOCK_STREAM :
116
+ self .disconnect ()
117
+ stamp = time .monotonic ()
118
+ while self .status == adafruit_wiznet5k .SNSR_SOCK_FIN_WAIT :
119
+ if time .monotonic () - stamp > 1000 :
120
+ raise RuntimeError ("Failed to disconnect socket" )
120
121
self .close ()
121
122
stamp = time .monotonic ()
122
123
while self .status != adafruit_wiznet5k .SNSR_SOCK_CLOSED :
@@ -216,18 +217,19 @@ def accept(self):
216
217
return client_sock , addr
217
218
218
219
def connect (self , address , conntype = None ):
219
- """Connect to a remote socket at address. (The format of address depends
220
- on the address family — see above.)
220
+ """Connect to a remote socket at address.
221
221
:param tuple address: Remote socket as a (host, port) tuple.
222
-
223
222
"""
224
223
assert (
225
224
conntype != 0x03
226
225
), "Error: SSL/TLS is not currently supported by CircuitPython."
227
226
host , port = address
228
227
229
228
if hasattr (host , "split" ):
230
- host = tuple (map (int , host .split ("." )))
229
+ try :
230
+ host = tuple (map (int , host .split ("." )))
231
+ except ValueError :
232
+ host = _the_interface .get_host_by_name (host )
231
233
if not _the_interface .socket_connect (
232
234
self .socknum , host , port , conn_mode = self ._sock_type
233
235
):
@@ -238,23 +240,29 @@ def send(self, data):
238
240
"""Send data to the socket. The socket must be connected to
239
241
a remote socket.
240
242
:param bytearray data: Desired data to send to the socket.
241
-
242
243
"""
243
244
_the_interface .socket_write (self .socknum , data , self ._timeout )
244
245
gc .collect ()
245
246
246
- def recv (self , bufsize = 0 ): # pylint: disable=too-many-branches
247
+ def sendto (self , data , address ):
248
+ """Send data to the socket. The socket must be connected to
249
+ a remote socket.
250
+ :param bytearray data: Desired data to send to the socket.
251
+ :param tuple address: Remote socket as a (host, port) tuple.
252
+ """
253
+ self .connect (address )
254
+ return self .send (data )
255
+
256
+ def recv (self , bufsize = 0 , flags = 0 ): # pylint: disable=too-many-branches
247
257
"""Reads some bytes from the connected remote address.
248
258
:param int bufsize: Maximum number of bytes to receive.
259
+ :param int flags: ignored, present for compatibility.
249
260
"""
250
261
# print("Socket read", bufsize)
251
262
if bufsize == 0 :
252
263
# read everything on the socket
253
264
while True :
254
- if self ._sock_type == SOCK_STREAM :
255
- avail = self .available ()
256
- elif self ._sock_type == SOCK_DGRAM :
257
- avail = _the_interface .udp_remaining ()
265
+ avail = self .available ()
258
266
if avail :
259
267
if self ._sock_type == SOCK_STREAM :
260
268
self ._buffer += _the_interface .socket_read (self .socknum , avail )[
@@ -275,10 +283,7 @@ def recv(self, bufsize=0): # pylint: disable=too-many-branches
275
283
received = []
276
284
while to_read > 0 :
277
285
# print("Bytes to read:", to_read)
278
- if self ._sock_type == SOCK_STREAM :
279
- avail = self .available ()
280
- elif self ._sock_type == SOCK_DGRAM :
281
- avail = _the_interface .udp_remaining ()
286
+ avail = self .available ()
282
287
if avail :
283
288
stamp = time .monotonic ()
284
289
if self ._sock_type == SOCK_STREAM :
@@ -305,21 +310,62 @@ def recv(self, bufsize=0): # pylint: disable=too-many-branches
305
310
gc .collect ()
306
311
return ret
307
312
313
+ def recvfrom (self , bufsize = 0 , flags = 0 ):
314
+ """Reads some bytes from the connected remote address.
315
+ :param int bufsize: Maximum number of bytes to receive.
316
+ :param int flags: ignored, present for compatibility.
317
+ :returns: a tuple (bytes, address) where address is a tuple (ip, port)
318
+ """
319
+ return (
320
+ self .recv (bufsize ),
321
+ (
322
+ _the_interface .remote_ip (self .socknum ),
323
+ _the_interface .remote_port (self .socknum ),
324
+ ),
325
+ )
326
+
327
+ def recv_into (self , buf , nbytes = 0 , flags = 0 ):
328
+ """Reads some bytes from the connected remote address info the provided buffer.
329
+ :param bytearray buf: Data buffer
330
+ :param nbytes: Maximum number of bytes to receive
331
+ :param int flags: ignored, present for compatibility.
332
+ :returns: the number of bytes received
333
+ """
334
+ if nbytes == 0 :
335
+ nbytes = len (buf )
336
+ ret = self .recv (nbytes )
337
+ nbytes = len (ret )
338
+ buf [:nbytes ] = ret
339
+ return nbytes
340
+
341
+ def recvfrom_into (self , buf , nbytes = 0 , flags = 0 ):
342
+ """Reads some bytes from the connected remote address info the provided buffer.
343
+ :param bytearray buf: Data buffer
344
+ :param nbytes: Maximum number of bytes to receive
345
+ :param int flags: ignored, present for compatibility.
346
+ :returns a tuple (nbytes, address) where address is a tuple (ip, port)
347
+ """
348
+ return (
349
+ self .recv_into (buf , nbytes ),
350
+ (
351
+ _the_interface .remote_ip (self .socknum ),
352
+ _the_interface .remote_port (self .socknum ),
353
+ ),
354
+ )
355
+
308
356
def readline (self ):
309
357
"""Attempt to return as many bytes as we can up to \
310
358
but not including '\r \n '.
311
359
312
360
"""
313
361
stamp = time .monotonic ()
314
362
while b"\r \n " not in self ._buffer :
315
- if self . _sock_type == SOCK_STREAM :
316
- avail = self . available ()
317
- if avail :
363
+ avail = self . available ()
364
+ if avail :
365
+ if self . _sock_type == SOCK_STREAM :
318
366
self ._buffer += _the_interface .socket_read (self .socknum , avail )[1 ]
319
- elif self ._sock_type == SOCK_DGRAM :
320
- avail = _the_interface .udp_remaining ()
321
- if avail :
322
- self ._buffer += _the_interface .read_udp (self .socknum , avail )
367
+ elif self ._sock_type == SOCK_DGRAM :
368
+ self ._buffer += _the_interface .read_udp (self .socknum , avail )[1 ]
323
369
if (
324
370
not avail
325
371
and self ._timeout > 0
0 commit comments