From 50b50c99540b85b307bb0e0b169928594c088838 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Sat, 24 Sep 2011 11:30:45 +0200 Subject: Use an asynchronous connection initiation So we arent blocked, and unable to handle cmdline interaction while connecting. --- src/connection.c | 75 +++++++++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 44 deletions(-) (limited to 'src/connection.c') diff --git a/src/connection.c b/src/connection.c index 58a5e67..c93dbc7 100644 --- a/src/connection.c +++ b/src/connection.c @@ -18,55 +18,20 @@ read_cb(GObject *pollable_stream, gpointer data) return TRUE; } -static gboolean -do_ping(struct cmumble_context *ctx) -{ - MumbleProto__Ping ping; - GTimeVal tv; - - g_get_current_time(&tv); - mumble_proto__ping__init(&ping); - ping.timestamp = tv.tv_sec; - ping.resync = 1; - cmumble_send_msg(ctx, &ping.base); - - return TRUE; -} - static void -setup_ping_timer(struct cmumble_context *ctx) -{ - GSource *source; - - source = g_timeout_source_new_seconds(5); - g_source_set_callback(source, (GSourceFunc) do_ping, ctx, NULL); - g_source_attach(source, NULL); - g_source_unref(source); -} - -int -cmumble_connection_init(struct cmumble_context *ctx, - const char *host, int port) +connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) { + struct cmumble_context *ctx = user_data; struct cmumble_connection *con = &ctx->con; GError *error = NULL; - con->sock_client = g_socket_client_new(); - g_socket_client_set_tls(con->sock_client, TRUE); - g_socket_client_set_tls_validation_flags(con->sock_client, - G_TLS_CERTIFICATE_INSECURE); - g_socket_client_set_family(con->sock_client, G_SOCKET_FAMILY_IPV4); - g_socket_client_set_protocol(con->sock_client, - G_SOCKET_PROTOCOL_TCP); - g_socket_client_set_socket_type(con->sock_client, - G_SOCKET_TYPE_STREAM); - - con->conn = - g_socket_client_connect_to_host(con->sock_client, - host, port, NULL, &error); + con->conn = g_socket_client_connect_to_host_finish (con->sock_client, + res, &error); if (error) { g_printerr("connect failed: %s\n", error->message); - return -1; + g_main_loop_quit(ctx->loop); + g_error_free(error); + return; } g_object_get(G_OBJECT(con->conn), @@ -76,7 +41,8 @@ cmumble_connection_init(struct cmumble_context *ctx, if (!G_IS_POLLABLE_INPUT_STREAM(con->input) || !g_pollable_input_stream_can_poll(con->input)) { g_printerr("Error: GSocketConnection is not pollable\n"); - return 1; + g_main_loop_quit(ctx->loop); + return; } con->source = g_pollable_input_stream_create_source(con->input, NULL); @@ -84,7 +50,28 @@ cmumble_connection_init(struct cmumble_context *ctx, g_source_attach(con->source, NULL); g_source_unref(con->source); - setup_ping_timer(ctx); + cmumble_protocol_init(ctx); +} + +int +cmumble_connection_init(struct cmumble_context *ctx, + const char *host, int port) +{ + struct cmumble_connection *con = &ctx->con; + + con->sock_client = g_socket_client_new(); + g_socket_client_set_tls(con->sock_client, TRUE); + g_socket_client_set_tls_validation_flags(con->sock_client, + G_TLS_CERTIFICATE_INSECURE); + g_socket_client_set_family(con->sock_client, G_SOCKET_FAMILY_IPV4); + g_socket_client_set_protocol(con->sock_client, + G_SOCKET_PROTOCOL_TCP); + g_socket_client_set_socket_type(con->sock_client, + G_SOCKET_TYPE_STREAM); + + g_socket_client_connect_to_host_async(con->sock_client, + host, port, NULL, + connection_ready, ctx); return 0; } -- cgit