一种IPC通信机制Gdbus详解

2023-10-27 22:10

本文主要是介绍一种IPC通信机制Gdbus详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、DBus介绍

常规进程间通信有管道,消息队列,共享内存,以及socket等,每个都有优劣,这次我们介绍一种高阶的进程间通信方式DBus。
DBus通信是IPC通信机制的一种方式,本身是建立在socket机制之上,它支持远程函数调用和信号传递。
它有两种模式,分别为:session(会话模式)、system(总线模式)
总线模式(system bus):采用总线模式时,系统需要维护一个DBus Daemon,每个DBus的请求通过DBus Daemon转发。这种模式Server只需要维护一个与DBus Daemon的链接,结构清晰,可以通过广播的方式发送消息到各个Client。
会话模式(session bus):这种模式一般称之为点对点的星型结构,Client与Server之间是直接的Peer2Peer的连接,少了DBus Daemon的中转,因此性能较好。
在这里插入图片描述

二、DBus API

glib API中有两种可以用的,分别是dbus-glib和gdbus,dbus-glib是一种老的API,现在不推荐使用了,gdbus是从glib2.26开始添加的,目前推荐使用。gdbus和glib-dbus都是GNU组织开发的,gdbus可以认为是dbus-glib的升级版,其编程过程比起dbus-glib来要简单得多。

三、DBus 开发

多进程之间需要通信,可能有一对一,可能有一对多,多对多的通信方式。为简单起见,我们假设某个系统中就只有两个进程需要通信——我们称之为Server和Client。基于GDBus的进程间通信,首先就是要定义好Server和Client之间的通信接口。在GDBus编程框架下,我们使用xml文件来描述接口,然后通过GDbus提供的工具生成对应的C/C++代码。
通常应用的组织形式如下,服务端发布的时候同时提供接口库供其他客户端程序调用,客户端连接接口库即可,像正常的接口调用,如下图,下面我们通过一个设置、获取一个人姓名和年龄,并且设置完姓名和年龄后会广播给所有客户端的程序来学习一下GDBus。
在这里插入图片描述
interface_people.xml

<?xml version="1.0" encoding="UTF-8"?>
<node><interface name="gdbus.demo.people"><method name="set_name"><arg name="name" type="s" direction="in"/><arg name="ret" type="b" direction="out"/></method><method name="get_name"><arg name="name" type="s" direction="out"/></method><method name="set_age"><arg name="age" type="i" direction="in"/><arg name="ret" type="b" direction="out"/></method><method name="get_age"><arg name="age" type="i" direction="out"/></method><signal name="send_info"><arg name="name" type="s"/><arg name="age" type="i"/></signal></interface>
</node>

四、数据类型介绍

上面XML中使用了一些数据类型,对应关系如下表。
1、基本数据类型

NameCode in D-BusData Type in glibData Type in libdbus-C++
BYTE‘y’gucharunsigned char
BOOLEAN‘b’gbooleanbool
INT16‘n’gint16signed short
UINT16‘q’guint16unsigned short
INT32‘i’gintint
UINT32‘u’guintunsigned int
INT64‘x’gint64signed long long
UINT64‘t’guint64unsigned long long
DOUBLE‘d’gdoubledouble
STRING‘s’const gchar *std::string
OBJECT_PATH‘o’const gchar *DBus::Path :public std::string
UNIX_FD‘h’GVariant *int
SIGNATURE‘g’const gchar *DBus::Signature :public std::string

2、复杂数据类型

NameCode in D-BusData Type in glibData Type in libdbus-C++
STRUCT‘(‘ and ‘)’GvariantDBus::Struct<>
ARRAY‘a’Gvariantstd::vector<>
VARIANT‘v’GvariantDBus::Variant
DICT_ENTRY‘{’ and ‘}’(Only appear after ‘a’)GvariantWhen it is used together with ‘a’,it is representedby std::map<>

五、程序运行

gdbus运行需要先运行deamon和环境变量
方法一:
1、运行下面脚本,生成地址,同时这个daemon要跑着

dbus-daemon --config-file=/etc/dbus-1/session.conf --print-address

2、生成的地址替换掉下面的地址部分

export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-4z6nMwqewd,guid=e6359f4ef81d07fc7185fb8b6059d267

3、在需要运行程序的终端中设置上面的环境变量
方法二:
1、执行完下面脚本,然后在同一终端运行Server和Client

eval dbus-launch --auto-syntax

方法三:

export DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-cwrMdqoyqU,guid=4780330fcb5755ab1492ce8800000024"
dbus-daemon --config-file=/etc/dbus-1/session.conf --address=$DBUS_SESSION_BUS_ADDRESS

六、代码附录

文件结构如下
在这里插入图片描述
重点代码
client.cpp

#include <iostream>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>#include "interface.h"using namespace std;int main(void)
{string name = "";int age = 0;interface iface;iface.initDBusCommunication();name = "dongdong.fan";iface.set_name(name);iface.set_age(30);name = iface.get_name();age = iface.get_age();cout << "Have a people name:" << name << "age:" << age;while (1){sleep(10);}return 0;
}

service.cpp

#include <unistd.h>#include "people.h"
#include "gdbusServer.h"int main()
{initDBusCommunication();while (1){sleep(10);}return 0;
}

gdbusServer.cpp

#include "gdbusServer.h"#include <ctype.h>
#include <gio/gio.h>
#include <glib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>#include "gdbusCommon.h"
#include "people.h"
#include "people_gen_interface.h"static guint g_own_id = 0;
static GMainLoop *g_pLoop = NULL;
static GdbusDemoPeople *g_pSkeleton = NULL;
static people g_people;// start
static gboolean set_name_method(GdbusDemoPeople *object,GDBusMethodInvocation *invocation,const gchar *in_arg,gpointer user_data) {my_printf("Server set_name_method is call, arg is : %s.\n", in_arg);bool ret = 0;string strTemp = in_arg;ret = g_people.set_name(strTemp);gdbus_demo_people_complete_set_name(object, invocation, ret);// 同时广播gdbus_demo_people_emit_send_info(object,g_people.get_name().c_str(),g_people.get_age());return TRUE;
}static gboolean get_name_method(GdbusDemoPeople *object,GDBusMethodInvocation *invocation,const gchar *in_arg,gpointer user_data) {my_printf("Server get_name_method is call.\n");string strTemp = g_people.get_name();gdbus_demo_people_complete_get_name(object, invocation, strTemp.c_str());return TRUE;
}static gboolean set_age_method(GdbusDemoPeople *object,GDBusMethodInvocation *invocation,gint in_arg,gpointer user_data) {my_printf("Server set_age_method is call, arg is : %d.\n", in_arg);bool ret = 0;ret = g_people.set_age(in_arg);gdbus_demo_people_complete_set_age(object, invocation, ret);// 同时广播gdbus_demo_people_emit_send_info(object,g_people.get_name().c_str(),g_people.get_age());return TRUE;
}static gboolean get_age_method(GdbusDemoPeople *object,GDBusMethodInvocation *invocation,gpointer user_data) {my_printf("Server get_age_method is call.\n");int age = g_people.get_age();gdbus_demo_people_complete_get_age(object, invocation, age);return TRUE;
}
// endstatic void GBusAcquired_Callback(GDBusConnection *connection,const gchar *name,gpointer user_data) {GError *pError = NULL;g_pSkeleton = gdbus_demo_people_skeleton_new();// Attach to dbus signals// start(void)g_signal_connect(g_pSkeleton, "handle-set-name", G_CALLBACK(set_name_method), NULL);(void)g_signal_connect(g_pSkeleton, "handle-get-name", G_CALLBACK(get_name_method), NULL);(void)g_signal_connect(g_pSkeleton, "handle-set-age", G_CALLBACK(set_age_method), NULL);(void)g_signal_connect(g_pSkeleton, "handle-get-age", G_CALLBACK(get_age_method), NULL);// end(void)g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(g_pSkeleton),connection,GDBUS_DEMO_PEOPLE_OBJECT_PATH,&pError);if (pError == 0) {my_printf("skeleton export successfully. \n");} else {my_printf("Error: Failed to export object. Reason: %s.\n", pError->message);g_error_free(pError);g_main_loop_quit(g_pLoop);return;}
}static void GBusNameAcquired_Callback(GDBusConnection *connection,const gchar *name,gpointer user_data) {my_printf("GBusNameAcquired_Callback, Acquired bus name: %s \n", GDBUS_DEMO_PEOPLE_NAME);
}static void GBusNameLost_Callback(GDBusConnection *connection,const gchar *name,gpointer user_data) {if (connection == NULL) {my_printf("GBusNameLost_Callback, Error: Failed to connect to dbus. \n");} else {my_printf("GBusNameLost_Callback, Error: Failed to get dbus name : %s\n", GDBUS_DEMO_PEOPLE_NAME);}g_main_loop_quit(g_pLoop);
}static void *run_callback(void *arg) {g_main_loop_run(g_pLoop);return NULL;
}bool initDBusCommunication(void) {bool ret = false;pthread_t tid;g_pLoop = g_main_loop_new(NULL, FALSE);if (NULL == g_pLoop) {my_printf("g_pLoop is NULL\n");goto LEAVE;}g_own_id = g_bus_own_name(G_BUS_TYPE_SESSION,GDBUS_DEMO_PEOPLE_NAME,G_BUS_NAME_OWNER_FLAGS_NONE,&GBusAcquired_Callback,&GBusNameAcquired_Callback,&GBusNameLost_Callback,NULL,NULL);pthread_create(&tid, NULL, run_callback, NULL);ret = true;
LEAVE:return ret;
}bool uinitDBusCommunication(void) {g_bus_unown_name(g_own_id);g_main_loop_unref(g_pLoop);return true;
}

gdbusServer.cpp

#ifndef _GDBUS_SERVER_H_
#define _GDBUS_SERVER_H_bool initDBusCommunication(void);
bool uinitDBusCommunication(void);#endif

people.cpp

#include "people.h"people::people()
{m_name = "default";m_age = 20;
}people::~people()
{//todo
}bool people::set_name(string &name)
{m_name = name;return true;
}string people::get_name()
{return m_name;
}bool people::set_age(int age)
{m_age = age;return true;
}int people::get_age()
{return m_age;
}

people.hpp

#ifndef _PEOPLE_H_
#define _PEOPLE_H_#include <string>using namespace std;class people
{public:people();~people();bool set_name(string &name);string get_name();bool set_age(int age);int get_age();private:string m_name;     // 姓名int m_age;         // 年龄  
};#endif

interface.hpp

#ifndef  _INTERFACE_H_
#define  _INTERFACE_H_#include <string>class interface
{public:interface();virtual ~interface();bool initDBusCommunication(void);//peoplebool set_name(std::string &name);std::string get_name();bool set_age(int age);int get_age();
};#endif

这篇关于一种IPC通信机制Gdbus详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/288728

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

【STM32】SPI通信-软件与硬件读写SPI

SPI通信-软件与硬件读写SPI 软件SPI一、SPI通信协议1、SPI通信2、硬件电路3、移位示意图4、SPI时序基本单元(1)开始通信和结束通信(2)模式0---用的最多(3)模式1(4)模式2(5)模式3 5、SPI时序(1)写使能(2)指定地址写(3)指定地址读 二、W25Q64模块介绍1、W25Q64简介2、硬件电路3、W25Q64框图4、Flash操作注意事项软件SPI读写W2

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

【Tools】大模型中的自注意力机制

摇来摇去摇碎点点的金黄 伸手牵来一片梦的霞光 南方的小巷推开多情的门窗 年轻和我们歌唱 摇来摇去摇着温柔的阳光 轻轻托起一件梦的衣裳 古老的都市每天都改变模样                      🎵 方芳《摇太阳》 自注意力机制(Self-Attention)是一种在Transformer等大模型中经常使用的注意力机制。该机制通过对输入序列中的每个元素计算与其他元素之间的相似性,