@@ -152,6 +152,8 @@ def __init__(
152
152
self ._cs .direction = Direction .OUTPUT
153
153
self ._ready .direction = Direction .INPUT
154
154
self ._reset .direction = Direction .OUTPUT
155
+ # Only one TLS socket at a time is supported so track when we already have one.
156
+ self ._tls_socket = None
155
157
if self ._gpio0 :
156
158
self ._gpio0 .direction = Direction .INPUT
157
159
self .reset ()
@@ -330,11 +332,9 @@ def status(self):
330
332
(not found), WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED,
331
333
WL_CONNECTED, WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED,
332
334
WL_AP_LISTENING, WL_AP_CONNECTED, WL_AP_FAILED"""
333
- if self ._debug :
334
- print ("Connection status" )
335
335
resp = self ._send_command_get_response (_GET_CONN_STATUS_CMD )
336
336
if self ._debug :
337
- print ("Conn status:" , resp [0 ][0 ])
337
+ print ("Connection status:" , resp [0 ][0 ])
338
338
return resp [0 ][0 ] # one byte response
339
339
340
340
@property
@@ -623,7 +623,7 @@ def get_socket(self):
623
623
resp = self ._send_command_get_response (_GET_SOCKET_CMD )
624
624
resp = resp [0 ][0 ]
625
625
if resp == 255 :
626
- raise RuntimeError ( "No sockets available" )
626
+ raise OSError ( 23 ) # ENFILE - File table overflow
627
627
if self ._debug :
628
628
print ("Allocated socket #%d" % resp )
629
629
return resp
@@ -635,7 +635,9 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
635
635
(dest must be hostname for TLS_MODE!)"""
636
636
self ._socknum_ll [0 ][0 ] = socket_num
637
637
if self ._debug :
638
- print ("*** Open socket" )
638
+ print ("*** Open socket to" , dest , port , conn_mode )
639
+ if conn_mode == ESP_SPIcontrol .TLS_MODE and self ._tls_socket is not None :
640
+ raise OSError (23 ) # ENFILE - File table overflow
639
641
port_param = struct .pack (">H" , port )
640
642
if isinstance (dest , str ): # use the 5 arg version
641
643
dest = bytes (dest , "utf-8" )
@@ -656,6 +658,8 @@ def socket_open(self, socket_num, dest, port, conn_mode=TCP_MODE):
656
658
)
657
659
if resp [0 ][0 ] != 1 :
658
660
raise RuntimeError ("Could not connect to remote server" )
661
+ if conn_mode == ESP_SPIcontrol .TLS_MODE :
662
+ self ._tls_socket = socket_num
659
663
660
664
def socket_status (self , socket_num ):
661
665
"""Get the socket connection status, can be SOCKET_CLOSED, SOCKET_LISTEN,
@@ -706,6 +710,7 @@ def socket_write(self, socket_num, buffer, conn_mode=TCP_MODE):
706
710
return
707
711
708
712
if sent != len (buffer ):
713
+ self .socket_close (socket_num )
709
714
raise RuntimeError (
710
715
"Failed to send %d bytes (sent %d)" % (len (buffer ), sent )
711
716
)
@@ -766,9 +771,12 @@ def socket_close(self, socket_num):
766
771
if self ._debug :
767
772
print ("*** Closing socket #%d" % socket_num )
768
773
self ._socknum_ll [0 ][0 ] = socket_num
769
- resp = self ._send_command_get_response (_STOP_CLIENT_TCP_CMD , self ._socknum_ll )
770
- if resp [0 ][0 ] != 1 :
771
- raise RuntimeError ("Failed to close socket" )
774
+ try :
775
+ self ._send_command_get_response (_STOP_CLIENT_TCP_CMD , self ._socknum_ll )
776
+ except RuntimeError :
777
+ pass
778
+ if socket_num == self ._tls_socket :
779
+ self ._tls_socket = None
772
780
773
781
def start_server (
774
782
self , port , socket_num , conn_mode = TCP_MODE , ip = None
0 commit comments