@@ -245,11 +245,7 @@ def _dhcp_connection_setup(self, timeout: float = 5.0) -> None:
245
245
self ._eth .write_snmr (self ._wiz_sock , 0x02 ) # Set UDP connection
246
246
self ._eth .write_sock_port (self ._wiz_sock , 68 ) # Set DHCP client port.
247
247
self ._eth .write_sncr (self ._wiz_sock , 0x01 ) # Open the socket.
248
- while (
249
- self ._eth .read_sncr (self ._wiz_sock ) != b"\x00 "
250
- ): # Wait for command to complete.
251
- time .sleep (0.001 )
252
- if self ._eth .read_snsr (self ._wiz_sock ) == b"\x22 " :
248
+ if self ._eth .read_snsr (self ._wiz_sock ) == 0x22 :
253
249
self ._eth .write_sndport (2 , _DHCP_SERVER_PORT )
254
250
debug_msg ("+ Connection OK, port set." , self ._debug )
255
251
return
@@ -299,30 +295,18 @@ def _receive_dhcp_response(self, timeout: float) -> int:
299
295
:returns int: The number of bytes stored in the global buffer.
300
296
"""
301
297
debug_msg ("Receiving a DHCP response." , self ._debug )
302
- # DHCP returns the query plus additional data. The query length is 236 bytes.
303
- minimum_packet_length = 236
304
- buffer = bytearray (b"" )
305
- bytes_read = 0
306
- debug_msg ("+ Beginning to receive…" , self ._debug )
307
- while bytes_read < minimum_packet_length and time .monotonic () < timeout :
308
- if self ._eth .socket_available (self ._wiz_sock , _SNMR_UDP ):
309
- x = self ._eth .read_udp (self ._wiz_sock , _BUFF_LENGTH - bytes_read )[1 ]
310
- buffer .extend (x )
311
- bytes_read = len (buffer )
312
- debug_msg ("+ Bytes read so far {}" .format (bytes_read ), self ._debug )
313
- debug_msg (x , self ._debug )
314
- if bytes_read == _BUFF_LENGTH :
315
- break
316
- debug_msg ("Received {} bytes" .format (bytes_read ), self ._debug )
317
- if bytes_read < minimum_packet_length :
318
- bytes_read = 0
319
- else :
320
- _BUFF [:bytes_read ] = buffer
321
- _BUFF [bytes_read :] = bytearray (_BUFF_LENGTH - bytes_read )
322
- del buffer
323
- gc .collect ()
324
- debug_msg (_BUFF [:bytes_read ], self ._debug )
325
- return bytes_read
298
+ while time .monotonic () < timeout :
299
+ # DHCP returns the query plus additional data. The query length is 236 bytes.
300
+ if self ._eth .socket_available (self ._wiz_sock , _SNMR_UDP ) > 236 :
301
+ bytes_count , bytes_read = self ._eth .read_udp (
302
+ self ._wiz_sock , _BUFF_LENGTH
303
+ )
304
+ _BUFF [:bytes_count ] = bytes_read
305
+ debug_msg ("Received {} bytes" .format (bytes_count ), self ._debug )
306
+ del bytes_read
307
+ gc .collect ()
308
+ return bytes_count
309
+ raise TimeoutError ("No DHCP response received." )
326
310
327
311
def _process_messaging_states (self , * , message_type : int ):
328
312
"""
0 commit comments