async tcp formatting

This commit is contained in:
MichaelDvP
2024-02-05 08:42:38 +01:00
parent 738d9b1d94
commit 99d7ff0dc7
2 changed files with 644 additions and 594 deletions

View File

@@ -38,7 +38,15 @@ extern "C"{
* */
typedef enum {
LWIP_TCP_SENT, LWIP_TCP_RECV, LWIP_TCP_FIN, LWIP_TCP_ERROR, LWIP_TCP_POLL, LWIP_TCP_CLEAR, LWIP_TCP_ACCEPT, LWIP_TCP_CONNECTED, LWIP_TCP_DNS
LWIP_TCP_SENT,
LWIP_TCP_RECV,
LWIP_TCP_FIN,
LWIP_TCP_ERROR,
LWIP_TCP_POLL,
LWIP_TCP_CLEAR,
LWIP_TCP_ACCEPT,
LWIP_TCP_CONNECTED,
LWIP_TCP_DNS
} lwip_event_t;
typedef struct {
@@ -78,7 +86,7 @@ typedef struct {
};
} lwip_event_packet_t;
static xQueueHandle _async_queue;
static QueueHandle_t _async_queue;
static TaskHandle_t _async_service_task_handle = NULL;
@@ -97,7 +105,8 @@ static uint32_t _closed_index = []() {
static inline bool _init_async_event_queue() {
if (!_async_queue) {
_async_queue = xQueueCreate(32, sizeof(lwip_event_packet_t *));
_async_queue =
xQueueCreate(CONFIG_ASYNC_TCP_QUEUE, sizeof(lwip_event_packet_t *)); // double queue to 128 see https://github.com/emsesp/EMS-ESP32/issues/177
if (!_async_queue) {
return false;
}
@@ -216,8 +225,7 @@ static void _stop_async_task(){
}
*/
static bool customTaskCreateUniversal(
TaskFunction_t pxTaskCode,
static bool customTaskCreateUniversal(TaskFunction_t pxTaskCode,
const char * const pcName,
const uint32_t usStackDepth,
void * const pvParameters,
@@ -240,7 +248,7 @@ static bool _start_async_task(){
return false;
}
if (!_async_service_task_handle) {
customTaskCreateUniversal(_async_service_task, "async_tcp", CONFIG_ASYNC_TCP_STACK_SIZE, NULL, 3, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);
customTaskCreateUniversal(_async_service_task, "async_tcp", CONFIG_ASYNC_TCP_STACK_SIZE, NULL, CONFIG_ASYNC_TCP_TASK_PRIORITY, &_async_service_task_handle, CONFIG_ASYNC_TCP_RUNNING_CORE);
if (!_async_service_task_handle) {
return false;
}
@@ -584,8 +592,7 @@ AsyncClient::AsyncClient(tcp_pcb* pcb)
, _ack_timeout(ASYNC_MAX_ACK_TIME)
, _connect_port(0)
, prev(NULL)
, next(NULL)
{
, next(NULL) {
_pcb = pcb;
_closed_slot = -1;
if (_pcb) {
@@ -1232,41 +1239,71 @@ bool AsyncClient::canSend(){
const char * AsyncClient::errorToString(int8_t error) {
switch (error) {
case ERR_OK: return "OK";
case ERR_MEM: return "Out of memory error";
case ERR_BUF: return "Buffer error";
case ERR_TIMEOUT: return "Timeout";
case ERR_RTE: return "Routing problem";
case ERR_INPROGRESS: return "Operation in progress";
case ERR_VAL: return "Illegal value";
case ERR_WOULDBLOCK: return "Operation would block";
case ERR_USE: return "Address in use";
case ERR_ALREADY: return "Already connected";
case ERR_CONN: return "Not connected";
case ERR_IF: return "Low-level netif error";
case ERR_ABRT: return "Connection aborted";
case ERR_RST: return "Connection reset";
case ERR_CLSD: return "Connection closed";
case ERR_ARG: return "Illegal argument";
case -55: return "DNS failed";
default: return "UNKNOWN";
case ERR_OK:
return "OK";
case ERR_MEM:
return "Out of memory error";
case ERR_BUF:
return "Buffer error";
case ERR_TIMEOUT:
return "Timeout";
case ERR_RTE:
return "Routing problem";
case ERR_INPROGRESS:
return "Operation in progress";
case ERR_VAL:
return "Illegal value";
case ERR_WOULDBLOCK:
return "Operation would block";
case ERR_USE:
return "Address in use";
case ERR_ALREADY:
return "Already connected";
case ERR_CONN:
return "Not connected";
case ERR_IF:
return "Low-level netif error";
case ERR_ABRT:
return "Connection aborted";
case ERR_RST:
return "Connection reset";
case ERR_CLSD:
return "Connection closed";
case ERR_ARG:
return "Illegal argument";
case -55:
return "DNS failed";
default:
return "UNKNOWN";
}
}
const char * AsyncClient::stateToString() {
switch (state()) {
case 0: return "Closed";
case 1: return "Listen";
case 2: return "SYN Sent";
case 3: return "SYN Received";
case 4: return "Established";
case 5: return "FIN Wait 1";
case 6: return "FIN Wait 2";
case 7: return "Close Wait";
case 8: return "Closing";
case 9: return "Last ACK";
case 10: return "Time Wait";
default: return "UNKNOWN";
case 0:
return "Closed";
case 1:
return "Listen";
case 2:
return "SYN Sent";
case 3:
return "SYN Received";
case 4:
return "Established";
case 5:
return "FIN Wait 1";
case 6:
return "FIN Wait 2";
case 7:
return "Close Wait";
case 8:
return "Closing";
case 9:
return "Last ACK";
case 10:
return "Time Wait";
default:
return "UNKNOWN";
}
}
@@ -1317,8 +1354,8 @@ AsyncServer::AsyncServer(IPAddress addr, uint16_t port)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
{}
, _connect_cb_arg(0) {
}
AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
: _port(port)
@@ -1327,8 +1364,8 @@ AsyncServer::AsyncServer(IPv6Address addr, uint16_t port)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
{}
, _connect_cb_arg(0) {
}
AsyncServer::AsyncServer(uint16_t port)
: _port(port)
@@ -1339,8 +1376,8 @@ AsyncServer::AsyncServer(uint16_t port)
, _noDelay(false)
, _pcb(0)
, _connect_cb(0)
, _connect_cb_arg(0)
{}
, _connect_cb_arg(0) {
}
AsyncServer::~AsyncServer() {
end();

View File

@@ -46,11 +46,20 @@ extern "C" {
//If core is not defined, then we are running in Arduino or PIO
#ifndef CONFIG_ASYNC_TCP_RUNNING_CORE
#define CONFIG_ASYNC_TCP_RUNNING_CORE -1 //any available core
#define CONFIG_ASYNC_TCP_USE_WDT 1 //if enabled, adds between 33us and 200us per event
#define CONFIG_ASYNC_TCP_USE_WDT 0 //if enabled, adds between 33us and 200us per event
#endif
#ifndef CONFIG_ASYNC_TCP_TASK_PRIORITY
#define CONFIG_ASYNC_TCP_TASK_PRIORITY 5
#endif
#ifndef CONFIG_ASYNC_TCP_STACK_SIZE
#define CONFIG_ASYNC_TCP_STACK_SIZE 8192 * 2
#define CONFIG_ASYNC_TCP_STACK_SIZE 5120
#endif
#ifndef CONFIG_ASYNC_TCP_QUEUE
#define CONFIG_ASYNC_TCP_QUEUE 128
#endif
class AsyncClient;
@@ -143,7 +152,9 @@ class AsyncClient {
void ackPacket(struct pbuf * pb); //ack pbuf from onPacket
size_t ack(size_t len); //ack data that you have not acked using the method below
void ackLater(){ _ack_pcb = false; } //will not ack the current packet. Call from onData
void ackLater() {
_ack_pcb = false;
} //will not ack the current packet. Call from onData
const char * errorToString(int8_t error);
const char * stateToString();
@@ -159,7 +170,9 @@ class AsyncClient {
static void _s_dns_found(const char * name, struct ip_addr * ipaddr, void * arg);
int8_t _recv(tcp_pcb * pcb, pbuf * pb, int8_t err);
tcp_pcb * pcb(){ return _pcb; }
tcp_pcb * pcb() {
return _pcb;
}
protected:
bool _connect(ip_addr_t addr, uint16_t port);