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
上一篇:骷髅病毒分析