Non-blocking socket communication with GIO
- From: Gergely Polonkai <gergely polonkai eu>
- To: gtk-list gnome org
- Subject: Non-blocking socket communication with GIO
- Date: Thu, 10 Jan 2019 15:02:38 +0100
Hello,
i’m working on an app that communicates with a network service using a
custom binary protocol. For that i have a GSocketClient. The
destination can, in theory, be either a TCP/IP or a UNIX socket (only
TCP/IP is implemented yet). I initiate the connection like this:
scuttler->socket_client = g_socket_client_new();
address = g_inet_address_new_from_string("127.0.0.1");
destination = g_inet_socket_address_new(address, 8008);
g_object_unref(address);
g_task_set_task_data(task, g_object_ref(scuttler), (GDestroyNotify)g_object_unref);
g_socket_client_connect_async(scuttler->socket_client, G_SOCKET_CONNECTABLE(destination), cancellable,
connection_finished, task);
Then, in connecion_finished(), among other things, i do this:
scuttler->connection = connection;
if (!ssb_scuttler_shs_connect(scuttler, &err)) {
g_task_return_error(task, err);
g_object_unref(task);
return;
}
socket = g_socket_connection_get_socket(connection);
g_socket_set_blocking(socket, FALSE);
socket_source = g_socket_create_source(socket, G_IO_IN | G_IO_PRI, NULL);
g_source_set_callback(socket_source,
(GSourceFunc)read_inbound,
g_object_ref(scuttler), g_object_unref);
g_source_attach(socket_source, g_main_context_get_thread_default());
However, my read_inbound() function never gets called, even though my
request ends up in the server (i can see it in the logs). Am i missing
something fundamental here?
(Also, this is not really GTK, but more like GLib/GIO specific; if there
is a better mailing list to send this, please advise.)
Best,
Gergely
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]