Skip to content

Commit

Permalink
keep alive on by default on community feed
Browse files Browse the repository at this point in the history
  • Loading branch information
jvde-github committed Jan 29, 2025
1 parent 7a8b6d4 commit 8f3a402
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Application/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void Config::setSharing(const std::vector<JSON::Property> &props)
_msg.push_back(std::unique_ptr<IO::OutputMessage>(new IO::TCPClientStreamer()));
commm_feed = _msg.back().get();

commm_feed->Set("HOST", "aiscatcher.org").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off");
commm_feed->Set("HOST", "aiscatcher.org").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off").Set("KEEP_ALIVE", "on");
}
if (!uuid.empty() && commm_feed)
commm_feed->Set("UUID", uuid);
Expand Down
2 changes: 1 addition & 1 deletion Application/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ int main(int argc, char *argv[])
{
msg.push_back(std::unique_ptr<IO::OutputMessage>(new IO::TCPClientStreamer()));
commm_feed = msg.back().get();
commm_feed->Set("HOST", "185.77.96.227").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off");
commm_feed->Set("HOST", "185.77.96.227").Set("PORT", "4242").Set("JSON", "on").Set("FILTER", "on").Set("GPS", "off").Set("KEEP_ALIVE", "on");
}

if (count == 1 && commm_feed)
Expand Down
91 changes: 85 additions & 6 deletions Library/TCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@
#include <arpa/inet.h> // For inet_addr() and INADDR_ANY
#endif

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mstcpip.h>

#elif defined(__APPLE__)
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>

#elif defined(__ANDROID__)

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <android/log.h>
#else

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>

#endif

#include "TCP.h"

namespace TCP
Expand Down Expand Up @@ -120,7 +153,7 @@ namespace TCP
if (errno != EWOULDBLOCK && errno != EAGAIN)
{
#endif
if(verbose)
if (verbose)
Error() << "TCP Connection: error message to client: " << strerror(errno);

CloseUnsafe();
Expand Down Expand Up @@ -168,9 +201,9 @@ namespace TCP
if (errno != EWOULDBLOCK && errno != EAGAIN)
{
#endif
if(verbose)
if (verbose)
Error() << "TCP Connection: error message to client: " << strerror(errno);

CloseUnsafe();
return false;
}
Expand Down Expand Up @@ -456,7 +489,7 @@ namespace TCP
state = DISCONNECTED;
}

bool Client::connect(std::string host, std::string port, bool persist, int timeout, bool keep_alive)
bool Client::connect(std::string host, std::string port, bool persist, int timeout, bool keep_alive, int idle, int interval, int count)
{
int r;
struct addrinfo h;
Expand Down Expand Up @@ -490,13 +523,59 @@ namespace TCP
#ifndef _WIN32
if (keep_alive)
{
int optval = 1;
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1)
int yes = 1;

#ifdef _WIN32
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (const char *)&yes, sizeof(yes)))
#else
if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&yes, sizeof(yes)))
#endif
{
freeaddrinfo(address);
disconnect();
return false;
}
#if defined(__APPLE__)
if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPALIVE, &idle, sizeof(idle)))
{
freeaddrinfo(address);
disconnect();
return false;
}
#elif defined(_WIN32)
// Windows specific keepalive
struct tcp_keepalive keepalive;
keepalive.onoff = 1;
keepalive.keepalivetime = idle * 1000;
keepalive.keepaliveinterval = interval * 1000;
DWORD br;
if (WSAIoctl(sock, SIO_KEEPALIVE_VALS, &keepalive, sizeof(keepalive), NULL, 0, &br, NULL, NULL) == SOCKET_ERROR)
{
freeaddrinfo(address);
disconnect();
return false;
}
#elif defined(__ANDROID__)
// Android uses same config as Linux
if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) ||
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)) ||
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count)))
{
freeaddrinfo(address);

disconnect();
return false;
}
#else
if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) ||
setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval)) ||
setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &count, sizeof(count)))
{
freeaddrinfo(address);
disconnect();
return false;
}
#endif
}
#endif
if (persistent)
Expand Down
2 changes: 1 addition & 1 deletion Library/TCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace TCP
~Client() { disconnect(); }

void disconnect();
bool connect(std::string host, std::string port, bool persist, int timeout, bool keep_alive = false);
bool connect(std::string host, std::string port, bool persist, int timeout, bool keep_alive = false, int idle = 300, int interval = 180, int count = 3);

void setResetTime(int t) { reset_time = t; }
int read(void *data, int length, int t = 1, bool wait = false);
Expand Down

0 comments on commit 8f3a402

Please sign in to comment.