add keepalive to socket connections
This commit is contained in:
parent
0a40ee7b1f
commit
ff1c6432af
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
|
@ -3,6 +3,7 @@
|
||||||
#include <lwip/sockets.h>
|
#include <lwip/sockets.h>
|
||||||
#include "GwBuffer.h"
|
#include "GwBuffer.h"
|
||||||
#include "GwSocketConnection.h"
|
#include "GwSocketConnection.h"
|
||||||
|
#include "GwSocketHelper.h"
|
||||||
|
|
||||||
GwSocketServer::GwSocketServer(const GwConfigHandler *config, GwLog *logger, int minId)
|
GwSocketServer::GwSocketServer(const GwConfigHandler *config, GwLog *logger, int minId)
|
||||||
{
|
{
|
||||||
|
@ -62,11 +63,13 @@ int GwSocketServer::available()
|
||||||
if (client_sock >= 0)
|
if (client_sock >= 0)
|
||||||
{
|
{
|
||||||
int val = 1;
|
int val = 1;
|
||||||
if (setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(int)) == ESP_OK)
|
if (! GwSocketHelper::setKeepAlive(client_sock,true)){
|
||||||
|
LOG_DEBUG(GwLog::ERROR,"unable to set keepalive, nodelay on socket");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (setsockopt(client_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(int)) == ESP_OK)
|
fcntl(client_sock, F_SETFL, O_NONBLOCK);
|
||||||
fcntl(client_sock, F_SETFL, O_NONBLOCK);
|
return client_sock;
|
||||||
return client_sock;
|
|
||||||
}
|
}
|
||||||
close(client_sock);
|
close(client_sock);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "GwTcpClient.h"
|
#include "GwTcpClient.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
|
#include "GwSocketHelper.h"
|
||||||
|
|
||||||
class ResolveArgs{
|
class ResolveArgs{
|
||||||
public:
|
public:
|
||||||
|
@ -72,6 +73,12 @@ void GwTcpClient::startConnection()
|
||||||
LOG_DEBUG(GwLog::ERROR,"unable to create socket: %d", errno);
|
LOG_DEBUG(GwLog::ERROR,"unable to create socket: %d", errno);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (! GwSocketHelper::setKeepAlive(sockfd,true)){
|
||||||
|
error="unable to set keepalive, nodelay on socket";
|
||||||
|
LOG_DEBUG(GwLog::ERROR,"%s",error.c_str());
|
||||||
|
close(sockfd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
fcntl( sockfd, F_SETFL, fcntl( sockfd, F_GETFL, 0 ) | O_NONBLOCK );
|
fcntl( sockfd, F_SETFL, fcntl( sockfd, F_GETFL, 0 ) | O_NONBLOCK );
|
||||||
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
|
||||||
if (res < 0 ) {
|
if (res < 0 ) {
|
||||||
|
|
Loading…
Reference in New Issue