DBUS入门笔记

这篇博客介绍了DBUS的基本概念,包括其三层结构和应用场景。DBUS作为Linux下的进程间通信(IPC)工具,提供异步处理、安全连接等特点。文中详细解释了Object Path、接口、代理和Bus Name等核心概念,并通过QTDBUS的remotecontrolledcar案例展示了如何使用DBUS进行通信。在Mac上,博主分享了如何配置dbus-daemon作为守护进程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DBUS简介

学习网址:https://dbus.freedesktop.org/doc/dbus-tutorial.html

DBUS三层:
libdbus,消息分发的守护进程Dbus daemon,应用程序框架的封装库或绑定 (For example, libdbus-glib and libdbus-qt)

libdbus仅仅支持one-to-one connection

关于传递,对象是message = header (filed) + body (param)
header常包括body中的数据类型信息。

DBUS的应用场景:
同一会话中的桌面应用程序
桌面应用程序和OS(系统级别的守护进程)

DBUS可以解决传统Linux IPC不能应付的远端系统管理员问题:


           A gap in current Linux support is that policies with any sort of
           dynamic "interact with user" component aren't currently
           supported. For example, that's often needed the first time a network
           adapter or printer is connected, and to determine appropriate places
           to mount disk drives. It would seem that such actions could be
           supported for any case where a responsible human can be identified:
           single user workstations, or any system which is remotely
           administered.


            This is a classic "remote sysadmin" problem, where in this case
            hotplugging needs to deliver an event from one security domain
            (operating system kernel, in this case) to another (desktop for
            logged-in user, or remote sysadmin). Any effective response must go
            the other way: the remote domain taking some action that lets the
            kernel expose the desired device capabilities. (The action can often
            be taken asynchronously, for example letting new hardware be idle
            
### QtDBus 快速入门教程 #### 了解 DBus 基本概念 对于希望掌握 QtDBus 的开发者来说,理解 DBus 是至关重要的第一步。DBus 提供了一种进程间通信的方式,在 Linux 和其他 Unix 类操作系统上被广泛应用。通过这种机制,不同应用之间可以交换消息和服务请求[^2]。 #### 设置开发环境 为了开始使用 QtDBus 进行编码,确保安装了最新版本的 Qt 库以及相应的工具链。如果计划创建自定义接口,则可能还需要 `qdbusviewer` 工具来浏览现有的 D-Bus 接口并测试命令。 #### 创建简单的 D-Bus 客户端与服务器程序 下面是一个简化版的例子展示如何利用 C++ 实现一个基本的服务端和客户端交互: ##### 服务端代码 (server.cpp) ```cpp #include <QCoreApplication> #include <QtDBus/QDBusConnection> #include <QDebug> class MyService : public QObject { Q_OBJECT public slots: void greet(const QString &name) const { qDebug() << "Hello," << name; } }; int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // Register the service on session bus. new MyService(); if (!QDBusConnection::sessionBus().registerObject("/myservice", this)) { qCritical() << "Failed to register object"; return 1; } if (!QDBusConnection::sessionBus().registerService("com.example.myservice")) { qCritical() << "Failed to register service"; return 1; } return app.exec(); } ``` ##### 客户端代码 (client.cpp) ```cpp #include <QCoreApplication> #include <QtDBus/QtDBus> #include <iostream> void callRemoteMethod() { QDBusInterface interface( "com.example.myservice", "/myservice", "", QDBusConnection::sessionBus()); QList<QVariant> args; args.append(QVariant("World")); QDBusMessage reply = interface.callWithArgumentList(QDBus::Block, "greet", args); } int main(int argc, char **argv) { QCoreApplication app(argc, argv); callRemoteMethod(); return 0; } ``` 此示例展示了怎样注册一个新的对象到会话总线上,并提供了一个名为 `greet` 的槽函数作为远程可调用的方法;而客户端则演示了发起对该方法的调用过程。 #### 使用 qdbusxml2cpp 自动生成 Adaptors 当项目变得复杂时,手动编写 adaptor 可能既耗时又容易出错。幸运的是,Qt 提供了实用工具 `qdbusxml2cpp` 来帮助自动化这一流程。例如,给定 XML 文件描述的服务接口定义,可以通过如下方式生成对应的 C++ 头文件和实现类: ```bash qdbusxml2cpp com.scorpio.test.xml -i test.h -a valueAdaptor ``` 这将依据指定模板生成适配器源码,从而大大减少了手写重复性代码的工作量[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值