如何解决使用结果启动异步操作
我目前正在开发一个使用 boost asio 进行网络连接的项目。我想实现一个如下所示的函数:
template<command T,response R = typename T::response_type>
auto send_command(T c) -> boost::asio::awaitable<R> {
// ...
}
在我的代码中,当我将命令写入套接字时,我最终会收到带有结果的响应。在获得对特定命令的响应之前,我可能会在套接字中收到任意数量的其他响应。
我想我可以使用 async_initiate
使发送命令成为由写入和读取组成的异步操作。
我已经使用 async_initiate
来启动异步操作,但它属于 boost::asio::awaitable<void>
类型。代码如下所示:
// returns the awaitable for the coroutine to suspend
return asio::async_initiate<CompletionToken,void(void)>(
[self = shared_from_this()](auto&& handler) {
self->callback_queue.push(std::forward<decltype(handler)>(handler));
}
);
// later
auto& resume = callback_queue.front();
resume(); // resume the suspended coroutine
但是当 It 不是 void 时,我不知道将返回值放在哪里!
auto const command_id = std::int32_t{...};
auto& resume_command = commands_continuations[command_id];
resume_command(response_result); // Send the `co_await` result in parameters??
我只是对如何发送可等待对象的返回以及如何使用结果启动异步操作感到困惑。
解决方法
事实证明,异步操作可以采用许多固定签名。
void(void)
是一个,但要得到结果,我们可以简单地使用 void(T)
:
return asio::async_initiate<CompletionToken,void(R)>(
[self = shared_from_this()](auto&& handler) {
self->callback_queue.push(std::forward<decltype(handler)>(handler));
}
);
handler
将是一个以 R
作为参数的可调用对象,并将成为 co_await
操作的结果。
为了进行异常处理、取消,我尝试了两种不同的方式来传播异常。一种是发送 std::exception_ptr
,另一种是使用 boost::system::error_code
。就我而言,我选择错误代码是因为我更习惯使用它们。
return asio::async_initiate<CompletionToken,void(boost::system::error_code,R)>(
[self = shared_from_this()](auto&& handler) {
self->callback_queue.push(std::forward<decltype(handler)>(handler));
}
);
回调采用与签名 void(boost::system::error_code,R)
匹配的仅移动函数的形式。我这样称呼它:
auto callback = std::exchange(callback_queue[command_id],{});
if (result) {
// resumes the coroutine normally with the result
callback(boost::system::error_code{},*result);
} else {
// Throws exception in the coroutine
callback(make_error_code(boost::system::errc::operation_canceled),R{});
}
的文档中有更多关于它的信息版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。