Skip to content

Commit 13c3fcc

Browse files
committed
Set default wait time to 0 (no wait) to enqueue tcp events
1 parent 6820981 commit 13c3fcc

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ I personally use the following configuration in my projects:
4444
```c++
4545
-D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000 // (keep default)
4646
-D CONFIG_ASYNC_TCP_PRIORITY=10 // (keep default)
47-
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=64 // (keep default)
47+
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=128 // (keep default)
4848
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1 // force async_tcp task to be on same core as the app (default is core 0)
4949
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096 // reduce the stack size (default is 16K)
5050
```

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ build_flags =
99
-Wall -Wextra
1010
-D CONFIG_ASYNC_TCP_MAX_ACK_TIME=5000
1111
-D CONFIG_ASYNC_TCP_PRIORITY=10
12-
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=64
12+
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=128
1313
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1
1414
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096
1515
-D CONFIG_ARDUHAL_LOG_COLORS

src/AsyncTCP.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ static inline bool _init_async_event_queue() {
128128
return true;
129129
}
130130

131-
static inline bool _send_async_event(lwip_tcp_event_packet_t **e, TickType_t wait = portMAX_DELAY) {
131+
static inline bool _send_async_event(lwip_tcp_event_packet_t **e, TickType_t wait = 0) {
132132
return _async_queue && xQueueSend(_async_queue, e, wait) == pdPASS;
133133
}
134134

135-
static inline bool _prepend_async_event(lwip_tcp_event_packet_t **e, TickType_t wait = portMAX_DELAY) {
135+
static inline bool _prepend_async_event(lwip_tcp_event_packet_t **e, TickType_t wait = 0) {
136136
return _async_queue && xQueueSendToFront(_async_queue, e, wait) == pdPASS;
137137
}
138138

@@ -376,6 +376,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
376376
if (!_prepend_async_event(&e)) {
377377
free((void *)(e));
378378
log_e("Failed to queue event: LWIP_TCP_CONNECTED");
379+
tcp_abort(pcb);
379380
return ERR_TIMEOUT;
380381
}
381382
return ERR_OK;
@@ -402,6 +403,7 @@ static int8_t _tcp_poll(void *arg, struct tcp_pcb *pcb) {
402403
if (!_send_async_event(&e, 0)) {
403404
free((void *)(e));
404405
log_e("Failed to queue event: LWIP_TCP_POLL");
406+
tcp_abort(pcb);
405407
return ERR_TIMEOUT;
406408
}
407409
return ERR_OK;
@@ -431,6 +433,7 @@ static int8_t _tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, int8_t
431433
if (!_send_async_event(&e)) {
432434
free((void *)(e));
433435
log_e("Failed to queue event: LWIP_TCP_RECV or LWIP_TCP_FIN");
436+
tcp_abort(pcb);
434437
return ERR_TIMEOUT;
435438
}
436439
return ERR_OK;
@@ -450,6 +453,7 @@ static int8_t _tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
450453
if (!_send_async_event(&e)) {
451454
free((void *)(e));
452455
log_e("Failed to queue event: LWIP_TCP_SENT");
456+
tcp_abort(pcb);
453457
return ERR_TIMEOUT;
454458
}
455459
return ERR_OK;
@@ -505,6 +509,7 @@ static int8_t _tcp_accept(void *arg, AsyncClient *client) {
505509
if (!_prepend_async_event(&e)) {
506510
free((void *)(e));
507511
log_e("Failed to queue event: LWIP_TCP_ACCEPT");
512+
client->abort();
508513
return ERR_TIMEOUT;
509514
}
510515
return ERR_OK;
@@ -1639,28 +1644,32 @@ int8_t AsyncServer::_accept(tcp_pcb *pcb, int8_t err) {
16391644
return ERR_ABRT;
16401645
}
16411646
if (_connect_cb) {
1642-
AsyncClient *c = new (std::nothrow) AsyncClient(pcb);
1643-
if (c && c->pcb()) {
1644-
c->setNoDelay(_noDelay);
1645-
if (_tcp_accept(this, c) == ERR_OK) {
1646-
return ERR_OK; // success
1647+
if (uxQueueMessagesWaiting(_async_queue) < CONFIG_ASYNC_TCP_QUEUE_SIZE * 90 / 100) {
1648+
AsyncClient *c = new (std::nothrow) AsyncClient(pcb);
1649+
if (c && c->pcb()) {
1650+
c->setNoDelay(_noDelay);
1651+
if (_tcp_accept(this, c) == ERR_OK) {
1652+
return ERR_OK; // success
1653+
}
1654+
// Couldn't allocate accept event
1655+
// We can't let the client object call in to close, as we're on the LWIP thread; it could deadlock trying to RPC to itself
1656+
c->_pcb = nullptr;
1657+
tcp_abort(pcb);
1658+
log_e("_accept failed: couldn't accept client");
1659+
return ERR_ABRT;
16471660
}
1648-
// Couldn't allocate accept event
1649-
// We can't let the client object call in to close, as we're on the LWIP thread; it could deadlock trying to RPC to itself
1650-
c->_pcb = nullptr;
1651-
tcp_abort(pcb);
1652-
log_e("_accept failed: couldn't accept client");
1653-
return ERR_ABRT;
1654-
}
1655-
if (c) {
1656-
// Couldn't complete setup
1657-
// pcb has already been aborted
1658-
delete c;
1659-
pcb = nullptr;
1660-
log_e("_accept failed: couldn't complete setup");
1661-
return ERR_ABRT;
1661+
if (c) {
1662+
// Couldn't complete setup
1663+
// pcb has already been aborted
1664+
delete c;
1665+
pcb = nullptr;
1666+
log_e("_accept failed: couldn't complete setup");
1667+
return ERR_ABRT;
1668+
}
1669+
log_e("_accept failed: couldn't allocate client");
1670+
} else {
1671+
log_e("_accept failed: queue full");
16621672
}
1663-
log_e("_accept failed: couldn't allocate client");
16641673
} else {
16651674
log_e("_accept failed: no onConnect callback");
16661675
}

src/AsyncTCP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extern "C" {
4848
#endif
4949

5050
#ifndef CONFIG_ASYNC_TCP_QUEUE_SIZE
51-
#define CONFIG_ASYNC_TCP_QUEUE_SIZE 64
51+
#define CONFIG_ASYNC_TCP_QUEUE_SIZE 128
5252
#endif
5353

5454
#ifndef CONFIG_ASYNC_TCP_MAX_ACK_TIME

0 commit comments

Comments
 (0)