Skip to content

Commit 0621a83

Browse files
committed
Set default wait time to 0 (no wait) to enqueue tcp events
1 parent 7a76d1e commit 0621a83

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
@@ -126,11 +126,11 @@ static inline bool _init_async_event_queue() {
126126
return true;
127127
}
128128

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

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

@@ -374,6 +374,7 @@ static int8_t _tcp_connected(void *arg, tcp_pcb *pcb, int8_t err) {
374374
if (!_prepend_async_event(&e)) {
375375
free((void *)(e));
376376
log_e("Failed to queue event: LWIP_TCP_CONNECTED");
377+
tcp_abort(pcb);
377378
return ERR_TIMEOUT;
378379
}
379380
return ERR_OK;
@@ -400,6 +401,7 @@ static int8_t _tcp_poll(void *arg, struct tcp_pcb *pcb) {
400401
if (!_send_async_event(&e, 0)) {
401402
free((void *)(e));
402403
log_e("Failed to queue event: LWIP_TCP_POLL");
404+
tcp_abort(pcb);
403405
return ERR_TIMEOUT;
404406
}
405407
return ERR_OK;
@@ -429,6 +431,7 @@ static int8_t _tcp_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, int8_t
429431
if (!_send_async_event(&e)) {
430432
free((void *)(e));
431433
log_e("Failed to queue event: LWIP_TCP_RECV or LWIP_TCP_FIN");
434+
tcp_abort(pcb);
432435
return ERR_TIMEOUT;
433436
}
434437
return ERR_OK;
@@ -448,6 +451,7 @@ static int8_t _tcp_sent(void *arg, struct tcp_pcb *pcb, uint16_t len) {
448451
if (!_send_async_event(&e)) {
449452
free((void *)(e));
450453
log_e("Failed to queue event: LWIP_TCP_SENT");
454+
tcp_abort(pcb);
451455
return ERR_TIMEOUT;
452456
}
453457
return ERR_OK;
@@ -503,6 +507,7 @@ static int8_t _tcp_accept(void *arg, AsyncClient *client) {
503507
if (!_prepend_async_event(&e)) {
504508
free((void *)(e));
505509
log_e("Failed to queue event: LWIP_TCP_ACCEPT");
510+
client->abort();
506511
return ERR_TIMEOUT;
507512
}
508513
return ERR_OK;
@@ -1637,28 +1642,32 @@ int8_t AsyncServer::_accept(tcp_pcb *pcb, int8_t err) {
16371642
return ERR_ABRT;
16381643
}
16391644
if (_connect_cb) {
1640-
AsyncClient *c = new (std::nothrow) AsyncClient(pcb);
1641-
if (c && c->pcb()) {
1642-
c->setNoDelay(_noDelay);
1643-
if (_tcp_accept(this, c) == ERR_OK) {
1644-
return ERR_OK; // success
1645+
if (uxQueueMessagesWaiting(_async_queue) < CONFIG_ASYNC_TCP_QUEUE_SIZE * 90 / 100) {
1646+
AsyncClient *c = new (std::nothrow) AsyncClient(pcb);
1647+
if (c && c->pcb()) {
1648+
c->setNoDelay(_noDelay);
1649+
if (_tcp_accept(this, c) == ERR_OK) {
1650+
return ERR_OK; // success
1651+
}
1652+
// Couldn't allocate accept event
1653+
// 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
1654+
c->_pcb = nullptr;
1655+
tcp_abort(pcb);
1656+
log_e("_accept failed: couldn't accept client");
1657+
return ERR_ABRT;
16451658
}
1646-
// Couldn't allocate accept event
1647-
// 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
1648-
c->_pcb = nullptr;
1649-
tcp_abort(pcb);
1650-
log_e("_accept failed: couldn't accept client");
1651-
return ERR_ABRT;
1652-
}
1653-
if (c) {
1654-
// Couldn't complete setup
1655-
// pcb has already been aborted
1656-
delete c;
1657-
pcb = nullptr;
1658-
log_e("_accept failed: couldn't complete setup");
1659-
return ERR_ABRT;
1659+
if (c) {
1660+
// Couldn't complete setup
1661+
// pcb has already been aborted
1662+
delete c;
1663+
pcb = nullptr;
1664+
log_e("_accept failed: couldn't complete setup");
1665+
return ERR_ABRT;
1666+
}
1667+
log_e("_accept failed: couldn't allocate client");
1668+
} else {
1669+
log_e("_accept failed: queue full");
16601670
}
1661-
log_e("_accept failed: couldn't allocate client");
16621671
} else {
16631672
log_e("_accept failed: no onConnect callback");
16641673
}

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)