From 1f47dc990d8ffc948f3f931aff496dfcc9b0c569 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 10 Aug 2017 19:25:02 +0200 Subject: [PATCH] net/SocketDescriptor: add method SetKeepAlive() --- src/event/ServerSocket.cxx | 2 +- src/net/SocketDescriptor.cxx | 6 ++++++ src/net/SocketDescriptor.hxx | 1 + src/net/SocketUtil.cxx | 9 --------- src/net/SocketUtil.hxx | 3 --- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index 85cf93dbe..55000318f 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -158,7 +158,7 @@ OneServerSocket::Accept() noexcept return; } - if (socket_keepalive(peer_fd.Get())) { + if (!peer_fd.SetKeepAlive()) { const SocketErrorMessage msg; FormatError(server_socket_domain, "Could not set TCP keepalive option: %s", diff --git a/src/net/SocketDescriptor.cxx b/src/net/SocketDescriptor.cxx index 9a3b97c9d..6c31616b2 100644 --- a/src/net/SocketDescriptor.cxx +++ b/src/net/SocketDescriptor.cxx @@ -206,6 +206,12 @@ SocketDescriptor::SetOption(int level, int name, return setsockopt(fd, level, name, (const char *)value, size) == 0; } +bool +SocketDescriptor::SetKeepAlive(bool value) +{ + return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value); +} + bool SocketDescriptor::SetReuseAddress(bool value) { diff --git a/src/net/SocketDescriptor.hxx b/src/net/SocketDescriptor.hxx index 6b2c6bdfa..bc2d86d16 100644 --- a/src/net/SocketDescriptor.hxx +++ b/src/net/SocketDescriptor.hxx @@ -141,6 +141,7 @@ public: return SetOption(level, name, &value, sizeof(value)); } + bool SetKeepAlive(bool value=true); bool SetReuseAddress(bool value=true); #ifdef __linux__ diff --git a/src/net/SocketUtil.cxx b/src/net/SocketUtil.cxx index ab029cdea..aaf7e1351 100644 --- a/src/net/SocketUtil.cxx +++ b/src/net/SocketUtil.cxx @@ -49,12 +49,3 @@ socket_bind_listen(int domain, int type, int protocol, return fd; } - -int -socket_keepalive(int fd) -{ - const int reuse = 1; - - return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, - (const char *)&reuse, sizeof(reuse)); -} diff --git a/src/net/SocketUtil.hxx b/src/net/SocketUtil.hxx index 55d3bb621..a8ae998a4 100644 --- a/src/net/SocketUtil.hxx +++ b/src/net/SocketUtil.hxx @@ -49,7 +49,4 @@ socket_bind_listen(int domain, int type, int protocol, SocketAddress address, int backlog); -int -socket_keepalive(int fd); - #endif