-
Notifications
You must be signed in to change notification settings - Fork 397
/
ESPAsyncTCP.cpp
1084 lines (970 loc) · 23.6 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
*/
#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 TCP Client
*/
#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)
, _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_last_packet(0)
, _rx_since_timeout(0)
, _ack_timeout(ASYNC_MAX_ACK_TIME)
, _connect_port(0)
, 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
}
}
AsyncClient::~AsyncClient(){
if(_pcb)
_close();
}
#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;
addr.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;
}
#if ASYNC_TCP_SSL_ENABLED
_pcb_secure = secure;
_handshake_done = !secure;
#endif
tcp_arg(pcb, this);
tcp_err(pcb, &_s_error);
tcp_connect(pcb, &addr, port,(tcp_connected_fn)&_s_connected);
return true;
}
#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.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)
_close();
_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) {
return (_pcb != NULL && other._pcb != NULL && (_pcb->remote_ip.addr == other._pcb->remote_ip.addr) && (_pcb->remote_port == other._pcb->remote_port));
}
int8_t AsyncClient::abort(){
if(_pcb) {
tcp_abort(_pcb);
_pcb = NULL;
}
return ERR_ABRT;
}
void AsyncClient::close(bool now){
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;
int8_t err = tcp_write(_pcb, data, will_send, apiflags);
if(err != ERR_OK)
return 0;
_tx_unsent_len += will_send;
return will_send;
}
bool AsyncClient::send(){
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure)
return true;
#endif
if(tcp_output(_pcb) == ERR_OK){
_pcb_busy = true;
_pcb_sent_at = millis();
_tx_unacked_len += _tx_unsent_len;
_tx_unsent_len = 0;
return true;
}
_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
err_t AsyncClient::_connected(void* pcb, err_t err){
_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){
return _close();
}
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 ERR_OK;
}
int8_t AsyncClient::_close(){
int8_t err = ERR_OK;
if(_pcb) {
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
tcp_ssl_free(_pcb);
}
#endif
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
err = tcp_close(_pcb);
if(err != ERR_OK) {
err = abort();
}
_pcb = NULL;
if(_discard_cb)
_discard_cb(_discard_cb_arg, this);
}
return err;
}
void AsyncClient::_error(err_t err) {
if(_pcb){
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
tcp_ssl_free(_pcb);
}
#endif
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_poll(_pcb, NULL, 0);
_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
err_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
_rx_last_packet = millis();
ASYNC_TCP_DEBUG("_sent: %u\n", len);
_tx_unacked_len -= len;
_tx_acked_len += len;
if(_tx_unacked_len == 0){
_pcb_busy = false;
if(_sent_cb)
_sent_cb(_sent_cb_arg, this, _tx_acked_len, (millis() - _pcb_sent_at));
_tx_acked_len = 0;
}
return ERR_OK;
}
err_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, err_t err) {
if(pb == NULL){
ASYNC_TCP_DEBUG("_recv: pb == NULL! Closing... %d\n", err);
return _close();
}
_rx_last_packet = millis();
#if ASYNC_TCP_SSL_ENABLED
if(_pcb_secure){
ASYNC_TCP_DEBUG("_recv: %d\n", 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 err: %d\n", read_bytes);
_close();
}
return read_bytes;
}
return ERR_OK;
}
#endif
while(pb != NULL){
//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: %d\n", b->len);
if(_pb_cb){
_pb_cb(_pb_cb_arg, this, b);
} else {
if(_recv_cb)
_recv_cb(_recv_cb_arg, this, b->payload, b->len);
if(!_ack_pcb)
_rx_ack_len += b->len;
else
tcp_recved(pcb, b->len);
pbuf_free(b);
}
}
return ERR_OK;
}
err_t AsyncClient::_poll(tcp_pcb* pcb){
// Close requested
if(_close_pcb){
_close_pcb = false;
_close();
return ERR_OK;
}
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 ERR_OK;
}
// RX Timeout
if(_rx_since_timeout && (now - _rx_last_packet) >= (_rx_since_timeout * 1000)){
_close();
return ERR_OK;
}
#if ASYNC_TCP_SSL_ENABLED
// SSL Handshake Timeout
if(_pcb_secure && !_handshake_done && (now - _rx_last_packet) >= 2000){
_close();
return ERR_OK;
}
#endif
// Everything is fine
if(_poll_cb)
_poll_cb(_poll_cb_arg, this);
return ERR_OK;
}
#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->addr), _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
reinterpret_cast<AsyncClient*>(arg)->_dns_found(ipaddr);
}
err_t AsyncClient::_s_poll(void *arg, struct tcp_pcb *tpcb) {
return reinterpret_cast<AsyncClient*>(arg)->_poll(tpcb);
}
err_t AsyncClient::_s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
return reinterpret_cast<AsyncClient*>(arg)->_recv(tpcb, pb, err);
}
void AsyncClient::_s_error(void *arg, err_t err) {
reinterpret_cast<AsyncClient*>(arg)->_error(err);
}
err_t AsyncClient::_s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
return reinterpret_cast<AsyncClient*>(arg)->_sent(tpcb, len);
}
err_t AsyncClient::_s_connected(void* arg, void* tpcb, err_t err){
return reinterpret_cast<AsyncClient*>(arg)->_connected(tpcb, err);
}
#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;
}
uint32_t AsyncClient::getRemoteAddress() {
if(!_pcb)
return 0;
return _pcb->remote_ip.addr;
}
uint16_t AsyncClient::getRemotePort() {
if(!_pcb)
return 0;
return _pcb->remote_port;
}
uint32_t AsyncClient::getLocalAddress() {
if(!_pcb)
return 0;
return _pcb->local_ip.addr;
}
uint16_t AsyncClient::getLocalPort() {
if(!_pcb)
return 0;
return _pcb->local_port;
}
IPAddress AsyncClient::remoteIP() {
return IPAddress(getRemoteAddress());
}
uint16_t AsyncClient::remotePort() {
return getRemotePort();
}
IPAddress AsyncClient::localIP() {
return IPAddress(getLocalAddress());
}
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
if((_pcb != NULL) && (_pcb->state == 4)){
return tcp_sndbuf(_pcb);
}
#endif
return 0;
}
void AsyncClient::ackPacket(struct pbuf * pb){
if(!pb){
return;
}
tcp_recved(_pcb, pb->len);
pbuf_free(pb);
}
const char * AsyncClient::errorToString(int8_t error){
switch(error){
case 0: return "OK";
case -1: return "Out of memory error";
case -2: return "Buffer error";
case -3: return "Timeout";
case -4: return "Routing problem";
case -5: return "Operation in progress";
case -6: return "Illegal value";
case -7: return "Operation would block";
case -8: return "Connection aborted";
case -9: return "Connection reset";
case -10: return "Connection closed";
case -11: return "Not connected";
case -12: return "Illegal argument";
case -13: return "Address in use";
case -14: return "Low-level netif error";
case -15: return "Already connected";
case -55: return "DNS failed";
default: return "UNKNOWN";
}
}
const char * AsyncClient::stateToString(){
switch(state()){
case 0: return "Closed";
case 1: return "Listen";
case 2: return "SYN Sent";
case 3: return "SYN Received";
case 4: return "Established";
case 5: return "FIN Wait 1";
case 6: return "FIN Wait 2";
case 7: return "Close Wait";
case 8: return "Closing";
case 9: return "Last ACK";
case 10: return "Time Wait";
default: return "UNKNOWN";
}
}
/*
Async TCP Server
*/
struct pending_pcb {
tcp_pcb* pcb;
pbuf *pb;
struct pending_pcb * next;
};
AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
: _port(port)
, _addr(addr)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
#if ASYNC_TCP_SSL_ENABLED
, _pending(NULL)
, _ssl_ctx(NULL)
, _file_cb(0)
, _file_cb_arg(0)
#endif
{}
AsyncServer::AsyncServer(uint16_t port)
: _port(port)
, _addr((uint32_t) IPADDR_ANY)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
#if ASYNC_TCP_SSL_ENABLED
, _pending(NULL)
, _ssl_ctx(NULL)
, _file_cb(0)
, _file_cb_arg(0)
#endif
{}
AsyncServer::~AsyncServer(){
end();
}
void AsyncServer::onClient(AcConnectHandler cb, void* arg){
_connect_cb = cb;
_connect_cb_arg = arg;
}
#if ASYNC_TCP_SSL_ENABLED
void AsyncServer::onSslFileRequest(AcSSlFileHandler cb, void* arg){
_file_cb = cb;
_file_cb_arg = arg;
}
#endif
void AsyncServer::begin(){
if(_pcb)
return;
int8_t err;
tcp_pcb* pcb = tcp_new();
if (!pcb){
return;
}
ip_addr_t local_addr;
local_addr.addr = (uint32_t) _addr;
err = tcp_bind(pcb, &local_addr, _port);
if (err != ERR_OK) {
tcp_close(pcb);
return;
}
tcp_pcb* listen_pcb = tcp_listen(pcb);
if (!listen_pcb) {
tcp_close(pcb);
return;
}
_pcb = listen_pcb;
tcp_arg(_pcb, (void*) this);
tcp_accept(_pcb, &_s_accept);
}
#if ASYNC_TCP_SSL_ENABLED
void AsyncServer::beginSecure(const char *cert, const char *key, const char *password){
if(_ssl_ctx){
return;
}
tcp_ssl_file(_s_cert, this);
_ssl_ctx = tcp_ssl_new_server_ctx(cert, key, password);
if(_ssl_ctx){
begin();
}
}
#endif
void AsyncServer::end(){
if(_pcb){
//cleanup all connections?
tcp_arg(_pcb, NULL);
tcp_accept(_pcb, NULL);
if(tcp_close(_pcb) != ERR_OK){
tcp_abort(_pcb);
}
_pcb = NULL;
}
#if ASYNC_TCP_SSL_ENABLED
if(_ssl_ctx){
ssl_ctx_free(_ssl_ctx);
_ssl_ctx = NULL;
if(_pending){
struct pending_pcb * p;
while(_pending){
p = _pending;
_pending = _pending->next;
if(p->pb){
pbuf_free(p->pb);
}
free(p);
}
}
}
#endif
}
void AsyncServer::setNoDelay(bool nodelay){
_noDelay = nodelay;
}
bool AsyncServer::getNoDelay(){
return _noDelay;
}
uint8_t AsyncServer::status(){
if (!_pcb)
return 0;
return _pcb->state;
}
err_t AsyncServer::_accept(tcp_pcb* pcb, err_t err){
if(_connect_cb){
#if ASYNC_TCP_SSL_ENABLED
if (_noDelay || _ssl_ctx)
#else
if (_noDelay)
#endif
tcp_nagle_disable(pcb);
else
tcp_nagle_enable(pcb);
#if ASYNC_TCP_SSL_ENABLED
if(_ssl_ctx){
if(tcp_ssl_has_client() || _pending){
struct pending_pcb * new_item = (struct pending_pcb*)malloc(sizeof(struct pending_pcb));
if(!new_item){
ASYNC_TCP_DEBUG("### malloc new pending failed!\n");
if(tcp_close(pcb) != ERR_OK){
tcp_abort(pcb);
}
return ERR_OK;
}
ASYNC_TCP_DEBUG("### put to wait: %d\n", _clients_waiting);
new_item->pcb = pcb;
new_item->pb = NULL;
new_item->next = NULL;
tcp_setprio(_pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
tcp_poll(pcb, &_s_poll, 1);
tcp_recv(pcb, &_s_recv);
if(_pending == NULL){
_pending = new_item;
} else {
struct pending_pcb * p = _pending;
while(p->next != NULL)
p = p->next;
p->next = new_item;
}
} else {
AsyncClient *c = new AsyncClient(pcb, _ssl_ctx);
if(c){
c->onConnect([this](void * arg, AsyncClient *c){
_connect_cb(_connect_cb_arg, c);
}, this);
}
}
return ERR_OK;
} else {
AsyncClient *c = new AsyncClient(pcb, NULL);
#else
AsyncClient *c = new AsyncClient(pcb);
#endif
if(c){
_connect_cb(_connect_cb_arg, c);
return ERR_OK;
}
#if ASYNC_TCP_SSL_ENABLED
}
#endif
}
if(tcp_close(pcb) != ERR_OK){
tcp_abort(pcb);
}
return ERR_OK;
}
err_t AsyncServer::_s_accept(void *arg, tcp_pcb* pcb, err_t err){
return reinterpret_cast<AsyncServer*>(arg)->_accept(pcb, err);
}
#if ASYNC_TCP_SSL_ENABLED
err_t AsyncServer::_poll(tcp_pcb* pcb){
if(!tcp_ssl_has_client() && _pending){