From 051c7f37107650ba9e999c7bf4b956ce3a0fca8b Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Wed, 17 Aug 2011 23:15:45 +0300 Subject: [PATCH 12/18] IdleServerConnection: Async-ify idle_server_connection_disconnect Fixes: https://bugs.freedesktop.org/37145 --- src/idle-connection.c | 6 +--- src/idle-server-connection.c | 47 ++++++++++++++++++++++++----------------- src/idle-server-connection.h | 5 ++- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/idle-connection.c b/src/idle-connection.c index ab66715..4e44bb5 100644 --- a/src/idle-connection.c +++ b/src/idle-connection.c @@ -559,7 +559,7 @@ _force_disconnect (gpointer data) { IdleConnection *conn = IDLE_CONNECTION(data); IdleConnectionPrivate *priv = IDLE_CONNECTION_GET_PRIVATE(conn); - idle_server_connection_disconnect(priv->conn, NULL); + idle_server_connection_disconnect_async(priv->conn, NULL, NULL, NULL); return FALSE; } @@ -586,7 +586,6 @@ static void _iface_disconnected(TpBaseConnection *self) { static void _iface_shut_down(TpBaseConnection *self) { IdleConnectionPrivate *priv = IDLE_CONNECTION_GET_PRIVATE(self); - GError *error = NULL; if (priv->quitting) return; @@ -597,8 +596,7 @@ static void _iface_shut_down(TpBaseConnection *self) { if (priv->conn == NULL) { g_idle_add(_finish_shutdown_idle_func, self);; } else { - if (!idle_server_connection_disconnect(priv->conn, &error)) - g_error_free(error); + idle_server_connection_disconnect_async(priv->conn, NULL, NULL, NULL); } } diff --git a/src/idle-server-connection.c b/src/idle-server-connection.c index 1fd8c26..c82448b 100644 --- a/src/idle-server-connection.c +++ b/src/idle-server-connection.c @@ -106,7 +106,7 @@ static void idle_server_connection_dispose(GObject *obj) { priv->dispose_has_run = TRUE; if (priv->state == SERVER_CONNECTION_STATE_CONNECTED) - idle_server_connection_disconnect(conn, NULL); + idle_server_connection_disconnect_async(conn, NULL, NULL, NULL); if (priv->cancellable != NULL) { g_cancellable_cancel(priv->cancellable); @@ -239,15 +239,6 @@ static void _input_stream_read(IdleServerConnection *conn, GInputStream *input_s g_input_stream_read_async (input_stream, &priv->input_buffer, sizeof(priv->input_buffer) - 1, G_PRIORITY_DEFAULT, priv->cancellable, callback, conn); } -static void io_err_cleanup_func(gpointer data) { - GError *error = NULL; - - if (!idle_server_connection_disconnect_full(IDLE_SERVER_CONNECTION(data), &error, SERVER_CONNECTION_STATE_REASON_ERROR)) { - IDLE_DEBUG("disconnect: %s", error->message); - g_error_free(error); - } -} - static void _input_stream_read_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { GInputStream *input_stream = G_INPUT_STREAM(source_object); IdleServerConnection *conn = IDLE_SERVER_CONNECTION(user_data); @@ -272,7 +263,7 @@ static void _input_stream_read_ready(GObject *source_object, GAsyncResult *res, disconnect: if (priv->state == SERVER_CONNECTION_STATE_CONNECTED) - io_err_cleanup_func(conn); + idle_server_connection_disconnect_full_async(conn, SERVER_CONNECTION_STATE_REASON_ERROR, NULL, NULL, NULL); cleanup: g_object_unref(conn); } @@ -362,7 +353,8 @@ gboolean idle_server_connection_connect_finish(IdleServerConnection *conn, GAsyn static void _close_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { GIOStream *io_stream = G_IO_STREAM(source_object); - IdleServerConnection *conn = IDLE_SERVER_CONNECTION(user_data); + GSimpleAsyncResult *result = G_SIMPLE_ASYNC_RESULT(user_data); + IdleServerConnection *conn = IDLE_SERVER_CONNECTION(g_async_result_get_source_object(G_ASYNC_RESULT(result))); IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn); GError *error = NULL; @@ -371,35 +363,50 @@ static void _close_ready(GObject *source_object, GAsyncResult *res, gpointer use if (!g_io_stream_close_finish(io_stream, res, &error)) { IDLE_DEBUG("g_io_stream_close failed: %s", error->message); + g_simple_async_result_set_error(result, TP_ERRORS, TP_ERROR_NETWORK_ERROR, "%s", error->message); g_error_free(error); } + + g_simple_async_result_complete(result); + g_object_unref(result); } -gboolean idle_server_connection_disconnect(IdleServerConnection *conn, GError **error) { - return idle_server_connection_disconnect_full(conn, error, SERVER_CONNECTION_STATE_REASON_REQUESTED); +void idle_server_connection_disconnect_async(IdleServerConnection *conn, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { + idle_server_connection_disconnect_full_async(conn, + SERVER_CONNECTION_STATE_REASON_REQUESTED, + cancellable, + callback, + user_data); } -gboolean idle_server_connection_disconnect_full(IdleServerConnection *conn, GError **error, guint reason) { +void idle_server_connection_disconnect_full_async(IdleServerConnection *conn, guint reason, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn); + GSimpleAsyncResult *result; g_assert(priv != NULL); if (priv->state != SERVER_CONNECTION_STATE_CONNECTED) { IDLE_DEBUG("the connection was not open"); - g_set_error(error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "the connection was not open"); - return FALSE; + g_simple_async_report_error_in_idle(G_OBJECT(conn), + callback, user_data, + TP_ERRORS, TP_ERROR_NOT_AVAILABLE, + "the connection was not open"); + return; } g_cancellable_cancel(priv->cancellable); priv->reason = reason; - g_object_ref(conn); - g_io_stream_close_async(priv->io_stream, G_PRIORITY_DEFAULT, NULL, _close_ready, conn); + result = g_simple_async_result_new(G_OBJECT(conn), callback, user_data, idle_server_connection_disconnect_full_async); + g_io_stream_close_async(priv->io_stream, G_PRIORITY_DEFAULT, cancellable, _close_ready, result); g_object_unref(priv->io_stream); priv->io_stream = NULL; +} - return TRUE; +gboolean idle_server_connection_disconnect_finish(IdleServerConnection *conn, GAsyncResult *result, GError **error) { + g_return_val_if_fail(g_simple_async_result_is_valid(result, G_OBJECT(conn), idle_server_connection_disconnect_full_async), FALSE); + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT(result), error); } static void _write_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { diff --git a/src/idle-server-connection.h b/src/idle-server-connection.h index 27139d2..207fedc 100644 --- a/src/idle-server-connection.h +++ b/src/idle-server-connection.h @@ -71,8 +71,9 @@ GType idle_server_connection_get_type(void); void idle_server_connection_connect_async(IdleServerConnection *conn, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gboolean idle_server_connection_connect_finish(IdleServerConnection *conn, GAsyncResult *result, GError **error); -gboolean idle_server_connection_disconnect(IdleServerConnection *conn, GError **error); -gboolean idle_server_connection_disconnect_full(IdleServerConnection *conn, GError **error, guint reason); +void idle_server_connection_disconnect_async(IdleServerConnection *conn, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); +void idle_server_connection_disconnect_full_async(IdleServerConnection *conn, guint reason, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); +gboolean idle_server_connection_disconnect_finish(IdleServerConnection *conn, GAsyncResult *result, GError **error); void idle_server_connection_send_async(IdleServerConnection *conn, const gchar *cmd, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gboolean idle_server_connection_send_finish(IdleServerConnection *conn, GAsyncResult *result, GError **error); IdleServerConnectionState idle_server_connection_get_state(IdleServerConnection *conn); -- 1.7.6.2