|
4 | 4 | #include <cstring> |
5 | 5 | #include <net/if.h> |
6 | 6 | #include <netdb.h> |
| 7 | +#include <netinet/tcp.h> |
7 | 8 | #include <sys/socket.h> |
8 | 9 | #include <sys/types.h> |
9 | 10 | #include <unistd.h> |
10 | | -#include <netinet/tcp.h> |
11 | 11 |
|
12 | 12 | #include <loglet/loglet.hpp> |
13 | 13 |
|
@@ -188,28 +188,34 @@ bool TcpClient::connect(std::string const& host, int port, std::string const& in |
188 | 188 | } |
189 | 189 |
|
190 | 190 | // 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 |
195 | 196 |
|
196 | 197 | 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); |
199 | 200 |
|
200 | 201 | // Configure keep-alive parameters |
201 | 202 | 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, |
203 | 204 | sizeof(idle), result); |
204 | 205 |
|
205 | 206 | 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); |
208 | 209 |
|
209 | 210 | 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, |
211 | 212 | sizeof(count), result); |
212 | 213 |
|
| 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 | + |
213 | 219 | mState = State::CONNECTING; |
214 | 220 | return true; |
215 | 221 | } |
|
0 commit comments