telepathy-idle: Connect to the server in the non-main thread

Simon McVittie smcv at kemper.freedesktop.org
Wed May 1 09:01:42 PDT 2013


Module: telepathy-idle
Branch: master
Commit: da178c9dbd46c0c067e9d44355687080939678a1
URL:    http://cgit.freedesktop.org/telepathy/telepathy-idle/commit/?id=da178c9dbd46c0c067e9d44355687080939678a1

Author: Sjoerd Simons <sjoerd at luon.net>
Date:   Sun Apr 28 15:30:10 2013 +0200

Connect to the server in the non-main thread

To do interactive certificate verification with GTls one needs to block
in the accept-certificate signal handler, which practically means the
connection needs to be done in a seperate check. As a first step,
instead using _connect_to_host_async in the main thread use a GTask to
synchronously connect in a different thread instead.

---

 configure.ac                 |    4 ++--
 src/idle-server-connection.c |   24 +++++++++++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index df9c964..f7fb648 100644
--- a/configure.ac
+++ b/configure.ac
@@ -86,10 +86,10 @@ AC_HEADER_STDC([])
 AC_C_INLINE
 
 AC_DEFINE(GLIB_VERSION_MIN_REQUIRED, GLIB_VERSION_2_28, [Ignore post 2.28 deprecations])
-AC_DEFINE(GLIB_VERSION_MAX_ALLOWED, GLIB_VERSION_2_30, [Prevent post 2.30 APIs])
+AC_DEFINE(GLIB_VERSION_MAX_ALLOWED, GLIB_VERSION_2_36, [Prevent post 2.36 APIs])
 
 PKG_CHECK_MODULES([GLIB],
-  [glib-2.0 >= 2.30.0, gobject-2.0 >= 2.30.0, gio-2.0 >= 2.30.0 ])
+  [glib-2.0 >= 2.36.0, gobject-2.0 >= 2.36.0, gio-2.0 >= 2.36.0 ])
 
 PKG_CHECK_MODULES([DBUS], [dbus-1 >= 0.51, dbus-glib-1 >= 0.51])
 
diff --git a/src/idle-server-connection.c b/src/idle-server-connection.c
index 6c5894f..1605b61 100644
--- a/src/idle-server-connection.c
+++ b/src/idle-server-connection.c
@@ -254,7 +254,6 @@ cleanup:
 }
 
 static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) {
-	GSocketClient *socket_client = G_SOCKET_CLIENT(source_object);
 	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);
@@ -265,7 +264,7 @@ static void _connect_to_host_ready(GObject *source_object, GAsyncResult *res, gp
 	gint socket_fd;
 	GError *error = NULL;
 
-	socket_connection = g_socket_client_connect_to_host_finish(socket_client, res, &error);
+	socket_connection = g_task_propagate_pointer (G_TASK (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_ERROR, TP_ERROR_NETWORK_ERROR, "%s", error->message);
@@ -294,9 +293,24 @@ cleanup:
 	g_object_unref(result);
 }
 
+static void _connect_in_thread (GTask *task, gpointer source_object, gpointer task_data, GCancellable *cancellable)
+{
+	IdleServerConnection *conn = source_object;
+	IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn);
+	GError *error = NULL;
+	GSocketConnection *socket_connection;
+
+	socket_connection = g_socket_client_connect_to_host (priv->socket_client, priv->host, priv->port, cancellable, &error);
+	if (socket_connection != NULL)
+		g_task_return_pointer (task, socket_connection, NULL);
+	else
+		g_task_return_error (task, 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;
+	GTask *task;
 
 	if (priv->state != SERVER_CONNECTION_STATE_NOT_CONNECTED) {
 		IDLE_DEBUG("already connecting or connected!");
@@ -326,7 +340,11 @@ void idle_server_connection_connect_async(IdleServerConnection *conn, GCancellab
 	}
 
 	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);
+
+	task = g_task_new (conn, cancellable,
+		_connect_to_host_ready, result);
+	g_task_run_in_thread (task, _connect_in_thread);
+
 	change_state(conn, SERVER_CONNECTION_STATE_CONNECTING, SERVER_CONNECTION_STATE_REASON_REQUESTED);
 }
 



More information about the telepathy-commits mailing list