[Spice-commits] 2 commits - configure.ac server/cursor-channel-client.c server/cursor-channel-client.h server/dcc.c server/dcc.h server/dcc-private.h server/display-channel.c server/dummy-channel-client.c server/dummy-channel-client.h server/inputs-channel-client.c server/inputs-channel-client.h server/main-channel-client.c server/main-channel-client.h server/Makefile.am server/red-channel-client.c server/red-channel-client.h server/red-channel-client-private.h server/red-channel.h server/smartcard.c server/smartcard-channel-client.c server/smartcard-channel-client.h server/smartcard.h server/sound.c server/spice-server.h server/spicevmc.c

Jonathon Jongsma jjongsma at kemper.freedesktop.org
Fri Oct 7 19:47:33 UTC 2016


 configure.ac                        |    4 
 server/Makefile.am                  |    4 
 server/cursor-channel-client.c      |   67 ++--
 server/cursor-channel-client.h      |   39 ++
 server/dcc-private.h                |    8 
 server/dcc.c                        |  195 ++++++++++--
 server/dcc.h                        |   42 ++
 server/display-channel.c            |    4 
 server/dummy-channel-client.c       |  158 ++++++++++
 server/dummy-channel-client.h       |   64 ++++
 server/inputs-channel-client.c      |   58 ++-
 server/inputs-channel-client.h      |   51 ++-
 server/main-channel-client.c        |  139 +++++++-
 server/main-channel-client.h        |   39 ++
 server/red-channel-client-private.h |    4 
 server/red-channel-client.c         |  561 +++++++++++++++++++++++-------------
 server/red-channel-client.h         |   85 ++++-
 server/red-channel.h                |   31 -
 server/smartcard-channel-client.c   |  418 ++++++++++++++++++++++++++
 server/smartcard-channel-client.h   |  113 +++++++
 server/smartcard.c                  |  374 +++---------------------
 server/smartcard.h                  |   21 +
 server/sound.c                      |    9 
 server/spice-server.h               |   16 +
 server/spicevmc.c                   |    6 
 25 files changed, 1831 insertions(+), 679 deletions(-)

New commits:
commit 5193360594d461a015d43e7b7ca6ed51e8481035
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Thu Sep 22 16:40:55 2016 +0100

    Convert RedChannelClient hierarchy to GObject
    
    Convert the RedChannelClient heirarchy into GObjects. Since the existing
    constructors could fail and return NULL, I inherited the base channel
    client from GInitable, which introduces a dependency on gio.
    
    When using private structs with GObject, there's a maximum size of (I
    think) 64k, which was exceeded by some of the private structs. To avoid
    this limitation I changed some members to dynamically allocated.

diff --git a/configure.ac b/configure.ac
index f8284f6..483f18b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -147,8 +147,8 @@ SPICE_PROTOCOL_MIN_VER=0.12.12
 PKG_CHECK_MODULES([SPICE_PROTOCOL], [spice-protocol >= $SPICE_PROTOCOL_MIN_VER])
 AC_SUBST([SPICE_PROTOCOL_MIN_VER])
 
-PKG_CHECK_MODULES([GLIB2], [glib-2.0 >= 2.22])
-AS_VAR_APPEND([SPICE_REQUIRES], [" glib-2.0 >= 2.22"])
+PKG_CHECK_MODULES([GLIB2], [glib-2.0 >= 2.22 gio-2.0 >= 2.22])
+AS_VAR_APPEND([SPICE_REQUIRES], [" glib-2.0 >= 2.22 gio-2.0 >= 2.22"])
 
 PKG_CHECK_MODULES([GOBJECT2], [gobject-2.0 >= 2.22])
 AS_VAR_APPEND([SPICE_REQUIRES], [" gobject-2.0 >= 2.22"])
diff --git a/server/Makefile.am b/server/Makefile.am
index abbec16..f217399 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -100,6 +100,8 @@ libserver_la_SOURCES =				\
 	red-channel-client.c			\
 	red-channel-client.h			\
 	red-channel-client-private.h		\
+	dummy-channel-client.c			\
+	dummy-channel-client.h			\
 	red-common.h				\
 	dispatcher.c				\
 	dispatcher.h				\
diff --git a/server/cursor-channel-client.c b/server/cursor-channel-client.c
index 89c23a3..25082d7 100644
--- a/server/cursor-channel-client.c
+++ b/server/cursor-channel-client.c
@@ -40,7 +40,11 @@ enum {
     RED_PIPE_ITEM_TYPE_INVAL_CURSOR_CACHE,
 };
 
-typedef struct CursorChannelClientPrivate CursorChannelClientPrivate;
+G_DEFINE_TYPE(CursorChannelClient, cursor_channel_client, RED_TYPE_CHANNEL_CLIENT)
+
+#define CURSOR_CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_CURSOR_CHANNEL_CLIENT, CursorChannelClientPrivate))
+
 struct CursorChannelClientPrivate
 {
     RedCacheItem *cursor_cache[CURSOR_CACHE_HASH_SIZE];
@@ -49,12 +53,19 @@ struct CursorChannelClientPrivate
     uint32_t cursor_cache_items;
 };
 
-struct CursorChannelClient
+static void
+cursor_channel_client_class_init(CursorChannelClientClass *klass)
 {
-    RedChannelClient base;
+    g_type_class_add_private(klass, sizeof(CursorChannelClientPrivate));
+}
 
-    CursorChannelClientPrivate priv[1];
-};
+static void
+cursor_channel_client_init(CursorChannelClient *self)
+{
+    self->priv = CURSOR_CHANNEL_CLIENT_PRIVATE(self);
+    ring_init(&self->priv->cursor_cache_lru);
+    self->priv->cursor_cache_available = CLIENT_CURSOR_CACHE_SIZE;
+}
 
 #define CLIENT_CURSOR_CACHE
 #include "cache-item.tmpl.c"
@@ -90,28 +101,36 @@ CursorChannelClient* cursor_channel_client_new(CursorChannel *cursor, RedClient
                                                uint32_t *common_caps, int num_common_caps,
                                                uint32_t *caps, int num_caps)
 {
-    spice_return_val_if_fail(cursor, NULL);
-    spice_return_val_if_fail(client, NULL);
-    spice_return_val_if_fail(stream, NULL);
-    spice_return_val_if_fail(!num_common_caps || common_caps, NULL);
-    spice_return_val_if_fail(!num_caps || caps, NULL);
-
-    CursorChannelClient *ccc =
-        CURSOR_CHANNEL_CLIENT(red_channel_client_create(sizeof(CursorChannelClient),
-                                                        RED_CHANNEL(cursor),
-                                                        client, stream,
-                                                        FALSE,
-                                                        num_common_caps,
-                                                        common_caps,
-                                                        num_caps,
-                                                        caps));
-    spice_return_val_if_fail(ccc != NULL, NULL);
+    CursorChannelClient *rcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
+
+    rcc = g_initable_new(TYPE_CURSOR_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", cursor,
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", FALSE,
+                         "common-caps", common_caps_array,
+                         "caps", caps_array,
+                         NULL);
     COMMON_GRAPHICS_CHANNEL(cursor)->during_target_migrate = mig_target;
 
-    ring_init(&ccc->priv->cursor_cache_lru);
-    ccc->priv->cursor_cache_available = CLIENT_CURSOR_CACHE_SIZE;
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
 
-    return ccc;
+    return rcc;
 }
 
 RedCacheItem* cursor_channel_client_cache_find(CursorChannelClient *ccc, uint64_t id)
diff --git a/server/cursor-channel-client.h b/server/cursor-channel-client.h
index 278005c..2336b95 100644
--- a/server/cursor-channel-client.h
+++ b/server/cursor-channel-client.h
@@ -18,15 +18,46 @@
 #ifndef CURSOR_CHANNEL_CLIENT_H_
 # define CURSOR_CHANNEL_CLIENT_H_
 
+#include <glib-object.h>
+
 #include "cache-item.h"
 #include "red-common.h"
-#include "red-channel.h"
+#include "red-channel-client.h"
 #include "reds-stream.h"
 
-typedef struct CursorChannel CursorChannel;
+G_BEGIN_DECLS
+
+#define TYPE_CURSOR_CHANNEL_CLIENT cursor_channel_client_get_type()
+
+#define CURSOR_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_CURSOR_CHANNEL_CLIENT, CursorChannelClient))
+#define CURSOR_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_CURSOR_CHANNEL_CLIENT, CursorChannelClientClass))
+#define IS_CURSOR_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_CURSOR_CHANNEL_CLIENT))
+#define IS_CURSOR_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_CURSOR_CHANNEL_CLIENT))
+#define CURSOR_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_CURSOR_CHANNEL_CLIENT, CursorChannelClientClass))
+
 typedef struct CursorChannelClient CursorChannelClient;
+typedef struct CursorChannelClientClass CursorChannelClientClass;
+typedef struct CursorChannelClientPrivate CursorChannelClientPrivate;
+typedef struct CursorChannel CursorChannel;
+
+struct CursorChannelClient
+{
+    RedChannelClient parent;
+
+    CursorChannelClientPrivate *priv;
+};
 
-#define CURSOR_CHANNEL_CLIENT(Client) ((CursorChannelClient*)(Client))
+struct CursorChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
+
+GType cursor_channel_client_get_type(void) G_GNUC_CONST;
 
 CursorChannelClient* cursor_channel_client_new(CursorChannel *cursor,
                                                RedClient *client,
@@ -41,4 +72,6 @@ void cursor_channel_client_on_disconnect(RedChannelClient *rcc);
 RedCacheItem* cursor_channel_client_cache_find(CursorChannelClient *ccc, uint64_t id);
 int cursor_channel_client_cache_add(CursorChannelClient *ccc, uint64_t id, size_t size);
 
+G_END_DECLS
+
 #endif /* CURSOR_CHANNEL_CLIENT_H_ */
diff --git a/server/dcc-private.h b/server/dcc-private.h
index 46f1db3..de6ea92 100644
--- a/server/dcc-private.h
+++ b/server/dcc-private.h
@@ -61,12 +61,4 @@ struct DisplayChannelClientPrivate
     bool gl_draw_ongoing;
 };
 
-struct DisplayChannelClient
-{
-    RedChannelClient base;
-    int is_low_bandwidth;
-
-    DisplayChannelClientPrivate priv[1];
-};
-
 #endif /* DCC_PRIVATE_H_ */
diff --git a/server/dcc.c b/server/dcc.c
index ce8677d..3ded1b9 100644
--- a/server/dcc.c
+++ b/server/dcc.c
@@ -22,8 +22,150 @@
 #include "dcc-private.h"
 #include "display-channel.h"
 #include "red-channel-client-private.h"
+#include "spice-server-enums.h"
+
+G_DEFINE_TYPE(DisplayChannelClient, display_channel_client, RED_TYPE_CHANNEL_CLIENT)
 
 #define DISPLAY_CLIENT_SHORT_TIMEOUT 15000000000ULL //nano
+#define DISPLAY_FREE_LIST_DEFAULT_SIZE 128
+
+enum
+{
+    PROP0,
+    PROP_IMAGE_COMPRESSION,
+    PROP_JPEG_STATE,
+    PROP_ZLIB_GLZ_STATE
+};
+
+static void
+display_channel_client_get_property(GObject *object,
+                                    guint property_id,
+                                    GValue *value,
+                                    GParamSpec *pspec)
+{
+    DisplayChannelClient *self = DISPLAY_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_IMAGE_COMPRESSION:
+             g_value_set_enum(value, self->priv->image_compression);
+            break;
+        case PROP_JPEG_STATE:
+             g_value_set_enum(value, self->priv->jpeg_state);
+            break;
+        case PROP_ZLIB_GLZ_STATE:
+             g_value_set_enum(value, self->priv->zlib_glz_state);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void
+display_channel_client_set_property(GObject *object,
+                                    guint property_id,
+                                    const GValue *value,
+                                    GParamSpec *pspec)
+{
+    DisplayChannelClient *self = DISPLAY_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_IMAGE_COMPRESSION:
+            self->priv->image_compression = g_value_get_enum(value);
+            break;
+        case PROP_JPEG_STATE:
+            self->priv->jpeg_state = g_value_get_enum(value);
+            break;
+        case PROP_ZLIB_GLZ_STATE:
+            self->priv->zlib_glz_state = g_value_get_enum(value);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void dcc_init_stream_agents(DisplayChannelClient *dcc);
+
+static void
+display_channel_client_constructed(GObject *object)
+{
+    DisplayChannelClient *self = DISPLAY_CHANNEL_CLIENT(object);
+
+    G_OBJECT_CLASS(display_channel_client_parent_class)->constructed(object);
+
+    dcc_init_stream_agents(self);
+
+    image_encoders_init(&self->priv->encoders, &DCC_TO_DC(self)->priv->encoder_shared_data);
+}
+
+static void
+display_channel_client_finalize(GObject *object)
+{
+    DisplayChannelClient *self = DISPLAY_CHANNEL_CLIENT(object);
+    g_free(self->priv);
+
+    G_OBJECT_CLASS(display_channel_client_parent_class)->finalize(object);
+}
+
+static void
+display_channel_client_class_init(DisplayChannelClientClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    object_class->get_property = display_channel_client_get_property;
+    object_class->set_property = display_channel_client_set_property;
+    object_class->constructed = display_channel_client_constructed;
+    object_class->finalize = display_channel_client_finalize;
+
+    g_object_class_install_property(object_class,
+                                    PROP_IMAGE_COMPRESSION,
+                                    g_param_spec_enum("image-compression",
+                                                      "image compression",
+                                                      "Image compression type",
+                                                      SPICE_TYPE_SPICE_IMAGE_COMPRESSION_T,
+                                                      SPICE_IMAGE_COMPRESSION_INVALID,
+                                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
+
+    g_object_class_install_property(object_class,
+                                    PROP_JPEG_STATE,
+                                    g_param_spec_enum("jpeg-state",
+                                                      "jpeg state",
+                                                      "JPEG compression state",
+                                                      SPICE_TYPE_SPICE_WAN_COMPRESSION_T,
+                                                      SPICE_WAN_COMPRESSION_INVALID,
+                                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
+
+    g_object_class_install_property(object_class,
+                                    PROP_ZLIB_GLZ_STATE,
+                                    g_param_spec_enum("zlib-glz-state",
+                                                      "zlib glz state",
+                                                      "zlib glz state",
+                                                      SPICE_TYPE_SPICE_WAN_COMPRESSION_T,
+                                                      SPICE_WAN_COMPRESSION_INVALID,
+                                                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
+}
+
+static void display_channel_client_init(DisplayChannelClient *self)
+{
+    /* we need to allocate the private data manually here since
+     * g_type_class_add_private() doesn't support private structs larger than
+     * 64k */
+    self->priv = g_new0(DisplayChannelClientPrivate, 1);
+
+    ring_init(&self->priv->palette_cache_lru);
+    self->priv->palette_cache_available = CLIENT_PALETTE_CACHE_SIZE;
+    // todo: tune quality according to bandwidth
+    self->priv->encoders.jpeg_quality = 85;
+
+    self->priv->send_data.free_list.res =
+        spice_malloc(sizeof(SpiceResourceList) +
+                     DISPLAY_FREE_LIST_DEFAULT_SIZE * sizeof(SpiceResourceID));
+    self->priv->send_data.free_list.res_size = DISPLAY_FREE_LIST_DEFAULT_SIZE;
+}
 
 static RedSurfaceCreateItem *red_surface_create_item_new(RedChannel* channel,
                                                          uint32_t surface_id,
@@ -336,8 +478,6 @@ static void dcc_init_stream_agents(DisplayChannelClient *dcc)
         red_channel_client_test_remote_cap(RED_CHANNEL_CLIENT(dcc), SPICE_DISPLAY_CAP_STREAM_REPORT);
 }
 
-#define DISPLAY_FREE_LIST_DEFAULT_SIZE 128
-
 DisplayChannelClient *dcc_new(DisplayChannel *display,
                               RedClient *client, RedsStream *stream,
                               int mig_target,
@@ -349,35 +489,38 @@ DisplayChannelClient *dcc_new(DisplayChannel *display,
 
 {
     DisplayChannelClient *dcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
 
-    dcc = DISPLAY_CHANNEL_CLIENT(red_channel_client_create(
-        sizeof(DisplayChannelClient),
-        &COMMON_GRAPHICS_CHANNEL(display)->base,
-        client, stream, TRUE,
-        num_common_caps, common_caps,
-        num_caps, caps));
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
 
+    dcc = g_initable_new(TYPE_DISPLAY_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", display,
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", TRUE,
+                         "common-caps", common_caps_array,
+                         "caps", caps_array,
+                         "image-compression", image_compression,
+                         "jpeg-state", jpeg_state,
+                         "zlib-glz-state", zlib_glz_state,
+                         NULL);
+    spice_info("New display (client %p) dcc %p stream %p", client, dcc, stream);
     display->common.during_target_migrate = mig_target;
     dcc->priv->id = display->common.qxl->id;
-    spice_return_val_if_fail(dcc, NULL);
-    spice_info("New display (client %p) dcc %p stream %p", client, dcc, stream);
-
-    ring_init(&dcc->priv->palette_cache_lru);
-    dcc->priv->palette_cache_available = CLIENT_PALETTE_CACHE_SIZE;
-    dcc->priv->image_compression = image_compression;
-    dcc->priv->jpeg_state = jpeg_state;
-    dcc->priv->zlib_glz_state = zlib_glz_state;
-    // TODO: tune quality according to bandwidth
-    dcc->priv->encoders.jpeg_quality = 85;
-
-    dcc->priv->send_data.free_list.res =
-        spice_malloc(sizeof(SpiceResourceList) +
-                     DISPLAY_FREE_LIST_DEFAULT_SIZE * sizeof(SpiceResourceID));
-    dcc->priv->send_data.free_list.res_size = DISPLAY_FREE_LIST_DEFAULT_SIZE;
-
-    dcc_init_stream_agents(dcc);
 
-    image_encoders_init(&dcc->priv->encoders, &display->priv->encoder_shared_data);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
+    if (caps_array)
+        g_array_unref(caps_array);
 
     return dcc;
 }
diff --git a/server/dcc.h b/server/dcc.h
index 932e051..2456f09 100644
--- a/server/dcc.h
+++ b/server/dcc.h
@@ -18,12 +18,49 @@
 #ifndef DCC_H_
 # define DCC_H_
 
+#include <glib-object.h>
+
 #include "image-encoders.h"
 #include "image-cache.h"
 #include "pixmap-cache.h"
 #include "red-worker.h"
 #include "display-limits.h"
 
+G_BEGIN_DECLS
+
+#define TYPE_DISPLAY_CHANNEL_CLIENT display_channel_client_get_type()
+
+#define DISPLAY_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_DISPLAY_CHANNEL_CLIENT, DisplayChannelClient))
+#define DISPLAY_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_DISPLAY_CHANNEL_CLIENT, DisplayChannelClientClass))
+#define IS_DISPLAY_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_DISPLAY_CHANNEL_CLIENT))
+#define IS_DISPLAY_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_DISPLAY_CHANNEL_CLIENT))
+#define DISPLAY_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_DISPLAY_CHANNEL_CLIENT, DisplayChannelClientClass))
+
+typedef struct DisplayChannelClient DisplayChannelClient;
+typedef struct DisplayChannelClientClass DisplayChannelClientClass;
+typedef struct DisplayChannelClientPrivate DisplayChannelClientPrivate;
+
+struct DisplayChannelClient
+{
+    RedChannelClient parent;
+
+    int is_low_bandwidth;
+
+    DisplayChannelClientPrivate *priv;
+};
+
+struct DisplayChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
+
+GType display_channel_client_get_type(void) G_GNUC_CONST;
+
 #define PALETTE_CACHE_HASH_SHIFT 8
 #define PALETTE_CACHE_HASH_SIZE (1 << PALETTE_CACHE_HASH_SHIFT)
 #define PALETTE_CACHE_HASH_MASK (PALETTE_CACHE_HASH_SIZE - 1)
@@ -57,10 +94,7 @@ typedef struct FreeList {
     WaitForChannels wait;
 } FreeList;
 
-typedef struct DisplayChannelClient DisplayChannelClient;
-
 #define DCC_TO_DC(dcc) ((DisplayChannel*)red_channel_client_get_channel((RedChannelClient*)dcc))
-#define DISPLAY_CHANNEL_CLIENT(rcc) ((DisplayChannelClient*)rcc)
 
 typedef struct RedSurfaceCreateItem {
     RedPipeItem pipe_item;
@@ -172,4 +206,6 @@ void dcc_set_max_stream_bit_rate(DisplayChannelClient *dcc, uint64_t rate);
 int dcc_config_socket(RedChannelClient *rcc);
 gboolean dcc_is_low_bandwidth(DisplayChannelClient *dcc);
 
+G_END_DECLS
+
 #endif /* DCC_H_ */
diff --git a/server/display-channel.c b/server/display-channel.c
index 32f8e67..b9366b5 100644
--- a/server/display-channel.c
+++ b/server/display-channel.c
@@ -1128,7 +1128,7 @@ int display_channel_wait_for_migrate_data(DisplayChannel *display)
 
     rcc = g_list_nth_data(channel->clients, 0);
 
-    red_channel_client_ref(rcc);
+    g_object_ref(rcc);
     for (;;) {
         red_channel_client_receive(rcc);
         if (!red_channel_client_is_connected(rcc)) {
@@ -1146,7 +1146,7 @@ int display_channel_wait_for_migrate_data(DisplayChannel *display)
         }
         usleep(DISPLAY_CLIENT_RETRY_INTERVAL);
     }
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
     return ret;
 }
 
diff --git a/server/dummy-channel-client.c b/server/dummy-channel-client.c
new file mode 100644
index 0000000..1b72137
--- /dev/null
+++ b/server/dummy-channel-client.c
@@ -0,0 +1,158 @@
+/*
+   Copyright (C) 2009-2015 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "dummy-channel-client.h"
+#include "red-channel.h"
+
+static void dummy_channel_client_initable_interface_init(GInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE(DummyChannelClient, dummy_channel_client, RED_TYPE_CHANNEL_CLIENT,
+                        G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE,
+                                              dummy_channel_client_initable_interface_init))
+
+#define DUMMY_CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_DUMMY_CHANNEL_CLIENT, DummyChannelClientPrivate))
+
+struct DummyChannelClientPrivate
+{
+    gboolean connected;
+};
+
+static int dummy_channel_client_pre_create_validate(RedChannel *channel, RedClient  *client)
+{
+    if (red_client_get_channel(client, channel->type, channel->id)) {
+        spice_printerr("Error client %p: duplicate channel type %d id %d",
+                       client, channel->type, channel->id);
+        return FALSE;
+    }
+    return TRUE;
+}
+
+static gboolean dummy_channel_client_initable_init(GInitable *initable,
+                                                   GCancellable *cancellable,
+                                                   GError **error)
+{
+    GError *local_error = NULL;
+    DummyChannelClient *self = DUMMY_CHANNEL_CLIENT(initable);
+    RedChannelClient *rcc = RED_CHANNEL_CLIENT(self);
+    RedClient *client = red_channel_client_get_client(rcc);
+    RedChannel *channel = red_channel_client_get_channel(rcc);
+    pthread_mutex_lock(&client->lock);
+    if (!dummy_channel_client_pre_create_validate(channel,
+                                                  client)) {
+        g_set_error(&local_error,
+                    SPICE_SERVER_ERROR,
+                    SPICE_SERVER_ERROR_FAILED,
+                    "Client %p: duplicate channel type %d id %d",
+                    client, channel->type, channel->id);
+        goto cleanup;
+    }
+
+    rcc->incoming.header.data = rcc->incoming.header_buf;
+
+    red_channel_add_client(channel, rcc);
+    red_client_add_channel(client, rcc);
+
+cleanup:
+    pthread_mutex_unlock(&client->lock);
+    if (local_error) {
+        g_warning("Failed to create channel client: %s", local_error->message);
+        g_propagate_error(error, local_error);
+    }
+    return local_error == NULL;
+}
+
+static void dummy_channel_client_initable_interface_init(GInitableIface *iface)
+{
+    iface->init = dummy_channel_client_initable_init;
+}
+
+static gboolean dummy_channel_client_is_connected(RedChannelClient *rcc)
+{
+    return DUMMY_CHANNEL_CLIENT(rcc)->priv->connected;
+}
+
+static void dummy_channel_client_disconnect(RedChannelClient *rcc)
+{
+    DummyChannelClient *self = DUMMY_CHANNEL_CLIENT(rcc);
+    RedChannel *channel = red_channel_client_get_channel(rcc);
+    GList *link;
+
+    if (channel && (link = g_list_find(channel->clients, rcc))) {
+        spice_printerr("rcc=%p (channel=%p type=%d id=%d)", rcc, channel,
+                       channel->type, channel->id);
+        red_channel_remove_client(channel, link->data);
+    }
+    self->priv->connected = FALSE;
+}
+
+static void
+dummy_channel_client_class_init(DummyChannelClientClass *klass)
+{
+    RedChannelClientClass *cc_class = RED_CHANNEL_CLIENT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof(DummyChannelClientPrivate));
+
+    cc_class->is_connected = dummy_channel_client_is_connected;
+    cc_class->disconnect = dummy_channel_client_disconnect;
+}
+
+static void
+dummy_channel_client_init(DummyChannelClient *self)
+{
+    self->priv = DUMMY_CHANNEL_CLIENT_PRIVATE(self);
+
+    self->priv->connected = TRUE;
+}
+
+RedChannelClient* dummy_channel_client_create(RedChannel *channel,
+                                              RedClient  *client,
+                                              int num_common_caps,
+                                              uint32_t *common_caps,
+                                              int num_caps, uint32_t *caps)
+{
+    RedChannelClient *rcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
+
+    rcc = g_initable_new(TYPE_DUMMY_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", channel,
+                         "client", client,
+                         "caps", caps_array,
+                         "common-caps", common_caps_array,
+                         NULL);
+
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
+
+    return rcc;
+}
diff --git a/server/dummy-channel-client.h b/server/dummy-channel-client.h
new file mode 100644
index 0000000..8013aa2
--- /dev/null
+++ b/server/dummy-channel-client.h
@@ -0,0 +1,64 @@
+/*
+   Copyright (C) 2009-2015 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+   */
+#ifndef __DUMMY_CHANNEL_CLIENT_H__
+#define __DUMMY_CHANNEL_CLIENT_H__
+
+#include <glib-object.h>
+
+#include "red-channel-client.h"
+
+G_BEGIN_DECLS
+
+#define TYPE_DUMMY_CHANNEL_CLIENT dummy_channel_client_get_type()
+
+#define DUMMY_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DUMMY_CHANNEL_CLIENT, DummyChannelClient))
+#define DUMMY_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DUMMY_CHANNEL_CLIENT, DummyChannelClientClass))
+#define IS_DUMMY_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DUMMY_CHANNEL_CLIENT))
+#define IS_DUMMY_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DUMMY_CHANNEL_CLIENT))
+#define DUMMY_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DUMMY_CHANNEL_CLIENT, DummyChannelClientClass))
+
+typedef struct DummyChannelClient DummyChannelClient;
+typedef struct DummyChannelClientClass DummyChannelClientClass;
+typedef struct DummyChannelClientPrivate DummyChannelClientPrivate;
+
+struct DummyChannelClient
+{
+    RedChannelClient parent;
+
+    DummyChannelClientPrivate *priv;
+};
+
+struct DummyChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
+
+GType dummy_channel_client_get_type(void) G_GNUC_CONST;
+
+RedChannelClient *dummy_channel_client_create(RedChannel *channel,
+                                              RedClient  *client,
+                                              int num_common_caps, uint32_t *common_caps,
+                                              int num_caps, uint32_t *caps);
+
+G_END_DECLS
+
+#endif /* __DUMMY_CHANNEL_CLIENT_H__ */
diff --git a/server/inputs-channel-client.c b/server/inputs-channel-client.c
index 7b38625..76de382 100644
--- a/server/inputs-channel-client.c
+++ b/server/inputs-channel-client.c
@@ -19,22 +19,30 @@
 #endif
 
 #include "inputs-channel-client.h"
-#include "inputs-channel.h"
 #include "migration-protocol.h"
 #include "red-channel-client.h"
 
-typedef struct InputsChannelClientPrivate InputsChannelClientPrivate;
+G_DEFINE_TYPE(InputsChannelClient, inputs_channel_client, RED_TYPE_CHANNEL_CLIENT)
+
+#define INPUTS_CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_INPUTS_CHANNEL_CLIENT, InputsChannelClientPrivate))
+
 struct InputsChannelClientPrivate
 {
     uint16_t motion_count;
 };
 
-struct InputsChannelClient
+static void
+inputs_channel_client_class_init(InputsChannelClientClass *klass)
 {
-    RedChannelClient base;
+    g_type_class_add_private(klass, sizeof(InputsChannelClientPrivate));
+}
 
-    InputsChannelClientPrivate priv[1];
-};
+static void
+inputs_channel_client_init(InputsChannelClient *self)
+{
+    self->priv = INPUTS_CHANNEL_CLIENT_PRIVATE(self);
+}
 
 RedChannelClient* inputs_channel_client_create(RedChannel *channel,
                                                RedClient *client,
@@ -45,18 +53,34 @@ RedChannelClient* inputs_channel_client_create(RedChannel *channel,
                                                int num_caps,
                                                uint32_t *caps)
 {
-    InputsChannelClient* icc =
-        INPUTS_CHANNEL_CLIENT(red_channel_client_create(sizeof(InputsChannelClient),
-                                                        channel, client,
-                                                        stream,
-                                                        monitor_latency,
-                                                        num_common_caps,
-                                                        common_caps, num_caps,
-                                                        caps));
-    if (icc) {
-        icc->priv->motion_count = 0;
+    RedChannelClient *rcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
     }
-    return RED_CHANNEL_CLIENT(icc);
+    rcc = g_initable_new(TYPE_INPUTS_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", channel,
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", monitor_latency,
+                         "caps", caps_array,
+                         "common-caps", common_caps_array,
+                         NULL);
+
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
+
+    return rcc;
 }
 
 void inputs_channel_client_send_migrate_data(RedChannelClient *rcc,
diff --git a/server/inputs-channel-client.h b/server/inputs-channel-client.h
index 82dbbaf..7550b3c 100644
--- a/server/inputs-channel-client.h
+++ b/server/inputs-channel-client.h
@@ -18,9 +18,43 @@
 #ifndef _INPUTS_CHANNEL_CLIENT_H_
 #define _INPUTS_CHANNEL_CLIENT_H_
 
-#include "red-channel.h"
+#include <glib-object.h>
+
+#include "red-channel-client.h"
+#include "inputs-channel.h"
+
+G_BEGIN_DECLS
+
+#define TYPE_INPUTS_CHANNEL_CLIENT inputs_channel_client_get_type()
+
+#define INPUTS_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_INPUTS_CHANNEL_CLIENT, InputsChannelClient))
+#define INPUTS_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_INPUTS_CHANNEL_CLIENT, InputsChannelClientClass))
+#define IS_INPUTS_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_INPUTS_CHANNEL_CLIENT))
+#define IS_INPUTS_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_INPUTS_CHANNEL_CLIENT))
+#define INPUTS_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_INPUTS_CHANNEL_CLIENT, InputsChannelClientClass))
 
 typedef struct InputsChannelClient InputsChannelClient;
+typedef struct InputsChannelClientClass InputsChannelClientClass;
+typedef struct InputsChannelClientPrivate InputsChannelClientPrivate;
+
+struct InputsChannelClient
+{
+    RedChannelClient parent;
+
+    InputsChannelClientPrivate *priv;
+};
+
+struct InputsChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
+
+GType inputs_channel_client_get_type(void) G_GNUC_CONST;
 
 RedChannelClient* inputs_channel_client_create(RedChannel *channel,
                                                RedClient *client,
@@ -31,12 +65,15 @@ RedChannelClient* inputs_channel_client_create(RedChannel *channel,
                                                int num_caps,
                                                uint32_t *caps);
 
+uint16_t inputs_channel_client_get_motion_count(InputsChannelClient* self);
+/* only for migration */
+void inputs_channel_client_set_motion_count(InputsChannelClient* self, uint16_t count);
+void inputs_channel_client_on_mouse_motion(InputsChannelClient* self);
 void inputs_channel_client_send_migrate_data(RedChannelClient *rcc,
-                                             SpiceMarshaller *m,
-                                             RedPipeItem *item);
-void inputs_channel_client_handle_migrate_data(InputsChannelClient *icc,
-                                               uint16_t motion_count);
-void inputs_channel_client_on_mouse_motion(InputsChannelClient *icc);
+                                             SpiceMarshaller *m, RedPipeItem *item);
+void inputs_channel_client_handle_migrate_data(InputsChannelClient *icc, uint16_t motion_count);
+
+G_END_DECLS
 
 enum {
     RED_PIPE_ITEM_INPUTS_INIT = RED_PIPE_ITEM_TYPE_CHANNEL_BASE,
@@ -45,6 +82,4 @@ enum {
     RED_PIPE_ITEM_MIGRATE_DATA,
 };
 
-#define INPUTS_CHANNEL_CLIENT(rcc) ((InputsChannelClient*)rcc)
-
 #endif /* _INPUTS_CHANNEL_CLIENT_H_ */
diff --git a/server/main-channel-client.c b/server/main-channel-client.c
index 2390784..9966a38 100644
--- a/server/main-channel-client.c
+++ b/server/main-channel-client.c
@@ -42,7 +42,11 @@ enum NetTestStage {
 #define CLIENT_CONNECTIVITY_TIMEOUT (MSEC_PER_SEC * 30)
 #define PING_INTERVAL (MSEC_PER_SEC * 10)
 
-typedef struct MainChannelClientPrivate MainChannelClientPrivate;
+G_DEFINE_TYPE(MainChannelClient, main_channel_client, RED_TYPE_CHANNEL_CLIENT)
+
+#define MAIN_CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_MAIN_CHANNEL_CLIENT, MainChannelClientPrivate))
+
 struct MainChannelClientPrivate {
     uint32_t connection_id;
     uint32_t ping_id;
@@ -62,12 +66,6 @@ struct MainChannelClientPrivate {
     int seamless_mig_dst;
 };
 
-struct MainChannelClient {
-    RedChannelClient base;
-
-    MainChannelClientPrivate priv[1];
-};
-
 typedef struct RedPingPipeItem {
     RedPipeItem base;
     int size;
@@ -126,6 +124,93 @@ typedef struct RedMultiMediaTimePipeItem {
 
 static const uint8_t zero_page[ZERO_BUF_SIZE] = {0};
 
+enum {
+    PROP0,
+    PROP_CONNECTION_ID
+};
+
+static void main_channel_client_get_property(GObject *object,
+                                             guint property_id,
+                                             GValue *value,
+                                             GParamSpec *pspec)
+{
+    MainChannelClient *self = MAIN_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_CONNECTION_ID:
+            g_value_set_uint(value, self->priv->connection_id);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void main_channel_client_set_property(GObject *object,
+                                             guint property_id,
+                                             const GValue *value,
+                                             GParamSpec *pspec)
+{
+    MainChannelClient *self = MAIN_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_CONNECTION_ID:
+            self->priv->connection_id = g_value_get_uint(value);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void ping_timer_cb(void *opaque);
+static void main_channel_client_constructed(GObject *object)
+{
+    G_OBJECT_CLASS(main_channel_client_parent_class)->constructed(object);
+#ifdef RED_STATISTICS
+    MainChannelClient *self = MAIN_CHANNEL_CLIENT(object);
+    RedsState *reds =
+        red_channel_get_server(red_channel_client_get_channel(RED_CHANNEL_CLIENT(object)));
+
+    self->priv->ping_timer =
+        reds_get_core_interface(reds)->timer_add(reds_get_core_interface(reds),
+                                                 ping_timer_cb, self);
+    if (!self->priv->ping_timer) {
+        spice_error("ping timer create failed");
+    }
+    self->priv->ping_interval = PING_INTERVAL;
+#endif
+}
+
+static void main_channel_client_class_init(MainChannelClientClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof(MainChannelClientPrivate));
+
+    object_class->get_property = main_channel_client_get_property;
+    object_class->set_property = main_channel_client_set_property;
+    object_class->constructed = main_channel_client_constructed;
+
+    g_object_class_install_property(object_class,
+                                    PROP_CONNECTION_ID,
+                                    g_param_spec_uint("connection-id",
+                                                      "Connection ID",
+                                                      "Connection ID",
+                                                      0,
+                                                      G_MAXUINT,
+                                                      0,
+                                                      G_PARAM_CONSTRUCT_ONLY |
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_STATIC_STRINGS));
+}
+
+static void main_channel_client_init(MainChannelClient *self)
+{
+    self->priv = MAIN_CHANNEL_CLIENT_PRIVATE(self);
+    self->priv->bitrate_per_sec = ~0;
+}
+
 static int main_channel_client_push_ping(MainChannelClient *mcc, int size);
 
 static void main_notify_item_free(RedPipeItem *base)
@@ -556,19 +641,35 @@ MainChannelClient *main_channel_client_create(MainChannel *main_chan, RedClient
                                               int num_common_caps, uint32_t *common_caps,
                                               int num_caps, uint32_t *caps)
 {
-    MainChannelClient *mcc = MAIN_CHANNEL_CLIENT(
-                             red_channel_client_create(sizeof(MainChannelClient), &main_chan->base,
-                                                       client, stream, FALSE, num_common_caps,
-                                                       common_caps, num_caps, caps));
-    spice_assert(mcc != NULL);
-    mcc->priv->connection_id = connection_id;
-    mcc->priv->bitrate_per_sec = ~0;
-#ifdef RED_STATISTICS
-    if (!(mcc->priv->ping_timer = reds_core_timer_add(red_channel_get_server(&main_chan->base), ping_timer_cb, mcc))) {
-        spice_error("ping timer create failed");
+    MainChannelClient *mcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
     }
-    mcc->priv->ping_interval = PING_INTERVAL;
-#endif
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
+
+    mcc = g_initable_new(TYPE_MAIN_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", RED_CHANNEL(main_chan),
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", FALSE,
+                         "caps", caps_array,
+                         "common-caps", common_caps_array,
+                         "connection-id", connection_id,
+                         NULL);
+
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
+
     return mcc;
 }
 
diff --git a/server/main-channel-client.h b/server/main-channel-client.h
index 3bb8729..360c61e 100644
--- a/server/main-channel-client.h
+++ b/server/main-channel-client.h
@@ -17,11 +17,46 @@
 #ifndef __MAIN_CHANNEL_CLIENT_H__
 #define __MAIN_CHANNEL_CLIENT_H__
 
-#include "red-channel.h"
+#include <glib-object.h>
+#include <common/messages.h>
+
+#include "red-channel-client.h"
+
+G_BEGIN_DECLS
 
 /* FIXME: remove extra MainChannel typedef when possible */
 typedef struct MainChannel MainChannel;
+
+#define TYPE_MAIN_CHANNEL_CLIENT main_channel_client_get_type()
+
+#define MAIN_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_MAIN_CHANNEL_CLIENT, MainChannelClient))
+#define MAIN_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_MAIN_CHANNEL_CLIENT, MainChannelClientClass))
+#define IS_MAIN_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_MAIN_CHANNEL_CLIENT))
+#define IS_MAIN_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_MAIN_CHANNEL_CLIENT))
+#define MAIN_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_MAIN_CHANNEL_CLIENT, MainChannelClientClass))
+
 typedef struct MainChannelClient MainChannelClient;
+typedef struct MainChannelClientClass MainChannelClientClass;
+typedef struct MainChannelClientPrivate MainChannelClientPrivate;
+
+struct MainChannelClient
+{
+    RedChannelClient parent;
+
+    MainChannelClientPrivate *priv;
+};
+
+struct MainChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
+
+GType main_channel_client_get_type(void) G_GNUC_CONST;
 
 MainChannelClient *main_channel_client_create(MainChannel *main_chan, RedClient *client,
                                               RedsStream *stream, uint32_t connection_id,
@@ -105,6 +140,6 @@ typedef struct MainMultiMediaTimeItemInfo {
 RedPipeItem *main_multi_media_time_item_new(RedChannelClient *rcc,
                                             void *data, int num);
 
-#define MAIN_CHANNEL_CLIENT(rcc) ((MainChannelClient*)rcc)
+G_END_DECLS
 
 #endif /* __MAIN_CHANNEL_CLIENT_H__ */
diff --git a/server/red-channel-client-private.h b/server/red-channel-client-private.h
index 804b39a..f94e042 100644
--- a/server/red-channel-client-private.h
+++ b/server/red-channel-client-private.h
@@ -19,14 +19,14 @@
 #define _H_RED_CHANNEL_CLIENT_PRIVATE
 
 #include "red-channel.h"
+#include "red-channel-client.h"
 
 struct RedChannelClientPrivate
 {
     RedChannel *channel;
     RedClient  *client;
     RedsStream *stream;
-    int dummy;
-    int dummy_connected;
+    gboolean monitor_latency;
 
     uint32_t refs;
 
diff --git a/server/red-channel-client.c b/server/red-channel-client.c
index c59e4d0..e860390 100644
--- a/server/red-channel-client.c
+++ b/server/red-channel-client.c
@@ -37,6 +37,32 @@
 #include "red-channel-client-private.h"
 #include "red-channel.h"
 
+static const SpiceDataHeaderOpaque full_header_wrapper;
+static const SpiceDataHeaderOpaque mini_header_wrapper;
+static void red_channel_client_destroy_remote_caps(RedChannelClient* rcc);
+static void red_channel_client_initable_interface_init(GInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE(RedChannelClient, red_channel_client, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE(G_TYPE_INITABLE,
+                                              red_channel_client_initable_interface_init))
+
+#define CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), RED_TYPE_CHANNEL_CLIENT, RedChannelClientPrivate))
+
+static gboolean red_channel_client_initable_init(GInitable *initable,
+                                                 GCancellable *cancellable,
+                                                 GError **error);
+
+enum {
+    PROP0,
+    PROP_STREAM,
+    PROP_CHANNEL,
+    PROP_CLIENT,
+    PROP_MONITOR_LATENCY,
+    PROP_COMMON_CAPS,
+    PROP_CAPS
+};
+
 #define PING_TEST_TIMEOUT_MS (MSEC_PER_SEC * 15)
 #define PING_TEST_IDLE_NET_TIMEOUT_MS (MSEC_PER_SEC / 10)
 
@@ -102,6 +128,226 @@ static void red_channel_client_restart_ping_timer(RedChannelClient *rcc)
     red_channel_client_start_ping_timer(rcc, timeout);
 }
 
+static void
+red_channel_client_get_property(GObject *object,
+                                guint property_id,
+                                GValue *value,
+                                GParamSpec *pspec)
+{
+    RedChannelClient *self = RED_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_STREAM:
+            g_value_set_pointer(value, self->priv->stream);
+            break;
+        case PROP_CHANNEL:
+            g_value_set_pointer(value, self->priv->channel);
+            break;
+        case PROP_CLIENT:
+            g_value_set_pointer(value, self->priv->client);
+            break;
+        case PROP_MONITOR_LATENCY:
+            g_value_set_boolean(value, self->priv->monitor_latency);
+            break;
+        case PROP_COMMON_CAPS:
+            {
+                GArray *arr = g_array_sized_new(FALSE, FALSE,
+                                                sizeof(*self->priv->remote_caps.common_caps),
+                                                self->priv->remote_caps.num_common_caps);
+                g_value_take_boxed(value, arr);
+            }
+            break;
+        case PROP_CAPS:
+            {
+                GArray *arr = g_array_sized_new(FALSE, FALSE,
+                                                sizeof(*self->priv->remote_caps.caps),
+                                                self->priv->remote_caps.num_caps);
+                g_value_take_boxed(value, arr);
+            }
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void
+red_channel_client_set_property(GObject *object,
+                                guint property_id,
+                                const GValue *value,
+                                GParamSpec *pspec)
+{
+    RedChannelClient *self = RED_CHANNEL_CLIENT(object);
+
+    switch (property_id)
+    {
+        case PROP_STREAM:
+            self->priv->stream = g_value_get_pointer(value);
+            break;
+        case PROP_CHANNEL:
+            if (self->priv->channel)
+                red_channel_unref(self->priv->channel);
+            self->priv->channel = g_value_get_pointer(value);
+            if (self->priv->channel)
+                red_channel_ref(self->priv->channel);
+            break;
+        case PROP_CLIENT:
+            self->priv->client = g_value_get_pointer(value);
+            break;
+        case PROP_MONITOR_LATENCY:
+            self->priv->monitor_latency = g_value_get_boolean(value);
+            break;
+        case PROP_COMMON_CAPS:
+            {
+                GArray *caps = g_value_get_boxed(value);
+                if (caps) {
+                    self->priv->remote_caps.num_common_caps = caps->len;
+                    free(self->priv->remote_caps.common_caps);
+                    self->priv->remote_caps.common_caps =
+                        spice_memdup(caps->data, caps->len * sizeof(uint32_t));
+                }
+            }
+            break;
+        case PROP_CAPS:
+            {
+                GArray *caps = g_value_get_boxed(value);
+                if (caps) {
+                    self->priv->remote_caps.num_caps = caps->len;
+                    free(self->priv->remote_caps.caps);
+                    self->priv->remote_caps.caps =
+                        spice_memdup(caps->data, caps->len * sizeof(uint32_t));
+                }
+            }
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void
+red_channel_client_finalize(GObject *object)
+{
+    RedChannelClient *self = RED_CHANNEL_CLIENT(object);
+
+    reds_stream_free(self->priv->stream);
+    self->priv->stream = NULL;
+
+    if (self->priv->send_data.main.marshaller) {
+        spice_marshaller_destroy(self->priv->send_data.main.marshaller);
+    }
+
+    if (self->priv->send_data.urgent.marshaller) {
+        spice_marshaller_destroy(self->priv->send_data.urgent.marshaller);
+    }
+
+    red_channel_client_destroy_remote_caps(self);
+    if (self->priv->channel) {
+        red_channel_unref(self->priv->channel);
+    }
+
+    G_OBJECT_CLASS(red_channel_client_parent_class)->finalize(object);
+}
+
+static void red_channel_client_initable_interface_init(GInitableIface *iface)
+{
+    iface->init = red_channel_client_initable_init;
+}
+
+static gboolean red_channel_client_default_is_connected(RedChannelClient *rcc);
+static void red_channel_client_default_disconnect(RedChannelClient *rcc);
+
+
+static void red_channel_client_constructed(GObject *object)
+{
+    RedChannelClient *self =  RED_CHANNEL_CLIENT(object);
+
+    if (red_channel_client_test_remote_common_cap(self, SPICE_COMMON_CAP_MINI_HEADER)) {
+        self->incoming.header = mini_header_wrapper;
+        self->priv->send_data.header = mini_header_wrapper;
+        self->priv->is_mini_header = TRUE;
+    } else {
+        self->incoming.header = full_header_wrapper;
+        self->priv->send_data.header = full_header_wrapper;
+        self->priv->is_mini_header = FALSE;
+    }
+}
+
+static void red_channel_client_class_init(RedChannelClientClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+    GParamSpec *spec;
+
+    g_debug("%s", G_STRFUNC);
+    g_type_class_add_private(klass, sizeof(RedChannelClientPrivate));
+
+    object_class->get_property = red_channel_client_get_property;
+    object_class->set_property = red_channel_client_set_property;
+    object_class->finalize = red_channel_client_finalize;
+    object_class->constructed = red_channel_client_constructed;
+
+    klass->is_connected = red_channel_client_default_is_connected;
+    klass->disconnect = red_channel_client_default_disconnect;
+
+    spec = g_param_spec_pointer("stream", "stream",
+                                "Associated RedStream",
+                                G_PARAM_STATIC_STRINGS
+                                | G_PARAM_READWRITE
+                                | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_STREAM, spec);
+
+    spec = g_param_spec_pointer("channel", "channel",
+                                "Associated RedChannel",
+                                G_PARAM_STATIC_STRINGS
+                                | G_PARAM_READWRITE
+                                | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_CHANNEL, spec);
+
+    spec = g_param_spec_pointer("client", "client",
+                                "Associated RedClient",
+                                G_PARAM_STATIC_STRINGS
+                                | G_PARAM_READWRITE
+                                | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_CLIENT, spec);
+
+    spec = g_param_spec_boolean("monitor-latency", "monitor-latency",
+                                "Whether to monitor latency for this client",
+                                FALSE,
+                                G_PARAM_STATIC_STRINGS
+                                | G_PARAM_READWRITE
+                                | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_MONITOR_LATENCY, spec);
+
+    spec = g_param_spec_boxed("common-caps", "common-caps",
+                              "Common Capabilities",
+                              G_TYPE_ARRAY,
+                              G_PARAM_STATIC_STRINGS
+                              | G_PARAM_READWRITE
+                              | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_COMMON_CAPS, spec);
+
+    spec = g_param_spec_boxed("caps", "caps",
+                              "Capabilities",
+                              G_TYPE_ARRAY,
+                              G_PARAM_STATIC_STRINGS
+                              | G_PARAM_READWRITE
+                              | G_PARAM_CONSTRUCT_ONLY);
+    g_object_class_install_property(object_class, PROP_CAPS, spec);
+}
+
+static void
+red_channel_client_init(RedChannelClient *self)
+{
+    self->priv = CHANNEL_CLIENT_PRIVATE(self);
+    // blocks send message (maybe use send_data.blocked + block flags)
+    self->priv->ack_data.messages_window = ~0;
+    self->priv->ack_data.client_generation = ~0;
+    self->priv->ack_data.client_window = CLIENT_ACK_WINDOW;
+    self->priv->send_data.main.marshaller = spice_marshaller_new();
+    self->priv->send_data.urgent.marshaller = spice_marshaller_new();
+
+    self->priv->send_data.marshaller = self->priv->send_data.main.marshaller;
+}
+
 RedChannel* red_channel_client_get_channel(RedChannelClient *rcc)
 {
     return rcc->priv->channel;
@@ -336,19 +582,6 @@ static gboolean red_channel_client_pipe_remove(RedChannelClient *rcc, RedPipeIte
     return g_queue_remove(&rcc->priv->pipe, item);
 }
 
-static void red_channel_client_set_remote_caps(RedChannelClient* rcc,
-                                               int num_common_caps, uint32_t *common_caps,
-                                               int num_caps, uint32_t *caps)
-{
-    rcc->priv->remote_caps.num_common_caps = num_common_caps;
-    rcc->priv->remote_caps.common_caps = spice_memdup(common_caps,
-                                                      num_common_caps *
-                                                      sizeof(uint32_t));
-
-    rcc->priv->remote_caps.num_caps = num_caps;
-    rcc->priv->remote_caps.caps = spice_memdup(caps, num_caps * sizeof(uint32_t));
-}
-
 static void red_channel_client_destroy_remote_caps(RedChannelClient* rcc)
 {
     rcc->priv->remote_caps.num_common_caps = 0;
@@ -510,14 +743,14 @@ static void red_channel_client_event(int fd, int event, void *data)
 {
     RedChannelClient *rcc = RED_CHANNEL_CLIENT(data);
 
-    red_channel_client_ref(rcc);
+    g_object_ref(rcc);
     if (event & SPICE_WATCH_EVENT_READ) {
         red_channel_client_receive(rcc);
     }
     if (event & SPICE_WATCH_EVENT_WRITE) {
         red_channel_client_push(rcc);
     }
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
 }
 
 static uint32_t full_header_get_msg_size(SpiceDataHeaderOpaque *header)
@@ -606,129 +839,108 @@ static int red_channel_client_pre_create_validate(RedChannel *channel, RedClient
     return TRUE;
 }
 
-RedChannelClient *red_channel_client_create(int size, RedChannel *channel, RedClient  *client,
-                                            RedsStream *stream,
-                                            int monitor_latency,
-                                            int num_common_caps, uint32_t *common_caps,
-                                            int num_caps, uint32_t *caps)
-{
-    RedChannelClient *rcc = NULL;
-
-    pthread_mutex_lock(&client->lock);
-    if (!red_channel_client_pre_create_validate(channel, client)) {
-        goto error;
-    }
-    spice_assert(stream && channel && size >= sizeof(RedChannelClient));
-    rcc = spice_malloc0(size);
-    rcc->priv->stream = stream;
-    rcc->priv->channel = channel;
-    rcc->priv->client = client;
-    rcc->priv->refs = 1;
-    rcc->priv->ack_data.messages_window = ~0;  // blocks send message (maybe use send_data.blocked +
-                                               // block flags)
-    rcc->priv->ack_data.client_generation = ~0;
-    rcc->priv->ack_data.client_window = CLIENT_ACK_WINDOW;
-    rcc->priv->send_data.main.marshaller = spice_marshaller_new();
-    rcc->priv->send_data.urgent.marshaller = spice_marshaller_new();
-
-    rcc->priv->send_data.marshaller = rcc->priv->send_data.main.marshaller;
-
-    rcc->incoming.opaque = rcc;
-    rcc->incoming.cb = &channel->incoming_cb;
-
-    rcc->outgoing.opaque = rcc;
-    rcc->outgoing.cb = &channel->outgoing_cb;
-    rcc->outgoing.pos = 0;
-    rcc->outgoing.size = 0;
-
-    red_channel_client_set_remote_caps(rcc, num_common_caps, common_caps, num_caps, caps);
-    if (red_channel_client_test_remote_common_cap(rcc, SPICE_COMMON_CAP_MINI_HEADER)) {
-        rcc->incoming.header = mini_header_wrapper;
-        rcc->priv->send_data.header = mini_header_wrapper;
-        rcc->priv->is_mini_header = TRUE;
-    } else {
-        rcc->incoming.header = full_header_wrapper;
-        rcc->priv->send_data.header = full_header_wrapper;
-        rcc->priv->is_mini_header = FALSE;
+static gboolean red_channel_client_initable_init(GInitable *initable,
+                                                 GCancellable *cancellable,
+                                                 GError **error)
+{
+    GError *local_error = NULL;
+    RedChannelClient *self = RED_CHANNEL_CLIENT(initable);
+    pthread_mutex_lock(&self->priv->client->lock);
+    if (!red_channel_client_pre_create_validate(self->priv->channel, self->priv->client)) {
+        g_set_error(&local_error,
+                    SPICE_SERVER_ERROR,
+                    SPICE_SERVER_ERROR_FAILED,
+                    "Client %p: duplicate channel type %d id %d",
+                    self->priv->client, self->priv->channel->type,
+                    self->priv->channel->id);
+        goto cleanup;
+    }
+
+    if (self->priv->monitor_latency
+        && reds_stream_get_family(self->priv->stream) != AF_UNIX) {
+        self->priv->latency_monitor.timer =
+            self->priv->channel->core->timer_add(self->priv->channel->core,
+                                                 red_channel_client_ping_timer,
+                                                 self);
+
+        if (!self->priv->client->during_target_migrate) {
+            red_channel_client_start_ping_timer(self,
+                                                PING_TEST_IDLE_NET_TIMEOUT_MS);
+        }
+        self->priv->latency_monitor.roundtrip = -1;
     }
 
-    rcc->incoming.header.data = rcc->incoming.header_buf;
-
-    if (!channel->channel_cbs.config_socket(rcc)) {
-        goto error;
-    }
+    self->incoming.opaque = self;
+    self->incoming.cb = &self->priv->channel->incoming_cb;
+    self->incoming.header.data = self->incoming.header_buf;
 
-    g_queue_init(&rcc->priv->pipe);
+    self->outgoing.opaque = self;
+    self->outgoing.cb = &self->priv->channel->outgoing_cb;
+    self->outgoing.pos = 0;
+    self->outgoing.size = 0;
 
-    stream->watch = channel->core->watch_add(channel->core,
-                                           stream->socket,
-                                           SPICE_WATCH_EVENT_READ,
-                                           red_channel_client_event, rcc);
-    rcc->priv->id = g_list_length(channel->clients);
-    red_channel_add_client(channel, rcc);
-    red_client_add_channel(client, rcc);
-    red_channel_ref(channel);
-    pthread_mutex_unlock(&client->lock);
+    g_queue_init(&self->priv->pipe);
+    if (self->priv->stream)
+        self->priv->stream->watch =
+            self->priv->channel->core->watch_add(self->priv->channel->core,
+                                                 self->priv->stream->socket,
+                                                 SPICE_WATCH_EVENT_READ,
+                                                 red_channel_client_event,
+                                                 self);
+    self->priv->id = g_list_length(self->priv->channel->clients);
+    red_channel_add_client(self->priv->channel, self);
+    red_client_add_channel(self->priv->client, self);
 
-    if (monitor_latency && reds_stream_get_family(stream) != AF_UNIX) {
-        rcc->priv->latency_monitor.timer = channel->core->timer_add(
-            channel->core, red_channel_client_ping_timer, rcc);
-        if (!client->during_target_migrate) {
-            red_channel_client_start_ping_timer(rcc, PING_TEST_IDLE_NET_TIMEOUT_MS);
-        }
-        rcc->priv->latency_monitor.roundtrip = -1;
+    if (!self->priv->channel->channel_cbs.config_socket(self)) {
+        g_set_error_literal(&local_error,
+                            SPICE_SERVER_ERROR,
+                            SPICE_SERVER_ERROR_FAILED,
+                            "Unable to configure socket");
     }
 
-    return rcc;
-error:
-    free(rcc);
-    reds_stream_free(stream);
-    pthread_mutex_unlock(&client->lock);
-    return NULL;
-}
-
-RedChannelClient *red_channel_client_create_dummy(int size,
-                                                  RedChannel *channel,
-                                                  RedClient  *client,
-                                                  int num_common_caps, uint32_t *common_caps,
-                                                  int num_caps, uint32_t *caps)
-{
-    RedChannelClient *rcc = NULL;
-
-    spice_assert(size >= sizeof(RedChannelClient));
-
-    pthread_mutex_lock(&client->lock);
-    if (!red_channel_client_pre_create_validate(channel, client)) {
-        goto error;
-    }
-    rcc = spice_malloc0(size);
-    rcc->priv->refs = 1;
-    rcc->priv->client = client;
-    rcc->priv->channel = channel;
-    red_channel_ref(channel);
-    red_channel_client_set_remote_caps(rcc, num_common_caps, common_caps, num_caps, caps);
-    if (red_channel_client_test_remote_common_cap(rcc, SPICE_COMMON_CAP_MINI_HEADER)) {
-        rcc->incoming.header = mini_header_wrapper;
-        rcc->priv->send_data.header = mini_header_wrapper;
-        rcc->priv->is_mini_header = TRUE;
-    } else {
-        rcc->incoming.header = full_header_wrapper;
-        rcc->priv->send_data.header = full_header_wrapper;
-        rcc->priv->is_mini_header = FALSE;
+cleanup:
+    pthread_mutex_unlock(&self->priv->client->lock);
+    if (local_error) {
+        g_warning("Failed to create channel client: %s", local_error->message);
+        g_propagate_error(error, local_error);
     }
+    return local_error == NULL;
+}
 
-    rcc->incoming.header.data = rcc->incoming.header_buf;
-    g_queue_init(&rcc->priv->pipe);
+RedChannelClient *red_channel_client_create(RedChannel *channel, RedClient *client,
+                                            RedsStream *stream,
+                                            int monitor_latency,
+                                            int num_common_caps, uint32_t *common_caps,
+                                            int num_caps, uint32_t *caps)
+{
+    RedChannelClient *rcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
+    rcc = g_initable_new(RED_TYPE_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", channel,
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", monitor_latency,
+                         "caps", caps_array,
+                         "common-caps", common_caps_array,
+                         NULL);
+
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
 
-    rcc->priv->dummy = TRUE;
-    rcc->priv->dummy_connected = TRUE;
-    red_channel_add_client(channel, rcc);
-    red_client_add_channel(client, rcc);
-    pthread_mutex_unlock(&client->lock);
     return rcc;
-error:
-    pthread_mutex_unlock(&client->lock);
-    return NULL;
 }
 
 static void red_channel_client_seamless_migration_done(RedChannelClient *rcc)
@@ -772,43 +984,12 @@ void red_channel_client_default_migrate(RedChannelClient *rcc)
     red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_MIGRATE);
 }
 
-void red_channel_client_ref(RedChannelClient *rcc)
-{
-    rcc->priv->refs++;
-}
-
-void red_channel_client_unref(RedChannelClient *rcc)
-{
-    if (--rcc->priv->refs != 0) {
-        return;
-    }
-
-    spice_debug("destroy rcc=%p", rcc);
-
-    reds_stream_free(rcc->priv->stream);
-    rcc->priv->stream = NULL;
-
-    if (rcc->priv->send_data.main.marshaller) {
-        spice_marshaller_destroy(rcc->priv->send_data.main.marshaller);
-    }
-
-    if (rcc->priv->send_data.urgent.marshaller) {
-        spice_marshaller_destroy(rcc->priv->send_data.urgent.marshaller);
-    }
-
-    red_channel_client_destroy_remote_caps(rcc);
-    if (rcc->priv->channel) {
-        red_channel_unref(rcc->priv->channel);
-    }
-    free(rcc);
-}
-
 void red_channel_client_destroy(RedChannelClient *rcc)
 {
     rcc->priv->destroying = TRUE;
     red_channel_client_disconnect(rcc);
     red_client_remove_channel(rcc);
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
 }
 
 void red_channel_client_shutdown(RedChannelClient *rcc)
@@ -1000,16 +1181,16 @@ static void red_peer_handle_incoming(RedsStream *stream, IncomingHandler *handle
 
 void red_channel_client_receive(RedChannelClient *rcc)
 {
-    red_channel_client_ref(rcc);
+    g_object_ref(rcc);
     red_peer_handle_incoming(rcc->priv->stream, &rcc->incoming);
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
 }
 
 void red_channel_client_send(RedChannelClient *rcc)
 {
-    red_channel_client_ref(rcc);
+    g_object_ref(rcc);
     red_peer_handle_outgoing(rcc->priv->stream, &rcc->outgoing);
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
 }
 
 static inline RedPipeItem *red_channel_client_pipe_item_get(RedChannelClient *rcc)
@@ -1030,7 +1211,7 @@ void red_channel_client_push(RedChannelClient *rcc)
     } else {
         return;
     }
-    red_channel_client_ref(rcc);
+    g_object_ref(rcc);
     if (rcc->priv->send_data.blocked) {
         red_channel_client_send(rcc);
     }
@@ -1049,7 +1230,7 @@ void red_channel_client_push(RedChannelClient *rcc)
                                                     SPICE_WATCH_EVENT_READ);
     }
     rcc->priv->during_send = FALSE;
-    red_channel_client_unref(rcc);
+    g_object_unref(rcc);
 }
 
 int red_channel_client_get_roundtrip_ms(RedChannelClient *rcc)
@@ -1377,14 +1558,18 @@ gboolean red_channel_client_is_mini_header(RedChannelClient *rcc)
     return rcc->priv->is_mini_header;
 }
 
-int red_channel_client_is_connected(RedChannelClient *rcc)
+static gboolean red_channel_client_default_is_connected(RedChannelClient *rcc)
 {
-    if (!rcc->priv->dummy) {
-        return rcc->priv->channel
-            && (g_list_find(rcc->priv->channel->clients, rcc) != NULL);
-    } else {
-        return rcc->priv->dummy_connected;
-    }
+    return rcc->priv->channel
+        && (g_list_find(rcc->priv->channel->clients, rcc) != NULL);
+}
+
+gboolean red_channel_client_is_connected(RedChannelClient *rcc)
+{
+    RedChannelClientClass *klass = RED_CHANNEL_CLIENT_GET_CLASS(rcc);
+
+    g_return_val_if_fail(klass->is_connected != NULL, FALSE);
+    return klass->is_connected(rcc);
 }
 
 static void red_channel_client_clear_sent_item(RedChannelClient *rcc)
@@ -1424,27 +1609,10 @@ void red_channel_client_push_set_ack(RedChannelClient *rcc)
     red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_SET_ACK);
 }
 
-static void red_channel_client_disconnect_dummy(RedChannelClient *rcc)
-{
-    RedChannel *channel = red_channel_client_get_channel(rcc);
-    GList *link;
-    spice_assert(rcc->priv->dummy);
-    if (channel && (link = g_list_find(channel->clients, rcc))) {
-        spice_printerr("rcc=%p (channel=%p type=%d id=%d)", rcc, channel,
-                       channel->type, channel->id);
-        red_channel_remove_client(channel, link->data);
-    }
-    rcc->priv->dummy_connected = FALSE;
-}
-
-void red_channel_client_disconnect(RedChannelClient *rcc)
+static void red_channel_client_default_disconnect(RedChannelClient *rcc)
 {
     RedChannel *channel = rcc->priv->channel;
 
-    if (rcc->priv->dummy) {
-        red_channel_client_disconnect_dummy(rcc);
-        return;
-    }
     if (!red_channel_client_is_connected(rcc)) {
         return;
     }
@@ -1467,6 +1635,14 @@ void red_channel_client_disconnect(RedChannelClient *rcc)
     channel->channel_cbs.on_disconnect(rcc);
 }
 
+void red_channel_client_disconnect(RedChannelClient *rcc)
+{
+    RedChannelClientClass *klass = RED_CHANNEL_CLIENT_GET_CLASS(rcc);
+
+    g_return_if_fail(klass->is_connected != NULL);
+    klass->disconnect(rcc);
+}
+
 int red_channel_client_is_blocked(RedChannelClient *rcc)
 {
     return rcc && rcc->priv->send_data.blocked;
@@ -1644,3 +1820,8 @@ gboolean red_channel_client_is_destroying(RedChannelClient *rcc)
 {
     return rcc->priv->destroying;
 }
+
+GQuark spice_server_error_quark(void)
+{
+    return g_quark_from_static_string("spice-server-error-quark");
+}
diff --git a/server/red-channel-client.h b/server/red-channel-client.h
index 5bfac3a..0b180b3 100644
--- a/server/red-channel-client.h
+++ b/server/red-channel-client.h
@@ -18,21 +18,47 @@
 #ifndef _H_RED_CHANNEL_CLIENT
 #define _H_RED_CHANNEL_CLIENT
 
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <spice/protocol.h>
 #include <common/marshaller.h>
 
 #include "red-pipe-item.h"
 #include "reds-stream.h"
 #include "red-channel.h"
-/* FIXME: remove */
-#include "red-channel-client-private.h"
+
+G_BEGIN_DECLS
+
+#define MAX_HEADER_SIZE sizeof(SpiceDataHeader)
+#define CLIENT_ACK_WINDOW 20
+
+#ifndef IOV_MAX
+#define IOV_MAX 1024
+#endif
+
+#define RED_TYPE_CHANNEL_CLIENT red_channel_client_get_type()
+
+#define RED_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), RED_TYPE_CHANNEL_CLIENT, RedChannelClient))
+#define RED_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), RED_TYPE_CHANNEL_CLIENT, RedChannelClientClass))
+#define RED_IS_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), RED_TYPE_CHANNEL_CLIENT))
+#define RED_IS_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), RED_TYPE_CHANNEL_CLIENT))
+#define RED_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), RED_TYPE_CHANNEL_CLIENT, RedChannelClientClass))
 
 typedef struct RedChannel RedChannel;
 typedef struct RedClient RedClient;
 typedef struct IncomingHandler IncomingHandler;
 
 typedef struct RedChannelClient RedChannelClient;
+typedef struct RedChannelClientClass RedChannelClientClass;
 typedef struct RedChannelClientPrivate RedChannelClientPrivate;
 
+GType red_channel_client_get_type(void) G_GNUC_CONST;
+
 /*
  * When an error occurs over a channel, we treat it as a warning
  * for spice-server and shutdown the channel.
@@ -45,22 +71,13 @@ typedef struct RedChannelClientPrivate RedChannelClientPrivate;
         red_channel_client_shutdown(rcc);                                                \
     } while (0)
 
-RedChannelClient *red_channel_client_create(int size, RedChannel *channel,
+RedChannelClient *red_channel_client_create(RedChannel *channel,
                                             RedClient *client, RedsStream *stream,
                                             int monitor_latency,
                                             int num_common_caps, uint32_t *common_caps,
                                             int num_caps, uint32_t *caps);
 
-RedChannelClient *red_channel_client_create_dummy(int size,
-                                                  RedChannel *channel,
-                                                  RedClient  *client,
-                                                  int num_common_caps, uint32_t *common_caps,
-                                                  int num_caps, uint32_t *caps);
-
-void red_channel_client_ref(RedChannelClient *rcc);
-void red_channel_client_unref(RedChannelClient *rcc);
-
-int red_channel_client_is_connected(RedChannelClient *rcc);
+gboolean red_channel_client_is_connected(RedChannelClient *rcc);
 void red_channel_client_default_migrate(RedChannelClient *rcc);
 int red_channel_client_is_waiting_for_migrate_data(RedChannelClient *rcc);
 void red_channel_client_destroy(RedChannelClient *rcc);
@@ -175,8 +192,6 @@ gboolean red_channel_client_set_migration_seamless(RedChannelClient *rcc);
 void red_channel_client_set_destroying(RedChannelClient *rcc);
 gboolean red_channel_client_is_destroying(RedChannelClient *rcc);
 
-#define RED_CHANNEL_CLIENT(Client) ((RedChannelClient *)(Client))
-
 typedef struct OutgoingHandler {
     OutgoingHandlerInterface *cb;
     void *opaque;
@@ -197,12 +212,48 @@ typedef struct IncomingHandler {
     uint32_t msg_pos;
 } IncomingHandler;
 
-struct RedChannelClient {
+struct RedChannelClient
+{
+    GObject parent;
+
     /* protected */
     OutgoingHandler outgoing;
     IncomingHandler incoming;
 
-    RedChannelClientPrivate priv[1];
+    RedChannelClientPrivate *priv;
+};
+
+struct RedChannelClientClass
+{
+    GObjectClass parent_class;
+
+    gboolean (*is_connected)(RedChannelClient *rcc);
+    void (*disconnect)(RedChannelClient *rcc);
+};
+
+#define SPICE_SERVER_ERROR spice_server_error_quark()
+GQuark spice_server_error_quark(void);
+
+typedef enum
+{
+    SPICE_SERVER_ERROR_FAILED
+} SpiceServerError;
+
+/* Messages handled by red_channel
+ * SET_ACK - sent to client on channel connection
+ * Note that the numbers don't have to correspond to spice message types,
+ * but we keep the 100 first allocated for base channel approach.
+ * */
+enum {
+    RED_PIPE_ITEM_TYPE_SET_ACK=1,
+    RED_PIPE_ITEM_TYPE_MIGRATE,
+    RED_PIPE_ITEM_TYPE_EMPTY_MSG,
+    RED_PIPE_ITEM_TYPE_PING,
+    RED_PIPE_ITEM_TYPE_MARKER,
+
+    RED_PIPE_ITEM_TYPE_CHANNEL_BASE=101,
 };
 
+G_END_DECLS
+
 #endif /* _H_RED_CHANNEL_CLIENT */
diff --git a/server/red-channel.h b/server/red-channel.h
index fc59dee..8818a04 100644
--- a/server/red-channel.h
+++ b/server/red-channel.h
@@ -27,26 +27,13 @@
 #include <common/ring.h>
 #include <common/marshaller.h>
 
+#include "demarshallers.h"
 #include "spice.h"
 #include "red-common.h"
-#include "demarshallers.h"
 #include "reds-stream.h"
 #include "stat.h"
 #include "red-pipe-item.h"
 
-#define MAX_SEND_BUFS 1000
-#define CLIENT_ACK_WINDOW 20
-
-#ifndef IOV_MAX
-#define IOV_MAX 1024
-#endif
-
-#define MAX_HEADER_SIZE sizeof(SpiceDataHeader)
-
-/* Basic interface for channels, without using the RedChannel interface.
-   The intention is to move towards one channel interface gradually.
-   At the final stage, this interface shouldn't be exposed. Only RedChannel will use it. */
-
 typedef struct SpiceDataHeaderOpaque SpiceDataHeaderOpaque;
 
 typedef uint16_t (*get_msg_type_proc)(SpiceDataHeaderOpaque *header);
@@ -104,7 +91,6 @@ typedef struct OutgoingHandlerInterface {
     on_outgoing_msg_done_proc on_msg_done;
     on_output_proc on_output;
 } OutgoingHandlerInterface;
-
 /* Red Channel interface */
 
 typedef struct RedChannel RedChannel;
@@ -112,21 +98,6 @@ typedef struct RedChannelClient RedChannelClient;
 typedef struct RedClient RedClient;
 typedef struct MainChannelClient MainChannelClient;
 
-/* Messages handled by red_channel
- * SET_ACK - sent to client on channel connection
- * Note that the numbers don't have to correspond to spice message types,
- * but we keep the 100 first allocated for base channel approach.
- * */
-enum {
-    RED_PIPE_ITEM_TYPE_SET_ACK=1,
-    RED_PIPE_ITEM_TYPE_MIGRATE,
-    RED_PIPE_ITEM_TYPE_EMPTY_MSG,
-    RED_PIPE_ITEM_TYPE_PING,
-    RED_PIPE_ITEM_TYPE_MARKER,
-
-    RED_PIPE_ITEM_TYPE_CHANNEL_BASE=101,
-};
-
 typedef uint8_t *(*channel_alloc_msg_recv_buf_proc)(RedChannelClient *channel,
                                                     uint16_t type, uint32_t size);
 typedef int (*channel_handle_parsed_proc)(RedChannelClient *rcc, uint32_t size, uint16_t type,
diff --git a/server/smartcard-channel-client.c b/server/smartcard-channel-client.c
index 0622be5..ec99e07 100644
--- a/server/smartcard-channel-client.c
+++ b/server/smartcard-channel-client.c
@@ -20,12 +20,123 @@
 
 #include "smartcard-channel-client.h"
 
+G_DEFINE_TYPE(SmartCardChannelClient, smart_card_channel_client, RED_TYPE_CHANNEL_CLIENT)
+
+#define SMARTCARD_CHANNEL_CLIENT_PRIVATE(o) \
+    (G_TYPE_INSTANCE_GET_PRIVATE((o), TYPE_SMARTCARD_CHANNEL_CLIENT, \
+                                 SmartCardChannelClientPrivate))
+
+struct SmartCardChannelClientPrivate
+{
+    RedCharDeviceSmartcard *smartcard;
+
+    /* read_from_client/write_to_device buffer.
+     * The beginning of the buffer should always be VSCMsgHeader*/
+    RedCharDeviceWriteBuffer *write_buf;
+    int msg_in_write_buf; /* was the client msg received into a RedCharDeviceWriteBuffer
+                           * or was it explicitly malloced */
+};
+
 typedef struct RedErrorItem {
     RedPipeItem base;
     VSCMsgHeader vheader;
     VSCMsgError  error;
 } RedErrorItem;
 
+static void smart_card_channel_client_get_property(GObject *object,
+                                                   guint property_id,
+                                                   GValue *value,
+                                                   GParamSpec *pspec)
+{
+    switch (property_id)
+    {
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void smart_card_channel_client_set_property(GObject *object,
+                                                   guint property_id,
+                                                   const GValue *value,
+                                                   GParamSpec *pspec)
+{
+    switch (property_id)
+    {
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+    }
+}
+
+static void smart_card_channel_client_dispose(GObject *object)
+{
+    G_OBJECT_CLASS(smart_card_channel_client_parent_class)->dispose(object);
+}
+
+static void smart_card_channel_client_finalize(GObject *object)
+{
+    SmartCardChannelClient *self = SMARTCARD_CHANNEL_CLIENT(object);
+
+    if (self->priv->smartcard)
+        g_object_remove_weak_pointer(G_OBJECT(self->priv->smartcard),
+                                     (gpointer*)&self->priv->smartcard);
+    G_OBJECT_CLASS(smart_card_channel_client_parent_class)->finalize(object);
+}
+
+static void smart_card_channel_client_class_init(SmartCardChannelClientClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS(klass);
+
+    g_type_class_add_private(klass, sizeof(SmartCardChannelClientPrivate));
+
+    object_class->get_property = smart_card_channel_client_get_property;
+    object_class->set_property = smart_card_channel_client_set_property;
+    object_class->dispose = smart_card_channel_client_dispose;
+    object_class->finalize = smart_card_channel_client_finalize;
+}
+
+static void
+smart_card_channel_client_init(SmartCardChannelClient *self)
+{
+    self->priv = SMARTCARD_CHANNEL_CLIENT_PRIVATE(self);
+}
+
+SmartCardChannelClient* smartcard_channel_client_create(RedChannel *channel,
+                                                        RedClient *client, RedsStream *stream,
+                                                        int monitor_latency,
+                                                        int num_common_caps, uint32_t *common_caps,
+                                                        int num_caps, uint32_t *caps)
+{
+    SmartCardChannelClient *rcc;
+    GArray *common_caps_array = NULL, *caps_array = NULL;
+
+    if (common_caps) {
+        common_caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*common_caps),
+                                              num_common_caps);
+        g_array_append_vals(common_caps_array, common_caps, num_common_caps);
+    }
+    if (caps) {
+        caps_array = g_array_sized_new(FALSE, FALSE, sizeof (*caps), num_caps);
+        g_array_append_vals(caps_array, caps, num_caps);
+    }
+
+    rcc = g_initable_new(RED_TYPE_CHANNEL_CLIENT,
+                         NULL, NULL,
+                         "channel", channel,
+                         "client", client,
+                         "stream", stream,
+                         "monitor-latency", monitor_latency,
+                         "caps", caps_array,
+                         "common-caps", common_caps_array,
+                         NULL);
+
+    if (caps_array)
+        g_array_unref(caps_array);
+    if (common_caps_array)
+        g_array_unref(common_caps_array);
+
+    return rcc;
+}
+
 uint8_t *smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
                                                     uint16_t type,
                                                     uint32_t size)
@@ -290,7 +401,14 @@ void smartcard_channel_client_set_char_device(SmartCardChannelClient *scc,
         return;
     }
 
+    if (scc->priv->smartcard) {
+        g_object_remove_weak_pointer(G_OBJECT(scc->priv->smartcard),
+                                     (gpointer*)&scc->priv->smartcard);
+    }
+
     scc->priv->smartcard = device;
+    g_object_add_weak_pointer(G_OBJECT(scc->priv->smartcard),
+                              (gpointer*)&scc->priv->smartcard);
 }
 
 RedCharDeviceSmartcard* smartcard_channel_client_get_char_device(SmartCardChannelClient *scc)
diff --git a/server/smartcard-channel-client.h b/server/smartcard-channel-client.h
index 44a966a..4fe1c72 100644
--- a/server/smartcard-channel-client.h
+++ b/server/smartcard-channel-client.h
@@ -18,27 +18,42 @@
 #ifndef SMARTCARD_CHANNEL_CLIENT_H__
 #define SMARTCARD_CHANNEL_CLIENT_H__
 
+#include <glib-object.h>
+
 #include "smartcard.h"
-#include "red-channel-client.h"
 
+G_BEGIN_DECLS
+
+#define TYPE_SMARTCARD_CHANNEL_CLIENT smart_card_channel_client_get_type()
+
+#define SMARTCARD_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST((obj), TYPE_SMARTCARD_CHANNEL_CLIENT, SmartCardChannelClient))
+#define SMARTCARD_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST((klass), TYPE_SMARTCARD_CHANNEL_CLIENT, SmartCardChannelClientClass))
+#define IS_SMARTCARD_CHANNEL_CLIENT(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE((obj), TYPE_SMARTCARD_CHANNEL_CLIENT))
+#define IS_SMARTCARD_CHANNEL_CLIENT_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE((klass), TYPE_SMARTCARD_CHANNEL_CLIENT))
+#define SMARTCARD_CHANNEL_CLIENT_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_SMARTCARD_CHANNEL_CLIENT, SmartCardChannelClientClass))
+
+typedef struct SmartCardChannelClient SmartCardChannelClient;
+typedef struct SmartCardChannelClientClass SmartCardChannelClientClass;
 typedef struct SmartCardChannelClientPrivate SmartCardChannelClientPrivate;
-struct SmartCardChannelClientPrivate {
-    RedCharDeviceSmartcard *smartcard;
-
-    /* read_from_client/write_to_device buffer.
-     * The beginning of the buffer should always be VSCMsgHeader*/
-    RedCharDeviceWriteBuffer *write_buf;
-    int msg_in_write_buf; /* was the client msg received into a RedCharDeviceWriteBuffer
-                           * or was it explicitly malloced */
-};
 
-typedef struct SmartCardChannelClient {
-    RedChannelClient base;
+struct SmartCardChannelClient
+{
+    RedChannelClient parent;
 
-    SmartCardChannelClientPrivate priv[1];
-} SmartCardChannelClient;
+    SmartCardChannelClientPrivate *priv;
+};
+
+struct SmartCardChannelClientClass
+{
+    RedChannelClientClass parent_class;
+};
 
-#define SMARTCARD_CHANNEL_CLIENT(rcc) ((SmartCardChannelClient*)rcc)
+GType smart_card_channel_client_get_type(void) G_GNUC_CONST;
 
 SmartCardChannelClient* smartcard_channel_client_create(RedChannel *channel,
                                                         RedClient *client, RedsStream *stream,
@@ -93,4 +108,6 @@ uint8_t *smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
                                                     uint16_t type,
                                                     uint32_t size);
 
+G_END_DECLS
+
 #endif /* SMARTCARD_CHANNEL_CLIENT_H__ */
diff --git a/server/smartcard.c b/server/smartcard.c
index ab95260..13eed80 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -500,13 +500,12 @@ static void smartcard_connect_client(RedChannel *channel, RedClient *client,
 
     SmartCardChannelClient *scc;
 
-    scc = SMARTCARD_CHANNEL_CLIENT(red_channel_client_create(sizeof(SmartCardChannelClient),
-                                                             channel,
-                                                             client,
-                                                             stream,
-                                                             FALSE,
-                                                             num_common_caps, common_caps,
-                                                             num_caps, caps));
+    scc = smartcard_channel_client_create(channel,
+                                          client,
+                                          stream,
+                                          FALSE,
+                                          num_common_caps, common_caps,
+                                          num_caps, caps);
 
     if (!scc) {
         return;
@@ -564,9 +563,6 @@ red_char_device_smartcard_finalize(GObject *object)
     RedCharDeviceSmartcard *self = RED_CHAR_DEVICE_SMARTCARD(object);
 
     free(self->priv->buf);
-    if (self->priv->scc) {
-        smartcard_channel_client_set_char_device(self->priv->scc, NULL);
-    }
 
     G_OBJECT_CLASS(red_char_device_smartcard_parent_class)->finalize(object);
 }
diff --git a/server/sound.c b/server/sound.c
index c08bb35..4edf8ed 100644
--- a/server/sound.c
+++ b/server/sound.c
@@ -31,6 +31,7 @@
 
 #include "spice.h"
 #include "red-common.h"
+#include "dummy-channel-client.h"
 #include "main-channel.h"
 #include "reds.h"
 #include "red-qxl.h"
@@ -976,11 +977,9 @@ static SndChannel *__new_channel(SndWorker *worker, int size, uint32_t channel_i
     channel->on_message_done = on_message_done;
     channel->cleanup = cleanup;
 
-    channel->channel_client = red_channel_client_create_dummy(sizeof(RedChannelClient),
-                                                              worker->base_channel,
-                                                              client,
-                                                              num_common_caps, common_caps,
-                                                              num_caps, caps);
+    channel->channel_client =
+        dummy_channel_client_create(worker->base_channel, client,
+                                    num_common_caps, common_caps, num_caps, caps);
     if (!channel->channel_client) {
         goto error2;
     }
diff --git a/server/spice-server.h b/server/spice-server.h
index a5c7a95..5f572f4 100644
--- a/server/spice-server.h
+++ b/server/spice-server.h
@@ -72,6 +72,22 @@ int spice_server_add_interface(SpiceServer *s,
  */
 int spice_server_remove_interface(SpiceBaseInstance *sin);
 
+/* XXX This definition is here only to make glib generation
+ * of enumerators possible
+ */
+#if 0
+typedef enum {
+    SPICE_IMAGE_COMPRESSION_INVALID  = 0,
+    SPICE_IMAGE_COMPRESSION_OFF      = 1,
+    SPICE_IMAGE_COMPRESSION_AUTO_GLZ = 2,
+    SPICE_IMAGE_COMPRESSION_AUTO_LZ  = 3,
+    SPICE_IMAGE_COMPRESSION_QUIC     = 4,
+    SPICE_IMAGE_COMPRESSION_GLZ      = 5,
+    SPICE_IMAGE_COMPRESSION_LZ       = 6,
+    SPICE_IMAGE_COMPRESSION_LZ4      = 7,
+} spice_image_compression_t;
+#endif
+
 // Needed for backward API compatibility
 typedef SpiceImageCompression spice_image_compression_t;
 #define SPICE_IMAGE_COMPRESS_INVALID SPICE_IMAGE_COMPRESSION_INVALID
diff --git a/server/spicevmc.c b/server/spicevmc.c
index 3533f3f..e710111 100644
--- a/server/spicevmc.c
+++ b/server/spicevmc.c
@@ -577,10 +577,8 @@ static void spicevmc_connect(RedChannel *channel, RedClient *client,
         return;
     }
 
-    rcc = red_channel_client_create(sizeof(RedChannelClient), channel, client, stream,
-                                    FALSE,
-                                    num_common_caps, common_caps,
-                                    num_caps, caps);
+    rcc = red_channel_client_create(channel, client, stream, FALSE,
+                                    num_common_caps, common_caps, num_caps, caps);
     if (!rcc) {
         return;
     }
commit 32aa710d22ecf0a20cae27e728c927fcf4984c9f
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Thu Sep 22 12:44:23 2016 +0100

    Split RedCharDeviceSmartcard and SmartCardChannelClient

diff --git a/server/Makefile.am b/server/Makefile.am
index 9c5b3b6..abbec16 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -172,6 +172,8 @@ if HAVE_SMARTCARD
 libserver_la_SOURCES +=	\
 	smartcard.c		\
 	smartcard.h		\
+	smartcard-channel-client.c	\
+	smartcard-channel-client.h	\
 	$(NULL)
 endif
 
diff --git a/server/smartcard-channel-client.c b/server/smartcard-channel-client.c
new file mode 100644
index 0000000..0622be5
--- /dev/null
+++ b/server/smartcard-channel-client.c
@@ -0,0 +1,300 @@
+/*
+    Copyright (C) 2009-2015 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "smartcard-channel-client.h"
+
+typedef struct RedErrorItem {
+    RedPipeItem base;
+    VSCMsgHeader vheader;
+    VSCMsgError  error;
+} RedErrorItem;
+
+uint8_t *smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
+                                                    uint16_t type,
+                                                    uint32_t size)
+{
+    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
+    RedClient *client = red_channel_client_get_client(rcc);
+
+    /* todo: only one reader is actually supported. When we fix the code to support
+     * multiple readers, we will porbably associate different devices to
+     * differenc channels */
+    if (!scc->priv->smartcard) {
+        scc->priv->msg_in_write_buf = FALSE;
+        return spice_malloc(size);
+    } else {
+        RedCharDeviceSmartcard *smartcard;
+
+        spice_assert(smartcard_get_n_readers() == 1);
+        smartcard = scc->priv->smartcard;
+        spice_assert(smartcard_char_device_get_client(smartcard) || scc->priv->smartcard);
+        spice_assert(!scc->priv->write_buf);
+        scc->priv->write_buf =
+            red_char_device_write_buffer_get(RED_CHAR_DEVICE(smartcard), client,
+                                             size);
+
+        if (!scc->priv->write_buf) {
+            spice_error("failed to allocate write buffer");
+            return NULL;
+        }
+        scc->priv->msg_in_write_buf = TRUE;
+        return scc->priv->write_buf->buf;
+    }
+}
+
+void smartcard_channel_client_release_msg_rcv_buf(RedChannelClient *rcc,
+                                                  uint16_t type,
+                                                  uint32_t size,
+                                                  uint8_t *msg)
+{
+    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
+
+    /* todo: only one reader is actually supported. When we fix the code to support
+     * multiple readers, we will porbably associate different devices to
+     * differenc channels */
+
+    if (!scc->priv->msg_in_write_buf) {
+        spice_assert(!scc->priv->write_buf);
+        free(msg);
+    } else {
+        if (scc->priv->write_buf) { /* msg hasn't been pushed to the guest */
+            spice_assert(scc->priv->write_buf->buf == msg);
+            red_char_device_write_buffer_release(RED_CHAR_DEVICE(scc->priv->smartcard),
+                                                 &scc->priv->write_buf);
+        }
+    }
+}
+
+void smartcard_channel_client_on_disconnect(RedChannelClient *rcc)
+{
+    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
+    RedCharDeviceSmartcard *device = scc->priv->smartcard;
+
+    if (device) {
+        smartcard_char_device_detach_client(device, scc);
+        smartcard_char_device_notify_reader_remove(device);
+    }
+}
+
+void smartcard_channel_client_send_data(RedChannelClient *rcc,
+                                        SpiceMarshaller *m,
+                                        RedPipeItem *item,
+                                        VSCMsgHeader *vheader)
+{
+    spice_assert(rcc);
+    spice_assert(vheader);
+    red_channel_client_init_send_data(rcc, SPICE_MSG_SMARTCARD_DATA, item);
+    spice_marshaller_add_ref(m, (uint8_t*)vheader, sizeof(VSCMsgHeader));
+    if (vheader->length > 0) {
+        spice_marshaller_add_ref(m, (uint8_t*)(vheader+1), vheader->length);
+    }
+}
+
+void smartcard_channel_client_send_error(RedChannelClient *rcc, SpiceMarshaller *m, RedPipeItem *item)
+{
+    RedErrorItem* error_item = SPICE_UPCAST(RedErrorItem, item);
+
+    smartcard_channel_client_send_data(rcc, m, item, &error_item->vheader);
+}
+
+static void smartcard_channel_client_push_error(RedChannelClient *rcc,
+                                                uint32_t reader_id,
+                                                VSCErrorCode error)
+{
+    RedErrorItem *error_item = spice_new0(RedErrorItem, 1);
+
+    red_pipe_item_init(&error_item->base, RED_PIPE_ITEM_TYPE_ERROR);
+
+    error_item->vheader.reader_id = reader_id;
+    error_item->vheader.type = VSC_Error;
+    error_item->vheader.length = sizeof(error_item->error);
+    error_item->error.code = error;
+    red_channel_client_pipe_add_push(rcc, &error_item->base);
+}
+
+static void smartcard_channel_client_add_reader(SmartCardChannelClient *scc,
+                                                uint8_t *name)
+{
+    if (!scc->priv->smartcard) { /* we already tried to attach a reader to the client
+                                          when it connected */
+        SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
+
+        if (!char_device) {
+            smartcard_channel_client_push_error(RED_CHANNEL_CLIENT(scc),
+                                                VSCARD_UNDEFINED_READER_ID,
+                                                VSC_CANNOT_ADD_MORE_READERS);
+            return;
+        }
+        smartcard_char_device_attach_client(char_device, scc);
+    }
+    smartcard_char_device_notify_reader_add(scc->priv->smartcard);
+    // The device sends a VSC_Error message, we will let it through, no
+    // need to send our own. We already set the correct reader_id, from
+    // our RedCharDeviceSmartcard.
+}
+
+static void smartcard_channel_client_remove_reader(SmartCardChannelClient *scc,
+                                                   uint32_t reader_id)
+{
+    SpiceCharDeviceInstance *char_device = smartcard_readers_get(reader_id);
+    RedCharDeviceSmartcard *dev;
+
+    if (char_device == NULL) {
+        smartcard_channel_client_push_error(RED_CHANNEL_CLIENT(scc),
+                                            reader_id, VSC_GENERAL_ERROR);
+        return;
+    }
+
+    dev = red_char_device_opaque_get(char_device->st);
+    spice_assert(scc->priv->smartcard == dev);
+    if (!smartcard_char_device_notify_reader_remove(dev)) {
+        smartcard_channel_client_push_error(RED_CHANNEL_CLIENT(scc),
+                                            reader_id, VSC_GENERAL_ERROR);
+        return;
+    }
+}
+
+RedCharDeviceSmartcard* smartcard_channel_client_get_device(SmartCardChannelClient *scc)
+{
+    return scc->priv->smartcard;
+}
+
+static void smartcard_channel_client_write_to_reader(SmartCardChannelClient *scc)
+{
+    g_return_if_fail(scc);
+
+    smartcard_channel_write_to_reader(scc->priv->write_buf);
+    scc->priv->write_buf = NULL;
+}
+
+
+int smartcard_channel_client_handle_message(RedChannelClient *rcc,
+                                            uint16_t type,
+                                            uint32_t size,
+                                            uint8_t *msg)
+{
+    VSCMsgHeader* vheader = (VSCMsgHeader*)msg;
+    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
+
+    if (type != SPICE_MSGC_SMARTCARD_DATA) {
+        /* Handles seamless migration protocol. Also handles ack's,
+         * spicy sends them while spicec does not */
+        return red_channel_client_handle_message(rcc, size, type, msg);
+    }
+
+    spice_assert(size == vheader->length + sizeof(VSCMsgHeader));
+    switch (vheader->type) {
+        case VSC_ReaderAdd:
+            smartcard_channel_client_add_reader(scc, msg + sizeof(VSCMsgHeader));
+            return TRUE;
+            break;
+        case VSC_ReaderRemove:
+            smartcard_channel_client_remove_reader(scc, vheader->reader_id);
+            return TRUE;
+            break;
+        case VSC_Init:
+            // ignore - we should never get this anyway
+            return TRUE;
+            break;
+        case VSC_Error:
+        case VSC_ATR:
+        case VSC_CardRemove:
+        case VSC_APDU:
+            break; // passed on to device
+        default:
+            printf("ERROR: unexpected message on smartcard channel\n");
+            return TRUE;
+    }
+
+    /* todo: fix */
+    if (vheader->reader_id >= smartcard_get_n_readers()) {
+        spice_printerr("ERROR: received message for non existing reader: %d, %d, %d", vheader->reader_id,
+                       vheader->type, vheader->length);
+        return FALSE;
+    }
+    spice_assert(scc->priv->write_buf->buf == msg);
+    smartcard_channel_client_write_to_reader(scc);
+
+    return TRUE;
+}
+
+int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
+                                                 uint32_t size,
+                                                 void *message)
+{
+    SmartCardChannelClient *scc;
+    SpiceMigrateDataHeader *header;
+    SpiceMigrateDataSmartcard *mig_data;
+
+    scc = SMARTCARD_CHANNEL_CLIENT(rcc);
+    header = (SpiceMigrateDataHeader *)message;
+    mig_data = (SpiceMigrateDataSmartcard *)(header + 1);
+    if (size < sizeof(SpiceMigrateDataHeader) + sizeof(SpiceMigrateDataSmartcard)) {
+        spice_error("bad message size");
+        return FALSE;
+    }
+    if (!migration_protocol_validate_header(header,
+                                            SPICE_MIGRATE_DATA_SMARTCARD_MAGIC,
+                                            SPICE_MIGRATE_DATA_SMARTCARD_VERSION)) {
+        spice_error("bad header");
+        return FALSE;
+    }
+
+    if (!mig_data->base.connected) { /* client wasn't attached to a smartcard */
+        return TRUE;
+    }
+
+    if (!scc->priv->smartcard) {
+        SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
+
+        if (!char_device) {
+            spice_warning("no unattached device available");
+            return TRUE;
+        } else {
+            smartcard_char_device_attach_client(char_device, scc);
+        }
+    }
+    spice_debug("reader added %d partial read_size %u", mig_data->reader_added, mig_data->read_size);
+
+    return smartcard_char_device_handle_migrate_data(scc->priv->smartcard,
+                                                     mig_data);
+}
+
+int smartcard_channel_client_handle_migrate_flush_mark(RedChannelClient *rcc)
+{
+    red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_SMARTCARD_MIGRATE_DATA);
+    return TRUE;
+}
+
+void smartcard_channel_client_set_char_device(SmartCardChannelClient *scc,
+                                              RedCharDeviceSmartcard *device)
+{
+    if (device == scc->priv->smartcard) {
+        return;
+    }
+
+    scc->priv->smartcard = device;
+}
+
+RedCharDeviceSmartcard* smartcard_channel_client_get_char_device(SmartCardChannelClient *scc)
+{
+    return scc->priv->smartcard;
+}
+
diff --git a/server/smartcard-channel-client.h b/server/smartcard-channel-client.h
new file mode 100644
index 0000000..44a966a
--- /dev/null
+++ b/server/smartcard-channel-client.h
@@ -0,0 +1,96 @@
+/*
+    Copyright (C) 2009-2015 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef SMARTCARD_CHANNEL_CLIENT_H__
+#define SMARTCARD_CHANNEL_CLIENT_H__
+
+#include "smartcard.h"
+#include "red-channel-client.h"
+
+typedef struct SmartCardChannelClientPrivate SmartCardChannelClientPrivate;
+struct SmartCardChannelClientPrivate {
+    RedCharDeviceSmartcard *smartcard;
+
+    /* read_from_client/write_to_device buffer.
+     * The beginning of the buffer should always be VSCMsgHeader*/
+    RedCharDeviceWriteBuffer *write_buf;
+    int msg_in_write_buf; /* was the client msg received into a RedCharDeviceWriteBuffer
+                           * or was it explicitly malloced */
+};
+
+typedef struct SmartCardChannelClient {
+    RedChannelClient base;
+
+    SmartCardChannelClientPrivate priv[1];
+} SmartCardChannelClient;
+
+#define SMARTCARD_CHANNEL_CLIENT(rcc) ((SmartCardChannelClient*)rcc)
+
+SmartCardChannelClient* smartcard_channel_client_create(RedChannel *channel,
+                                                        RedClient *client, RedsStream *stream,
+                                                        int monitor_latency,
+                                                        int num_common_caps, uint32_t *common_caps,
+                                                        int num_caps, uint32_t *caps);
+
+uint8_t* smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
+                                                    uint16_t type,
+                                                    uint32_t size);
+
+void smartcard_channel_client_release_msg_rcv_buf(RedChannelClient *rcc,
+                                                  uint16_t type,
+                                                  uint32_t size,
+                                                  uint8_t *msg);
+
+int smartcard_channel_client_handle_migrate_flush_mark(RedChannelClient *rcc);
+
+void smartcard_channel_client_on_disconnect(RedChannelClient *rcc);
+
+void smartcard_channel_client_send_data(RedChannelClient *rcc,
+                                        SpiceMarshaller *m,
+                                        RedPipeItem *item,
+                                        VSCMsgHeader *vheader);
+
+void smartcard_channel_client_send_error(RedChannelClient *rcc,
+                                         SpiceMarshaller *m,
+                                         RedPipeItem *item);
+
+RedCharDeviceSmartcard* smartcard_channel_client_get_device(SmartCardChannelClient *scc);
+
+int smartcard_channel_client_handle_message(RedChannelClient *rcc,
+                                            uint16_t type,
+                                            uint32_t size,
+                                            uint8_t *msg);
+
+int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
+                                                 uint32_t size,
+                                                 void *message);
+
+void smartcard_channel_client_set_char_device(SmartCardChannelClient *scc,
+                                              RedCharDeviceSmartcard *device);
+
+RedCharDeviceSmartcard* smartcard_channel_client_get_char_device(SmartCardChannelClient *scc);
+
+void smartcard_channel_client_release_msg_rcv_buf(RedChannelClient *rcc,
+                                                  uint16_t type,
+                                                  uint32_t size,
+                                                  uint8_t *msg);
+
+uint8_t *smartcard_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc,
+                                                    uint16_t type,
+                                                    uint32_t size);
+
+#endif /* SMARTCARD_CHANNEL_CLIENT_H__ */
diff --git a/server/smartcard.c b/server/smartcard.c
index 41dc106..ab95260 100644
--- a/server/smartcard.c
+++ b/server/smartcard.c
@@ -30,8 +30,8 @@
 
 #include "reds.h"
 #include "char-device.h"
-#include "red-channel-client.h"
 #include "smartcard.h"
+#include "smartcard-channel-client.h"
 #include "migration-protocol.h"
 
 /*
@@ -49,25 +49,6 @@
 // Maximal length of APDU
 #define APDUBufSize 270
 
-typedef struct SmartCardChannelClientPrivate SmartCardChannelClientPrivate;
-struct SmartCardChannelClientPrivate {
-    RedCharDeviceSmartcard *smartcard;
-
-    /* read_from_client/write_to_device buffer.
-     * The beginning of the buffer should always be VSCMsgHeader*/
-    RedCharDeviceWriteBuffer *write_buf;
-    int msg_in_write_buf; /* was the client msg received into a RedCharDeviceWriteBuffer
-                           * or was it explicitly malloced */
-};
-
-typedef struct SmartCardChannelClient {
-    RedChannelClient base;
-
-    SmartCardChannelClientPrivate priv[1];
-} SmartCardChannelClient;
-
-#define SMARTCARD_CHANNEL_CLIENT(rcc) ((SmartCardChannelClient*)rcc)
-
 G_DEFINE_TYPE(RedCharDeviceSmartcard, red_char_device_smartcard, RED_TYPE_CHAR_DEVICE)
 
 #define RED_CHAR_DEVICE_SMARTCARD_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RED_TYPE_CHAR_DEVICE_SMARTCARD, RedCharDeviceSmartcardPrivate))
@@ -84,18 +65,6 @@ struct RedCharDeviceSmartcardPrivate {
     int                  reader_added; // has reader_add been sent to the device
 };
 
-enum {
-    RED_PIPE_ITEM_TYPE_ERROR = RED_PIPE_ITEM_TYPE_CHANNEL_BASE,
-    RED_PIPE_ITEM_TYPE_SMARTCARD_DATA,
-    RED_PIPE_ITEM_TYPE_SMARTCARD_MIGRATE_DATA,
-};
-
-typedef struct RedErrorItem {
-    RedPipeItem base;
-    VSCMsgHeader vheader;
-    VSCMsgError  error;
-} RedErrorItem;
-
 typedef struct RedMsgItem {
     RedPipeItem base;
 
@@ -114,12 +83,7 @@ static struct Readers {
     SpiceCharDeviceInstance* sin[SMARTCARD_MAX_READERS];
 } g_smartcard_readers = {0, {NULL}};
 
-static SpiceCharDeviceInstance* smartcard_readers_get_unattached(void);
-static SpiceCharDeviceInstance* smartcard_readers_get(uint32_t reader_id);
 static int smartcard_char_device_add_to_readers(RedsState *reds, SpiceCharDeviceInstance *sin);
-static void smartcard_char_device_attach_client(
-    SpiceCharDeviceInstance *char_device, SmartCardChannelClient *scc);
-static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf);
 
 static RedMsgItem *smartcard_char_device_on_message_from_device(
     RedCharDeviceSmartcard *dev, VSCMsgHeader *header);
@@ -179,11 +143,12 @@ static void smartcard_send_msg_to_client(RedPipeItem *msg,
                                          void *opaque)
 {
     RedCharDeviceSmartcard *dev = opaque;
+    RedChannelClient *rcc = RED_CHANNEL_CLIENT(dev->priv->scc);
 
     spice_assert(dev->priv->scc &&
-                 red_channel_client_get_client(&dev->priv->scc->base) == client);
+                 red_channel_client_get_client(rcc) == client);
     red_pipe_item_ref(msg);
-    smartcard_channel_client_pipe_add_push(&dev->priv->scc->base, msg);
+    smartcard_channel_client_pipe_add_push(rcc, msg);
 }
 
 static void smartcard_send_tokens_to_client(RedClient *client, uint32_t tokens, void *opaque)
@@ -194,11 +159,12 @@ static void smartcard_send_tokens_to_client(RedClient *client, uint32_t tokens,
 static void smartcard_remove_client(RedClient *client, void *opaque)
 {
     RedCharDeviceSmartcard *dev = opaque;
+    RedChannelClient *rcc = RED_CHANNEL_CLIENT(dev->priv->scc);
 
     spice_printerr("smartcard  dev %p, client %p", dev, client);
     spice_assert(dev->priv->scc &&
-                 red_channel_client_get_client(&dev->priv->scc->base) == client);
-    red_channel_client_shutdown(&dev->priv->scc->base);
+                 red_channel_client_get_client(rcc) == client);
+    red_channel_client_shutdown(rcc);
 }
 
 RedMsgItem *smartcard_char_device_on_message_from_device(RedCharDeviceSmartcard *dev,
@@ -225,7 +191,8 @@ RedMsgItem *smartcard_char_device_on_message_from_device(RedCharDeviceSmartcard
         /* We patch the reader_id, since the device only knows about itself, and
          * we know about the sum of readers. */
         sent_header->reader_id = dev->priv->reader_id;
-        return smartcard_get_vsc_msg_item(&dev->priv->scc->base, sent_header);
+        return smartcard_get_vsc_msg_item(RED_CHANNEL_CLIENT(dev->priv->scc),
+                                          sent_header);
     }
     return NULL;
 }
@@ -243,7 +210,7 @@ static int smartcard_char_device_add_to_readers(RedsState *reds, SpiceCharDevice
     return 0;
 }
 
-static SpiceCharDeviceInstance *smartcard_readers_get(uint32_t reader_id)
+SpiceCharDeviceInstance *smartcard_readers_get(uint32_t reader_id)
 {
     spice_assert(reader_id < g_smartcard_readers.num);
     return g_smartcard_readers.sin[reader_id];
@@ -251,7 +218,7 @@ static SpiceCharDeviceInstance *smartcard_readers_get(uint32_t reader_id)
 
 /* TODO: fix implementation for multiple readers. Each reader should have a separated
  * channel */
-static SpiceCharDeviceInstance *smartcard_readers_get_unattached(void)
+SpiceCharDeviceInstance *smartcard_readers_get_unattached(void)
 {
     int i;
     RedCharDeviceSmartcard* dev;
@@ -300,7 +267,7 @@ RedCharDevice *smartcard_device_connect(RedsState *reds, SpiceCharDeviceInstance
     return RED_CHAR_DEVICE(dev);
 }
 
-static void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
+void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
 {
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
@@ -318,15 +285,15 @@ static void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *dev)
     smartcard_channel_write_to_reader(write_buf);
 }
 
-static void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_device,
-                                                SmartCardChannelClient *scc)
+void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_device,
+                                         SmartCardChannelClient *scc)
 {
     RedCharDeviceSmartcard *dev = red_char_device_opaque_get(char_device->st);
     int client_added;
 
-    spice_assert(!scc->priv->smartcard && !dev->priv->scc);
+    spice_assert(!smartcard_channel_client_get_char_device(scc) && !dev->priv->scc);
     dev->priv->scc = scc;
-    scc->priv->smartcard = dev;
+    smartcard_channel_client_set_char_device(scc, dev);
     client_added = red_char_device_client_add(RED_CHAR_DEVICE(dev),
                                               red_channel_client_get_client(RED_CHANNEL_CLIENT(scc)),
                                               FALSE, /* no flow control yet */
@@ -338,24 +305,24 @@ static void smartcard_char_device_attach_client(SpiceCharDeviceInstance *char_de
     if (!client_added) {
         spice_warning("failed");
         dev->priv->scc = NULL;
-        scc->priv->smartcard = NULL;
+        smartcard_channel_client_set_char_device(scc, NULL);
         red_channel_client_disconnect(RED_CHANNEL_CLIENT(scc));
     }
 }
 
-static void smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *dev)
+gboolean smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *dev)
 {
     RedCharDeviceWriteBuffer *write_buf;
     VSCMsgHeader *vheader;
 
     if (!dev->priv->reader_added) {
         spice_debug("reader add was never sent to the device");
-        return;
+        return FALSE;
     }
     write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev), NULL, sizeof(*vheader));
     if (!write_buf) {
         spice_error("failed to allocate write buffer");
-        return;
+        return FALSE;
     }
     dev->priv->reader_added = FALSE;
     vheader = (VSCMsgHeader *)write_buf->buf;
@@ -363,21 +330,18 @@ static void smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *d
     vheader->reader_id = dev->priv->reader_id;
     vheader->length = 0;
     smartcard_channel_write_to_reader(write_buf);
+
+    return TRUE;
 }
 
-static void smartcard_char_device_detach_client(SmartCardChannelClient *scc)
+void smartcard_char_device_detach_client(RedCharDeviceSmartcard *smartcard,
+                                         SmartCardChannelClient *scc)
 {
-    RedCharDeviceSmartcard *dev;
-
-    if (!scc->priv->smartcard) {
-        return;
-    }
-    dev = scc->priv->smartcard;
-    spice_assert(dev->priv->scc == scc);
-    red_char_device_client_remove(RED_CHAR_DEVICE(dev),
+    spice_assert(smartcard->priv->scc == scc);
+    red_char_device_client_remove(RED_CHAR_DEVICE(smartcard),
                                   red_channel_client_get_client(RED_CHANNEL_CLIENT(scc)));
-    scc->priv->smartcard = NULL;
-    dev->priv->scc = NULL;
+    smartcard_channel_client_set_char_device(scc, NULL);
+    smartcard->priv->scc = NULL;
 }
 
 static int smartcard_channel_client_config_socket(RedChannelClient *rcc)
@@ -385,78 +349,9 @@ static int smartcard_channel_client_config_socket(RedChannelClient *rcc)
     return TRUE;
 }
 
-static uint8_t *smartcard_channel_alloc_msg_rcv_buf(RedChannelClient *rcc,
-                                                    uint16_t type,
-                                                    uint32_t size)
+SmartCardChannelClient* smartcard_char_device_get_client(RedCharDeviceSmartcard *smartcard)
 {
-    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-
-    /* todo: only one reader is actually supported. When we fix the code to support
-     * multiple readers, we will porbably associate different devices to
-     * differenc channels */
-    if (!scc->priv->smartcard) {
-        scc->priv->msg_in_write_buf = FALSE;
-        return spice_malloc(size);
-    } else {
-        RedCharDeviceSmartcard *dev;
-
-        spice_assert(g_smartcard_readers.num == 1);
-        dev = scc->priv->smartcard;
-        spice_assert(dev->priv->scc || scc->priv->smartcard);
-        spice_assert(!scc->priv->write_buf);
-        scc->priv->write_buf = red_char_device_write_buffer_get(RED_CHAR_DEVICE(dev),
-                                                                red_channel_client_get_client(rcc),
-                                                                size);
-
-        if (!scc->priv->write_buf) {
-            spice_error("failed to allocate write buffer");
-            return NULL;
-        }
-        scc->priv->msg_in_write_buf = TRUE;
-        return scc->priv->write_buf->buf;
-    }
-}
-
-static void smartcard_channel_release_msg_rcv_buf(RedChannelClient *rcc,
-                                                  uint16_t type,
-                                                  uint32_t size,
-                                                  uint8_t *msg)
-{
-    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-
-    /* todo: only one reader is actually supported. When we fix the code to support
-     * multiple readers, we will porbably associate different devices to
-     * differenc channels */
-
-    if (!scc->priv->msg_in_write_buf) {
-        spice_assert(!scc->priv->write_buf);
-        free(msg);
-    } else {
-        if (scc->priv->write_buf) { /* msg hasn't been pushed to the guest */
-            spice_assert(scc->priv->write_buf->buf == msg);
-            red_char_device_write_buffer_release(RED_CHAR_DEVICE(scc->priv->smartcard), &scc->priv->write_buf);
-        }
-    }
-}
-
-static void smartcard_channel_send_data(RedChannelClient *rcc, SpiceMarshaller *m,
-                                        RedPipeItem *item, VSCMsgHeader *vheader)
-{
-    spice_assert(rcc);
-    spice_assert(vheader);
-    red_channel_client_init_send_data(rcc, SPICE_MSG_SMARTCARD_DATA, item);
-    spice_marshaller_add_ref(m, (uint8_t*)vheader, sizeof(VSCMsgHeader));
-    if (vheader->length > 0) {
-        spice_marshaller_add_ref(m, (uint8_t*)(vheader+1), vheader->length);
-    }
-}
-
-static void smartcard_channel_send_error(
-    RedChannelClient *rcc, SpiceMarshaller *m, RedPipeItem *item)
-{
-    RedErrorItem* error_item = SPICE_UPCAST(RedErrorItem, item);
-
-    smartcard_channel_send_data(rcc, m, item, &error_item->vheader);
+    return smartcard->priv->scc;
 }
 
 static void smartcard_channel_send_msg(RedChannelClient *rcc,
@@ -464,7 +359,7 @@ static void smartcard_channel_send_msg(RedChannelClient *rcc,
 {
     RedMsgItem* msg_item = SPICE_UPCAST(RedMsgItem, item);
 
-    smartcard_channel_send_data(rcc, m, item, msg_item->vheader);
+    smartcard_channel_client_send_data(rcc, m, item, msg_item->vheader);
 }
 
 static void smartcard_channel_send_migrate_data(RedChannelClient *rcc,
@@ -475,7 +370,7 @@ static void smartcard_channel_send_migrate_data(RedChannelClient *rcc,
     SpiceMarshaller *m2;
 
     scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-    dev = scc->priv->smartcard;
+    dev = smartcard_channel_client_get_char_device(scc);
     red_channel_client_init_send_data(rcc, SPICE_MSG_MIGRATE_DATA, item);
     spice_marshaller_add_uint32(m, SPICE_MIGRATE_DATA_SMARTCARD_MAGIC);
     spice_marshaller_add_uint32(m, SPICE_MIGRATE_DATA_SMARTCARD_VERSION);
@@ -502,7 +397,7 @@ static void smartcard_channel_send_item(RedChannelClient *rcc, RedPipeItem *item
 
     switch (item->type) {
     case RED_PIPE_ITEM_TYPE_ERROR:
-        smartcard_channel_send_error(rcc, m, item);
+        smartcard_channel_client_send_error(rcc, m, item);
         break;
     case RED_PIPE_ITEM_TYPE_SMARTCARD_DATA:
         smartcard_channel_send_msg(rcc, m, item);
@@ -517,18 +412,6 @@ static void smartcard_channel_send_item(RedChannelClient *rcc, RedPipeItem *item
     red_channel_client_begin_send_message(rcc);
 }
 
-static void smartcard_channel_on_disconnect(RedChannelClient *rcc)
-{
-    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-
-    if (scc->priv->smartcard) {
-        RedCharDeviceSmartcard *dev = scc->priv->smartcard;
-
-        smartcard_char_device_detach_client(scc);
-        smartcard_char_device_notify_reader_remove(dev);
-    }
-}
-
 /* this is called from both device input and client input. since the device is
  * a usb device, the context is still the main thread (kvm_main_loop, timers)
  * so no mutex is required. */
@@ -538,19 +421,6 @@ static void smartcard_channel_client_pipe_add_push(RedChannelClient *rcc,
     red_channel_client_pipe_add_push(rcc, item);
 }
 
-static void smartcard_push_error(RedChannelClient *rcc, uint32_t reader_id, VSCErrorCode error)
-{
-    RedErrorItem *error_item = spice_new0(RedErrorItem, 1);
-
-    red_pipe_item_init(&error_item->base, RED_PIPE_ITEM_TYPE_ERROR);
-
-    error_item->vheader.reader_id = reader_id;
-    error_item->vheader.type = VSC_Error;
-    error_item->vheader.length = sizeof(error_item->error);
-    error_item->error.code = error;
-    smartcard_channel_client_pipe_add_push(rcc, &error_item->base);
-}
-
 static void smartcard_free_vsc_msg_item(RedPipeItem *base)
 {
     RedMsgItem *item = SPICE_UPCAST(RedMsgItem, base);
@@ -569,48 +439,7 @@ static RedMsgItem *smartcard_get_vsc_msg_item(RedChannelClient *rcc,
     return msg_item;
 }
 
-static void smartcard_remove_reader(SmartCardChannelClient *scc, uint32_t reader_id)
-{
-    SpiceCharDeviceInstance *char_device = smartcard_readers_get(reader_id);
-    RedCharDeviceSmartcard *dev;
-
-    if (char_device == NULL) {
-        smartcard_push_error(RED_CHANNEL_CLIENT(scc), reader_id,
-            VSC_GENERAL_ERROR);
-        return;
-    }
-
-    dev = red_char_device_opaque_get(char_device->st);
-    if (dev->priv->reader_added == FALSE) {
-        smartcard_push_error(RED_CHANNEL_CLIENT(scc), reader_id,
-            VSC_GENERAL_ERROR);
-        return;
-    }
-    spice_assert(scc->priv->smartcard == dev);
-    smartcard_char_device_notify_reader_remove(dev);
-}
-
-static void smartcard_add_reader(SmartCardChannelClient *scc, uint8_t *name)
-{
-    if (!scc->priv->smartcard) { /* we already tried to attach a reader to the client
-                                    when it connected */
-        SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
-
-        if (!char_device) {
-            smartcard_push_error(RED_CHANNEL_CLIENT(scc),
-                                 VSCARD_UNDEFINED_READER_ID,
-                                VSC_CANNOT_ADD_MORE_READERS);
-            return;
-        }
-        smartcard_char_device_attach_client(char_device, scc);
-    }
-    smartcard_char_device_notify_reader_add(scc->priv->smartcard);
-    // The device sends a VSC_Error message, we will let it through, no
-    // need to send our own. We already set the correct reader_id, from
-    // our RedCharDeviceSmartcard.
-}
-
-static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf)
+void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf)
 {
     SpiceCharDeviceInstance *sin;
     RedCharDeviceSmartcard *dev;
@@ -623,7 +452,8 @@ static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_bu
     spice_assert(vheader->reader_id <= g_smartcard_readers.num);
     sin = g_smartcard_readers.sin[vheader->reader_id];
     dev = (RedCharDeviceSmartcard *)red_char_device_opaque_get(sin->st);
-    spice_assert(!dev->priv->scc || dev == dev->priv->scc->priv->smartcard);
+    spice_assert(!dev->priv->scc ||
+                 dev == smartcard_channel_client_get_device(dev->priv->scc));
     /* protocol requires messages to be in network endianess */
     vheader->type = htonl(vheader->type);
     vheader->length = htonl(vheader->length);
@@ -632,15 +462,6 @@ static void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_bu
     /* pushing the buffer to the write queue; It will be released
      * when it will be fully consumed by the device */
     red_char_device_write_buffer_add(sin->st, write_buf);
-    if (dev->priv->scc && write_buf == dev->priv->scc->priv->write_buf) {
-        dev->priv->scc->priv->write_buf = NULL;
-    }
-}
-
-static int smartcard_channel_client_handle_migrate_flush_mark(RedChannelClient *rcc)
-{
-    red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_SMARTCARD_MIGRATE_DATA);
-    return TRUE;
 }
 
 static void smartcard_device_restore_partial_read(RedCharDeviceSmartcard *dev,
@@ -660,96 +481,13 @@ static void smartcard_device_restore_partial_read(RedCharDeviceSmartcard *dev,
     dev->priv->buf_pos = dev->priv->buf + mig_data->read_size;
 }
 
-static int smartcard_channel_client_handle_migrate_data(RedChannelClient *rcc,
-                                                        uint32_t size, void *message)
+int smartcard_char_device_handle_migrate_data(RedCharDeviceSmartcard *smartcard,
+                                              SpiceMigrateDataSmartcard *mig_data)
 {
-    SmartCardChannelClient *scc;
-    SpiceMigrateDataHeader *header;
-    SpiceMigrateDataSmartcard *mig_data;
+    smartcard->priv->reader_added = mig_data->reader_added;
 
-    scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-    header = (SpiceMigrateDataHeader *)message;
-    mig_data = (SpiceMigrateDataSmartcard *)(header + 1);
-    if (size < sizeof(SpiceMigrateDataHeader) + sizeof(SpiceMigrateDataSmartcard)) {
-        spice_error("bad message size");
-        return FALSE;
-    }
-    if (!migration_protocol_validate_header(header,
-                                            SPICE_MIGRATE_DATA_SMARTCARD_MAGIC,
-                                            SPICE_MIGRATE_DATA_SMARTCARD_VERSION)) {
-        spice_error("bad header");
-        return FALSE;
-    }
-
-    if (!mig_data->base.connected) { /* client wasn't attached to a smartcard */
-        return TRUE;
-    }
-
-    if (!scc->priv->smartcard) {
-        SpiceCharDeviceInstance *char_device = smartcard_readers_get_unattached();
-
-        if (!char_device) {
-            spice_warning("no unattached device available");
-            return TRUE;
-        } else {
-            smartcard_char_device_attach_client(char_device, scc);
-        }
-    }
-    spice_debug("reader added %d partial read_size %u", mig_data->reader_added, mig_data->read_size);
-    scc->priv->smartcard->priv->reader_added = mig_data->reader_added;
-
-    smartcard_device_restore_partial_read(scc->priv->smartcard, mig_data);
-    return red_char_device_restore(RED_CHAR_DEVICE(scc->priv->smartcard), &mig_data->base);
-}
-
-static int smartcard_channel_handle_message(RedChannelClient *rcc,
-                                            uint16_t type,
-                                            uint32_t size,
-                                            uint8_t *msg)
-{
-    VSCMsgHeader* vheader = (VSCMsgHeader*)msg;
-    SmartCardChannelClient *scc = SMARTCARD_CHANNEL_CLIENT(rcc);
-
-    if (type != SPICE_MSGC_SMARTCARD_DATA) {
-        /* Handles seamless migration protocol. Also handles ack's,
-         * spicy sends them while spicec does not */
-        return red_channel_client_handle_message(rcc, size, type, msg);
-    }
-
-    spice_assert(size == vheader->length + sizeof(VSCMsgHeader));
-    switch (vheader->type) {
-        case VSC_ReaderAdd:
-            smartcard_add_reader(scc, msg + sizeof(VSCMsgHeader));
-            return TRUE;
-            break;
-        case VSC_ReaderRemove:
-            smartcard_remove_reader(scc, vheader->reader_id);
-            return TRUE;
-            break;
-        case VSC_Init:
-            // ignore - we should never get this anyway
-            return TRUE;
-            break;
-        case VSC_Error:
-        case VSC_ATR:
-        case VSC_CardRemove:
-        case VSC_APDU:
-            break; // passed on to device
-        default:
-            printf("ERROR: unexpected message on smartcard channel\n");
-            return TRUE;
-    }
-
-    /* todo: fix */
-    if (vheader->reader_id >= g_smartcard_readers.num) {
-        spice_printerr("ERROR: received message for non existing reader: %d, %d, %d", vheader->reader_id,
-            vheader->type, vheader->length);
-        return FALSE;
-    }
-    spice_assert(scc->priv->write_buf->buf == msg);
-    smartcard_channel_write_to_reader(scc->priv->write_buf);
-
-    return TRUE;
+    smartcard_device_restore_partial_read(smartcard, mig_data);
+    return red_char_device_restore(RED_CHAR_DEVICE(smartcard), &mig_data->base);
 }
 
 static void smartcard_connect_client(RedChannel *channel, RedClient *client,
@@ -769,6 +507,7 @@ static void smartcard_connect_client(RedChannel *channel, RedClient *client,
                                                              FALSE,
                                                              num_common_caps, common_caps,
                                                              num_caps, caps));
+
     if (!scc) {
         return;
     }
@@ -792,10 +531,10 @@ static void smartcard_init(RedsState *reds)
     spice_assert(!g_smartcard_channel);
 
     channel_cbs.config_socket = smartcard_channel_client_config_socket;
-    channel_cbs.on_disconnect = smartcard_channel_on_disconnect;
+    channel_cbs.on_disconnect = smartcard_channel_client_on_disconnect;
     channel_cbs.send_item = smartcard_channel_send_item;
-    channel_cbs.alloc_recv_buf = smartcard_channel_alloc_msg_rcv_buf;
-    channel_cbs.release_recv_buf = smartcard_channel_release_msg_rcv_buf;
+    channel_cbs.alloc_recv_buf = smartcard_channel_client_alloc_msg_rcv_buf;
+    channel_cbs.release_recv_buf = smartcard_channel_client_release_msg_rcv_buf;
     channel_cbs.handle_migrate_flush_mark = smartcard_channel_client_handle_migrate_flush_mark;
     channel_cbs.handle_migrate_data = smartcard_channel_client_handle_migrate_data;
 
@@ -804,7 +543,7 @@ static void smartcard_init(RedsState *reds)
                                              reds_get_core_interface(reds),
                                              SPICE_CHANNEL_SMARTCARD, 0,
                                              FALSE /* handle_acks */,
-                                             smartcard_channel_handle_message,
+                                             smartcard_channel_client_handle_message,
                                              &channel_cbs,
                                              migration_flags);
 
@@ -826,7 +565,7 @@ red_char_device_smartcard_finalize(GObject *object)
 
     free(self->priv->buf);
     if (self->priv->scc) {
-        self->priv->scc->priv->smartcard = NULL;
+        smartcard_channel_client_set_char_device(self->priv->scc, NULL);
     }
 
     G_OBJECT_CLASS(red_char_device_smartcard_parent_class)->finalize(object);
@@ -858,3 +597,8 @@ red_char_device_smartcard_init(RedCharDeviceSmartcard *self)
     self->priv->buf = spice_malloc(self->priv->buf_size);
     self->priv->buf_pos = self->priv->buf;
 }
+
+uint32_t smartcard_get_n_readers(void)
+{
+    return g_smartcard_readers.num;
+}
diff --git a/server/smartcard.h b/server/smartcard.h
index 2d1356e..e3932c0 100644
--- a/server/smartcard.h
+++ b/server/smartcard.h
@@ -21,6 +21,7 @@
 #include <glib-object.h>
 
 #include "char-device.h"
+#include "red-channel-client.h"
 
 #define RED_TYPE_CHAR_DEVICE_SMARTCARD red_char_device_smartcard_get_type()
 
@@ -33,6 +34,7 @@
 typedef struct RedCharDeviceSmartcard RedCharDeviceSmartcard;
 typedef struct RedCharDeviceSmartcardClass RedCharDeviceSmartcardClass;
 typedef struct RedCharDeviceSmartcardPrivate RedCharDeviceSmartcardPrivate;
+typedef struct SmartCardChannelClient SmartCardChannelClient;
 
 struct RedCharDeviceSmartcard
 {
@@ -53,5 +55,24 @@ GType red_char_device_smartcard_get_type(void) G_GNUC_CONST;
  */
 RedCharDevice *smartcard_device_connect(RedsState *reds, SpiceCharDeviceInstance *char_device);
 void smartcard_device_disconnect(SpiceCharDeviceInstance *char_device);
+void smartcard_channel_write_to_reader(RedCharDeviceWriteBuffer *write_buf);
+SpiceCharDeviceInstance* smartcard_readers_get(uint32_t reader_id);
+SpiceCharDeviceInstance *smartcard_readers_get_unattached(void);
+uint32_t smartcard_get_n_readers(void);
+void smartcard_char_device_notify_reader_add(RedCharDeviceSmartcard *smartcard);
+void smartcard_char_device_attach_client(SpiceCharDeviceInstance *smartcard,
+                                         SmartCardChannelClient *scc);
+gboolean smartcard_char_device_notify_reader_remove(RedCharDeviceSmartcard *smartcard);
+void smartcard_char_device_detach_client(RedCharDeviceSmartcard *smartcard,
+                                         SmartCardChannelClient *scc);
+SmartCardChannelClient* smartcard_char_device_get_client(RedCharDeviceSmartcard *smartcard);
+int smartcard_char_device_handle_migrate_data(RedCharDeviceSmartcard *smartcard,
+                                              SpiceMigrateDataSmartcard *mig_data);
+
+enum {
+    RED_PIPE_ITEM_TYPE_ERROR = RED_PIPE_ITEM_TYPE_CHANNEL_BASE,
+    RED_PIPE_ITEM_TYPE_SMARTCARD_DATA,
+    RED_PIPE_ITEM_TYPE_SMARTCARD_MIGRATE_DATA,
+};
 
 #endif // __SMART_CARD_H__


More information about the Spice-commits mailing list