blob: e9e036d5b92251b7dc5bf46c6d31e7fe5e8a27f9 [file] [log] [blame]
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once
#include <sdbusplus/message.hpp>
#include <sdbusplus/bus.hpp>
#include <systemd/sd-bus.h>
#include <boost/asio.hpp>
namespace sdbusplus
{
namespace asio
{
namespace detail
{
template <typename Handler> struct async_send_handler
{
Handler handler_;
async_send_handler(Handler&& handler) : handler_(std::move(handler))
{
}
async_send_handler(Handler& handler) : handler_(handler)
{
}
void operator()(sd_bus* conn, message::message& mesg)
{
async_send_handler* context = new async_send_handler(std::move(*this));
// 0 is the default timeout
int ec =
sd_bus_call_async(conn, NULL, mesg.get(), &callback, context, 0);
if (ec < 0)
{
auto err =
make_error_code(static_cast<boost::system::errc::errc_t>(ec));
context->handler_(err, mesg);
delete context;
}
}
static int callback(sd_bus_message* mesg, void* userdata,
sd_bus_error* error)
{
if (userdata == nullptr || mesg == nullptr)
{
return -1;
}
std::unique_ptr<async_send_handler> context(
static_cast<async_send_handler*>(userdata));
message::message message(mesg);
auto ec = make_error_code(
static_cast<boost::system::errc::errc_t>(message.get_errno()));
context->handler_(ec, message);
return 1;
}
};
} // namespace detail
} // namespace asio
} // namespace sdbusplus