From 0699e7170171221710626c8c70966a34ea0455ec Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Wed, 9 Nov 2011 17:40:50 +0100 Subject: No need to use g_socket_client_connect_to_host_async We can wait in main for successfull connection. --- src/pjctl.c | 59 +++++++++++++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/pjctl.c b/src/pjctl.c index 3c20d96..0069c9d 100644 --- a/src/pjctl.c +++ b/src/pjctl.c @@ -247,40 +247,6 @@ read_cb(GObject *pollable_stream, gpointer userdata) return TRUE; } -static void -connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data) -{ - struct pjctl *pjctl = user_data; - GError *error = NULL; - - pjctl->con = g_socket_client_connect_to_host_finish(pjctl->sc, res, - &error); - if (error) { - g_printerr("failed to connect: %s\n", error->message); - g_main_loop_quit(pjctl->loop); - return; - } - - g_object_get(G_OBJECT(pjctl->con), - "input-stream", &pjctl->in, - "output-stream", &pjctl->out, NULL); - - if (!G_IS_POLLABLE_INPUT_STREAM(pjctl->in) || - !g_pollable_input_stream_can_poll(pjctl->in)) { - g_printerr("Error: GSocketConnection is not pollable\n"); - g_main_loop_quit(pjctl->loop); - return; - } - - pjctl->insrc = g_pollable_input_stream_create_source(pjctl->in, NULL); - g_source_set_callback(pjctl->insrc, (GSourceFunc) read_cb, pjctl, NULL); - g_source_attach(pjctl->insrc, NULL); - g_source_unref(pjctl->insrc); - - - pjctl->state = PJCTL_AWAIT_INITIAL; -} - static void power_response(struct pjctl *pjctl, char *cmd, char *param) { @@ -637,6 +603,7 @@ main(int argc, char **argv) struct pjctl pjctl; char *host = argv[1]; int port = 4352; + GError *error = NULL; int i; memset(&pjctl, 0, sizeof pjctl); @@ -669,8 +636,28 @@ main(int argc, char **argv) g_socket_client_set_protocol(pjctl.sc, G_SOCKET_PROTOCOL_TCP); g_socket_client_set_socket_type(pjctl.sc, G_SOCKET_TYPE_STREAM); - g_socket_client_connect_to_host_async(pjctl.sc, host, port, NULL, - connection_ready, &pjctl); + pjctl.con = g_socket_client_connect_to_host(pjctl.sc, host, port, + NULL, &error); + if (error) { + g_printerr("failed to connect: %s\n", error->message); + return 1; + } + + g_object_get(G_OBJECT(pjctl.con), + "input-stream", &pjctl.in, + "output-stream", &pjctl.out, NULL); + + if (!G_IS_POLLABLE_INPUT_STREAM(pjctl.in) || + !g_pollable_input_stream_can_poll(pjctl.in)) { + g_printerr("Error: GSocketConnection is not pollable\n"); + return 1; + } + + pjctl.insrc = g_pollable_input_stream_create_source(pjctl.in, NULL); + g_source_set_callback(pjctl.insrc, (GSourceFunc) read_cb, &pjctl, NULL); + g_source_attach(pjctl.insrc, NULL); + g_source_unref(pjctl.insrc); + pjctl.state = PJCTL_AWAIT_INITIAL; g_main_loop_run(pjctl.loop); -- cgit