-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asio integration - completion token #73
Comments
Hi, template <typename Handler>
auto to_completion_token(Handler&& token) {
return [token = std::forward<Handler>(token)](auto&& continuation) {
auto executor = asio::get_associated_executor(token);
return std::forward<decltype(continuation)>(continuation)
.then(
[token = std::move(token)]() mutable {
std::move(token)();
},
[executor = std::move(executor)](auto&& work) mutable {
asio::post(executor, std::forward<decltype(work)>(work));
});
};
} with template <typename Handler>
auto async_wait_twice(asio::steady_timer& timer, Handler&& token) {
return cti::async([&] {
timer.expires_after(1s);
return timer.async_wait(cti::use_continuable);
})
.then([&] {
printf("First timer wait finished\n");
timer.expires_after(1s);
return timer.async_wait(cti::use_continuable);
})
.then([] {
printf("Second timer wait finished\n");
return cti::make_ready_continuable();
})
.apply(to_completion_token(std::forward<Handler>(token)));
}
int main() {
asio::io_context ctx;
asio::steady_timer timer(ctx);
auto guard = asio::make_work_guard(ctx);
std::thread t([&] {
ctx.run();
});
std::latch latch(1);
async_wait_twice(timer, [&] {
printf("Two timers called\n");
latch.count_down();
});
latch.wait();
ctx.stop();
t.join();
return 0;
} In case you intended to use a completion token like |
That's a great answer thank you. But I was hopping to have an adaptor (or whatever we want to call it) which would allow me to use But maybe that's too much work. Maybe the better approach is to juse use |
So the following is roughly equivalent using
However i'm ignoring all the error codes. Each |
@Naios
I would like to use continuable to build Asio composed operations as described https://think-async.com/Asio/boost_asio_1_30_2/doc/html/boost_asio/overview/composition/compose.html but instead of using
async_compose()
orco_composed()
ordeferred()
, I would like to use this library as i think it provides a better abstraction.Is this possible?
A few subtleties:
Commit Hash
23a724c
Expected Behavior
What I would like is a transformation algorithm
cti::transforms::to_completion_token()
which would allow the following to work:Actual Behavior
N/A
Steps to Reproduce
N/A
Your Environment
The text was updated successfully, but these errors were encountered: