[tracker/extractor-dbus-fd] libtracker-common: Add async send_and_splice
- From: Adrien Bustany <abustany src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker/extractor-dbus-fd] libtracker-common: Add async send_and_splice
- Date: Fri, 2 Jul 2010 13:03:45 +0000 (UTC)
commit 55a804c7d2fa2ff492e1852e88157bb4e9d45b6c
Author: Adrien Bustany <abustany gnome org>
Date: Fri Jul 2 14:47:38 2010 +0200
libtracker-common: Add async send_and_splice
src/libtracker-common/tracker-dbus.c | 139 +++++++++++++++++++++++++++++++++-
src/libtracker-common/tracker-dbus.h | 12 +++
2 files changed, 150 insertions(+), 1 deletions(-)
---
diff --git a/src/libtracker-common/tracker-dbus.c b/src/libtracker-common/tracker-dbus.c
index 8915cc8..042dc23 100644
--- a/src/libtracker-common/tracker-dbus.c
+++ b/src/libtracker-common/tracker-dbus.c
@@ -49,6 +49,15 @@ typedef struct {
GTimeVal last_time;
} ClientData;
+typedef struct {
+ GInputStream *unix_input_stream;
+ GInputStream *buffered_input_stream;
+ GOutputStream *output_stream;
+ DBusPendingCall *call;
+ TrackerSendAndSpliceCallback callback;
+ gpointer user_data;
+} SendAndSpliceData;
+
static GSList *hooks;
static gboolean block_hooks;
@@ -749,7 +758,7 @@ tracker_dbus_send_and_splice (DBusConnection *connection,
buffered_input_stream,
G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
- NULL,
+ cancellable,
&inner_error);
if (G_LIKELY (!inner_error)) {
@@ -795,3 +804,131 @@ tracker_dbus_send_and_splice (DBusConnection *connection,
return ret_value;
}
+
+static SendAndSpliceData*
+send_and_splice_data_new (GInputStream *unix_input_stream,
+ GInputStream *buffered_input_stream,
+ GOutputStream *output_stream,
+ DBusPendingCall *call,
+ TrackerSendAndSpliceCallback callback,
+ gpointer user_data)
+{
+ SendAndSpliceData *data;
+
+ data = g_slice_new0 (SendAndSpliceData);
+ data->unix_input_stream = unix_input_stream;
+ data->buffered_input_stream = buffered_input_stream;
+ data->output_stream = output_stream;
+ data->call = call;
+ data->callback = callback;
+ data->user_data = user_data;
+
+ return data;
+}
+
+static void
+send_and_splice_data_free (SendAndSpliceData *data)
+{
+ g_object_unref (data->unix_input_stream);
+ g_object_unref (data->buffered_input_stream);
+ g_object_unref (data->output_stream);
+ dbus_pending_call_unref (data->call);
+}
+
+static void
+send_and_splice_async_callback (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SendAndSpliceData *data = user_data;
+ DBusMessage *reply = NULL;
+ GError *error = NULL;
+
+ g_output_stream_splice_finish (data->output_stream,
+ result,
+ &error);
+
+ if (G_LIKELY (!error)) {
+ dbus_pending_call_block (data->call);
+ reply = dbus_pending_call_steal_reply (data->call);
+
+ if (G_UNLIKELY (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)) {
+ DBusError dbus_error;
+
+ dbus_error_init (&dbus_error);
+ dbus_set_error_from_message (&dbus_error, reply);
+ dbus_set_g_error (&error, &dbus_error);
+ dbus_error_free (&dbus_error);
+
+ (* data->callback) (NULL, -1, error, data->user_data);
+
+ g_error_free (error);
+ } else {
+ (* data->callback) (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->output_stream)),
+ g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->output_stream)),
+ NULL,
+ data->user_data);
+ }
+ } else {
+ (* data->callback) (NULL, -1, error, data->user_data);
+
+ g_error_free (error);
+ }
+
+ if (reply) {
+ dbus_message_unref (reply);
+ }
+
+ send_and_splice_data_free (data);
+}
+
+void
+tracker_dbus_send_and_splice_async (DBusConnection *connection,
+ DBusMessage *message,
+ int fd,
+ GCancellable *cancellable,
+ TrackerSendAndSpliceCallback callback,
+ gpointer user_data)
+{
+ DBusPendingCall *call;
+ GInputStream *unix_input_stream;
+ GInputStream *buffered_input_stream;
+ GOutputStream *output_stream;
+ SendAndSpliceData *data;
+
+ g_return_if_fail (connection);
+ g_return_if_fail (message);
+
+
+ dbus_connection_send_with_reply (connection,
+ message,
+ &call,
+ -1);
+ dbus_message_unref (message);
+
+ if (!call) {
+ g_critical ("FD passing unsupported or connection disconnected");
+ return;
+ }
+
+ unix_input_stream = g_unix_input_stream_new (fd, TRUE);
+ buffered_input_stream = g_buffered_input_stream_new_sized (unix_input_stream,
+ TRACKER_DBUS_PIPE_BUFFER_SIZE);
+ output_stream = g_memory_output_stream_new (NULL, 0, g_realloc, NULL);
+
+ data = send_and_splice_data_new (unix_input_stream,
+ buffered_input_stream,
+ output_stream,
+ call,
+ callback,
+ user_data);
+
+ g_output_stream_splice_async (output_stream,
+ buffered_input_stream,
+ G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
+ G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ 0,
+ cancellable,
+ send_and_splice_async_callback,
+ data);
+}
diff --git a/src/libtracker-common/tracker-dbus.h b/src/libtracker-common/tracker-dbus.h
index 4b66e21..68128c4 100644
--- a/src/libtracker-common/tracker-dbus.h
+++ b/src/libtracker-common/tracker-dbus.h
@@ -92,6 +92,11 @@ typedef struct TrackerDBusRequestHandler TrackerDBusRequestHandler;
typedef void (*TrackerDBusRequestFunc) (guint request_id,
gpointer user_data);
+typedef void (*TrackerSendAndSpliceCallback) (void *buffer,
+ gssize buffer_size,
+ GError *error,
+ gpointer user_data);
+
typedef struct {
guint id;
gpointer data1;
@@ -169,6 +174,13 @@ gboolean tracker_dbus_send_and_splice (DBusConnection
gssize *dest_buffer_size,
GError **error);
+void tracker_dbus_send_and_splice_async (DBusConnection *connection,
+ DBusMessage *message,
+ int fd,
+ GCancellable *cancellable,
+ TrackerSendAndSpliceCallback callback,
+ gpointer user_data);
+
G_END_DECLS
#endif /* __LIBTRACKER_COMMON_DBUS_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]