Skip to content

Commit 69c5f3e

Browse files
committed
Add exit timeout and remove socknum setter
Adds an __exit__ timeout for disconnect removes socknum setter in favor of direct reference Corrects documentation errors
1 parent edff777 commit 69c5f3e

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,17 @@ def __enter__(self):
113113

114114
def __exit__(self, exc_type, exc_val, exc_tb):
115115
self.disconnect()
116+
stamp = time.monotonic()
116117
while self.status == adafruit_wiznet5k.SNSR_SOCK_FIN_WAIT:
117-
pass
118+
if time.monotonic() - stamp > 1000:
119+
raise RuntimeError("Failed to disconnect socket")
118120
self.close()
119121

120122
@property
121123
def socknum(self):
122124
"""Returns the socket object's socket number."""
123125
return self._socknum
124126

125-
@socknum.setter
126-
def socknum(self, socknum):
127-
"""Sets the socket object's socket number."""
128-
self._socknum = socknum
129-
130127
@property
131128
def status(self):
132129
"""Returns the status of the socket"""
@@ -186,9 +183,10 @@ def listen(self, backlog=None):
186183
self._buffer = b""
187184

188185
def accept(self):
189-
"""Mimic python socket accept for compatibility. The socket where the
190-
connection originated is returned while a new socket is allocated and begins
191-
listening.
186+
"""Accept a connection. The socket must be bound to an address and listening for
187+
connections. The return value is a pair (conn, address) where conn is a new
188+
socket object usable to send and receive data on the connection, and address is
189+
the address bound to the socket on the other end of the connection.
192190
"""
193191
stamp = time.monotonic()
194192
while self.status not in (
@@ -205,12 +203,12 @@ def accept(self):
205203
current_socknum = self.socknum
206204
# Create a new socket object and swap socket nums so we can continue listening
207205
client_sock = socket()
208-
client_sock.socknum = current_socknum
209-
self.socknum = new_listen_socknum
206+
client_sock._socknum = current_socknum # pylint: disable=protected-access
207+
self._socknum = new_listen_socknum # pylint: disable=protected-access
210208
self.bind((None, self._listen_port))
211209
self.listen()
212210
while self.status != adafruit_wiznet5k.SNSR_SOCK_LISTEN:
213-
print("Waiting for socket to listen")
211+
raise RuntimeError("Failed to open new listening socket")
214212
return client_sock, addr
215213

216214
def connect(self, address, conntype=None):

examples/wiznet5k_simpleserver.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2021 Adam Cummick
3+
#
4+
# SPDX-License-Identifier: MIT
5+
6+
import board
7+
import busio
8+
import digitalio
9+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
10+
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
11+
12+
print("Wiznet5k SimpleServer Test")
13+
14+
# For Adafruit Ethernet FeatherWing
15+
cs = digitalio.DigitalInOut(board.D10)
16+
# For Particle Ethernet FeatherWing
17+
# cs = digitalio.DigitalInOut(board.D5)
18+
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
19+
20+
# Initialize ethernet interface
21+
eth = WIZNET5K(spi_bus, cs, is_dhcp=False)
22+
23+
# Initialize a socket for our server
24+
socket.set_interface(eth)
25+
server = socket.socket() # Allocate socket for the server
26+
server_ip = "192.168.10.1" # IP address of server
27+
server_port = 50007 # Port to listen on
28+
server.bind((server_ip, server_port)) # Bind to IP and Port
29+
server.listen() # Begin listening for incoming clients
30+
31+
while True:
32+
conn, addr = server.accept() # Wait for a connection from a client.
33+
with conn:
34+
data = conn.recv()
35+
print(data)
36+
conn.send(data) # Echo message back to client

0 commit comments

Comments
 (0)