Skip to content

Commit

Permalink
add keepalive to socket connections
Browse files Browse the repository at this point in the history
  • Loading branch information
wellenvogel committed Jan 12, 2022
1 parent 0a40ee7 commit ff1c643
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/socketserver/GwSocketHelper.h
Original file line number Diff line number Diff line change
@@ -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;
}
};
11 changes: 7 additions & 4 deletions lib/socketserver/GwSocketServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <lwip/sockets.h>
#include "GwBuffer.h"
#include "GwSocketConnection.h"
#include "GwSocketHelper.h"

GwSocketServer::GwSocketServer(const GwConfigHandler *config, GwLog *logger, int minId)
{
Expand Down Expand Up @@ -62,11 +63,13 @@ int GwSocketServer::available()
if (client_sock >= 0)
{
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);
return client_sock;
fcntl(client_sock, F_SETFL, O_NONBLOCK);
return client_sock;
}
close(client_sock);
}
Expand Down
7 changes: 7 additions & 0 deletions lib/socketserver/GwTcpClient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GwTcpClient.h"
#include <functional>
#include <ESPmDNS.h>
#include "GwSocketHelper.h"

class ResolveArgs{
public:
Expand Down Expand Up @@ -72,6 +73,12 @@ void GwTcpClient::startConnection()
LOG_DEBUG(GwLog::ERROR,"unable to create socket: %d", errno);
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 );
int res = lwip_connect_r(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
if (res < 0 ) {
Expand Down

0 comments on commit ff1c643

Please sign in to comment.