@@ -78,6 +78,7 @@ typedef struct {
78
78
79
79
static xQueueHandle _async_queue;
80
80
static TaskHandle_t _async_service_task_handle = NULL ;
81
+ static tcp_pcb * pcb_recently_closed = NULL ;
81
82
82
83
static inline bool _init_async_event_queue (){
83
84
if (!_async_queue){
@@ -161,6 +162,9 @@ static void _handle_async_event(lwip_event_packet_t * e){
161
162
} else if (e->event == LWIP_TCP_ACCEPT){
162
163
// ets_printf("A: 0x%08x 0x%08x\n", e->arg, e->accept.client);
163
164
AsyncServer::_s_accepted (e->arg , e->accept .client );
165
+ } else if (e->event == LWIP_TCP_DNS){
166
+ // ets_printf("D: 0x%08x %s = %s\n", e->arg, e->dns.name, ipaddr_ntoa(&e->dns.addr));
167
+ AsyncClient::_s_dns_found (e->dns .name , &e->dns .addr , e->arg );
164
168
}
165
169
free ((void *)(e));
166
170
}
@@ -329,7 +333,6 @@ static int8_t _tcp_accept(void * arg, AsyncClient * client) {
329
333
typedef struct {
330
334
struct tcpip_api_call_data call;
331
335
tcp_pcb * pcb;
332
- AsyncClient * client;
333
336
int8_t err;
334
337
union {
335
338
struct {
@@ -354,39 +357,39 @@ typedef struct {
354
357
static err_t _tcp_output_api (struct tcpip_api_call_data *api_call_msg){
355
358
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
356
359
msg->err = ERR_CONN;
357
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
360
+ if (msg->pcb != pcb_recently_closed) {
358
361
msg->err = tcp_output (msg->pcb );
359
362
}
363
+ pcb_recently_closed = NULL ;
360
364
return msg->err ;
361
365
}
362
366
363
- static esp_err_t _tcp_output (tcp_pcb * pcb, AsyncClient * client ) {
367
+ static esp_err_t _tcp_output (tcp_pcb * pcb) {
364
368
if (!pcb){
365
369
return ERR_CONN;
366
370
}
367
371
tcp_api_call_t msg;
368
372
msg.pcb = pcb;
369
- msg.client = client;
370
373
tcpip_api_call (_tcp_output_api, (struct tcpip_api_call_data *)&msg);
371
374
return msg.err ;
372
375
}
373
376
374
377
static err_t _tcp_write_api (struct tcpip_api_call_data *api_call_msg){
375
378
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
376
379
msg->err = ERR_CONN;
377
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
380
+ if (msg->pcb != pcb_recently_closed) {
378
381
msg->err = tcp_write (msg->pcb , msg->write .data , msg->write .size , msg->write .apiflags );
379
382
}
383
+ pcb_recently_closed = NULL ;
380
384
return msg->err ;
381
385
}
382
386
383
- static esp_err_t _tcp_write (tcp_pcb * pcb, const char * data, size_t size, uint8_t apiflags, AsyncClient * client ) {
387
+ static esp_err_t _tcp_write (tcp_pcb * pcb, const char * data, size_t size, uint8_t apiflags) {
384
388
if (!pcb){
385
389
return ERR_CONN;
386
390
}
387
391
tcp_api_call_t msg;
388
392
msg.pcb = pcb;
389
- msg.client = client;
390
393
msg.write .data = data;
391
394
msg.write .size = size;
392
395
msg.write .apiflags = apiflags;
@@ -397,20 +400,20 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, const char* data, size_t size, uint8_
397
400
static err_t _tcp_recved_api (struct tcpip_api_call_data *api_call_msg){
398
401
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
399
402
msg->err = ERR_CONN;
400
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
403
+ if (msg->pcb != pcb_recently_closed) {
401
404
msg->err = 0 ;
402
405
tcp_recved (msg->pcb , msg->received );
403
406
}
407
+ pcb_recently_closed = NULL ;
404
408
return msg->err ;
405
409
}
406
410
407
- static esp_err_t _tcp_recved (tcp_pcb * pcb, size_t len, AsyncClient * client ) {
411
+ static esp_err_t _tcp_recved (tcp_pcb * pcb, size_t len) {
408
412
if (!pcb){
409
413
return ERR_CONN;
410
414
}
411
415
tcp_api_call_t msg;
412
416
msg.pcb = pcb;
413
- msg.client = client;
414
417
msg.received = len;
415
418
tcpip_api_call (_tcp_recved_api, (struct tcpip_api_call_data *)&msg);
416
419
return msg.err ;
@@ -419,39 +422,39 @@ static esp_err_t _tcp_recved(tcp_pcb * pcb, size_t len, AsyncClient * client) {
419
422
static err_t _tcp_close_api (struct tcpip_api_call_data *api_call_msg){
420
423
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
421
424
msg->err = ERR_CONN;
422
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
425
+ if (msg->pcb != pcb_recently_closed) {
423
426
msg->err = tcp_close (msg->pcb );
424
427
}
428
+ pcb_recently_closed = NULL ;
425
429
return msg->err ;
426
430
}
427
431
428
- static esp_err_t _tcp_close (tcp_pcb * pcb, AsyncClient * client ) {
432
+ static esp_err_t _tcp_close (tcp_pcb * pcb) {
429
433
if (!pcb){
430
434
return ERR_CONN;
431
435
}
432
436
tcp_api_call_t msg;
433
437
msg.pcb = pcb;
434
- msg.client = client;
435
438
tcpip_api_call (_tcp_close_api, (struct tcpip_api_call_data *)&msg);
436
439
return msg.err ;
437
440
}
438
441
439
442
static err_t _tcp_abort_api (struct tcpip_api_call_data *api_call_msg){
440
443
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
441
444
msg->err = ERR_CONN;
442
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
445
+ if (msg->pcb != pcb_recently_closed) {
443
446
tcp_abort (msg->pcb );
444
447
}
448
+ pcb_recently_closed = NULL ;
445
449
return msg->err ;
446
450
}
447
451
448
- static esp_err_t _tcp_abort (tcp_pcb * pcb, AsyncClient * client ) {
452
+ static esp_err_t _tcp_abort (tcp_pcb * pcb) {
449
453
if (!pcb){
450
454
return ERR_CONN;
451
455
}
452
456
tcp_api_call_t msg;
453
457
msg.pcb = pcb;
454
- msg.client = client;
455
458
tcpip_api_call (_tcp_abort_api, (struct tcpip_api_call_data *)&msg);
456
459
return msg.err ;
457
460
}
@@ -679,6 +682,13 @@ bool AsyncClient::connect(IPAddress ip, uint16_t port){
679
682
680
683
bool AsyncClient::connect (const char * host, uint16_t port){
681
684
ip_addr_t addr;
685
+
686
+ if (!_start_async_task ()){
687
+ Serial.println (" failed to start task" );
688
+ log_e (" failed to start task" );
689
+ return false ;
690
+ }
691
+
682
692
err_t err = dns_gethostbyname (host, &addr, (dns_found_callback)&_tcp_dns_found, this );
683
693
if (err == ERR_OK) {
684
694
return connect (IPAddress (addr.u_addr .ip4 .addr ), port);
@@ -692,14 +702,14 @@ bool AsyncClient::connect(const char* host, uint16_t port){
692
702
693
703
void AsyncClient::close (bool now){
694
704
if (_pcb){
695
- _tcp_recved (_pcb, _rx_ack_len, this );
705
+ _tcp_recved (_pcb, _rx_ack_len);
696
706
}
697
707
_close ();
698
708
}
699
709
700
710
int8_t AsyncClient::abort (){
701
711
if (_pcb) {
702
- _tcp_abort (_pcb, this );
712
+ _tcp_abort (_pcb);
703
713
_pcb = NULL ;
704
714
}
705
715
return ERR_ABRT;
@@ -722,7 +732,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
722
732
}
723
733
size_t will_send = (room < size) ? room : size;
724
734
int8_t err = ERR_OK;
725
- err = _tcp_write (_pcb, data, will_send, apiflags, this );
735
+ err = _tcp_write (_pcb, data, will_send, apiflags);
726
736
if (err != ERR_OK) {
727
737
return 0 ;
728
738
}
@@ -731,7 +741,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
731
741
732
742
bool AsyncClient::send (){
733
743
int8_t err = ERR_OK;
734
- err = _tcp_output (_pcb, this );
744
+ err = _tcp_output (_pcb);
735
745
if (err == ERR_OK){
736
746
_pcb_busy = true ;
737
747
_pcb_sent_at = millis ();
@@ -744,7 +754,7 @@ size_t AsyncClient::ack(size_t len){
744
754
if (len > _rx_ack_len)
745
755
len = _rx_ack_len;
746
756
if (len){
747
- _tcp_recved (_pcb, len, this );
757
+ _tcp_recved (_pcb, len);
748
758
}
749
759
_rx_ack_len -= len;
750
760
return len;
@@ -754,7 +764,7 @@ void AsyncClient::ackPacket(struct pbuf * pb){
754
764
if (!pb){
755
765
return ;
756
766
}
757
- _tcp_recved (_pcb, pb->len , this );
767
+ _tcp_recved (_pcb, pb->len );
758
768
pbuf_free (pb);
759
769
}
760
770
@@ -773,7 +783,7 @@ int8_t AsyncClient::_close(){
773
783
tcp_err (_pcb, NULL );
774
784
tcp_poll (_pcb, NULL , 0 );
775
785
_tcp_clear_events (this );
776
- err = _tcp_close (_pcb, this );
786
+ err = _tcp_close (_pcb);
777
787
if (err != ERR_OK) {
778
788
err = abort ();
779
789
}
@@ -835,6 +845,7 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
835
845
if (tcp_close (_pcb) != ERR_OK) {
836
846
tcp_abort (_pcb);
837
847
}
848
+ pcb_recently_closed = _pcb;
838
849
_pcb = NULL ;
839
850
return ERR_OK;
840
851
}
@@ -875,7 +886,7 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
875
886
if (!_ack_pcb) {
876
887
_rx_ack_len += b->len ;
877
888
} else if (_pcb) {
878
- _tcp_recved (_pcb, b->len , this );
889
+ _tcp_recved (_pcb, b->len );
879
890
}
880
891
pbuf_free (b);
881
892
}
@@ -1222,7 +1233,7 @@ void AsyncServer::begin(){
1222
1233
err = _tcp_bind (_pcb, &local_addr, _port);
1223
1234
1224
1235
if (err != ERR_OK) {
1225
- _tcp_close (_pcb, NULL );
1236
+ _tcp_close (_pcb);
1226
1237
log_e (" bind error: %d" , err);
1227
1238
return ;
1228
1239
}
@@ -1241,7 +1252,7 @@ void AsyncServer::end(){
1241
1252
if (_pcb){
1242
1253
tcp_arg (_pcb, NULL );
1243
1254
tcp_accept (_pcb, NULL );
1244
- _tcp_abort (_pcb, NULL );
1255
+ _tcp_abort (_pcb);
1245
1256
_pcb = NULL ;
1246
1257
}
1247
1258
}
0 commit comments