[Spice-devel] [PATCH spice-gtk 1/2] channel: add spice_vmc_write_async()

Marc-André Lureau marcandre.lureau at gmail.com
Tue Feb 11 02:26:26 PST 2014


Refactor port code to create a private GIO async function that can send
SPICE_MSGC_SPICEVMC_DATA message over any channel.
---
 gtk/channel-base.c       | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/channel-port.c       | 33 +++------------------------------
 gtk/spice-channel-priv.h |  8 ++++++++
 3 files changed, 59 insertions(+), 30 deletions(-)

diff --git a/gtk/channel-base.c b/gtk/channel-base.c
index 646042d..363dda5 100644
--- a/gtk/channel-base.c
+++ b/gtk/channel-base.c
@@ -232,3 +232,51 @@ void spice_channel_set_handlers(SpiceChannelClass *klass,
     spice_channel_add_base_handlers(klass);
     set_handlers(klass, handlers, n);
 }
+
+static void
+vmc_write_free_cb(uint8_t *data, void *user_data)
+{
+    GSimpleAsyncResult *result = user_data;
+
+    g_simple_async_result_complete_in_idle(result);
+    g_object_unref(result);
+}
+
+G_GNUC_INTERNAL
+void spice_vmc_write_async(SpiceChannel *self,
+                           const void *buffer, gsize count,
+                           GCancellable *cancellable,
+                           GAsyncReadyCallback callback,
+                           gpointer user_data)
+{
+    SpiceMsgOut *msg;
+    GSimpleAsyncResult *simple;
+
+    simple = g_simple_async_result_new(G_OBJECT(self), callback, user_data,
+                                       spice_port_write_async);
+    g_simple_async_result_set_op_res_gssize(simple, count);
+
+    msg = spice_msg_out_new(SPICE_CHANNEL(self), SPICE_MSGC_SPICEVMC_DATA);
+    spice_marshaller_add_ref_full(msg->marshaller, (uint8_t*)buffer, count,
+                                  vmc_write_free_cb, simple);
+    spice_msg_out_send(msg);
+}
+
+G_GNUC_INTERNAL
+gssize spice_vmc_write_finish(SpiceChannel *self,
+                              GAsyncResult *result, GError **error)
+{
+    GSimpleAsyncResult *simple;
+
+    g_return_val_if_fail(result != NULL, -1);
+
+    simple = (GSimpleAsyncResult *)result;
+
+    if (g_simple_async_result_propagate_error(simple, error))
+        return -1;
+
+    g_return_val_if_fail(g_simple_async_result_is_valid(result, G_OBJECT(self),
+                                                        spice_port_write_async), -1);
+
+    return g_simple_async_result_get_op_res_gssize(simple);
+}
diff --git a/gtk/channel-port.c b/gtk/channel-port.c
index 0a8b37f..5512713 100644
--- a/gtk/channel-port.c
+++ b/gtk/channel-port.c
@@ -289,14 +289,6 @@ static void port_handle_msg(SpiceChannel *channel, SpiceMsgIn *in)
     emit_main_context(channel, SPICE_PORT_DATA, buf, size);
 }
 
-static void port_write_free_cb(uint8_t *data, void *user_data)
-{
-    GSimpleAsyncResult *result = user_data;
-
-    g_simple_async_result_complete(result);
-    g_object_unref(result);
-}
-
 /**
  * spice_port_write_async:
  * @port: A #SpicePortChannel
@@ -320,9 +312,7 @@ void spice_port_write_async(SpicePortChannel *self,
                             GAsyncReadyCallback callback,
                             gpointer user_data)
 {
-    GSimpleAsyncResult *simple;
     SpicePortChannelPrivate *c;
-    SpiceMsgOut *msg;
 
     g_return_if_fail(SPICE_IS_PORT_CHANNEL(self));
     g_return_if_fail(buffer != NULL);
@@ -335,14 +325,8 @@ void spice_port_write_async(SpicePortChannel *self,
         return;
     }
 
-    simple = g_simple_async_result_new(G_OBJECT(self), callback, user_data,
-                                       spice_port_write_async);
-    g_simple_async_result_set_op_res_gssize(simple, count);
-
-    msg = spice_msg_out_new(SPICE_CHANNEL(self), SPICE_MSGC_SPICEVMC_DATA);
-    spice_marshaller_add_ref_full(msg->marshaller, (uint8_t*)buffer, count,
-                                  port_write_free_cb, simple);
-    spice_msg_out_send(msg);
+    spice_vmc_write_async(SPICE_CHANNEL(self), buffer, count,
+                          cancellable, callback, user_data);
 }
 
 /**
@@ -360,20 +344,9 @@ void spice_port_write_async(SpicePortChannel *self,
 gssize spice_port_write_finish(SpicePortChannel *self,
                                GAsyncResult *result, GError **error)
 {
-    GSimpleAsyncResult *simple;
-
     g_return_val_if_fail(SPICE_IS_PORT_CHANNEL(self), -1);
-    g_return_val_if_fail(result != NULL, -1);
-
-    simple = (GSimpleAsyncResult *)result;
-
-    if (g_simple_async_result_propagate_error(simple, error))
-        return -1;
-
-    g_return_val_if_fail(g_simple_async_result_is_valid(result, G_OBJECT(self),
-                                                        spice_port_write_async), -1);
 
-    return g_simple_async_result_get_op_res_gssize(simple);
+    return spice_vmc_write_finish(SPICE_CHANNEL(self), result, error);
 }
 
 /**
diff --git a/gtk/spice-channel-priv.h b/gtk/spice-channel-priv.h
index 0816061..35704ea 100644
--- a/gtk/spice-channel-priv.h
+++ b/gtk/spice-channel-priv.h
@@ -196,6 +196,14 @@ void spice_caps_set(GArray *caps, guint32 cap, const gchar *desc);
 
 gchar *spice_channel_supported_string(void);
 
+void spice_vmc_write_async(SpiceChannel *self,
+                           const void *buffer, gsize count,
+                           GCancellable *cancellable,
+                           GAsyncReadyCallback callback,
+                           gpointer user_data);
+gssize spice_vmc_write_finish(SpiceChannel *self,
+                              GAsyncResult *result, GError **error);
+
 G_END_DECLS
 
 #endif /* __SPICE_CLIENT_CHANNEL_PRIV_H__ */
-- 
1.8.4.2



More information about the Spice-devel mailing list