Skip to content

Commit ac2970f

Browse files
committed
Directly reset callbacks without going through lwip functions (fix #30)
1 parent e0787d9 commit ac2970f

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/AsyncTCP.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,23 @@ static int8_t _tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
448448
return ERR_OK;
449449
}
450450

451-
static void _tcp_error(void *arg, int8_t err) {
451+
void AsyncClient::_tcp_error(void *arg, int8_t err) {
452452
// ets_printf("+E: 0x%08x\n", arg);
453+
AsyncClient *client = reinterpret_cast<AsyncClient *>(arg);
454+
if (client && client->_pcb) {
455+
if (client->_pcb->state == LISTEN) {
456+
client->_pcb->sent = NULL;
457+
client->_pcb->recv = NULL;
458+
client->_pcb->errf = NULL;
459+
client->_pcb->poll = NULL;
460+
client->_pcb->pollinterval = 0;
461+
}
462+
client->_pcb->callback_arg = NULL;
463+
client->_pcb = nullptr;
464+
client->_free_closed_slot();
465+
}
466+
467+
// enqueue event to be processed in the async task for the user callback
453468
lwip_tcp_event_packet_t *e = (lwip_tcp_event_packet_t *)malloc(sizeof(lwip_tcp_event_packet_t));
454469
if (!e) {
455470
log_e("Failed to allocate event packet");
@@ -459,7 +474,7 @@ static void _tcp_error(void *arg, int8_t err) {
459474
e->arg = arg;
460475
e->error.err = err;
461476
if (!_send_async_event(&e)) {
462-
free((void *)(e));
477+
::free((void *)(e));
463478
}
464479
}
465480

@@ -1046,19 +1061,6 @@ int8_t AsyncClient::_connected(tcp_pcb *pcb, int8_t err) {
10461061
}
10471062

10481063
void AsyncClient::_error(int8_t err) {
1049-
if (_pcb) {
1050-
TCP_MUTEX_LOCK();
1051-
tcp_arg(_pcb, NULL);
1052-
if (_pcb->state == LISTEN) {
1053-
tcp_sent(_pcb, NULL);
1054-
tcp_recv(_pcb, NULL);
1055-
tcp_err(_pcb, NULL);
1056-
tcp_poll(_pcb, NULL, 0);
1057-
}
1058-
TCP_MUTEX_UNLOCK();
1059-
_free_closed_slot();
1060-
_pcb = NULL;
1061-
}
10621064
if (_error_cb) {
10631065
_error_cb(_error_cb_arg, this, err);
10641066
}

src/AsyncTCP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class AsyncClient {
239239
static int8_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len);
240240
static int8_t _s_connected(void *arg, struct tcp_pcb *tpcb, int8_t err);
241241
static void _s_dns_found(const char *name, struct ip_addr *ipaddr, void *arg);
242+
static void _tcp_error(void *arg, int8_t err);
242243

243244
int8_t _recv(tcp_pcb *pcb, pbuf *pb, int8_t err);
244245
tcp_pcb *pcb() {

0 commit comments

Comments
 (0)