1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-16 07:23:07 +01:00

add keepalive to socket connections

This commit is contained in:
wellenvogel
2022-01-12 18:30:41 +01:00
parent 0a40ee7b1f
commit ff1c6432af
3 changed files with 34 additions and 4 deletions

View File

@@ -0,0 +1,20 @@
#pragma once
#include <lwip/sockets.h>
class GwSocketHelper{
public:
static bool setKeepAlive(int socket, bool noDelay=true){
int val=1;
if (noDelay){
if (setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(int)) != ESP_OK) return false;
}
if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(int)) != ESP_OK) return false;
val = 10; // seconds of idleness before first keepalive probe
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) != ESP_OK) return false;
val = 5; // interval between first and subsequent keepalives
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) != ESP_OK) return false;
val = 4; // number of lost keepalives before we close the socket
if (setsockopt(socket, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) != ESP_OK) return false;
return true;
}
};