sdbusplus除了提供以同步的方式调用service的method外,还提供了多种异步调用的方式。
下面的例子通过async_method_call完成异步调用:
//async_call.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 doAsyncCallMethod()
{
boost::asio::io_context io;
auto conn = make_shared<sdbusplus::asio::connection>(io);
conn->async_method_call(
[](boost::system::error_code ec, string res) {
if (ec)
{
cout<<"AddString returned error with async_method_call ec = "<<ec<<" "<<ec.message()<<endl;
return;
}
cout << "res:" << res << endl;
},
"calculate.servic