[Spice-devel] [spice-server 1/5] utils: Introduce helpers to map channel types to names

Christophe Fergeau cfergeau at redhat.com
Thu Oct 12 10:13:22 UTC 2017


spice_server_set_channel_security() is already mostly doing that. We can
make its code more generic, and introduce a red_channel_get_name()
method. This method will then be used to make debug messages more
readable by showing the actual channel name rather than its type as
an int.
---
 server/red-channel.c |  5 +++++
 server/red-channel.h |  2 ++
 server/reds.c        | 28 +++++++---------------------
 server/utils.c       | 35 +++++++++++++++++++++++++++++++++++
 server/utils.h       |  3 +++
 5 files changed, 52 insertions(+), 21 deletions(-)

diff --git a/server/red-channel.c b/server/red-channel.c
index 9736c54b1..4e5aba474 100644
--- a/server/red-channel.c
+++ b/server/red-channel.c
@@ -460,6 +460,11 @@ int red_channel_is_connected(RedChannel *channel)
     return channel && channel->priv->clients;
 }
 
+const char *red_channel_get_name(RedChannel *channel)
+{
+    return red_channel_type_to_str(channel->priv->type);
+}
+
 void red_channel_remove_client(RedChannel *channel, RedChannelClient *rcc)
 {
     GList *link;
diff --git a/server/red-channel.h b/server/red-channel.h
index e0fe94fec..861887ed4 100644
--- a/server/red-channel.h
+++ b/server/red-channel.h
@@ -128,6 +128,8 @@ struct RedChannelClass
 
 GType red_channel_get_type(void) G_GNUC_CONST;
 
+const char *red_channel_get_name(RedChannel *channel);
+
 void red_channel_add_client(RedChannel *channel, RedChannelClient *rcc);
 void red_channel_remove_client(RedChannel *channel, RedChannelClient *rcc);
 
diff --git a/server/reds.c b/server/reds.c
index 6c8e06959..39890ff18 100644
--- a/server/reds.c
+++ b/server/reds.c
@@ -3975,32 +3975,18 @@ SPICE_GNUC_VISIBLE int spice_server_set_zlib_glz_compression(SpiceServer *s, spi
 
 SPICE_GNUC_VISIBLE int spice_server_set_channel_security(SpiceServer *s, const char *channel, int security)
 {
-    static const char *const names[] = {
-        [ SPICE_CHANNEL_MAIN     ] = "main",
-        [ SPICE_CHANNEL_DISPLAY  ] = "display",
-        [ SPICE_CHANNEL_INPUTS   ] = "inputs",
-        [ SPICE_CHANNEL_CURSOR   ] = "cursor",
-        [ SPICE_CHANNEL_PLAYBACK ] = "playback",
-        [ SPICE_CHANNEL_RECORD   ] = "record",
-#ifdef USE_SMARTCARD
-        [ SPICE_CHANNEL_SMARTCARD] = "smartcard",
-#endif
-        [ SPICE_CHANNEL_USBREDIR ] = "usbredir",
-        [ SPICE_CHANNEL_WEBDAV ] = "webdav",
-    };
-    int i;
-
+    int type;
     if (channel == NULL) {
         s->config->default_channel_security = security;
         return 0;
     }
-    for (i = 0; i < SPICE_N_ELEMENTS(names); i++) {
-        if (names[i] && strcmp(names[i], channel) == 0) {
-            reds_set_one_channel_security(s, i, security);
-            return 0;
-        }
+    type = red_channel_name_to_type(channel);
+    if (type == -1) {
+        return -1;
     }
-    return -1;
+
+    reds_set_one_channel_security(s, type, security);
+    return 0;
 }
 
 /* very obsolete and old function, retain only for ABI */
diff --git a/server/utils.c b/server/utils.c
index 66df86ff4..d5d3c175a 100644
--- a/server/utils.c
+++ b/server/utils.c
@@ -19,6 +19,7 @@
 #endif
 
 #include <glib.h>
+#include <spice/enums.h>
 #include "utils.h"
 
 int rgb32_data_has_alpha(int width, int height, size_t stride,
@@ -48,3 +49,37 @@ int rgb32_data_has_alpha(int width, int height, size_t stride,
     *all_set_out = has_alpha;
     return has_alpha;
 }
+
+static const char *const channel_names[] = {
+    [ SPICE_CHANNEL_MAIN     ] = "main",
+    [ SPICE_CHANNEL_DISPLAY  ] = "display",
+    [ SPICE_CHANNEL_INPUTS   ] = "inputs",
+    [ SPICE_CHANNEL_CURSOR   ] = "cursor",
+    [ SPICE_CHANNEL_PLAYBACK ] = "playback",
+    [ SPICE_CHANNEL_RECORD   ] = "record",
+#ifdef USE_SMARTCARD
+    [ SPICE_CHANNEL_SMARTCARD] = "smartcard",
+#endif
+    [ SPICE_CHANNEL_USBREDIR ] = "usbredir",
+    [ SPICE_CHANNEL_WEBDAV ] = "webdav",
+};
+
+const char *red_channel_type_to_str(int type)
+{
+    g_return_val_if_fail(type >= 0, NULL);
+    g_return_val_if_fail(type < G_N_ELEMENTS(channel_names), NULL);
+
+    return channel_names[type];
+}
+
+int red_channel_name_to_type(const char *name)
+{
+    int i;
+
+    for (i = 0; i < G_N_ELEMENTS(channel_names); i++) {
+        if (g_strcmp0(channel_names[i], name) == 0) {
+            return i;
+        }
+    }
+    return -1;
+}
diff --git a/server/utils.h b/server/utils.h
index ec2db2c90..3f735754d 100644
--- a/server/utils.h
+++ b/server/utils.h
@@ -74,4 +74,7 @@ static inline red_time_t spice_get_monotonic_time_ms(void)
 int rgb32_data_has_alpha(int width, int height, size_t stride,
                          uint8_t *data, int *all_set_out);
 
+const char *red_channel_type_to_str(int type);
+int red_channel_name_to_type(const char *name);
+
 #endif /* UTILS_H_ */
-- 
2.13.6



More information about the Spice-devel mailing list