Skip to content

Commit bb60e7c

Browse files
author
BiffoBear
committed
refactored bind() to allow it to be called with and without error checking.
1 parent ee6c82e commit bb60e7c

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,18 @@ def bind(self, address: Tuple[Optional[str], int]) -> None:
310310
:raises ValueError: If the IPv4 address specified is not the address
311311
assigned to the WIZNET5K interface.
312312
"""
313-
# Check to see if the socket is bound is disabled to allow socket.accept to swap sockets.
314-
# if self._listen_port:
315-
# raise ConnectionError("The socket is already bound.")
313+
# Check to see if the socket is bound.
314+
if self._listen_port:
315+
raise ConnectionError("The socket is already bound.")
316+
self._bind(address)
317+
318+
def _bind(self, address: Tuple[Optional[str], int]) -> None:
319+
"""
320+
Helper function to allow bind() to check for an existing connection and for
321+
accept() to generate a new socket connection.
322+
323+
:param Tuple[Optional[str], int] address: Address as a (host, port) tuple.
324+
"""
316325
if address[0]:
317326
if gethostbyname(address[0]) != _the_interface.pretty_ip(
318327
_the_interface.ip_address
@@ -373,7 +382,7 @@ def accept(
373382
client_sock = socket()
374383
client_sock._socknum = current_socknum # pylint: disable=protected-access
375384
self._socknum = new_listen_socknum
376-
self.bind((None, self._listen_port))
385+
self._bind((None, self._listen_port))
377386
self.listen()
378387
while self._status != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_LISTEN:
379388
raise RuntimeError("Failed to open new listening socket")

0 commit comments

Comments
 (0)