@@ -120,6 +120,7 @@ def __init__(
120
120
self ._sock_type = type
121
121
self ._buffer = b""
122
122
self ._timeout = 0
123
+ self ._listen_port = None
123
124
124
125
self ._socknum = _the_interface .get_socket ()
125
126
@@ -134,8 +135,7 @@ def connected(self):
134
135
if self .socknum >= _the_interface .max_sockets :
135
136
return False
136
137
status = _the_interface .socket_status (self .socknum )[0 ]
137
- if (status == adafruit_wiznet5k .SNSR_SOCK_CLOSE_WAIT
138
- and self .available () == 0 ):
138
+ if status == adafruit_wiznet5k .SNSR_SOCK_CLOSE_WAIT and self .available () == 0 :
139
139
result = False
140
140
else :
141
141
result = status not in (
@@ -162,11 +162,18 @@ def inet_aton(self, ip_string):
162
162
self ._buffer = bytearray (self ._buffer )
163
163
return self ._buffer
164
164
165
- def listen (self , port ):
166
- """Listen on the specified port.
167
- :param int port: The port to listen on .
165
+ def bind (self , address ):
166
+ """Bind the socket to the listen port, we ignore the host .
167
+ :param tuple address: local socket as a (host, port) tuple, host is ignored .
168
168
"""
169
- _the_interface .socket_listen (self .socknum , port )
169
+ _ , self ._listen_port = address
170
+
171
+ def listen (self , backlog = None ):
172
+ """Listen on the port specified by bind.
173
+ :param backlog: For compatibility but ignored.
174
+ """
175
+ assert self ._listen_port is not None , "Use bind to set the port before listen!"
176
+ _the_interface .socket_listen (self .socknum , self ._listen_port )
170
177
self ._buffer = b""
171
178
172
179
def connect (self , address , conntype = None ):
@@ -211,11 +218,11 @@ def recv(self, bufsize=0): # pylint: disable=too-many-branches
211
218
avail = _the_interface .udp_remaining ()
212
219
if avail :
213
220
if self ._sock_type == SOCK_STREAM :
214
- self ._buffer += _the_interface .socket_read (
215
- self .socknum , avail )[1 ]
221
+ self ._buffer += _the_interface .socket_read (self .socknum , avail )[
222
+ 1
223
+ ]
216
224
elif self ._sock_type == SOCK_DGRAM :
217
- self ._buffer += _the_interface .read_udp (
218
- self .socknum , avail )[1 ]
225
+ self ._buffer += _the_interface .read_udp (self .socknum , avail )[1 ]
219
226
else :
220
227
break
221
228
gc .collect ()
@@ -237,10 +244,10 @@ def recv(self, bufsize=0): # pylint: disable=too-many-branches
237
244
stamp = time .monotonic ()
238
245
if self ._sock_type == SOCK_STREAM :
239
246
recv = _the_interface .socket_read (
240
- self .socknum , min (to_read , avail ))[1 ]
247
+ self .socknum , min (to_read , avail )
248
+ )[1 ]
241
249
elif self ._sock_type == SOCK_DGRAM :
242
- recv = _the_interface .read_udp (
243
- self .socknum , min (to_read , avail ))[1 ]
250
+ recv = _the_interface .read_udp (self .socknum , min (to_read , avail ))[1 ]
244
251
recv = bytes (recv )
245
252
received .append (recv )
246
253
to_read -= len (recv )
@@ -269,14 +276,16 @@ def readline(self):
269
276
if self ._sock_type == SOCK_STREAM :
270
277
avail = self .available ()
271
278
if avail :
272
- self ._buffer += _the_interface .socket_read (
273
- self .socknum , avail )[1 ]
279
+ self ._buffer += _the_interface .socket_read (self .socknum , avail )[1 ]
274
280
elif self ._sock_type == SOCK_DGRAM :
275
281
avail = _the_interface .udp_remaining ()
276
282
if avail :
277
283
self ._buffer += _the_interface .read_udp (self .socknum , avail )
278
- if not avail and self ._timeout > 0 and \
279
- time .monotonic () - stamp > self ._timeout :
284
+ if (
285
+ not avail
286
+ and self ._timeout > 0
287
+ and time .monotonic () - stamp > self ._timeout
288
+ ):
280
289
self .close ()
281
290
raise RuntimeError ("Didn't receive response, failing out..." )
282
291
firstline , self ._buffer = self ._buffer .split (b"\r \n " , 1 )
0 commit comments