dbus-glib 和 GDBus 的区别
时间:2022-03-10 18:08
Conceptual differences
The central concepts of D-Bus are modelled in a very similar way in dbus-glib and GDBus. Both have a objects representing connections, proxies and method invocations. But there are some important differences:
D-Bus最重要的概念在dbus-glib和GDBus中都是相似的.都用对象表示连接,代理 和 方法执行,但也有一些重要的不同点:
-
dbus-glib uses the , GDBus doesn‘t. Instead, it relies on GIO streams as transport layer, and has its own implementation for the the D-Bus connection setup and authentication. Apart from using streams as transport, avoiding libdbus also lets GDBus avoid some thorny multithreading issues.
dbus-glib使用 ,GDBus不使用,而是依赖GIO流作为传输层,并且拥有一套自己实现的D-Bus连接设置和授权的方法.暂且不说GDBus使用流传输,
不使用libdbus可以使GDBus没有一些多线程方面的问题.
-
dbus-glib uses the GObject type system for method arguments and return values, including a homegrown container specialization mechanism. GDBus relies on the type system which is explicitly designed to match D-Bus types.
dbus-glib的方法参数和返回值使用Gobject类型系统,其中包含了一个特定结构的自有容器.GDBus依赖专为匹配D-Bus类型而设计的GVariant类型系统
-
dbus-glib models only D-Bus interfaces and does not provide any types for objects. GDBus models both D-Bus interfaces (via the , and types) and objects (via the , and types).
dbus-glib 只能提供D-Bus接口,不为对象提供任何类型,
GDBus同时提供D-Bus接口(通过, 和 类型)和对象(通过, 和 类型)
-
GDBus includes native support for the (via the type) and D-Bus interfaces, dbus-glib doesn‘t.
GDBus 为 (via the type) 和 D-Bus接口,包含了本地支持,dbus-glib没有.
-
The typical way to export an object in dbus-glib involves generating glue code from XML introspection data using dbus-binding-tool. GDBus provides a similar tool called that can also generate Docbook D-Bus interface documentation.
dbus-glib中导出对象的典型方法是使用dbus-binding-tool根据XML内省数据生成代码,GDBus也提供了一个类似的工具叫做gdbus-codegen,这个工具也可以生成Docbook D-Bus接口文件.
-
dbus-glib doesn‘t provide any convenience API for owning and watching bus names, GDBus provides the and family of convenience functions.
dbus-glib不提供战友和监视总线名字的方便的API,GDBus提供了 和 系列的方便函数.
-
GDBus provides API to parse, generate and work with , dbus-glib doesn‘t.
GDBus提供API来解析、生成 和 工作的内省XML,dbus-glib不提供。
API comparison
Table. dbus-glib APIs and their GDBus counterparts
dbus-glib | GDBus |
---|---|
, - also see | |
DBusGObject | , - also see |
, also see | |
and , also see | |
not needed, use the generic | |
use with | |
- also see | |
- also see | |
introspection data is installed while registering an object, see | |
no direct equivalent, see | |
and variants | |
|
Owning bus names
Using dbus-glib, you typically call RequestName manually to own a name, like in the following excerpt:
使用dbus-glib来占有一个总线名字的典型做法是手动调用RequestName ,像下面的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
static void on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) { /* further setup ... */ } /* ... */ owner_id = (, NAME_TO_CLAIM, , on_bus_acquired, on_name_acquired, on_name_lost, , ); (loop); (owner_id); |
Note that works asynchronously and requires you to enter your mainloop to await the on_name_aquired()
callback. Also note that in order to avoid race conditions (e.g. when your service is activated by a method call), you have to export your manager object before acquiring the name. The on_bus_acquired()
callback is the right place to do such preparations.
需要注意的是 工作方式是异步的,并且需要你进入mainloop来等待on_name_aquired()这个callback被调用.还需要注意的是为了防止条件竞争(例如你的服务是同方法调用来启动的),你必须在就收名字之前导出管理对象,on_bus_acquired()这个callback中是导出管理对象的正确位置.
Creating proxies for well-known names
dbus-glib lets you create proxy objects for well-known names, like the following example:
1 2 3 4 |
proxy = (system_bus_connection, "org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts"); |
For a constructed like this, method calls will be sent to the current owner of the name, and that owner can change over time.
The same can be achieved with :
1 2 3 4 5 6 7 8 9 |
error = ; proxy = (, , , /* GDBusInterfaceInfo */ "org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", , /* GCancellable */ &error); |
For an added layer of safety, you can specify what D-Bus interface the proxy is expected to conform to by using the type. Additionally, loads, caches and tracks changes to the D-Bus properties on the remote object. It also sets up match rules so D-Bus signals from the remote object are delivered locally.
The type normally isn‘t used directly - instead proxies subclassing generated by is used, see
dbus-glib 和 GDBus 的区别,布布扣,bubuko.com