[Spice-devel] [PATCH spice-server v2] reds-stream: Introduce reds_stream_set_no_delay() helper

Frediano Ziglio fziglio at redhat.com
Mon Feb 27 12:35:15 UTC 2017


From: Christophe Fergeau <cfergeau at redhat.com>

The code to enable/disable on a TCP socket is duplicated in multiple
places in the code base, this commit replaces this duplicated code with
a helper in RedsStream.
---
 server/common-graphics-channel.c | 15 +++------------
 server/red-channel-client.c      | 17 ++---------------
 server/reds-stream.c             | 16 ++++++++++++++++
 server/reds-stream.h             |  1 +
 server/reds.c                    |  8 +-------
 server/sound.c                   |  8 +-------
 6 files changed, 24 insertions(+), 41 deletions(-)

Changes since v1:
- boolean style updates

diff --git a/server/common-graphics-channel.c b/server/common-graphics-channel.c
index bad6e8c..d04c509 100644
--- a/server/common-graphics-channel.c
+++ b/server/common-graphics-channel.c
@@ -19,9 +19,6 @@
 #endif
 
 #include <fcntl.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
 
 #include "common-graphics-channel.h"
 #include "dcc.h"
@@ -117,24 +114,18 @@ int common_channel_config_socket(RedChannelClient *rcc)
     RedClient *client = red_channel_client_get_client(rcc);
     MainChannelClient *mcc = red_client_get_main(client);
     RedsStream *stream = red_channel_client_get_stream(rcc);
-    int delay_val;
-    gboolean is_low_bandwidth;
+    bool is_low_bandwidth;
 
     // TODO - this should be dynamic, not one time at channel creation
     is_low_bandwidth = main_channel_client_is_low_bandwidth(mcc);
-    delay_val = is_low_bandwidth ? 0 : 1;
     /* FIXME: Using Nagle's Algorithm can lead to apparent delays, depending
      * on the delayed ack timeout on the other side.
      * Instead of using Nagle's, we need to implement message buffering on
      * the application level.
      * see: http://www.stuartcheshire.org/papers/NagleDelayedAck/
      */
-    if (setsockopt(stream->socket, IPPROTO_TCP, TCP_NODELAY, &delay_val,
-                   sizeof(delay_val)) == -1) {
-        if (errno != ENOTSUP) {
-            spice_warning("setsockopt failed, %s", strerror(errno));
-        }
-    }
+    reds_stream_set_no_delay(stream, is_low_bandwidth);
+
     // TODO: move wide/narrow ack setting to red_channel.
     red_channel_client_ack_set_client_window(rcc,
         is_low_bandwidth ?
diff --git a/server/red-channel-client.c b/server/red-channel-client.c
index b583da2..ef52701 100644
--- a/server/red-channel-client.c
+++ b/server/red-channel-client.c
@@ -600,13 +600,7 @@ static void red_channel_client_send_ping(RedChannelClient *rcc)
         }  else {
             rcc->priv->latency_monitor.tcp_nodelay = delay_val;
             if (!delay_val) {
-                delay_val = 1;
-                if (setsockopt(rcc->priv->stream->socket, IPPROTO_TCP, TCP_NODELAY, &delay_val,
-                               sizeof(delay_val)) == -1) {
-                   if (errno != ENOTSUP) {
-                        spice_warning("setsockopt failed, %s", strerror(errno));
-                    }
-                }
+                reds_stream_set_no_delay(rcc->priv->stream, true);
             }
         }
     }
@@ -1430,14 +1424,7 @@ static void red_channel_client_handle_pong(RedChannelClient *rcc, SpiceMsgPing *
 
     /* set TCP_NODELAY=0, in case we reverted it for the test*/
     if (!rcc->priv->latency_monitor.tcp_nodelay) {
-        int delay_val = 0;
-
-        if (setsockopt(rcc->priv->stream->socket, IPPROTO_TCP, TCP_NODELAY, &delay_val,
-                       sizeof(delay_val)) == -1) {
-            if (errno != ENOTSUP) {
-                spice_warning("setsockopt failed, %s", strerror(errno));
-            }
-        }
+        reds_stream_set_no_delay(rcc->priv->stream, false);
     }
 
     /*
diff --git a/server/reds-stream.c b/server/reds-stream.c
index d0dadb9..2c6aaba 100644
--- a/server/reds-stream.c
+++ b/server/reds-stream.c
@@ -21,6 +21,7 @@
 
 #include <errno.h>
 #include <netdb.h>
+#include <netinet/tcp.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <fcntl.h>
@@ -256,6 +257,21 @@ int reds_stream_is_plain_unix(const RedsStream *s)
 
 }
 
+bool reds_stream_set_no_delay(RedsStream *stream, bool no_delay)
+{
+    int delay_val = no_delay;
+
+    if (setsockopt(stream->socket, IPPROTO_TCP, TCP_NODELAY,
+                   &delay_val, sizeof(delay_val)) != 0) {
+        if (errno != ENOTSUP && errno != ENOPROTOOPT) {
+            spice_printerr("setsockopt 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 a8d1736..c76014b 100644
--- a/server/reds-stream.h
+++ b/server/reds-stream.h
@@ -74,6 +74,7 @@ int reds_stream_enable_ssl(RedsStream *stream, SSL_CTX *ctx);
 void reds_stream_set_info_flag(RedsStream *stream, unsigned int flag);
 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);
 int reds_stream_send_msgfd(RedsStream *stream, int fd);
 
 typedef enum {
diff --git a/server/reds.c b/server/reds.c
index 39a7a31..ad9fcc4 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -2387,7 +2387,6 @@ static bool reds_init_keepalive(int socket)
 static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
 {
     RedLinkInfo *link;
-    int delay_val = 1;
     int flags;
 
     if ((flags = fcntl(socket, F_GETFL)) == -1) {
@@ -2400,17 +2399,12 @@ static RedLinkInfo *reds_init_client_connection(RedsState *reds, int socket)
         goto error;
     }
 
-    if (setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &delay_val, sizeof(delay_val)) == -1) {
-        if (errno != ENOTSUP) {
-            spice_warning("setsockopt failed, %s", strerror(errno));
-        }
-    }
-
     reds_init_keepalive(socket);
 
     link = spice_new0(RedLinkInfo, 1);
     link->reds = reds;
     link->stream = reds_stream_new(reds, socket);
+    reds_stream_set_no_delay(link->stream, true);
 
     /* gather info + send event */
 
diff --git a/server/sound.c b/server/sound.c
index e83a547..0c92480 100644
--- a/server/sound.c
+++ b/server/sound.c
@@ -739,7 +739,6 @@ static void record_channel_send_item(RedChannelClient *rcc, G_GNUC_UNUSED RedPip
 
 static int snd_channel_config_socket(RedChannelClient *rcc)
 {
-    int delay_val;
 #ifdef SO_PRIORITY
     int priority;
 #endif
@@ -765,12 +764,7 @@ static int snd_channel_config_socket(RedChannelClient *rcc)
         }
     }
 
-    delay_val = main_channel_client_is_low_bandwidth(mcc) ? 0 : 1;
-    if (setsockopt(stream->socket, IPPROTO_TCP, TCP_NODELAY, &delay_val, sizeof(delay_val)) == -1) {
-        if (errno != ENOTSUP) {
-            spice_printerr("setsockopt failed, %s", strerror(errno));
-        }
-    }
+    reds_stream_set_no_delay(stream, main_channel_client_is_low_bandwidth(mcc));
 
     return TRUE;
 }
-- 
2.9.3



More information about the Spice-devel mailing list