sdbusplus:通过async_send异步调用service的method
2023-04-18 16:49:12 时间
下面的例子通过async_send完成异步调用:
//async_send.cpp
#include <iostream>
#include <boost/asio.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
using namespace std;
using namespace sdbusplus;
void asyncCallSend(shared_ptr<sdbusplus::asio::connection> conn)
{
auto mesg1 = conn->new_method_call("calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt");
mesg1.append(1, 2);
cout<<"1 async_send begin"<<endl;
conn->async_send(mesg1, [](boost::system::error_code ec, message::message& ret) {
cout << "async_send callback
";
if (ec)
{
cout<<"error with async_send"<<endl;
return;
}
int data = 0;
ret.read(data);
cout<<"ret data="<<data<<endl;
});
cout<<"2 async_send end"<<endl;
auto mesg2 = conn->new_method_call("calculate.service", "/calculate_obj", "calculate_infterface.data", "AddInt");
mesg2.append(3, 4);
cout<<"3 async_send begin"<<endl;
conn->async_send(mesg2, [](boost::system::error_code ec, message::message& ret) {
cout << "async_send callback
";
if (ec)
{
cout<<"error with async_send"<<endl;
return;
}
int data = 0;
ret.read(data);
cout<<"ret data="<<data<<endl;
});
cout<<"4 async_send end"<<endl;
}
int main()
{
boost::asio::io_context io;
auto conn = make_shared<sdbusplus::asio::connection>(io);
cout<<"asyncCallSend begin"<<endl;
asyncCallSend(conn);
cout<<"asyncCallSend end"<<endl;
io.run();
return 0;
}
编译程序:
g++ -o async_send ./async_send.cpp -lsdbusplus -lsystemd
运行程序输出:
asyncCallSend begin
1 async_send begin
2 async_send end
3 async_send begin
4 async_send end
asyncCallSend end
async_send callback
ret data=3
async_send callback
ret data=7
可以看出asyncCallSend被调用后,函数体执行时,相当于注册了两个回调函数,函数体顺序执行后,再通过回调从service的method拿到了返回值。
相关文章
- 苹果2017秋季发布会:Apple Watch新功能抢先看
- Android Input子系统:Input进程的创建,监听线程的启动
- 奥利奥才发,谷歌已经着手开发Android P
- 苹果申请声波屏下指纹识别专利 精度足以取代Touch ID
- 擦亮自己的眼睛去看SQL Server之说说跟踪
- 开发者分享:20个关于Unity使用建议和技巧
- 别说自己懂手机 还玩Root的才是发烧友
- 苹果机器学习期刊「Siri 三部曲」之一:通过跨带宽和跨语言初始化提升神经网络声学模型
- 挨踢部落故事汇(26):全栈攻城狮之葵花宝典
- 【直播】向日葵技术总监张小峰:国内首例谈MySQL InnoDB memcached plugin实践
- 在Object-C中学习排序算法
- 大数据时代已来临,你准备好了吗?
- 机器学习在金融领域的四大优势和五大应用
- iOS如何进行优化项目?
- 2017 Android GitHub常用开源框架汇总
- 函数调用中堆栈的个人理解
- 初识Rust语言的所有权概念
- 深入理解游戏中寻路算法
- 甲骨文收益同比增长 用户抱怨声也越来越多
- Home Contact Gallery RSS Git 在团队中的最佳实践--如何正确使用Git Flow