This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathESPAsyncTCP.cpp
1400 lines (1263 loc) · 36.5 KB
/
ESPAsyncTCP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Asynchronous TCP library for Espressif MCUs
Copyright (c) 2016 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Changes for July 2019
The operator "new ..." was changed to "new (std::nothrow) ...", which will
return NULL when the heap is out of memory. Without the change "soft WDT"
was the result, starting with Arduino ESP8266 Core 2.5.0. (Note, RE:"soft
WDT" - the error reporting may improve with core 2.6.) With proir core
versions the library appears to work fine.
ref: https://github.com/esp8266/Arduino/issues/6269#issue-464978944
To support newer lwIP versions and buffer models. All references to 1460
were replaced with TCP_MSS. If TCP_MSS is not defined (exp. 1.4v lwIP)
1460 is assumed.
The ESPAsyncTCP library should build for Arduino ESP8266 Core releases:
2.3.0, 2.4.1, 2.4.2, 2.5.1, 2.5.2. It may still build with core versions
2.4.0 and 2.5.0. I did not do any regression testing with these, since
they had too many issues and were quickly superseded.
lwIP tcp_err() callback often resulted in crashes. The problem was a
tcp_err() would come in, while processing a send or receive in the
forground. The tcp_err() callback would be passed down to a client's
registered disconnect CB. A common problem with SyncClient and other
modules as well as some client code was: the freeing of ESPAsyncTCP
AsyncClient objects via disconnect CB handlers while the library was
waiting for an operstion to finished. Attempts to access bad pointers
followed. For SyncClient this commonly occured during a call to delay().
On return to SyncClient _client was invalid. Also the problem described by
issue #94 also surfaced
Use of tcp_abort() required some very special handling and was very
challenging to make work without changing client API. ERR_ABRT can only be
used once on a return to lwIP for a given connection and since the
AsyncClient structure was sometimes deleted before returning to lwIP, the
state tracking became tricky. While ugly, a global variable for this
seemed to work; however, I abanded it when I saw a possible
reentrancy/concurrency issue. After several approaches I settled the
problem by creating "class ACErrorTracker" to manage the issue.
Additional Async Client considerations:
The client sketch must always test if the connection is still up at loop()
entry and after the return of any function call, that may have done a
delay() or yield() or any ESPAsyncTCP library family call. For example,
the connection could be lost during a call to _client->write(...). Client
sketches that delete _client as part of their onDisconnect() handler must
be very careful as _client will become invalid after calls to delay(),
yield(), etc.
*/
#include "Arduino.h"
#include "ESPAsyncTCP.h"
extern "C"{
#include "lwip/opt.h"
#include "lwip/tcp.h"
#include "lwip/inet.h"
#include "lwip/dns.h"
#include "lwip/init.h"
}
#include <tcp_axtls.h>
/*
Async Client Error Return Tracker
*/
// Assumption: callbacks are never called with err == ERR_ABRT; however,
// they may return ERR_ABRT.
ACErrorTracker::ACErrorTracker(AsyncClient *c):
_client(c)
, _close_error(ERR_OK)
, _errored(EE_OK)
#ifdef DEBUG_MORE
, _error_event_cb(NULL)
, _error_event_cb_arg(NULL)
#endif
{}
#ifdef DEBUG_MORE
/**
* This is not necessary, but a start at gathering some statistics on
* errored out connections. Used from AsyncServer.
*/
void ACErrorTracker::onErrorEvent(AsNotifyHandler cb, void *arg) {
_error_event_cb = cb;
_error_event_cb_arg = arg;
}
#endif
void ACErrorTracker::setCloseError(err_t e) {
if (e != ERR_OK)
ASYNC_TCP_DEBUG("setCloseError() to: %s(%ld)\n", _client->errorToString(e), e);
if(_errored == EE_OK)
_close_error = e;
}
/**
* Called mainly by callback routines, called when err is not ERR_OK.
* This prevents the possiblity of aborting an already errored out
* connection.
*/
void ACErrorTracker::setErrored(size_t errorEvent){
if(EE_OK == _errored)
_errored = errorEvent;
#ifdef DEBUG_MORE
if (_error_event_cb)
_error_event_cb(_error_event_cb_arg, errorEvent);
#endif
}
/**
* Used by callback functions only. Used for proper ERR_ABRT return value
* reporting. ERR_ABRT is only reported/returned once; thereafter ERR_OK
* is always returned.
*/
err_t ACErrorTracker::getCallbackCloseError(void){
if (EE_OK != _errored)
return ERR_OK;
if (ERR_ABRT == _close_error)
setErrored(EE_ABORTED);
return _close_error;
}
/*
Async TCP Client
*/
#if DEBUG_ESP_ASYNC_TCP
static size_t _connectionCount=0;
#endif
#if ASYNC_TCP_SSL_ENABLED
AsyncClient::AsyncClient(tcp_pcb* pcb, SSL_CTX * ssl_ctx):
#else
AsyncClient::AsyncClient(tcp_pcb* pcb):
#endif
_connect_cb(0)
, _connect_cb_arg(0)
, _discard_cb(0)
, _discard_cb_arg(0)
, _sent_cb(0)
, _sent_cb_arg(0)
, _error_cb(0)
, _error_cb_arg(0)
, _recv_cb(0)
, _recv_cb_arg(0)
, _pb_cb(0)
, _pb_cb_arg(0)
, _timeout_cb(0)
, _timeout_cb_arg(0)
, _poll_cb(0)
, _poll_cb_arg(0)
, _pcb_busy(false)
#if ASYNC_TCP_SSL_ENABLED
, _pcb_secure(false)
, _handshake_done(true)
#endif
, _pcb_sent_at(0)
, _close_pcb(false)
, _ack_pcb(true)
, _tx_unacked_len(0)
, _tx_acked_len(0)
, _tx_unsent_len(0)
, _rx_ack_len(0)
, _rx_last_packet(0)
, _rx_since_timeout(0)
, _ack_timeout(ASYNC_MAX_ACK_TIME)
, _connect_port(0)
, _recv_pbuf_flags(0)
, _errorTracker(NULL)
, prev(NULL)
, next(NULL)
{
_pcb = pcb;
if(_pcb){
_rx_last_packet = millis();
tcp_setprio(_pcb, TCP_PRIO_MIN);
tcp_arg(_pcb, this);
tcp_recv(_pcb, &_s_recv);
tcp_sent(_pcb, &_s_sent);
tcp_err(_pcb, &_s_error);
tcp_poll(_pcb, &_s_poll, 1);
#if ASYNC_TCP_SSL_ENABLED
if(ssl_ctx){
if(tcp_ssl_new_server(_pcb, ssl_ctx) < 0){
_close();
return;
}
tcp_ssl_arg(_pcb, this);
tcp_ssl_data(_pcb, &_s_data);
tcp_ssl_handshake(_pcb, &_s_handshake);
tcp_ssl_err(_pcb, &_s_ssl_error);
_pcb_secure = true;
_handshake_done = false;
}
#endif
}
_errorTracker = std::make_shared<ACErrorTracker>(this);
#if DEBUG_ESP_ASYNC_TCP
_errorTracker->setConnectionId(++_connectionCount);
#endif
}
AsyncClient::~AsyncClient(){
if(_pcb)
_close();
_errorTracker->clearClient();
}
inline void clearTcpCallbacks(tcp_pcb* pcb){
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_err(pcb, NULL);
tcp_poll(pcb, NULL, 0);
}
#if ASYNC_TCP_SSL_ENABLED
bool AsyncClient::connect(IPAddress ip, uint16_t port, bool secure){
#else
bool AsyncClient::connect(IPAddress ip, uint16_t port){
#endif
if (_pcb) //already connected
return false;
ip_addr_t addr = ip;
#if LWIP_VERSION_MAJOR == 1
netif* interface = ip_route(&addr);
if (!interface){ //no route to host
return false;
}
#endif
tcp_pcb* pcb = tcp_new();
if (!pcb){ //could not allocate pcb
return false;
}
tcp_setprio(pcb, TCP_PRIO_MIN);
#if ASYNC_TCP_SSL_ENABLED
_pcb_secure = secure;
_handshake_done = !secure;
#endif
tcp_arg(pcb, this);
tcp_err(pcb, &_s_error);
size_t err = tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
return (ERR_OK == err);
}
#if ASYNC_TCP_SSL_ENABLED
bool AsyncClient::connect(const char* host, uint16_t port, bool secure){
#else
bool AsyncClient::connect(const char* host, uint16_t port){
#endif
ip_addr_t addr;
err_t err = dns_gethostbyname(host, &addr, (dns_found_callback)&_s_dns_found, this);
if(err == ERR_OK) {
#if ASYNC_TCP_SSL_ENABLED
return connect(IPAddress(addr.addr), port, secure);
#else
return connect(IPAddress(addr), port);
#endif
} else if(err == ERR_INPROGRESS) {
#if ASYNC_TCP_SSL_ENABLED
_pcb_secure = secure;
_handshake_done = !secure;
#endif
_connect_port = port;
return true;
}
return false;
}
AsyncClient& AsyncClient::operator=(const AsyncClient& other){
if (_pcb) {
ASYNC_TCP_DEBUG("operator=[%u]: Abandoned _pcb(0x%" PRIXPTR ") forced close.\n", getConnectionId(), uintptr_t(_pcb));
_close();
}
_errorTracker = other._errorTracker;
// I am confused when "other._pcb" falls out of scope the destructor will
// close it? TODO: Look to see where this is used and how it might work.
_pcb = other._pcb;
if (_pcb) {
_rx_last_packet = millis();
tcp_setprio(_pcb, TCP_PRIO_MIN);
tcp_arg(_pcb, this);
tcp_recv(_pcb, &_s_recv);
tcp_sent(_pcb, &_s_sent);
tcp_err(_pcb, &_s_error);
tcp_poll(_pcb, &_s_poll, 1);
#if ASYNC_TCP_SSL_ENABLED
if(tcp_ssl_has(_pcb)){
_pcb_secure = true;
_handshake_done = false;
tcp_ssl_arg(_pcb, this);
tcp_ssl_data(_pcb, &_s_data);
tcp_ssl_handshake(_pcb, &_s_handshake);
tcp_ssl_err(_pcb, &_s_ssl_error);
} else {
_pcb_secure = false;
_handshake_done = true;
}
#endif
}
return *this;
}
bool AsyncClient::operator==(const AsyncClient &other) {
if (_pcb == NULL || other._pcb == NULL) {
return false;
}
if (_pcb->remote_ip.type != other._pcb->remote_ip.type) {
return false;
}
if (_pcb->remote_ip.type == IPADDR_TYPE_V4) {
if (_pcb->remote_ip.u_addr.ip4.addr != other._pcb->remote_ip.u_addr.ip4.addr) {
return false;
}
} else {
if (_pcb->remote_ip.u_addr.ip6.addr != other._pcb->remote_ip.u_addr.ip6.addr) {
return false;
}
}
return true;
}
void AsyncClient::abort(){
// Notes:
// 1) _pcb is set to NULL, so we cannot call tcp_abort() more than once.
// 2) setCloseError(ERR_ABRT) is only done here!
// 3) Using this abort() function guarantees only one tcp_abort() call is
// made and only one CB returns with ERR_ABORT.
// 4) After abort() is called from _close(), no callbacks with an err
// parameter will be called. eg. _recv(), _error(), _connected().
// _close() will reset there CB handlers before calling.
// 5) A callback to _error(), will set _pcb to NULL, thus avoiding the
// of a 2nd call to tcp_abort().
// 6) Callbacks to _recv() or _connected() with err set, will result in _pcb
// set to NULL. Thus, preventing possible calls later to tcp_abort().
if(_pcb) {
tcp_abort(_pcb);
_pcb = NULL;
setCloseError(ERR_ABRT);
}
return;
}
void AsyncClient::close(bool now){
if(_pcb)
tcp_recved(_pcb, _rx_ack_len);
if(now)
_close();
else
_close_pcb = true;
}
void AsyncClient::stop() {
close(false);
}
bool AsyncClient::free(){
if(!_pcb)
return true;
if(_pcb->state == 0 || _pcb->state > 4)
return true;
return false;
}
size_t AsyncClient::write(const char* data) {
if(data == NULL)
return 0;
return write(data, strlen(data));
}
size_t AsyncClient::write(const char* data, size_t size, uint8_t apiflags) {
size_t will_send = add(data, size, apiflags);
if(!will_send || !send())
return 0;
return will_send;
}
size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
if(!_pcb || size == 0 || data == NULL)
return 0;
size_t room = space();
if(!room)
return 0;
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
int sent = tcp_ssl_write(_pcb, (uint8_t*)data, size);
if(sent >= 0){
_tx_unacked_len += sent;
return sent;
}
_close();
return 0;
}
#endif
size_t will_send = (room < size) ? room : size;
err_t err = tcp_write(_pcb, data, will_send, apiflags);
if(err != ERR_OK) {
ASYNC_TCP_DEBUG("_add[%u]: tcp_write() returned err: %s(%ld)\n", getConnectionId(), errorToString(err), err);
return 0;
}
_tx_unsent_len += will_send;
return will_send;
}
bool AsyncClient::send(){
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure)
return true;
#endif
err_t err = tcp_output(_pcb);
if(err == ERR_OK){
_pcb_busy = true;
_pcb_sent_at = millis();
_tx_unacked_len += _tx_unsent_len;
_tx_unsent_len = 0;
return true;
}
ASYNC_TCP_DEBUG("send[%u]: tcp_output() returned err: %s(%ld)", getConnectionId(), errorToString(err), err);
_tx_unsent_len = 0;
return false;
}
size_t AsyncClient::ack(size_t len){
if(len > _rx_ack_len)
len = _rx_ack_len;
if(len)
tcp_recved(_pcb, len);
_rx_ack_len -= len;
return len;
}
// Private Callbacks
void AsyncClient::_connected(std::shared_ptr<ACErrorTracker>& errorTracker, void* pcb, err_t err){
//(void)err; // LWIP v1.4 appears to always call with ERR_OK
// Documentation for 2.1.0 also says:
// "err - An unused error code, always ERR_OK currently ;-)"
// https://www.nongnu.org/lwip/2_1_x/tcp_8h.html#a939867106bd492caf2d85852fb7f6ae8
// Based on that wording and emoji lets just handle it now.
// After all, the API does allow for an err != ERR_OK.
if(NULL == pcb || ERR_OK != err) {
ASYNC_TCP_DEBUG("_connected[%u]:%s err: %s(%ld)\n", errorTracker->getConnectionId(), ((NULL == pcb) ? " NULL == pcb!," : ""), errorToString(err), err);
errorTracker->setCloseError(err);
errorTracker->setErrored(EE_CONNECTED_CB);
_pcb = reinterpret_cast<tcp_pcb*>(pcb);
if (_pcb)
clearTcpCallbacks(_pcb);
_pcb = NULL;
_error(err);
return;
}
_pcb = reinterpret_cast<tcp_pcb*>(pcb);
if(_pcb){
_pcb_busy = false;
_rx_last_packet = millis();
tcp_setprio(_pcb, TCP_PRIO_MIN);
tcp_recv(_pcb, &_s_recv);
tcp_sent(_pcb, &_s_sent);
tcp_poll(_pcb, &_s_poll, 1);
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
if(tcp_ssl_new_client(_pcb) < 0){
_close();
return;
}
tcp_ssl_arg(_pcb, this);
tcp_ssl_data(_pcb, &_s_data);
tcp_ssl_handshake(_pcb, &_s_handshake);
tcp_ssl_err(_pcb, &_s_ssl_error);
}
}
if(!_pcb_secure && _connect_cb)
#else
}
if(_connect_cb)
#endif
_connect_cb(_connect_cb_arg, this);
return;
}
void AsyncClient::_close(){
if(_pcb) {
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
tcp_ssl_free(_pcb);
}
#endif
clearTcpCallbacks(_pcb);
err_t err = tcp_close(_pcb);
if(ERR_OK == err) {
setCloseError(err);
} else {
ASYNC_TCP_DEBUG("_close[%u]: abort() called for AsyncClient 0x%" PRIXPTR "\n", getConnectionId(), uintptr_t(this));
abort();
}
_pcb = NULL;
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
}
return;
}
void AsyncClient::_error(err_t err) {
ASYNC_TCP_DEBUG("_error[%u]:%s err: %s(%ld)\n", getConnectionId(), ((NULL == _pcb) ? " NULL == _pcb!," : ""), errorToString(err), err);
if(_pcb){
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
tcp_ssl_free(_pcb);
}
#endif
// At this callback _pcb is possible already freed. Thus, no calls are
// made to set to NULL other callbacks.
_pcb = NULL;
}
if(_error_cb)
_error_cb(_error_cb_arg, this, err);
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
}
#if ASYNC_TCP_SSL_ENABLED
void AsyncClient::_ssl_error(int8_t err){
if(_error_cb)
_error_cb(_error_cb_arg, this, err+64);
}
#endif
void AsyncClient::_sent(std::shared_ptr<ACErrorTracker>& errorTracker, tcp_pcb* pcb, uint16_t len) {
(void)pcb;
#if ASYNC_TCP_SSL_ENABLED
if (_pcb_secure && !_handshake_done)
return;
#endif
_rx_last_packet = millis();
_tx_unacked_len -= len;
_tx_acked_len += len;
ASYNC_TCP_DEBUG("_sent[%u]: %4u, unacked=%4u, acked=%4u, space=%4u\n", errorTracker->getConnectionId(), len, _tx_unacked_len, _tx_acked_len, space());
if(_tx_unacked_len == 0){
_pcb_busy = false;
errorTracker->setCloseError(ERR_OK);
if(_sent_cb) {
_sent_cb(_sent_cb_arg, this, _tx_acked_len, (millis() - _pcb_sent_at));
if(!errorTracker->hasClient())
return;
}
_tx_acked_len = 0;
}
return;
}
void AsyncClient::_recv(std::shared_ptr<ACErrorTracker>& errorTracker, tcp_pcb* pcb, pbuf* pb, err_t err) {
// While lwIP v1.4 appears to always call with ERR_OK, 2.x lwIP may present
// a non-ERR_OK value.
// https://www.nongnu.org/lwip/2_1_x/tcp_8h.html#a780cfac08b02c66948ab94ea974202e8
if(NULL == pcb || ERR_OK != err){
ASYNC_TCP_DEBUG("_recv[%u]:%s err: %s(%ld)\n", errorTracker->getConnectionId(), ((NULL == pcb) ? " NULL == pcb!," : ""), errorToString(err), err);
ASYNC_TCP_ASSERT(ERR_ABRT != err);
errorTracker->setCloseError(err);
errorTracker->setErrored(EE_RECV_CB);
_pcb = pcb;
if(_pcb)
clearTcpCallbacks(_pcb);
_pcb = NULL;
// I think we are safe from being called from an interrupt context.
// Best Hint that calling _error() is safe:
// https://www.nongnu.org/lwip/2_1_x/group__lwip__nosys.html
// "Feed incoming packets to netif->input(pbuf, netif) function from
// mainloop, not from interrupt context. You can allocate a Packet buffers
// (PBUF) in interrupt context and put them into a queue which is processed
// from mainloop."
// And the description of "Mainloop Mode" option 2:
// https://www.nongnu.org/lwip/2_1_x/pitfalls.html
// "2) Run lwIP in a mainloop. ... lwIP is ONLY called from mainloop
// callstacks here. The ethernet IRQ has to put received telegrams into a
// queue which is polled in the mainloop. Ensure lwIP is NEVER called from
// an interrupt, ...!"
// Based on these comments I am thinking tcp_recv_fn() is called
// from somebody's mainloop(), which could only have been reached from a
// delay like function or the Arduino sketch loop() function has returned.
// What I don't want is for the client sketch to delete the AsyncClient
// object via _error() while it is in the middle of using it. However,
// the client sketch must always test that the connection is still up
// at loop() entry and after the return of any function call, that may
// have done a delay() or yield().
_error(err);
return;
}
if(pb == NULL){
ASYNC_TCP_DEBUG("_recv[%u]: pb == NULL! Closing... %ld\n", errorTracker->getConnectionId(), err);
_close();
return;
}
_rx_last_packet = millis();
errorTracker->setCloseError(ERR_OK);
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
ASYNC_TCP_DEBUG("_recv[%u]: %d\n", getConnectionId(), pb->tot_len);
int read_bytes = tcp_ssl_read(pcb, pb);
if(read_bytes < 0){
if (read_bytes != SSL_CLOSE_NOTIFY) {
ASYNC_TCP_DEBUG("_recv[%u] err: %d\n", getConnectionId(), read_bytes);
_close();
}
}
return;
}
#endif
while(pb != NULL){
// IF this callback function returns ERR_OK or ERR_ABRT
// then it is assummed we freed the pbufs.
// https://www.nongnu.org/lwip/2_1_x/group__tcp__raw.html#ga8afd0b316a87a5eeff4726dc95006ed0
if(!errorTracker->hasClient()){
while(pb != NULL){
pbuf *b = pb;
pb = b->next;
b->next = NULL;
pbuf_free(b);
}
return;
}
//we should not ack before we assimilate the data
_ack_pcb = true;
pbuf *b = pb;
pb = b->next;
b->next = NULL;
ASYNC_TCP_DEBUG("_recv[%u]: %d%s\n", errorTracker->getConnectionId(), b->len, (b->flags&PBUF_FLAG_PUSH)?", PBUF_FLAG_PUSH":"");
if(_pb_cb){
_pb_cb(_pb_cb_arg, this, b);
} else {
if(_recv_cb){
_recv_pbuf_flags = b->flags;
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
}
if(errorTracker->hasClient()){
if(!_ack_pcb)
_rx_ack_len += b->len;
else
tcp_recved(pcb, b->len);
}
pbuf_free(b);
}
}
return;
}
void AsyncClient::_poll(std::shared_ptr<ACErrorTracker>& errorTracker, tcp_pcb* pcb){
(void)pcb;
errorTracker->setCloseError(ERR_OK);
// Close requested
if(_close_pcb){
_close_pcb = false;
_close();
return;
}
uint32_t now = millis();
// ACK Timeout
if(_pcb_busy && _ack_timeout && (now - _pcb_sent_at) >= _ack_timeout){
_pcb_busy = false;
if(_timeout_cb)
_timeout_cb(_timeout_cb_arg, this, (now - _pcb_sent_at));
return;
}
// RX Timeout
if(_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000)){
_close();
return;
}
#if ASYNC_TCP_SSL_ENABLED
// SSL Handshake Timeout
if(_pcb_secure && !_handshake_done && (now - _rx_last_packet) >= 2000){
_close();
return;
}
#endif
// Everything is fine
if(_poll_cb)
_poll_cb(_poll_cb_arg, this);
return;
}
#if LWIP_VERSION_MAJOR == 1
void AsyncClient::_dns_found(struct ip_addr *ipaddr){
#else
void AsyncClient::_dns_found(const ip_addr *ipaddr){
#endif
if(ipaddr){
#if ASYNC_TCP_SSL_ENABLED
connect(IPAddress(ipaddr->addr), _connect_port, _pcb_secure);
#else
connect(IPAddress(ipaddr), _connect_port);
#endif
} else {
if(_error_cb)
_error_cb(_error_cb_arg, this, -55);
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
}
}
// lwIP Callbacks
#if LWIP_VERSION_MAJOR == 1
void AsyncClient::_s_dns_found(const char *name, ip_addr_t *ipaddr, void *arg){
#else
void AsyncClient::_s_dns_found(const char *name, const ip_addr *ipaddr, void *arg){
#endif
(void)name;
reinterpret_cast<AsyncClient*>(arg)->_dns_found(ipaddr);
}
err_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) {
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
std::shared_ptr<ACErrorTracker>errorTracker = c->getACErrorTracker();
c->_poll(errorTracker, tpcb);
return errorTracker->getCallbackCloseError();
}
err_t AsyncClient::_s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
auto errorTracker = c->getACErrorTracker();
c->_recv(errorTracker, tpcb, pb, err);
return errorTracker->getCallbackCloseError();
}
void AsyncClient::_s_error(void *arg, err_t err) {
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
auto errorTracker = c->getACErrorTracker();
errorTracker->setCloseError(err);
errorTracker->setErrored(EE_ERROR_CB);
c->_error(err);
}
err_t AsyncClient::_s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
auto errorTracker = c->getACErrorTracker();
c->_sent(errorTracker, tpcb, len);
return errorTracker->getCallbackCloseError();
}
err_t AsyncClient::_s_connected(void* arg, void* tpcb, err_t err){
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
auto errorTracker = c->getACErrorTracker();
c->_connected(errorTracker, tpcb, err);
return errorTracker->getCallbackCloseError();
}
#if ASYNC_TCP_SSL_ENABLED
void AsyncClient::_s_data(void *arg, struct tcp_pcb *tcp, uint8_t * data, size_t len){
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
if(c->_recv_cb)
c->_recv_cb(c->_recv_cb_arg, c, data, len);
}
void AsyncClient::_s_handshake(void *arg, struct tcp_pcb *tcp, SSL *ssl){
AsyncClient *c = reinterpret_cast<AsyncClient*>(arg);
c->_handshake_done = true;
if(c->_connect_cb)
c->_connect_cb(c->_connect_cb_arg, c);
}
void AsyncClient::_s_ssl_error(void *arg, struct tcp_pcb *tcp, int8_t err){
reinterpret_cast<AsyncClient*>(arg)->_ssl_error(err);
}
#endif
// Operators
AsyncClient & AsyncClient::operator+=(const AsyncClient &other) {
if(next == NULL){
next = (AsyncClient*)(&other);
next->prev = this;
} else {
AsyncClient *c = next;
while(c->next != NULL) c = c->next;
c->next =(AsyncClient*)(&other);
c->next->prev = c;
}
return *this;
}
void AsyncClient::setRxTimeout(uint32_t timeout){
_rx_since_timeout = timeout;
}
uint32_t AsyncClient::getRxTimeout(){
return _rx_since_timeout;
}
uint32_t AsyncClient::getAckTimeout(){
return _ack_timeout;
}
void AsyncClient::setAckTimeout(uint32_t timeout){
_ack_timeout = timeout;
}
void AsyncClient::setNoDelay(bool nodelay){
if(!_pcb)
return;
if(nodelay)
tcp_nagle_disable(_pcb);
else
tcp_nagle_enable(_pcb);
}
bool AsyncClient::getNoDelay(){
if(!_pcb)
return false;
return tcp_nagle_disabled(_pcb);
}
uint16_t AsyncClient::getMss(){
if(_pcb)
return tcp_mss(_pcb);
return 0;
}
uint16_t AsyncClient::getRemotePort() {
if(!_pcb)
return 0;
return _pcb->remote_port;
}
uint16_t AsyncClient::getLocalPort() {
if(!_pcb)
return 0;
return _pcb->local_port;
}
IPAddress AsyncClient::remoteIP() {
return IPAddress(_pcb->remote_ip);
}
uint16_t AsyncClient::remotePort() {
return getRemotePort();
}
IPAddress AsyncClient::localIP() {
return IPAddress(_pcb->local_ip);
}
uint16_t AsyncClient::localPort() {
return getLocalPort();
}
#if ASYNC_TCP_SSL_ENABLED
SSL * AsyncClient::getSSL(){
if(_pcb && _pcb_secure){
return tcp_ssl_get_ssl(_pcb);
}
return NULL;
}
#endif
uint8_t AsyncClient::state() {
if(!_pcb)
return 0;
return _pcb->state;
}
bool AsyncClient::connected(){
if (!_pcb)
return false;
#if ASYNC_TCP_SSL_ENABLED
return _pcb->state == 4 && _handshake_done;
#else
return _pcb->state == 4;
#endif
}
bool AsyncClient::connecting(){
if (!_pcb)
return false;
return _pcb->state > 0 && _pcb->state < 4;
}
bool AsyncClient::disconnecting(){
if (!_pcb)
return false;
return _pcb->state > 4 && _pcb->state < 10;
}
bool AsyncClient::disconnected(){
if (!_pcb)
return true;
return _pcb->state == 0 || _pcb->state == 10;
}
bool AsyncClient::freeable(){
if (!_pcb)
return true;
return _pcb->state == 0 || _pcb->state > 4;
}
bool AsyncClient::canSend(){
return !_pcb_busy && (space() > 0);
}
// Callback Setters
void AsyncClient::onConnect(AcConnectHandler cb, void* arg){
_connect_cb = cb;
_connect_cb_arg = arg;
}
void AsyncClient::onDisconnect(AcConnectHandler cb, void* arg){
_discard_cb = cb;
_discard_cb_arg = arg;
}
void AsyncClient::onAck(AcAckHandler cb, void* arg){
_sent_cb = cb;
_sent_cb_arg = arg;
}
void AsyncClient::onError(AcErrorHandler cb, void* arg){
_error_cb = cb;
_error_cb_arg = arg;
}
void AsyncClient::onData(AcDataHandler cb, void* arg){
_recv_cb = cb;
_recv_cb_arg = arg;
}
void AsyncClient::onPacket(AcPacketHandler cb, void* arg){
_pb_cb = cb;
_pb_cb_arg = arg;
}
void AsyncClient::onTimeout(AcTimeoutHandler cb, void* arg){
_timeout_cb = cb;
_timeout_cb_arg = arg;
}
void AsyncClient::onPoll(AcConnectHandler cb, void* arg){
_poll_cb = cb;
_poll_cb_arg = arg;
}
size_t AsyncClient::space(){
#if ASYNC_TCP_SSL_ENABLED
if((_pcb != NULL) && (_pcb->state == 4) && _handshake_done){
uint16_t s = tcp_sndbuf(_pcb);
if(_pcb_secure){
#ifdef AXTLS_2_0_0_SNDBUF
return tcp_ssl_sndbuf(_pcb);
#else
if(s >= 128) //safe approach
return s - 128;
return 0;
#endif
}
return s;
}
#else // ASYNC_TCP_SSL_ENABLED
if((_pcb != NULL) && (_pcb->state == 4)){
return tcp_sndbuf(_pcb);
}
#endif // ASYNC_TCP_SSL_ENABLED
return 0;
}