From caa2a336f2d3b457cd1c26d5ef7c38fd62a8ec9f Mon Sep 17 00:00:00 2001 From: pictures23333 Date: Wed, 17 Dec 2025 13:41:09 +0800 Subject: [PATCH] feat(tcpclient): reconnect and handle network exceptions --- main/main.cpp | 2 +- main/main.h | 8 +++--- main/tcphelper.c | 64 +++++++++++++++++++++++++++++------------------- main/tcphelper.h | 1 + 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index e28fad0..3610c96 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -168,7 +168,7 @@ extern "C" void app_main() { mesh_main(); // tcp client - //start_tcp_client(); + tcp_client_initialize(); // Arduino-like setup() diff --git a/main/main.h b/main/main.h index 6330857..b7b7657 100644 --- a/main/main.h +++ b/main/main.h @@ -13,10 +13,10 @@ // change these static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x76 }; -//#define CONFIG_MESH_ROUTER_SSID "FBK_the_cutest_fox" -//#define CONFIG_MESH_ROUTER_PASSWD "zsfv3210" -#define CONFIG_MESH_ROUTER_SSID "FastCyberRoom" -#define CONFIG_MESH_ROUTER_PASSWD "dyes1107@" +#define CONFIG_MESH_ROUTER_SSID "FBK_the_cutest_fox" +#define CONFIG_MESH_ROUTER_PASSWD "zsfv3210" +//#define CONFIG_MESH_ROUTER_SSID "FastCyberRoom" +//#define CONFIG_MESH_ROUTER_PASSWD "dyes1107@" #define CONFIG_MESH_AP_AUTHMODE WIFI_AUTH_WPA2_PSK #define CONFIG_MESH_AP_PASSWD "RASRASRAS" #define CONFIG_MESH_AP_CONNECTIONS 6 // number of nodes diff --git a/main/tcphelper.c b/main/tcphelper.c index bdfda87..10822e0 100644 --- a/main/tcphelper.c +++ b/main/tcphelper.c @@ -106,14 +106,26 @@ static int socket_send(const char *tag, const int sock, const char * data, const return len; } +// variables +static const char *TAG = "nonblocking-socket-client"; + +SemaphoreHandle_t lock = NULL; +TaskHandle_t *task_handle = NULL; + +// tcp client +void tcp_client_initialize() { + lock = xSemaphoreCreateMutex(); + + return; +} + static void tcp_client_task(void *pvParameters) { - static const char *TAG = "nonblocking-socket-client"; static char rx_buffer[CONFIG_TCP_RXBUFFER_SIZE]; + int sock = INVALID_SOCK; struct addrinfo hints = { .ai_socktype = SOCK_STREAM }; struct addrinfo *address_info; - int sock = INVALID_SOCK; int res = getaddrinfo(CONFIG_TCP_SERVER_ADDRESS, CONFIG_TCP_SERVER_PORT, &hints, &address_info); if (res != 0 || address_info == NULL) { @@ -217,31 +229,33 @@ error: close(sock); } free(address_info); + + free(task_handle); + task_handle = NULL; vTaskDelete(NULL); } -/** - * @brief Returns the string representation of client's address (accepted on this server) - */ -static inline char* get_clients_address(struct sockaddr_storage *source_addr) -{ - static char address_str[128]; - char *res = NULL; - // Convert ip address to string - if (source_addr->ss_family == PF_INET) { - res = inet_ntoa_r(((struct sockaddr_in *)source_addr)->sin_addr, address_str, sizeof(address_str) - 1); - } -#ifdef CONFIG_LWIP_IPV6 - else if (source_addr->ss_family == PF_INET6) { - res = inet6_ntoa_r(((struct sockaddr_in6 *)source_addr)->sin6_addr, address_str, sizeof(address_str) - 1); - } -#endif - if (!res) { - address_str[0] = '\0'; // Returns empty string if conversion didn't succeed - } - return address_str; -} - void start_tcp_client(void) { - xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL); + if (xSemaphoreTake(lock, 1000 / portTICK_PERIOD_MS) == false) + { + return; + } + + // stop old task + if (task_handle) { + ESP_LOGI(TAG, "Detected old task, deleting..."); + + vTaskDelete(*task_handle); + free(task_handle); + task_handle = NULL; + } + + // new task + task_handle = malloc(sizeof(TaskHandle_t)); + memset(task_handle, 0, sizeof(TaskHandle_t)); + + xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, task_handle); + + xSemaphoreGive(lock); + return; } \ No newline at end of file diff --git a/main/tcphelper.h b/main/tcphelper.h index eae93d1..e5455a4 100644 --- a/main/tcphelper.h +++ b/main/tcphelper.h @@ -5,6 +5,7 @@ extern "C" { #endif // functions +void tcp_client_initialize(void); void start_tcp_client(void); #ifdef __cplusplus