I'm migrating my asio application from its stackful coroutines to c++20 stackless coroutines. I have an existing class method that looks like this:
int my_async_op(asio::yield_context yield) {
using tResult = asio::async_result<asio::yield_context, void(boost::system::error_code, int)>;
tResult::completion_handler_type handler(yield);
tResult result(handler);
...
boost::system::error_code ec;
asio::some_async_op(yield[ec]);
...
handler(boost::system::error_code(), 42);
return result.get();
}
...and is called like this:
boost::system::error_code ec;
x = my_async_op(yield[ec]);
When migrating to C++20 stackless coroutines, chaining is required and a skeleton of my function now looks something like this:
asio::awaitable<int> my_async_op(...) {
...
boost::system::error_code ec;
co_await asio::some_async_op(net::redirect_error(net::use_awaitable, ec));
...
co_return 42;
}
...but is called like this:
boost::system::error_code ec;
x = co_await my_async_op(net::redirect_error(net::use_awaitable, ec));
So the skeleton needs updating to take a completion token, the same as native asio async ops, but I can find a reference example to work off of and I admit to finding asio source code difficult to parse.
Any guidance or references would be appreciated.
Edit: I think I'm getting close with asio::async_initiate per http://open-std.org/JTC1/SC22/WG21/docs/papers/2019/p1943r0.html. My function now looks like this:
template<typename T>
auto my_async_op<T&& token) {
return asio::async_initiate<T, void(boost::system::error_code, int)>(
[&](auto handler)->void {
...
boost::system::error_code ec;
co_await asio::some_async_op(asio::redirect_error(asio::use_awaitable, ec));
...
handler(boost::system::error_code(), 42);
},
token
);
}
Only problem is that I get a compiler error on that nested co_await call:
XXX.h:97:12: error: unable to find the promise type for this coroutine
97 | co_await asio::some_async_op(net::redirect_error(net::use_awaitable, ec));
Will keep grinding away.
Edit: Looking into this now... https://github.com/chriskohlhoff/asio/issues/795