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:
20
lib/socketserver/GwSocketHelper.h
Normal file
20
lib/socketserver/GwSocketHelper.h
Normal 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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user