为什么gio.socket.create_source()返回null



我是插座的新手,试图通过gjs/gio中的某些插座编程工作,并撞到墙壁创建glib.source,以处理从插座接收的。相关代码(我认为)是:

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;
    }
});

一切顺利,直到我拨打错误时致电this.sock_source.set_callback()

(JSConnect:15118): Gjs-WARNING **: JS ERROR: TypeError: this.sock_source is null
DeviceChannel<.opened@application.js:184:9
wrapper@resource:///org/gnome/gjs/modules/lang.js:178:22
@application.js:427:2

我在代码的另一部分中的另一个套接字上拨打了Gio.Socket.create_source(),该套接字正常。调用create_source()本身不会丢任何错误(即使我使用G_MESSAGES_DEBUG=all运行脚本),并且没有提及文档中返回null的函数,因此我对自己做错了什么感到困惑。

编辑:

这里有3年的评论指出:

这确实是不起作用,因为1)socket.create_source不存在 Typelib标记为 使用(跳过)glib/gio/gsocket.c

但是i 假设这不再是正确的,因为我已经在UDP插座上创建了一个源,尽管该插座不是使用Gio.SocketClient()构建的"手工制作"。

您可能会无意中调用Gio.DatagramBased.create_source()方法。查看源代码,最终将调用g_socket_create_source(),但首先进行一些检查并在失败的情况下返回null。这是检查:https://github.com/gnome/glib/blob/master/gio/gsocket.c#l1114

该方法将简单地返回null而无需打印check_datagram_based()的错误。

,这似乎是一个较小的错误。

最新更新