[Spice-devel] [spice-server 3/3] reds-stream: Introduce reds_stream_set_keepalive() helper

Christophe Fergeau cfergeau at redhat.com
Wed Mar 8 16:18:17 UTC 2017


This new function removes one place outside of RedsStream which needs to
access RedsStream::socket
---
 server/reds-stream.c | 26 ++++++++++++++++++++++++++
 server/reds-stream.h |  1 +
 server/reds.c        | 26 +-------------------------
 3 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/server/reds-stream.c b/server/reds-stream.c
index dd0eb8a..9f4450b 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -289,6 +289,32 @@ bool reds_stream_set_non_blocking(RedsStream *stream, bool non_blocking)
     return TRUE;
 }
 
+bool reds_stream_set_keepalive(RedsStream *stream, bool enable, int timeout)
+{
+    int keepalive = !!enable;
+
+    if (setsockopt(stream->socket, 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(stream->socket, 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;
+}
+
+
 int reds_stream_send_msgfd(RedsStream *stream, int fd)
 {
     struct msghdr msgh = { 0, };
diff --git a/server/reds-stream.h b/server/reds-stream.h
index 97cfcd5..ee71c30 100644
--- a/server/reds-stream.h
+++ b/server/reds-stream.h
@@ -75,6 +75,7 @@ int reds_stream_get_family(const RedsStream *stream);
 int reds_stream_is_plain_unix(const RedsStream *stream);
 bool reds_stream_set_no_delay(RedsStream *stream, bool no_delay);
 bool reds_stream_set_non_blocking(RedsStream *stream, bool non_blocking);
+bool reds_stream_set_keepalive(RedsStream *stream, bool enable, int timeout);
 int reds_stream_send_msgfd(RedsStream *stream, int fd);
 
 typedef enum {
diff --git a/server/reds.c b/server/reds.c
index 77976e6..7c8d1d8 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -2378,35 +2378,10 @@ 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 = NULL;
 
-    reds_init_keepalive(socket);
-
     link = spice_new0(RedLinkInfo, 1);
     link->reds = reds;
     link->stream = reds_stream_new(reds, socket);
@@ -2415,6 +2390,7 @@ static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
        goto error;
     }
     reds_stream_set_no_delay(link->stream, TRUE);
+    reds_stream_set_keepalive(link->stream, TRUE, KEEPALIVE_TIMEOUT);
 
     /* gather info + send event */
 
-- 
2.9.3



More information about the Spice-devel mailing list