@@ -256,7 +256,7 @@ static void _tcp_error(void * arg, int8_t err) {
256
256
#include " lwip/priv/tcpip_priv.h"
257
257
258
258
typedef struct {
259
- struct tcpip_api_call call;
259
+ struct tcpip_api_call_data call;
260
260
tcp_pcb * pcb;
261
261
int8_t err;
262
262
union {
@@ -279,20 +279,24 @@ typedef struct {
279
279
};
280
280
} tcp_api_call_t ;
281
281
282
- static err_t _tcp_output_api (struct tcpip_api_call *api_call_msg){
282
+ static err_t _tcp_output_api (struct tcpip_api_call_data *api_call_msg){
283
283
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
284
- msg->err = tcp_output (msg->pcb );
284
+ if (msg->pcb ){
285
+ msg->err = tcp_output (msg->pcb );
286
+ } else {
287
+ msg->err = 0 ;
288
+ }
285
289
return msg->err ;
286
290
}
287
291
288
292
static esp_err_t _tcp_output (tcp_pcb * pcb) {
289
293
tcp_api_call_t msg;
290
294
msg.pcb = pcb;
291
- tcpip_api_call (_tcp_output_api, (struct tcpip_api_call *)&msg);
295
+ tcpip_api_call (_tcp_output_api, (struct tcpip_api_call_data *)&msg);
292
296
return msg.err ;
293
297
}
294
298
295
- static err_t _tcp_write_api (struct tcpip_api_call *api_call_msg){
299
+ static err_t _tcp_write_api (struct tcpip_api_call_data *api_call_msg){
296
300
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
297
301
msg->err = tcp_write (msg->pcb , msg->write .data , msg->write .size , msg->write .apiflags );
298
302
return msg->err ;
@@ -304,11 +308,11 @@ static esp_err_t _tcp_write(tcp_pcb * pcb, const char* data, size_t size, uint8_
304
308
msg.write .data = data;
305
309
msg.write .size = size;
306
310
msg.write .apiflags = apiflags;
307
- tcpip_api_call (_tcp_write_api, (struct tcpip_api_call *)&msg);
311
+ tcpip_api_call (_tcp_write_api, (struct tcpip_api_call_data *)&msg);
308
312
return msg.err ;
309
313
}
310
314
311
- static err_t _tcp_recved_api (struct tcpip_api_call *api_call_msg){
315
+ static err_t _tcp_recved_api (struct tcpip_api_call_data *api_call_msg){
312
316
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
313
317
msg->err = 0 ;
314
318
tcp_recved (msg->pcb , msg->received );
@@ -319,11 +323,11 @@ static esp_err_t _tcp_recved(tcp_pcb * pcb, size_t len) {
319
323
tcp_api_call_t msg;
320
324
msg.pcb = pcb;
321
325
msg.received = len;
322
- tcpip_api_call (_tcp_recved_api, (struct tcpip_api_call *)&msg);
326
+ tcpip_api_call (_tcp_recved_api, (struct tcpip_api_call_data *)&msg);
323
327
return msg.err ;
324
328
}
325
329
326
- static err_t _tcp_connect_api (struct tcpip_api_call *api_call_msg){
330
+ static err_t _tcp_connect_api (struct tcpip_api_call_data *api_call_msg){
327
331
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
328
332
msg->err = tcp_connect (msg->pcb , msg->connect .addr , msg->connect .port , msg->connect .cb );
329
333
return msg->err ;
@@ -335,11 +339,11 @@ static esp_err_t _tcp_connect(tcp_pcb * pcb, ip_addr_t * addr, uint16_t port, tc
335
339
msg.connect .addr = addr;
336
340
msg.connect .port = port;
337
341
msg.connect .cb = cb;
338
- tcpip_api_call (_tcp_connect_api, (struct tcpip_api_call *)&msg);
342
+ tcpip_api_call (_tcp_connect_api, (struct tcpip_api_call_data *)&msg);
339
343
return msg.err ;
340
344
}
341
345
342
- static err_t _tcp_close_api (struct tcpip_api_call *api_call_msg){
346
+ static err_t _tcp_close_api (struct tcpip_api_call_data *api_call_msg){
343
347
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
344
348
msg->err = tcp_close (msg->pcb );
345
349
return msg->err ;
@@ -349,11 +353,11 @@ static esp_err_t _tcp_close(tcp_pcb * pcb) {
349
353
tcp_api_call_t msg;
350
354
msg.pcb = pcb;
351
355
// ets_printf("close 0x%08x\n", (uint32_t)pcb);
352
- tcpip_api_call (_tcp_close_api, (struct tcpip_api_call *)&msg);
356
+ tcpip_api_call (_tcp_close_api, (struct tcpip_api_call_data *)&msg);
353
357
return msg.err ;
354
358
}
355
359
356
- static err_t _tcp_abort_api (struct tcpip_api_call *api_call_msg){
360
+ static err_t _tcp_abort_api (struct tcpip_api_call_data *api_call_msg){
357
361
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
358
362
msg->err = 0 ;
359
363
tcp_abort (msg->pcb );
@@ -364,11 +368,11 @@ static esp_err_t _tcp_abort(tcp_pcb * pcb) {
364
368
tcp_api_call_t msg;
365
369
msg.pcb = pcb;
366
370
// ets_printf("abort 0x%08x\n", (uint32_t)pcb);
367
- tcpip_api_call (_tcp_abort_api, (struct tcpip_api_call *)&msg);
371
+ tcpip_api_call (_tcp_abort_api, (struct tcpip_api_call_data *)&msg);
368
372
return msg.err ;
369
373
}
370
374
371
- static err_t _tcp_bind_api (struct tcpip_api_call *api_call_msg){
375
+ static err_t _tcp_bind_api (struct tcpip_api_call_data *api_call_msg){
372
376
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
373
377
msg->err = tcp_bind (msg->pcb , msg->bind .addr , msg->bind .port );
374
378
return msg->err ;
@@ -379,11 +383,11 @@ static esp_err_t _tcp_bind(tcp_pcb * pcb, ip_addr_t * addr, uint16_t port) {
379
383
msg.pcb = pcb;
380
384
msg.bind .addr = addr;
381
385
msg.bind .port = port;
382
- tcpip_api_call (_tcp_bind_api, (struct tcpip_api_call *)&msg);
386
+ tcpip_api_call (_tcp_bind_api, (struct tcpip_api_call_data *)&msg);
383
387
return msg.err ;
384
388
}
385
389
386
- static err_t _tcp_listen_api (struct tcpip_api_call *api_call_msg){
390
+ static err_t _tcp_listen_api (struct tcpip_api_call_data *api_call_msg){
387
391
tcp_api_call_t * msg = (tcp_api_call_t *)api_call_msg;
388
392
msg->err = 0 ;
389
393
msg->pcb = tcp_listen_with_backlog (msg->pcb , msg->backlog );
@@ -394,7 +398,7 @@ static tcp_pcb * _tcp_listen_with_backlog(tcp_pcb * pcb, uint8_t backlog) {
394
398
tcp_api_call_t msg;
395
399
msg.pcb = pcb;
396
400
msg.backlog = backlog?backlog:0xFF ;
397
- tcpip_api_call (_tcp_listen_api, (struct tcpip_api_call *)&msg);
401
+ tcpip_api_call (_tcp_listen_api, (struct tcpip_api_call_data *)&msg);
398
402
return msg.pcb ;
399
403
}
400
404
#define _tcp_listen (p ) _tcp_listen_with_backlog(p, 0xFF );
@@ -432,6 +436,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
432
436
, next(NULL )
433
437
, _in_lwip_thread(false )
434
438
{
439
+ // ets_printf("+: 0x%08x\n", (uint32_t)this);
440
+
435
441
_pcb = pcb;
436
442
if (_pcb){
437
443
_rx_last_packet = millis ();
@@ -447,6 +453,8 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
447
453
AsyncClient::~AsyncClient (){
448
454
if (_pcb)
449
455
_close ();
456
+
457
+ // ets_printf("-: 0x%08x\n", (uint32_t)this);
450
458
}
451
459
452
460
bool AsyncClient::connect (IPAddress ip, uint16_t port){
@@ -512,6 +520,7 @@ int8_t AsyncClient::_connected(void* pcb, int8_t err){
512
520
}
513
521
514
522
int8_t AsyncClient::_close (){
523
+ // ets_printf("X: 0x%08x\n", (uint32_t)this);
515
524
int8_t err = ERR_OK;
516
525
if (_pcb) {
517
526
// log_i("");
@@ -520,6 +529,7 @@ int8_t AsyncClient::_close(){
520
529
tcp_recv (_pcb, NULL );
521
530
tcp_err (_pcb, NULL );
522
531
tcp_poll (_pcb, NULL , 0 );
532
+ _tcp_clear_events (this );
523
533
if (_in_lwip_thread){
524
534
err = tcp_close (_pcb);
525
535
} else {
@@ -529,7 +539,6 @@ int8_t AsyncClient::_close(){
529
539
err = abort ();
530
540
}
531
541
_pcb = NULL ;
532
- _tcp_clear_events (this );
533
542
if (_discard_cb)
534
543
_discard_cb (_discard_cb_arg, this );
535
544
}
@@ -562,6 +571,13 @@ int8_t AsyncClient::_sent(tcp_pcb* pcb, uint16_t len) {
562
571
}
563
572
564
573
int8_t AsyncClient::_recv (tcp_pcb* pcb, pbuf* pb, int8_t err) {
574
+ if (!_pcb || pcb != _pcb){
575
+ log_e (" 0x%08x != 0x%08x" , (uint32_t )pcb, (uint32_t )_pcb);
576
+ if (pb){
577
+ pbuf_free (pb);
578
+ }
579
+ return ERR_OK;
580
+ }
565
581
_in_lwip_thread = false ;
566
582
if (pb == NULL ){
567
583
return _close ();
@@ -621,7 +637,7 @@ int8_t AsyncClient::_poll(tcp_pcb* pcb){
621
637
return ERR_OK;
622
638
}
623
639
624
- void AsyncClient::_dns_found (ip_addr_t *ipaddr){
640
+ void AsyncClient::_dns_found (struct ip_addr *ipaddr){
625
641
_in_lwip_thread = true ;
626
642
if (ipaddr){
627
643
connect (IPAddress (ipaddr->u_addr .ip4 .addr ), _connect_port);
@@ -943,31 +959,58 @@ void AsyncClient::onPoll(AcConnectHandler cb, void* arg){
943
959
}
944
960
945
961
946
- void AsyncClient::_s_dns_found (const char * name, ip_addr_t * ipaddr, void * arg){
947
- reinterpret_cast <AsyncClient*>(arg)->_dns_found (ipaddr);
962
+ void AsyncClient::_s_dns_found (const char * name, struct ip_addr * ipaddr, void * arg){
963
+ if (arg){
964
+ reinterpret_cast <AsyncClient*>(arg)->_dns_found (ipaddr);
965
+ } else {
966
+ log_e (" Bad Arg: 0x%08x" , arg);
967
+ }
948
968
}
949
969
950
970
int8_t AsyncClient::_s_poll (void * arg, struct tcp_pcb * pcb) {
951
- reinterpret_cast <AsyncClient*>(arg)->_poll (pcb);
971
+ if (arg && pcb){
972
+ reinterpret_cast <AsyncClient*>(arg)->_poll (pcb);
973
+ } else {
974
+ log_e (" Bad Args: 0x%08x 0x%08x" , arg, pcb);
975
+ }
952
976
return ERR_OK;
953
977
}
954
978
955
979
int8_t AsyncClient::_s_recv (void * arg, struct tcp_pcb * pcb, struct pbuf *pb, int8_t err) {
956
- reinterpret_cast <AsyncClient*>(arg)->_recv (pcb, pb, err);
980
+ if (arg && pcb){
981
+ reinterpret_cast <AsyncClient*>(arg)->_recv (pcb, pb, err);
982
+ } else {
983
+ if (pb){
984
+ pbuf_free (pb);
985
+ }
986
+ log_e (" Bad Args: 0x%08x 0x%08x" , arg, pcb);
987
+ }
957
988
return ERR_OK;
958
989
}
959
990
960
991
int8_t AsyncClient::_s_sent (void * arg, struct tcp_pcb * pcb, uint16_t len) {
961
- reinterpret_cast <AsyncClient*>(arg)->_sent (pcb, len);
992
+ if (arg && pcb){
993
+ reinterpret_cast <AsyncClient*>(arg)->_sent (pcb, len);
994
+ } else {
995
+ log_e (" Bad Args: 0x%08x 0x%08x" , arg, pcb);
996
+ }
962
997
return ERR_OK;
963
998
}
964
999
965
1000
void AsyncClient::_s_error (void * arg, int8_t err) {
966
- reinterpret_cast <AsyncClient*>(arg)->_error (err);
1001
+ if (arg){
1002
+ reinterpret_cast <AsyncClient*>(arg)->_error (err);
1003
+ } else {
1004
+ log_e (" Bad Arg: 0x%08x" , arg);
1005
+ }
967
1006
}
968
1007
969
1008
int8_t AsyncClient::_s_connected (void * arg, void * pcb, int8_t err){
970
- reinterpret_cast <AsyncClient*>(arg)->_connected (pcb, err);
1009
+ if (arg && pcb){
1010
+ reinterpret_cast <AsyncClient*>(arg)->_connected (pcb, err);
1011
+ } else {
1012
+ log_e (" Bad Args: 0x%08x 0x%08x" , arg, pcb);
1013
+ }
971
1014
return ERR_OK;
972
1015
}
973
1016
@@ -1122,9 +1165,9 @@ void AsyncServer::end(){
1122
1165
tcp_arg (_pcb, NULL );
1123
1166
tcp_accept (_pcb, NULL );
1124
1167
if (_in_lwip_thread){
1125
- tcp_abort (_pcb);
1168
+ tcp_close (_pcb);
1126
1169
} else {
1127
- _tcp_abort (_pcb);
1170
+ _tcp_close (_pcb);
1128
1171
}
1129
1172
_pcb = NULL ;
1130
1173
}
0 commit comments