9
9
10
10
A socket compatible interface with the Wiznet5k module.
11
11
12
- * Author(s): ladyada, Brent Rubell, Patrick Van Oosterwijck
12
+ * Author(s): ladyada, Brent Rubell, Patrick Van Oosterwijck, Adam Cummick
13
13
14
14
"""
15
15
import gc
@@ -108,11 +108,30 @@ def __init__(
108
108
if self ._socknum == SOCKET_INVALID :
109
109
raise RuntimeError ("Failed to allocate socket." )
110
110
111
+ def __enter__ (self ):
112
+ return self
113
+
114
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
115
+ self .disconnect ()
116
+ while self .status == adafruit_wiznet5k .SNSR_SOCK_FIN_WAIT :
117
+ pass
118
+ self .close ()
119
+
111
120
@property
112
121
def socknum (self ):
113
122
"""Returns the socket object's socket number."""
114
123
return self ._socknum
115
124
125
+ @socknum .setter
126
+ def socknum (self , socknum ):
127
+ """Sets the socket object's socket number."""
128
+ self ._socknum = socknum
129
+
130
+ @property
131
+ def status (self ):
132
+ """Returns the status of the socket"""
133
+ return _the_interface .socket_status (self .socknum )[0 ]
134
+
116
135
@property
117
136
def connected (self ):
118
137
"""Returns whether or not we are connected to the socket."""
@@ -147,10 +166,16 @@ def inet_aton(self, ip_string):
147
166
return self ._buffer
148
167
149
168
def bind (self , address ):
150
- """Bind the socket to the listen port, we ignore the host.
151
- :param tuple address: local socket as a (host, port) tuple, host is ignored.
169
+ """Bind the socket to the listen port, if host is specified the interface
170
+ will be reconfigured to that IP.
171
+ :param tuple address: local socket as a (host, port) tuple.
152
172
"""
153
- _ , self ._listen_port = address
173
+ if address [0 ] is not None :
174
+ ip_address = _the_interface .unpretty_ip (address [0 ])
175
+ current_ip , subnet_mask , gw_addr , dns = _the_interface .ifconfig
176
+ if ip_address != current_ip :
177
+ _the_interface .ifconfig = (ip_address , subnet_mask , gw_addr , dns )
178
+ self ._listen_port = address [1 ]
154
179
155
180
def listen (self , backlog = None ):
156
181
"""Listen on the port specified by bind.
@@ -160,6 +185,34 @@ def listen(self, backlog=None):
160
185
_the_interface .socket_listen (self .socknum , self ._listen_port )
161
186
self ._buffer = b""
162
187
188
+ 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.
192
+ """
193
+ stamp = time .monotonic ()
194
+ while self .status not in (
195
+ adafruit_wiznet5k .SNSR_SOCK_SYNRECV ,
196
+ adafruit_wiznet5k .SNSR_SOCK_ESTABLISHED ,
197
+ ):
198
+ if self ._timeout > 0 and time .monotonic () - stamp > self ._timeout :
199
+ return None
200
+ if self .status == adafruit_wiznet5k .SNSR_SOCK_CLOSED :
201
+ self .close ()
202
+ self .listen ()
203
+
204
+ new_listen_socknum , addr = _the_interface .socket_accept (self .socknum )
205
+ current_socknum = self .socknum
206
+ # Create a new socket object and swap socket nums so we can continue listening
207
+ client_sock = socket ()
208
+ client_sock .socknum = current_socknum
209
+ self .socknum = new_listen_socknum
210
+ self .bind ((None , self ._listen_port ))
211
+ self .listen ()
212
+ while self .status != adafruit_wiznet5k .SNSR_SOCK_LISTEN :
213
+ print ("Waiting for socket to listen" )
214
+ return client_sock , addr
215
+
163
216
def connect (self , address , conntype = None ):
164
217
"""Connect to a remote socket at address. (The format of address depends
165
218
on the address family — see above.)
0 commit comments