[Spice-devel] [spice-server v2 3/6] net: Introduce red_socket_set_keepalive() helper
Christophe Fergeau
cfergeau at redhat.com
Fri Mar 10 08:59:39 UTC 2017
This allows to move some low-level code out of reds.c
Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
---
server/net-utils.c | 25 +++++++++++++++++++++++++
server/net-utils.h | 1 +
server/reds.c | 27 ++-------------------------
3 files changed, 28 insertions(+), 25 deletions(-)
diff --git a/server/net-utils.c b/server/net-utils.c
index ce409be..995b0d4 100644
--- a/server/net-utils.c
+++ b/server/net-utils.c
@@ -31,6 +31,31 @@
#include "net-utils.h"
+bool red_socket_set_keepalive(int fd, bool enable, int timeout)
+{
+ int keepalive = !!enable;
+
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) {
+ if (errno != ENOTSUP) {
+ spice_printerr("setsockopt for keepalive failed, %s", strerror(errno));
+ return false;
+ }
+ }
+
+ if (!enable) {
+ return true;
+ }
+
+ if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout)) == -1) {
+ if (errno != ENOTSUP) {
+ spice_printerr("setsockopt for keepalive timeout failed, %s", strerror(errno));
+ return false;
+ }
+ }
+
+ return true;
+}
+
bool red_socket_set_no_delay(int fd, bool no_delay)
{
int delay_val = no_delay;
diff --git a/server/net-utils.h b/server/net-utils.h
index aed6956..d06932d 100644
--- a/server/net-utils.h
+++ b/server/net-utils.h
@@ -18,6 +18,7 @@
#ifndef _H_RED_NET_UTILS
#define _H_RED_NET_UTILS
+bool red_socket_set_keepalive(int fd, bool enable, int timeout);
bool red_socket_set_no_delay(int fd, bool no_delay);
bool red_socket_set_non_blocking(int fd, bool non_blocking);
diff --git a/server/reds.c b/server/reds.c
index 12c797b..024ff63 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -73,6 +73,7 @@
#include "main-channel-client.h"
#include "red-client.h"
#include "glib-compat.h"
+#include "net-utils.h"
#define REDS_MAX_STAT_NODES 100
@@ -2377,39 +2378,15 @@ static void reds_handle_ssl_accept(int fd, int event, void *data)
#define KEEPALIVE_TIMEOUT (10*60)
-static bool reds_init_keepalive(int socket)
-{
- int keepalive = 1;
- int keepalive_timeout = KEEPALIVE_TIMEOUT;
-
- if (setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)) == -1) {
- if (errno != ENOTSUP) {
- spice_printerr("setsockopt for keepalive failed, %s", strerror(errno));
- return false;
- }
- }
-
- if (setsockopt(socket, SOL_TCP, TCP_KEEPIDLE,
- &keepalive_timeout, sizeof(keepalive_timeout)) == -1) {
- if (errno != ENOTSUP) {
- spice_printerr("setsockopt for keepalive timeout failed, %s", strerror(errno));
- return false;
- }
- }
-
- return true;
-}
-
static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
{
RedLinkInfo *link;
- reds_init_keepalive(socket);
-
if (!red_socket_set_non_blocking(socket, TRUE))
{
goto error;
}
+ red_socket_set_keepalive(socket, TRUE, KEEPALIVE_TIMEOUT);
link = spice_new0(RedLinkInfo, 1);
link->reds = reds;
--
2.9.3
More information about the Spice-devel
mailing list