@@ -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){
@@ -297,10 +298,15 @@ static void _tcp_error(void * arg, int8_t err) {
297
298
298
299
static void _tcp_dns_found (const char * name, struct ip_addr * ipaddr, void * arg) {
299
300
lwip_event_packet_t * e = (lwip_event_packet_t *)malloc (sizeof (lwip_event_packet_t ));
301
+ // ets_printf("+DNS: name=%s ipaddr=0x%08x arg=%x\n", name, ipaddr, arg);
300
302
e->event = LWIP_TCP_DNS;
301
303
e->arg = arg;
302
304
e->dns .name = name;
303
- memcpy (&e->dns .addr , ipaddr, sizeof (struct ip_addr ));
305
+ if (ipaddr) {
306
+ memcpy (&e->dns .addr , ipaddr, sizeof (struct ip_addr ));
307
+ } else {
308
+ memset (&e->dns .addr , 0 , sizeof (e->dns .addr ));
309
+ }
304
310
if (!_send_async_event (&e)) {
305
311
free ((void *)(e));
306
312
}
@@ -327,7 +333,6 @@ static int8_t _tcp_accept(void * arg, AsyncClient * client) {
327
333
typedef struct {
328
334
struct tcpip_api_call_data call;
329
335
tcp_pcb * pcb;
330
- AsyncClient * client;
331
336
int8_t err;
332
337
union {
333
338
struct {
@@ -352,39 +357,39 @@ typedef struct {
352
357
static err_t _tcp_output_api (struct tcpip_api_call_data *api_call_msg){
353
358
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
354
359
msg->err = ERR_CONN;
355
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
360
+ if (msg->pcb != pcb_recently_closed) {
356
361
msg->err = tcp_output (msg->pcb );
357
362
}
363
+ pcb_recently_closed = NULL ;
358
364
return msg->err ;
359
365
}
360
366
361
- static esp_err_t _tcp_output (tcp_pcb * pcb, AsyncClient * client ) {
367
+ static esp_err_t _tcp_output (tcp_pcb * pcb) {
362
368
if (!pcb){
363
369
return ERR_CONN;
364
370
}
365
371
tcp_api_call_t msg;
366
372
msg.pcb = pcb;
367
- msg.client = client;
368
373
tcpip_api_call (_tcp_output_api, (struct tcpip_api_call_data *)&msg);
369
374
return msg.err ;
370
375
}
371
376
372
377
static err_t _tcp_write_api (struct tcpip_api_call_data *api_call_msg){
373
378
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
374
379
msg->err = ERR_CONN;
375
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
380
+ if (msg->pcb != pcb_recently_closed) {
376
381
msg->err = tcp_write (msg->pcb , msg->write .data , msg->write .size , msg->write .apiflags );
377
382
}
383
+ pcb_recently_closed = NULL ;
378
384
return msg->err ;
379
385
}
380
386
381
- 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) {
382
388
if (!pcb){
383
389
return ERR_CONN;
384
390
}
385
391
tcp_api_call_t msg;
386
392
msg.pcb = pcb;
387
- msg.client = client;
388
393
msg.write .data = data;
389
394
msg.write .size = size;
390
395
msg.write .apiflags = apiflags;
@@ -395,20 +400,20 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, const char* data, size_t size, uint8_
395
400
static err_t _tcp_recved_api (struct tcpip_api_call_data *api_call_msg){
396
401
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
397
402
msg->err = ERR_CONN;
398
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
403
+ if (msg->pcb != pcb_recently_closed) {
399
404
msg->err = 0 ;
400
405
tcp_recved (msg->pcb , msg->received );
401
406
}
407
+ pcb_recently_closed = NULL ;
402
408
return msg->err ;
403
409
}
404
410
405
- 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) {
406
412
if (!pcb){
407
413
return ERR_CONN;
408
414
}
409
415
tcp_api_call_t msg;
410
416
msg.pcb = pcb;
411
- msg.client = client;
412
417
msg.received = len;
413
418
tcpip_api_call (_tcp_recved_api, (struct tcpip_api_call_data *)&msg);
414
419
return msg.err ;
@@ -417,39 +422,39 @@ static esp_err_t _tcp_recved(tcp_pcb * pcb, size_t len, AsyncClient * client) {
417
422
static err_t _tcp_close_api (struct tcpip_api_call_data *api_call_msg){
418
423
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
419
424
msg->err = ERR_CONN;
420
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
425
+ if (msg->pcb != pcb_recently_closed) {
421
426
msg->err = tcp_close (msg->pcb );
422
427
}
428
+ pcb_recently_closed = NULL ;
423
429
return msg->err ;
424
430
}
425
431
426
- static esp_err_t _tcp_close (tcp_pcb * pcb, AsyncClient * client ) {
432
+ static esp_err_t _tcp_close (tcp_pcb * pcb) {
427
433
if (!pcb){
428
434
return ERR_CONN;
429
435
}
430
436
tcp_api_call_t msg;
431
437
msg.pcb = pcb;
432
- msg.client = client;
433
438
tcpip_api_call (_tcp_close_api, (struct tcpip_api_call_data *)&msg);
434
439
return msg.err ;
435
440
}
436
441
437
442
static err_t _tcp_abort_api (struct tcpip_api_call_data *api_call_msg){
438
443
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
439
444
msg->err = ERR_CONN;
440
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
445
+ if (msg->pcb != pcb_recently_closed) {
441
446
tcp_abort (msg->pcb );
442
447
}
448
+ pcb_recently_closed = NULL ;
443
449
return msg->err ;
444
450
}
445
451
446
- static esp_err_t _tcp_abort (tcp_pcb * pcb, AsyncClient * client ) {
452
+ static esp_err_t _tcp_abort (tcp_pcb * pcb) {
447
453
if (!pcb){
448
454
return ERR_CONN;
449
455
}
450
456
tcp_api_call_t msg;
451
457
msg.pcb = pcb;
452
- msg.client = client;
453
458
tcpip_api_call (_tcp_abort_api, (struct tcpip_api_call_data *)&msg);
454
459
return msg.err ;
455
460
}
@@ -697,14 +702,14 @@ bool AsyncClient::connect(const char* host, uint16_t port){
697
702
698
703
void AsyncClient::close (bool now){
699
704
if (_pcb){
700
- _tcp_recved (_pcb, _rx_ack_len, this );
705
+ _tcp_recved (_pcb, _rx_ack_len);
701
706
}
702
707
_close ();
703
708
}
704
709
705
710
int8_t AsyncClient::abort (){
706
711
if (_pcb) {
707
- _tcp_abort (_pcb, this );
712
+ _tcp_abort (_pcb);
708
713
_pcb = NULL ;
709
714
}
710
715
return ERR_ABRT;
@@ -727,7 +732,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
727
732
}
728
733
size_t will_send = (room < size) ? room : size;
729
734
int8_t err = ERR_OK;
730
- err = _tcp_write (_pcb, data, will_send, apiflags, this );
735
+ err = _tcp_write (_pcb, data, will_send, apiflags);
731
736
if (err != ERR_OK) {
732
737
return 0 ;
733
738
}
@@ -736,7 +741,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
736
741
737
742
bool AsyncClient::send (){
738
743
int8_t err = ERR_OK;
739
- err = _tcp_output (_pcb, this );
744
+ err = _tcp_output (_pcb);
740
745
if (err == ERR_OK){
741
746
_pcb_busy = true ;
742
747
_pcb_sent_at = millis ();
@@ -749,7 +754,7 @@ size_t AsyncClient::ack(size_t len){
749
754
if (len > _rx_ack_len)
750
755
len = _rx_ack_len;
751
756
if (len){
752
- _tcp_recved (_pcb, len, this );
757
+ _tcp_recved (_pcb, len);
753
758
}
754
759
_rx_ack_len -= len;
755
760
return len;
@@ -759,7 +764,7 @@ void AsyncClient::ackPacket(struct pbuf * pb){
759
764
if (!pb){
760
765
return ;
761
766
}
762
- _tcp_recved (_pcb, pb->len , this );
767
+ _tcp_recved (_pcb, pb->len );
763
768
pbuf_free (pb);
764
769
}
765
770
@@ -778,7 +783,7 @@ int8_t AsyncClient::_close(){
778
783
tcp_err (_pcb, NULL );
779
784
tcp_poll (_pcb, NULL , 0 );
780
785
_tcp_clear_events (this );
781
- err = _tcp_close (_pcb, this );
786
+ err = _tcp_close (_pcb);
782
787
if (err != ERR_OK) {
783
788
err = abort ();
784
789
}
@@ -840,6 +845,7 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
840
845
if (tcp_close (_pcb) != ERR_OK) {
841
846
tcp_abort (_pcb);
842
847
}
848
+ pcb_recently_closed = _pcb;
843
849
_pcb = NULL ;
844
850
return ERR_OK;
845
851
}
@@ -880,7 +886,7 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
880
886
if (!_ack_pcb) {
881
887
_rx_ack_len += b->len ;
882
888
} else if (_pcb) {
883
- _tcp_recved (_pcb, b->len , this );
889
+ _tcp_recved (_pcb, b->len );
884
890
}
885
891
pbuf_free (b);
886
892
}
@@ -922,7 +928,7 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
922
928
}
923
929
924
930
void AsyncClient::_dns_found (struct ip_addr *ipaddr){
925
- if (ipaddr){
931
+ if (ipaddr && ipaddr-> u_addr . ip4 . addr ){
926
932
connect (IPAddress (ipaddr->u_addr .ip4 .addr ), _connect_port);
927
933
} else {
928
934
if (_error_cb) {
@@ -1227,7 +1233,7 @@ void AsyncServer::begin(){
1227
1233
err = _tcp_bind (_pcb, &local_addr, _port);
1228
1234
1229
1235
if (err != ERR_OK) {
1230
- _tcp_close (_pcb, NULL );
1236
+ _tcp_close (_pcb);
1231
1237
log_e (" bind error: %d" , err);
1232
1238
return ;
1233
1239
}
0 commit comments