Enabled client-side keepalive.

Also adds a windows version of keepalive to the servers.

Resolves #1336
This commit is contained in:
Pentarctagon 2022-09-30 15:41:09 -05:00
parent d74c3cede0
commit c9c831101a
3 changed files with 40 additions and 13 deletions

View file

@ -128,20 +128,20 @@ void server_base::serve(boost::asio::yield_context yield, boost::asio::ip::tcp::
return;
}
#ifndef _WIN32
if(keep_alive_) {
int timeout = 30;
int timeout = 300;
#ifdef __linux__
int cnt = 10;
int interval = 30;
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout));
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
#endif
#if defined(__APPLE__) && defined(__MACH__)
setsockopt(socket->native_handle(), IPPROTO_TCP, TCP_KEEPALIVE, &timeout, sizeof(timeout));
#endif
}
int cnt = 10;
int interval = 30;
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout));
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
setsockopt(socket->native_handle(), SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
#elif defined(__APPLE__) && defined(__MACH__)
setsockopt(socket->native_handle(), IPPROTO_TCP, TCP_KEEPALIVE, &timeout, sizeof(timeout));
#elif defined(_WIN32)
// these are in milliseconds for windows
timeout *= 1000;
setsockopt(socket->native_handle(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
setsockopt(socket->native_handle(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
#endif
#ifdef __linux__

View file

@ -161,9 +161,13 @@ void wesnothd_connection::handle_connect(const boost::system::error_code& ec, en
}
// worker thread
// TODO: have timeout set via advanced preferences
void wesnothd_connection::handshake()
{
MPTEST_LOG;
set_keepalive(10);
static const uint32_t handshake = 0;
static const uint32_t tls_handshake = htonl(uint32_t(1));
@ -547,3 +551,24 @@ bool wesnothd_connection::wait_and_receive_data(config& data)
return receive_data(data);
};
void wesnothd_connection::set_keepalive(int seconds)
{
boost::asio::socket_base::keep_alive option(true);
utils::get<raw_socket>(socket_)->set_option(option);
#ifdef __linux__
int cnt = 10;
int interval = 30;
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), SOL_TCP, TCP_KEEPIDLE, &seconds, sizeof(seconds));
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
#elif defined(__APPLE__) && defined(__MACH__)
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), IPPROTO_TCP, TCP_KEEPALIVE, &seconds, sizeof(seconds));
#elif defined(_WIN32)
// these are in milliseconds for windows
seconds *= 1000;
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&seconds, sizeof(seconds));
setsockopt(utils::get<raw_socket>(socket_)->native_handle(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&seconds, sizeof(seconds));
#endif
}

View file

@ -182,6 +182,8 @@ private:
void send();
void recv();
void set_keepalive(int seconds);
template<typename T>
using data_queue = std::queue<T, std::list<T>>;