I am currently trying to use the new C++20 coroutines with boost::asio. However I am struggling to find out how to implement custom awaitable functions (like eg boost::asio::read_async). The problem I am trying to solve is the following:
I have a connection object where I can make multiple requests and register a callback for the response. The responses are not guaranteed to arrive in the order they have been requested. I tried wrapping the callback with a custom awaitable however I am unable to co_await this in the coroutine since there is no await_transform for my awaitable type in boost::asio::awaitable.
The code I tried to wrap the callback into an awaitable is adapted from here: https://books.google.de/books?id=tJIREAAAQBAJ&pg=PA457
auto async_request(const request& r)
{
struct awaitable
{
client* cli;
request req;
response resp{};
bool await_ready() { return false; }
void await_suspend(std::coroutine_handle<> h)
{
cli->send(req, [this, h](const response& r)
{
resp = r;
h.resume();
});
}
auto await_resume()
{
return resp;
}
};
return awaitable{this, r};
}
which I tried calling in a boost coroutine like this:
boost::asio::awaitable<void> network::sts::client::connect()
{
//...
auto res = co_await async_request(make_sts_connect());
//...
}
giving me the following error:
error C2664: 'boost::asio::detail::awaitable_frame_base<Executor>::await_transform::result boost::asio::detail::awaitable_frame_base<Executor>::await_transform(boost::asio::this_coro::executor_t) noexcept': cannot convert argument 1 from 'network::sts::client::async_request::awaitable' to 'boost::asio::this_coro::executor_t'
Is there any way to achieve this functionality ?