@@ -30,9 +30,6 @@ extern "C"{
30
30
}
31
31
#include < tcp_axtls.h>
32
32
33
-
34
- bool AsyncServer::_ssl_hasClient = false ;
35
-
36
33
/*
37
34
Async TCP Client
38
35
*/
@@ -233,7 +230,7 @@ size_t AsyncClient::add(const char* data, size_t size) {
233
230
int sent = tcp_ssl_write (_pcb, (uint8_t *)data, size);
234
231
if (sent >= 0 )
235
232
return sent;
236
- // ssl error
233
+ _close ();
237
234
return 0 ;
238
235
}
239
236
size_t will_send = (room < size) ? room : size;
@@ -294,9 +291,6 @@ int8_t AsyncClient::_close(){
294
291
int8_t err = ERR_OK;
295
292
if (_pcb) {
296
293
if (_pcb_secure){
297
- if (tcp_ssl_is_server (_pcb) == 1 && AsyncServer::_ssl_hasClient){
298
- AsyncServer::_ssl_hasClient = false ;
299
- }
300
294
tcp_ssl_free (_pcb);
301
295
}
302
296
tcp_arg (_pcb, NULL );
@@ -318,9 +312,6 @@ int8_t AsyncClient::_close(){
318
312
void AsyncClient::_error (int8_t err) {
319
313
if (_pcb){
320
314
if (_pcb_secure){
321
- if (tcp_ssl_is_server (_pcb) == 1 && AsyncServer::_ssl_hasClient){
322
- AsyncServer::_ssl_hasClient = false ;
323
- }
324
315
tcp_ssl_free (_pcb);
325
316
}
326
317
tcp_arg (_pcb, NULL );
@@ -361,6 +352,7 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
361
352
int read_bytes = tcp_ssl_read (pcb, pb);
362
353
if (read_bytes < 0 ){
363
354
if (read_bytes != SSL_CLOSE_NOTIFY) {
355
+ ets_printf (" _recv err: %d\n " , read_bytes);
364
356
_close ();
365
357
}
366
358
return read_bytes;
@@ -393,6 +385,7 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
393
385
return ERR_OK;
394
386
}
395
387
uint32_t now = millis ();
388
+
396
389
// ACK Timeout
397
390
if (_pcb_busy && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){
398
391
_pcb_busy = false ;
@@ -401,10 +394,16 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
401
394
return ERR_OK;
402
395
}
403
396
// RX Timeout
404
- if (_rx_since_timeout && now - _rx_last_packet >= _rx_since_timeout * 1000 ){
397
+ if (_rx_since_timeout && ( now - _rx_last_packet) >= ( _rx_since_timeout * 1000 ) ){
405
398
_close ();
406
399
return ERR_OK;
407
400
}
401
+ // SSL Handshake Timeout
402
+ if (_pcb_secure && !_handshake_done && (now - _rx_last_packet) >= 2000 ){
403
+ _close ();
404
+ return ERR_OK;
405
+ }
406
+
408
407
// Everything is fine
409
408
if (_poll_cb)
410
409
_poll_cb (_poll_cb_arg, this );
@@ -711,7 +710,6 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
711
710
, _pcb(0 )
712
711
, _pending(NULL )
713
712
, _ssl_ctx(NULL )
714
- , _clients_waiting(0 )
715
713
, _connect_cb(0 )
716
714
, _connect_cb_arg(0 )
717
715
, _file_cb(0 )
@@ -725,7 +723,6 @@ AsyncServer::AsyncServer(uint16_t port)
725
723
, _pcb(0 )
726
724
, _pending(NULL )
727
725
, _ssl_ctx(NULL )
728
- , _clients_waiting(0 )
729
726
, _connect_cb(0 )
730
727
, _connect_cb_arg(0 )
731
728
, _file_cb(0 )
@@ -833,7 +830,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
833
830
tcp_nagle_enable (pcb);
834
831
835
832
if (_ssl_ctx){
836
- if (_ssl_hasClient || _pending){
833
+ if (tcp_ssl_has_client () || _pending){
837
834
struct pending_pcb * new_item = (struct pending_pcb *)malloc (sizeof (struct pending_pcb ));
838
835
if (!new_item){
839
836
// ets_printf("### malloc new pending failed!\n");
@@ -842,7 +839,6 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
842
839
}
843
840
return ERR_OK;
844
841
}
845
- _clients_waiting++;
846
842
// ets_printf("### put to wait: %d\n", _clients_waiting);
847
843
new_item->pcb = pcb;
848
844
new_item->pb = NULL ;
@@ -863,7 +859,6 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
863
859
} else {
864
860
AsyncClient *c = new AsyncClient (pcb, _ssl_ctx);
865
861
if (c){
866
- _ssl_hasClient = true ;
867
862
c->onConnect ([this ](void * arg, AsyncClient *c){
868
863
_connect_cb (_connect_cb_arg, c);
869
864
}, this );
@@ -885,7 +880,7 @@ int8_t AsyncServer::_accept(tcp_pcb* pcb, int8_t err){
885
880
}
886
881
887
882
int8_t AsyncServer::_poll (tcp_pcb* pcb){
888
- if (!_ssl_hasClient && _pending){
883
+ if (!tcp_ssl_has_client () && _pending){
889
884
struct pending_pcb * p = _pending;
890
885
if (p->pcb == pcb){
891
886
_pending = _pending->next ;
@@ -896,8 +891,6 @@ int8_t AsyncServer::_poll(tcp_pcb* pcb){
896
891
p->next = b->next ;
897
892
p = b;
898
893
}
899
- _ssl_hasClient = true ;
900
- _clients_waiting--;
901
894
// ets_printf("### remove from wait: %d\n", _clients_waiting);
902
895
AsyncClient *c = new AsyncClient (pcb, _ssl_ctx);
903
896
if (c){
@@ -919,7 +912,6 @@ int8_t AsyncServer::_recv(struct tcp_pcb *pcb, struct pbuf *pb, int8_t err){
919
912
struct pending_pcb * p;
920
913
921
914
if (!pb){
922
- _clients_waiting--;
923
915
// ets_printf("### close from wait: %d\n", _clients_waiting);
924
916
p = _pending;
925
917
if (p->pcb == pcb){
0 commit comments