Skip to content

Commit 480b485

Browse files
author
Jonathan Wase
committed
feat: add tcp user timeout to handle connection issues
1 parent 4974658 commit 480b485

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

dependency/supl/tcp_client.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <cstring>
55
#include <net/if.h>
66
#include <netdb.h>
7+
#include <netinet/tcp.h>
78
#include <sys/socket.h>
89
#include <sys/types.h>
910
#include <unistd.h>
10-
#include <netinet/tcp.h>
1111

1212
#include <loglet/loglet.hpp>
1313

@@ -188,28 +188,34 @@ bool TcpClient::connect(std::string const& host, int port, std::string const& in
188188
}
189189

190190
// set socket keepalive options
191-
int enable = 1;
192-
int idle = 15;
193-
int interval = 5;
194-
int count = 3;
191+
int enable = 1;
192+
int idle = 1; // First probe after 1 second idle
193+
int interval = 2; // Subsequent probes every 2 seconds
194+
int count = 3; // 3 probes before giving up
195+
int user_timeout = 8000; // 8 seconds total timeout
195196

196197
auto result = setsockopt(mSocket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));
197-
VERBOSEF("setsockopt(%d, SOL_SOCKET, SO_KEEPALIVE, %p, %zu) = %d", mSocket, &enable,
198-
sizeof(enable), result);
198+
VERBOSEF("::setsockopt(%d, SOL_SOCKET, SO_KEEPALIVE, *%p=%d, %zu) = %d", mSocket, &enable,
199+
enable, sizeof(enable), result);
199200

200201
// Configure keep-alive parameters
201202
result = setsockopt(mSocket, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
202-
VERBOSEF("setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE, %p, %zu) = %d", mSocket, &idle,
203+
VERBOSEF("::setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE, *%p=%d, %zu) = %d", mSocket, &idle, idle,
203204
sizeof(idle), result);
204205

205206
result = setsockopt(mSocket, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
206-
VERBOSEF("setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL, %p, %zu) = %d", mSocket, &interval,
207-
sizeof(interval), result);
207+
VERBOSEF("::setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL, *%p=%d, %zu) = %d", mSocket, &interval,
208+
interval, sizeof(interval), result);
208209

209210
result = setsockopt(mSocket, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count));
210-
VERBOSEF("setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT, %p, %zu) = %d", mSocket, &count,
211+
VERBOSEF("::setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT, *%p=%d, %zu) = %d", mSocket, &count, count,
211212
sizeof(count), result);
212213

214+
result =
215+
setsockopt(mSocket, IPPROTO_TCP, TCP_USER_TIMEOUT, &user_timeout, sizeof(user_timeout));
216+
VERBOSEF("::setsockopt(%d, IPPROTO_TCP, TCP_USER_TIMEOUT, *%p=%d, %zu) = %d", mSocket,
217+
&user_timeout, user_timeout, sizeof(user_timeout), result);
218+
213219
mState = State::CONNECTING;
214220
return true;
215221
}

0 commit comments

Comments
 (0)