sdbusplus:通过connect调用new_method_call完成同步调用service的method
创始人
2025-05-29 14:08:11
sdbusplus:通过new_method_call同步调用service的method_风静如云的博客-CSDN博客

完成了通过bus的new_method_call方法同步调用service的method

sdbusplus还可以通过connect的new_method_call方法完成同步调用service的method的方式:

//sync_call.cpp
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include using namespace std;
using namespace sdbusplus;void syncCallMethod()
{using return_type = vector>;boost::asio::io_context io;auto b = bus::new_default_system();auto conn = make_shared(io, bus::details::bus_friend::get_busp(b));auto methodCall = conn->new_method_call("org.freedesktop.login1", "/org/freedesktop/login1","org.freedesktop.login1.Manager", "ListUsers");auto reply  = conn->call(methodCall);return_type users;reply.read(users);for (auto& user : users){cout << get(user) << "\n";}
}int main()
{syncCallMethod();return 0;
}
编译程序:
g++ -o sync_call sync_call.cpp -lsdbusplus -lsystemd
在ubuntu上运行,可以输出当前系统里的用户列表

同样需要注意的是:根据需要创建系统总线或用户总线的对象,参照:

//sync_call.cpp
#include 
#include 
#include 
#include 
#include using namespace std;
using namespace sdbusplus;void syncCallMethod()
{boost::asio::io_context io;auto b = bus::new_default_user();auto conn = make_shared(io, bus::details::bus_friend::get_busp(b));auto methodCall = conn->new_method_call("calculate.service", "/calculate_obj","calculate_infterface.data", "AddInt");methodCall.append(1, 2);auto reply  = conn->call(methodCall);int res = 0;reply.read(res);cout<<"res:"<
需要使用的service:
sdbusplus:添加service_风静如云的博客-CSDN博客
编译程序:
g++ -o sync_call sync_call.cpp -lsdbusplus -lsystemd

methodCall.append(1, 2); 用于添加method的参数
也可以调用多次:
methodCall.append(1);
methodCall.append(2);

运行程序:
res:3

相关内容

热门资讯

荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
cad打印线条粗细设置 cad... 004-线型(下)打印样式设置和线型文件使用一、线宽设置方法制图规范里边的线宽要求,我们已经定义好,...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...