在 Vala D-Bus Client Examples 中描述了如何通过定义 D-Bus 接口至 Vala 的 Interface 的映射,并创建一个实现了此本地 Interface 的 Proxy,进行方法调用、属性访问与信号侦听。
但在最近的开发中遇到了 D-Bus 接口中方法返回两个或多个值的情况,这样的情况又如何定义接口方法?看下面的例子:
例如接口 org.freedesktop.Telepathy.Connection.FUTURE 存在一个方法 EnsureSidecar,其原型如下:
EnsureSidecar (s: Main_Interface) → o: Path, a{sv}: Properties
正确定义 1:
namespace Hev {
[DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
public interface Future : GLib.Object {
public abstract void ensure_sidecar (string iface, out ObjectPath path,
out HashTable props) throws IOError;
}
}
正确定义2:
namespace Hev {
[DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
public interface Future : GLib.Object {
public abstract HashTable ensure_sidecar (string iface,
out ObjectPath path) throws IOError;
}
}
错误定义:
namespace Hev {
[DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
public interface Future : GLib.Object {
public abstract ObjectPath ensure_sidecar (string iface,
out HashTable props) throws IOError;
}
}
从上面的例子中可以看出优先级是:out 参数 > 返回值,为了更直观,建议使用正确定义1的方式。
Over!