From 813c0f00bf70ff5ca6582acf745e4250a529f383 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Wed, 17 Aug 2011 23:15:45 +0300 Subject: [PATCH 06/18] IdleServerConnection: Async-ify idle_server_connection_connect Fixes: https://bugs.freedesktop.org/37145 --- src/idle-connection.c | 68 +++++++++++++++++++++++------------------ src/idle-server-connection.c | 47 +++++++++++++++++++--------- src/idle-server-connection.h | 4 ++- 3 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/idle-connection.c b/src/idle-connection.c index 29b2b99..0080706 100644 --- a/src/idle-connection.c +++ b/src/idle-connection.c @@ -638,42 +638,21 @@ static gboolean _iface_start_connecting(TpBaseConnection *self, GError **error) return TRUE; } -static void _start_connecting_continue(IdleConnection *conn) { - TpBaseConnection *base_conn = TP_BASE_CONNECTION(conn); +static void _connection_connect_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { + IdleServerConnection *sconn = IDLE_SERVER_CONNECTION(source_object); + IdleConnection *conn = IDLE_CONNECTION(user_data); IdleConnectionPrivate *priv = IDLE_CONNECTION_GET_PRIVATE(conn); - GError *conn_error = NULL; - IdleServerConnection *sconn; - - if (!priv->realname || !priv->realname[0]) { - const gchar *g_realname = g_get_real_name(); - - g_free(priv->realname); - - if (g_realname && g_realname[0] && strcmp(g_realname, "Unknown")) - priv->realname = g_strdup(g_realname); - else - priv->realname = g_strdup(priv->nickname); - } - - if (!priv->username || !priv->username[0]) { - g_free(priv->username); - priv->username = g_strdup(g_get_user_name()); - } - - sconn = g_object_new(IDLE_TYPE_SERVER_CONNECTION, "host", priv->server, "port", priv->port, NULL); - if (priv->use_ssl) - idle_server_connection_set_tls(sconn, TRUE); - - g_signal_connect(sconn, "status-changed", (GCallback)(sconn_status_changed_cb), conn); + GError *error = NULL; - if (!idle_server_connection_connect(sconn, &conn_error)) { - IDLE_DEBUG("server connection failed to connect: %s", conn_error->message); + if (!idle_server_connection_connect_finish(sconn, res, &error)) { + IDLE_DEBUG("idle_server_connection_connect failed: %s", error->message); - tp_base_connection_disconnect_with_dbus_error(base_conn, - TP_ERROR_STR_NETWORK_ERROR, + tp_base_connection_disconnect_with_dbus_error(TP_BASE_CONNECTION(conn), + tp_error_get_dbus_name(error->code), NULL, TP_CONNECTION_STATUS_REASON_NETWORK_ERROR); + g_error_free(error); g_object_unref(sconn); return; } @@ -696,6 +675,35 @@ static void _start_connecting_continue(IdleConnection *conn) { irc_handshakes(conn); } +static void _start_connecting_continue(IdleConnection *conn) { + IdleConnectionPrivate *priv = IDLE_CONNECTION_GET_PRIVATE(conn); + IdleServerConnection *sconn; + + if (!priv->realname || !priv->realname[0]) { + const gchar *g_realname = g_get_real_name(); + + g_free(priv->realname); + + if (g_realname && g_realname[0] && strcmp(g_realname, "Unknown")) + priv->realname = g_strdup(g_realname); + else + priv->realname = g_strdup(priv->nickname); + } + + if (!priv->username || !priv->username[0]) { + g_free(priv->username); + priv->username = g_strdup(g_get_user_name()); + } + + sconn = g_object_new(IDLE_TYPE_SERVER_CONNECTION, "host", priv->server, "port", priv->port, NULL); + if (priv->use_ssl) + idle_server_connection_set_tls(sconn, TRUE); + + g_signal_connect(sconn, "status-changed", (GCallback)(sconn_status_changed_cb), conn); + + idle_server_connection_connect_async(sconn, NULL, _connection_connect_ready, conn); +} + static gboolean keepalive_timeout_cb(gpointer user_data); static void sconn_status_changed_cb(IdleServerConnection *sconn, IdleServerConnectionState state, IdleServerConnectionStateReason reason, IdleConnection *conn) { diff --git a/src/idle-server-connection.c b/src/idle-server-connection.c index 45e5e23..2259b56 100644 --- a/src/idle-server-connection.c +++ b/src/idle-server-connection.c @@ -26,7 +26,6 @@ #include #include -#include #include #define IDLE_DEBUG_FLAG IDLE_DEBUG_NETWORK @@ -280,7 +279,8 @@ cleanup: static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { GSocketClient *socket_client = G_SOCKET_CLIENT(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); GInputStream *input_stream; GSocket *socket; @@ -292,10 +292,11 @@ static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gp socket_connection = g_socket_client_connect_to_host_finish(socket_client, res, &error); if (socket_connection == NULL) { IDLE_DEBUG("g_socket_client_connect_to_host failed: %s", error->message); + g_simple_async_result_set_error(result, TP_ERRORS, TP_ERROR_NETWORK_ERROR, "%s", error->message); g_error_free(error); change_state(conn, SERVER_CONNECTION_STATE_NOT_CONNECTED, SERVER_CONNECTION_STATE_REASON_ERROR); g_object_unref(conn); - return; + goto cleanup; } socket = g_socket_connection_get_socket(socket_connection); @@ -308,39 +309,55 @@ static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gp priv->io_stream = G_IO_STREAM(socket_connection); + g_cancellable_reset(priv->cancellable); input_stream = g_io_stream_get_input_stream(priv->io_stream); _input_stream_read(conn, input_stream, _input_stream_read_ready); change_state(conn, SERVER_CONNECTION_STATE_CONNECTED, SERVER_CONNECTION_STATE_REASON_REQUESTED); + +cleanup: + g_simple_async_result_complete(result); + g_object_unref(result); } -gboolean idle_server_connection_connect(IdleServerConnection *conn, GError **error) { +void idle_server_connection_connect_async(IdleServerConnection *conn, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn); + GSimpleAsyncResult *result; if (priv->state != SERVER_CONNECTION_STATE_NOT_CONNECTED) { IDLE_DEBUG("already connecting or connected!"); - g_set_error(error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "already connecting or connected!"); - return FALSE; + g_simple_async_report_error_in_idle(G_OBJECT(conn), + callback, user_data, + TP_ERRORS, TP_ERROR_NOT_AVAILABLE, + "already connecting or connected!"); + return; } if ((priv->host == NULL) || (priv->host[0] == '\0')) { IDLE_DEBUG("host not set!"); - g_set_error(error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "host not set!"); - return FALSE; + g_simple_async_report_error_in_idle(G_OBJECT(conn), + callback, user_data, + TP_ERRORS, TP_ERROR_NOT_AVAILABLE, + "host not set!"); + return; } if (priv->port == 0) { IDLE_DEBUG("port not set!"); - g_set_error(error, TP_ERRORS, TP_ERROR_NOT_AVAILABLE, "port not set!"); - return FALSE; + g_simple_async_report_error_in_idle(G_OBJECT(conn), + callback, user_data, + TP_ERRORS, TP_ERROR_NOT_AVAILABLE, + "port not set!"); + return; } - g_cancellable_reset(priv->cancellable); - g_object_ref(conn); - g_socket_client_connect_to_host_async(priv->socket_client, priv->host, priv->port, priv->cancellable, _connect_to_host_ready, conn); - + result = g_simple_async_result_new(G_OBJECT(conn), callback, user_data, idle_server_connection_connect_async); + g_socket_client_connect_to_host_async(priv->socket_client, priv->host, priv->port, cancellable, _connect_to_host_ready, result); change_state(conn, SERVER_CONNECTION_STATE_CONNECTING, SERVER_CONNECTION_STATE_REASON_REQUESTED); +} - return TRUE; +gboolean idle_server_connection_connect_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_connect_async), FALSE); + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT(result), error); } static void _close_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { diff --git a/src/idle-server-connection.h b/src/idle-server-connection.h index 9402ab9..32d988f 100644 --- a/src/idle-server-connection.h +++ b/src/idle-server-connection.h @@ -22,6 +22,7 @@ #ifndef __IDLE_SERVER_CONNECTION_H__ #define __IDLE_SERVER_CONNECTION_H__ +#include #include G_BEGIN_DECLS @@ -68,7 +69,8 @@ GType idle_server_connection_get_type(void); #define IDLE_SERVER_CONNECTION_GET_CLASS(obj) \ (G_TYPE_INSTANCE_GET_CLASS((obj), IDLE_TYPE_SERVER_CONNECTION, IdleServerConnectionClass)) -gboolean idle_server_connection_connect(IdleServerConnection *conn, GError **error); +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); gboolean idle_server_connection_send(IdleServerConnection *conn, const gchar *cmd, GError **error); -- 1.7.6.2