net/SocketDescriptor: add method SetKeepAlive()

This commit is contained in:
Max Kellermann 2017-08-10 19:25:02 +02:00
parent 9a5bcc6db0
commit 1f47dc990d
5 changed files with 8 additions and 13 deletions

View File

@ -158,7 +158,7 @@ OneServerSocket::Accept() noexcept
return; return;
} }
if (socket_keepalive(peer_fd.Get())) { if (!peer_fd.SetKeepAlive()) {
const SocketErrorMessage msg; const SocketErrorMessage msg;
FormatError(server_socket_domain, FormatError(server_socket_domain,
"Could not set TCP keepalive option: %s", "Could not set TCP keepalive option: %s",

View File

@ -206,6 +206,12 @@ SocketDescriptor::SetOption(int level, int name,
return setsockopt(fd, level, name, (const char *)value, size) == 0; return setsockopt(fd, level, name, (const char *)value, size) == 0;
} }
bool
SocketDescriptor::SetKeepAlive(bool value)
{
return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value);
}
bool bool
SocketDescriptor::SetReuseAddress(bool value) SocketDescriptor::SetReuseAddress(bool value)
{ {

View File

@ -141,6 +141,7 @@ public:
return SetOption(level, name, &value, sizeof(value)); return SetOption(level, name, &value, sizeof(value));
} }
bool SetKeepAlive(bool value=true);
bool SetReuseAddress(bool value=true); bool SetReuseAddress(bool value=true);
#ifdef __linux__ #ifdef __linux__

View File

@ -49,12 +49,3 @@ socket_bind_listen(int domain, int type, int protocol,
return fd; return fd;
} }
int
socket_keepalive(int fd)
{
const int reuse = 1;
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
(const char *)&reuse, sizeof(reuse));
}

View File

@ -49,7 +49,4 @@ socket_bind_listen(int domain, int type, int protocol,
SocketAddress address, SocketAddress address,
int backlog); int backlog);
int
socket_keepalive(int fd);
#endif #endif