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

相关内容

热门资讯

世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...