feat(tcpclient): reconnect and handle network exceptions
This commit is contained in:
parent
38fe78427a
commit
caa2a336f2
4 changed files with 45 additions and 30 deletions
|
|
@ -168,7 +168,7 @@ extern "C" void app_main() {
|
|||
mesh_main();
|
||||
|
||||
// tcp client
|
||||
//start_tcp_client();
|
||||
tcp_client_initialize();
|
||||
|
||||
// Arduino-like setup()
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
// functions
|
||||
void tcp_client_initialize(void);
|
||||
void start_tcp_client(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue