@@ -408,33 +408,50 @@ def accept(
408
408
end of the connection.
409
409
"""
410
410
stamp = ticks_ms ()
411
- while self ._status not in (
412
- wiznet5k .adafruit_wiznet5k .SNSR_SOCK_SYNRECV ,
413
- wiznet5k .adafruit_wiznet5k .SNSR_SOCK_ESTABLISHED ,
414
- wiznet5k .adafruit_wiznet5k .SNSR_SOCK_LISTEN ,
415
- ):
411
+ while True :
412
+ while self ._status not in (
413
+ wiznet5k .adafruit_wiznet5k .SNSR_SOCK_SYNRECV ,
414
+ wiznet5k .adafruit_wiznet5k .SNSR_SOCK_ESTABLISHED ,
415
+ wiznet5k .adafruit_wiznet5k .SNSR_SOCK_LISTEN ,
416
+ ):
417
+ if (
418
+ self ._timeout
419
+ and 0 < self ._timeout < ticks_diff (ticks_ms (), stamp ) / 1000
420
+ ):
421
+ raise TimeoutError ("Failed to accept connection." )
422
+ if self ._status == wiznet5k .adafruit_wiznet5k .SNSR_SOCK_CLOSE_WAIT :
423
+ self ._disconnect ()
424
+ self .listen ()
425
+ if self ._status == wiznet5k .adafruit_wiznet5k .SNSR_SOCK_CLOSED :
426
+ self .close ()
427
+ self .listen ()
428
+
429
+ _ , addr = self ._interface .socket_accept (self ._socknum )
430
+ # if any of the following conditions are true, we haven't accepted a connection
416
431
if (
417
- self ._timeout
418
- and 0 < self ._timeout < ticks_diff (ticks_ms (), stamp ) / 1000
432
+ addr [0 ] == "0.0.0.0"
433
+ or addr [1 ] == 0
434
+ or self ._interface .socket_status (self ._socknum ) != wiznet5k .adafruit_wiznet5k .SNSR_SOCK_ESTABLISHED
419
435
):
420
- raise TimeoutError ("Failed to accept connection." )
421
- if self ._status == wiznet5k .adafruit_wiznet5k .SNSR_SOCK_CLOSE_WAIT :
422
- self ._disconnect ()
423
- self .listen ()
424
- if self ._status == wiznet5k .adafruit_wiznet5k .SNSR_SOCK_CLOSED :
425
- self .close ()
426
- self .listen ()
427
-
428
- _ , addr = self ._interface .socket_accept (self ._socknum )
429
- current_socknum = self ._socknum
430
- # Create a new socket object and swap socket nums, so we can continue listening
431
- client_sock = Socket (self ._socket_pool )
432
- self ._socknum = client_sock ._socknum # pylint: disable=protected-access
433
- client_sock ._socknum = current_socknum # pylint: disable=protected-access
434
- self ._bind ((None , self ._listen_port ))
435
- self .listen ()
436
- if self ._status != wiznet5k .adafruit_wiznet5k .SNSR_SOCK_LISTEN :
437
- raise RuntimeError ("Failed to open new listening socket" )
436
+ if self ._timeout == 0 :
437
+ # non-blocking mode
438
+ raise OSError (errno .EAGAIN )
439
+ elif self ._timeout and 0 < self ._timeout < ticks_diff (ticks_ms (), stamp ) / 1000 :
440
+ # blocking mode with timeout
441
+ raise OSError (errno .ETIMEDOUT )
442
+ else :
443
+ # blocking mode / timeout not expired
444
+ continue
445
+ current_socknum = self ._socknum
446
+ # Create a new socket object and swap socket nums, so we can continue listening
447
+ client_sock = Socket (self ._socket_pool )
448
+ self ._socknum = client_sock ._socknum # pylint: disable=protected-access
449
+ client_sock ._socknum = current_socknum # pylint: disable=protected-access
450
+ self ._bind ((None , self ._listen_port ))
451
+ self .listen ()
452
+ if self ._status != wiznet5k .adafruit_wiznet5k .SNSR_SOCK_LISTEN :
453
+ raise RuntimeError ("Failed to open new listening socket" )
454
+ break
438
455
return client_sock , addr
439
456
440
457
@_check_socket_closed
0 commit comments