sockets - 为什么Gio.Socket.create_source()返回null?

标签 sockets gio gjs

我是套接字的新手,正在尝试通过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

我在代码正常工作的另一部分中的另一个套接字(尽管是UDP)上调用了Gio.Socket.create_source()。调用create_source()本身不会引发任何错误(即使我使用G_MESSAGES_DEBUG=all运行我的脚本),并且也没有提到该函数曾经返回null in the documentation,因此我对自己的错误感到困惑。

编辑:

有3岁的注释here指出:

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



但是我认为这不再正确,因为我已经在UDP套接字上创建了一个源,尽管该套接字是“手工制作的”,而不是使用Gio.SocketClient()构建的。

最佳答案

您可能无意中调用了 Gio.DatagramBased.create_source() 方法。查看源代码,这最终将调用g_socket_create_source(),但首先进行一些检查,如果失败则返回null。这是检查:https://github.com/GNOME/glib/blob/master/gio/gsocket.c#L1114

看来the method will simply return null 是一个小错误,甚至没有从check_datagram_based()打印错误。

关于sockets - 为什么Gio.Socket.create_source()返回null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46111145/

相关文章:

sockets - TCP/IP 套接字是原子的吗?

bluetooth - 更新了 Bluez DBUS-API 文档?

c - glib 网络连接示例

c - 如果 GInterface 方法签名与 VirtualTable (struct) 方法不匹配怎么办?

python - 我如何与正在运行的进程通信?

客户端在 2-3 次成功连接后停止与服务器的连接。 c 中的套接字编程

java - 肉桂小程序 PopupSliderMenuItem 和标签

javascript - 使用 destroy() 和异步函数避免分配错误

gnome-shell-extensions - GJS:global.window_manager.get_workspaces()未定义

java - 将 HTTP 请求重定向到不同的服务器