I'm new to sockets and trying to work through some socket programming in GJS/Gio and hit a wall creating a GLib.Source to handle receiving from a socket. The relevant code (I think) is:
const DeviceChannel = new Lang.Class({
Name: "DeviceChannel",
_init: function (device) {
this.device = device;
this.connection = null;
this.inStream = null;
this.outStream = null;
this.socket = null;
this.sock_source = 0;
},
open: function () {
let client = new Gio.SocketClient();
this.addr = new Gio.InetSocketAddress({
address: this.device.tcpHost,
port: this.device.tcpPort
});
let conn = client.connect_async(
this.addr,
null,
Lang.bind(this, this.opened)
);
},
opened: function (client, res) {
this.connection = client.connect_finish(res);
// Streams
this.inStream = new Gio.DataInputStream({
base_stream: this.connection.get_input_stream()
});
this.outStream = new Gio.DataOutputStream({
base_stream: this.connection.get_output_stream()
});
// Socket
this.socket = this.connection.get_socket();
this.socket.set_option(6, 4, 10); // TCP_KEEPIDLE
this.socket.set_option(6, 5, 5); // TCP_KEEPINTVL
this.socket.set_option(6, 6, 3); // TCP_KEEPCNT
this.socket.set_keepalive(true);
this.sock_source = this.socket.create_source(GLib.IOCondition.IN, null);
this.sock_source.set_callback(Lang.bind(this, this._io_ready));
this.sock_source.attach(null);
},
_io_ready: function (condition) {
return true;
}
});
All goes well until I call this.sock_source.set_callback()
when I get the error:
(JSConnect:15118): Gjs-WARNING **: JS ERROR: TypeError: this.sock_source is null
DeviceChannel<[email protected]:184:9
wrapper@resource:///org/gnome/gjs/modules/lang.js:178:22
@application.js:427:2
I have called Gio.Socket.create_source()
on another socket (although UDP) in another part of the code which works fine. Calling create_source()
itself doesn't throw any errors (even if I run my script with G_MESSAGES_DEBUG=all
) and there is no mention of the function ever returning null
in the documentation so I'm baffled as to what I'm doing wrong.
Edit:
There is 3 year old comment here that states:
This does not work since 1) Socket.create_source doesn't exist in typelib as it is marked with (skip) in Glib/gio/gsocket.c
But I assume this is no longer true since I have created a source on my UDP socket, although that socket is "hand-made" not built using Gio.SocketClient()
.
You are probably inadvertently calling the
Gio.DatagramBased.create_source()
method. Looking at the source code, this will callg_socket_create_source()
eventually, but does some checks first and returnsnull
if those fail. Here are the checks: https://github.com/GNOME/glib/blob/master/gio/gsocket.c#L1114It looks to be a minor bug that the method will simply return
null
without even printing the error fromcheck_datagram_based()
.