mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-08 04:02:27 +00:00
Modify esp-tls and tcp_transport to support keep alive for tcp and ssl connection
Closes IDFGH-4543
This commit is contained in:
@@ -161,6 +161,34 @@ static void ms_to_timeval(int timeout_ms, struct timeval *tv)
|
||||
tv->tv_usec = (timeout_ms % 1000) * 1000;
|
||||
}
|
||||
|
||||
static int esp_tls_tcp_enable_keep_alive(int fd, tls_keep_alive_cfg_t *cfg)
|
||||
{
|
||||
int keep_alive_enable = 1;
|
||||
int keep_alive_idle = cfg->keep_alive_idle;
|
||||
int keep_alive_interval = cfg->keep_alive_interval;
|
||||
int keep_alive_count = cfg->keep_alive_count;
|
||||
|
||||
ESP_LOGD(TAG, "Enable TCP keep alive. idle: %d, interval: %d, count: %d", keep_alive_idle, keep_alive_interval, keep_alive_count);
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keep_alive_enable, sizeof(keep_alive_enable)) != 0) {
|
||||
ESP_LOGE(TAG, "Fail to setsockopt SO_KEEPALIVE");
|
||||
return -1;
|
||||
}
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_alive_idle, sizeof(keep_alive_idle)) != 0) {
|
||||
ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPIDLE");
|
||||
return -1;
|
||||
}
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keep_alive_interval, sizeof(keep_alive_interval)) != 0) {
|
||||
ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPINTVL");
|
||||
return -1;
|
||||
}
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keep_alive_count, sizeof(keep_alive_count)) != 0) {
|
||||
ESP_LOGE(TAG, "Fail to setsockopt TCP_KEEPCNT");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd, const esp_tls_t *tls, const esp_tls_cfg_t *cfg)
|
||||
{
|
||||
esp_err_t ret;
|
||||
@@ -199,6 +227,12 @@ static esp_err_t esp_tcp_connect(const char *host, int hostlen, int port, int *s
|
||||
ms_to_timeval(cfg->timeout_ms, &tv);
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
if (cfg->keep_alive_cfg && cfg->keep_alive_cfg->keep_alive_enable) {
|
||||
if (esp_tls_tcp_enable_keep_alive(fd, cfg->keep_alive_cfg) < 0) {
|
||||
ESP_LOGE(TAG, "Error setting keep-alive");
|
||||
goto err_freesocket;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cfg->non_block) {
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
|
Reference in New Issue
Block a user