From d98d1e235a8d3d726255d4410f2383fba25b68c6 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 17 Feb 2025 14:46:04 +0100 Subject: [PATCH] Directly reset callbacks without going through lwip functions (fix #30) --- src/AsyncTCP.cpp | 31 ++++++++++++++++--------------- src/AsyncTCP.h | 1 + 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 9d63acb..862c80f 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -448,8 +448,22 @@ static int8_t _tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) { return ERR_OK; } -static void _tcp_error(void *arg, int8_t err) { +void AsyncClient::_tcp_error(void *arg, int8_t err) { // ets_printf("+E: 0x%08x\n", arg); + AsyncClient *client = reinterpret_cast(arg); + if (client && client->_pcb) { + tcp_arg(client->_pcb, NULL); + if (client->_pcb->state == LISTEN) { + tcp_sent(client->_pcb, NULL); + tcp_recv(client->_pcb, NULL); + tcp_err(client->_pcb, NULL); + tcp_poll(client->_pcb, NULL, 0); + } + client->_pcb = nullptr; + client->_free_closed_slot(); + } + + // enqueue event to be processed in the async task for the user callback lwip_tcp_event_packet_t *e = (lwip_tcp_event_packet_t *)malloc(sizeof(lwip_tcp_event_packet_t)); if (!e) { log_e("Failed to allocate event packet"); @@ -459,7 +473,7 @@ static void _tcp_error(void *arg, int8_t err) { e->arg = arg; e->error.err = err; if (!_send_async_event(&e)) { - free((void *)(e)); + ::free((void *)(e)); } } @@ -1046,19 +1060,6 @@ int8_t AsyncClient::_connected(tcp_pcb *pcb, int8_t err) { } void AsyncClient::_error(int8_t err) { - if (_pcb) { - TCP_MUTEX_LOCK(); - tcp_arg(_pcb, NULL); - if (_pcb->state == LISTEN) { - tcp_sent(_pcb, NULL); - tcp_recv(_pcb, NULL); - tcp_err(_pcb, NULL); - tcp_poll(_pcb, NULL, 0); - } - TCP_MUTEX_UNLOCK(); - _free_closed_slot(); - _pcb = NULL; - } if (_error_cb) { _error_cb(_error_cb_arg, this, err); } diff --git a/src/AsyncTCP.h b/src/AsyncTCP.h index e041ca5..47f24ea 100644 --- a/src/AsyncTCP.h +++ b/src/AsyncTCP.h @@ -239,6 +239,7 @@ class AsyncClient { static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len); static int8_t _s_connected(void *arg, struct tcp_pcb *tpcb, int8_t err); static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg); + static void _tcp_error(void *arg, int8_t err); int8_t _recv(tcp_pcb *pcb, pbuf *pb, int8_t err); tcp_pcb *pcb() {