@@ -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){
@@ -327,7 +328,6 @@ static int8_t _tcp_accept(void * arg, AsyncClient * client) {
327
328
typedef struct {
328
329
struct tcpip_api_call_data call;
329
330
tcp_pcb * pcb;
330
- AsyncClient * client;
331
331
int8_t err;
332
332
union {
333
333
struct {
@@ -352,39 +352,39 @@ typedef struct {
352
352
static err_t _tcp_output_api (struct tcpip_api_call_data *api_call_msg){
353
353
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
354
354
msg->err = ERR_CONN;
355
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
355
+ if (msg->pcb != pcb_recently_closed) {
356
356
msg->err = tcp_output (msg->pcb );
357
357
}
358
+ pcb_recently_closed = NULL ;
358
359
return msg->err ;
359
360
}
360
361
361
- static esp_err_t _tcp_output (tcp_pcb * pcb, AsyncClient * client ) {
362
+ static esp_err_t _tcp_output (tcp_pcb * pcb) {
362
363
if (!pcb){
363
364
return ERR_CONN;
364
365
}
365
366
tcp_api_call_t msg;
366
367
msg.pcb = pcb;
367
- msg.client = client;
368
368
tcpip_api_call (_tcp_output_api, (struct tcpip_api_call_data *)&msg);
369
369
return msg.err ;
370
370
}
371
371
372
372
static err_t _tcp_write_api (struct tcpip_api_call_data *api_call_msg){
373
373
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
374
374
msg->err = ERR_CONN;
375
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
375
+ if (msg->pcb != pcb_recently_closed) {
376
376
msg->err = tcp_write (msg->pcb , msg->write .data , msg->write .size , msg->write .apiflags );
377
377
}
378
+ pcb_recently_closed = NULL ;
378
379
return msg->err ;
379
380
}
380
381
381
- static esp_err_t _tcp_write (tcp_pcb * pcb, const char * data, size_t size, uint8_t apiflags, AsyncClient * client ) {
382
+ static esp_err_t _tcp_write (tcp_pcb * pcb, const char * data, size_t size, uint8_t apiflags) {
382
383
if (!pcb){
383
384
return ERR_CONN;
384
385
}
385
386
tcp_api_call_t msg;
386
387
msg.pcb = pcb;
387
- msg.client = client;
388
388
msg.write .data = data;
389
389
msg.write .size = size;
390
390
msg.write .apiflags = apiflags;
@@ -395,20 +395,20 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, const char* data, size_t size, uint8_
395
395
static err_t _tcp_recved_api (struct tcpip_api_call_data *api_call_msg){
396
396
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
397
397
msg->err = ERR_CONN;
398
- if (msg->client && msg-> client -> pcb () == msg-> pcb ) {
398
+ if (msg->pcb != pcb_recently_closed) {
399
399
msg->err = 0 ;
400
400
tcp_recved (msg->pcb , msg->received );
401
401
}
402
+ pcb_recently_closed = NULL ;
402
403
return msg->err ;
403
404
}
404
405
405
- static esp_err_t _tcp_recved (tcp_pcb * pcb, size_t len, AsyncClient * client ) {
406
+ static esp_err_t _tcp_recved (tcp_pcb * pcb, size_t len) {
406
407
if (!pcb){
407
408
return ERR_CONN;
408
409
}
409
410
tcp_api_call_t msg;
410
411
msg.pcb = pcb;
411
- msg.client = client;
412
412
msg.received = len;
413
413
tcpip_api_call (_tcp_recved_api, (struct tcpip_api_call_data *)&msg);
414
414
return msg.err ;
@@ -417,39 +417,39 @@ static esp_err_t _tcp_recved(tcp_pcb * pcb, size_t len, AsyncClient * client) {
417
417
static err_t _tcp_close_api (struct tcpip_api_call_data *api_call_msg){
418
418
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
419
419
msg->err = ERR_CONN;
420
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
420
+ if (msg->pcb != pcb_recently_closed) {
421
421
msg->err = tcp_close (msg->pcb );
422
422
}
423
+ pcb_recently_closed = NULL ;
423
424
return msg->err ;
424
425
}
425
426
426
- static esp_err_t _tcp_close (tcp_pcb * pcb, AsyncClient * client ) {
427
+ static esp_err_t _tcp_close (tcp_pcb * pcb) {
427
428
if (!pcb){
428
429
return ERR_CONN;
429
430
}
430
431
tcp_api_call_t msg;
431
432
msg.pcb = pcb;
432
- msg.client = client;
433
433
tcpip_api_call (_tcp_close_api, (struct tcpip_api_call_data *)&msg);
434
434
return msg.err ;
435
435
}
436
436
437
437
static err_t _tcp_abort_api (struct tcpip_api_call_data *api_call_msg){
438
438
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
439
439
msg->err = ERR_CONN;
440
- if (! msg->client || msg-> client -> pcb () == msg-> pcb ) {
440
+ if (msg->pcb != pcb_recently_closed) {
441
441
tcp_abort (msg->pcb );
442
442
}
443
+ pcb_recently_closed = NULL ;
443
444
return msg->err ;
444
445
}
445
446
446
- static esp_err_t _tcp_abort (tcp_pcb * pcb, AsyncClient * client ) {
447
+ static esp_err_t _tcp_abort (tcp_pcb * pcb) {
447
448
if (!pcb){
448
449
return ERR_CONN;
449
450
}
450
451
tcp_api_call_t msg;
451
452
msg.pcb = pcb;
452
- msg.client = client;
453
453
tcpip_api_call (_tcp_abort_api, (struct tcpip_api_call_data *)&msg);
454
454
return msg.err ;
455
455
}
@@ -697,14 +697,14 @@ bool AsyncClient::connect(const char* host, uint16_t port){
697
697
698
698
void AsyncClient::close (bool now){
699
699
if (_pcb){
700
- _tcp_recved (_pcb, _rx_ack_len, this );
700
+ _tcp_recved (_pcb, _rx_ack_len);
701
701
}
702
702
_close ();
703
703
}
704
704
705
705
int8_t AsyncClient::abort (){
706
706
if (_pcb) {
707
- _tcp_abort (_pcb, this );
707
+ _tcp_abort (_pcb);
708
708
_pcb = NULL ;
709
709
}
710
710
return ERR_ABRT;
@@ -727,7 +727,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
727
727
}
728
728
size_t will_send = (room < size) ? room : size;
729
729
int8_t err = ERR_OK;
730
- err = _tcp_write (_pcb, data, will_send, apiflags, this );
730
+ err = _tcp_write (_pcb, data, will_send, apiflags);
731
731
if (err != ERR_OK) {
732
732
return 0 ;
733
733
}
@@ -736,7 +736,7 @@ size_t AsyncClient::add(const char* data, size_t size, uint8_t apiflags) {
736
736
737
737
bool AsyncClient::send (){
738
738
int8_t err = ERR_OK;
739
- err = _tcp_output (_pcb, this );
739
+ err = _tcp_output (_pcb);
740
740
if (err == ERR_OK){
741
741
_pcb_busy = true ;
742
742
_pcb_sent_at = millis ();
@@ -749,7 +749,7 @@ size_t AsyncClient::ack(size_t len){
749
749
if (len > _rx_ack_len)
750
750
len = _rx_ack_len;
751
751
if (len){
752
- _tcp_recved (_pcb, len, this );
752
+ _tcp_recved (_pcb, len);
753
753
}
754
754
_rx_ack_len -= len;
755
755
return len;
@@ -759,7 +759,7 @@ void AsyncClient::ackPacket(struct pbuf * pb){
759
759
if (!pb){
760
760
return ;
761
761
}
762
- _tcp_recved (_pcb, pb->len , this );
762
+ _tcp_recved (_pcb, pb->len );
763
763
pbuf_free (pb);
764
764
}
765
765
@@ -778,7 +778,7 @@ int8_t AsyncClient::_close(){
778
778
tcp_err (_pcb, NULL );
779
779
tcp_poll (_pcb, NULL , 0 );
780
780
_tcp_clear_events (this );
781
- err = _tcp_close (_pcb, this );
781
+ err = _tcp_close (_pcb);
782
782
if (err != ERR_OK) {
783
783
err = abort ();
784
784
}
@@ -840,6 +840,7 @@ int8_t AsyncClient::_lwip_fin(tcp_pcb* pcb, int8_t err) {
840
840
if (tcp_close (_pcb) != ERR_OK) {
841
841
tcp_abort (_pcb);
842
842
}
843
+ pcb_recently_closed = _pcb;
843
844
_pcb = NULL ;
844
845
return ERR_OK;
845
846
}
@@ -880,7 +881,7 @@ int8_t AsyncClient::_recv(tcp_pcb* pcb, pbuf* pb, int8_t err) {
880
881
if (!_ack_pcb) {
881
882
_rx_ack_len += b->len ;
882
883
} else if (_pcb) {
883
- _tcp_recved (_pcb, b->len , this );
884
+ _tcp_recved (_pcb, b->len );
884
885
}
885
886
pbuf_free (b);
886
887
}
@@ -1227,7 +1228,7 @@ void AsyncServer::begin(){
1227
1228
err = _tcp_bind (_pcb, &local_addr, _port);
1228
1229
1229
1230
if (err != ERR_OK) {
1230
- _tcp_close (_pcb, NULL );
1231
+ _tcp_close (_pcb);
1231
1232
log_e (" bind error: %d" , err);
1232
1233
return ;
1233
1234
}
@@ -1246,7 +1247,7 @@ void AsyncServer::end(){
1246
1247
if (_pcb){
1247
1248
tcp_arg (_pcb, NULL );
1248
1249
tcp_accept (_pcb, NULL );
1249
- _tcp_abort (_pcb, NULL );
1250
+ _tcp_abort (_pcb);
1250
1251
_pcb = NULL ;
1251
1252
}
1252
1253
}
0 commit comments