如何将error_code设置为 asio::yield_context
How to set error_code to asio::yield_context
我想创建一个异步函数,它将 boost::asio::yield_context 作为最后一个参数。 例如:
int async_meaning_of_life(asio::yield_context yield);
我还想与 Asio 返回错误代码的方式保持一致。也就是说,如果用户执行以下操作:
int result = async_meaning_of_life(yield);
并且函数失败,然后引发system_error
异常。但是,如果用户这样做:
boost::error_code ec;
int result = async_meaning_of_life(yield[ec]);
然后 - 而不是抛出 - 错误在 ec
中返回。
问题是在实现函数时,我似乎找不到一种干净的方法来检查是否使用了运算符[],如果是这样,则进行设置。我们想出了这样的东西:
inline void set_error(asio::yield_context yield, sys::error_code ec)
{
if (!yield.ec_) throw system_error(ec);
*(yield.ec_) = ec;
}
但这很笨拙,因为yield_context::ec_
被声明为私有的(尽管仅在文档中)。
我能想到的另一种方法是将yield
对象转换为asio::handler_type
并执行它。但这个解决方案充其量看起来很尴尬。
还有别的办法吗?
Asio 使用 async_result
在其 API 接口中透明地提供use_future
、yield_context
或完成处理程序。
模式是这样的:
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token)
{
typename asio::handler_type<Token, void(error_code, int)>::type
handler (std::forward<Token> (token));
asio::async_result<decltype (handler)> result (handler);
if (success)
handler(42);
else
handler(asio::error::operation_aborted, 0);
return result.get ();
}
更新
从 Boost 1.66 开始,该模式遵循为标准化提出的接口:
using result_type = typename asio::async_result<std::decay_t<Token>, void(error_code, int)>; typename result_type::completion_handler_type handler(std::forward<Token>(token)); result_type result(handler);
综合演示
展示如何使用它
- Coro's and yield[EC]
- CORO 和收益 + 例外
- 标准::未来
- 完成处理程序
住在科里鲁
#define BOOST_COROUTINES_NO_DEPRECATION_WARNING
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/use_future.hpp>
using boost::system::error_code;
namespace asio = boost::asio;
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token)
{
#if BOOST_VERSION >= 106600
using result_type = typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type
handler(std::forward<Token>(token));
asio::async_result<decltype (handler)> result (handler);
#endif
if (success)
handler(error_code{}, 42);
else
handler(asio::error::operation_aborted, 0);
return result.get ();
}
void using_yield_ec(asio::yield_context yield) {
for (bool success : { true, false }) {
boost::system::error_code ec;
auto answer = async_meaning_of_life(success, yield[ec]);
std::cout << __FUNCTION__ << ": Result: " << ec.message() << "n";
std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
}
}
void using_yield_catch(asio::yield_context yield) {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, yield);
std::cout << __FUNCTION__ << ": Answer: " << answer << "n";
} catch(boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
}
}
void using_future() {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "n";
} catch(boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "n";
}
}
void using_handler() {
for (bool success : { true, false })
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "n";
std::cout << "using_handler: Answer: " << answer << "n";
});
}
int main() {
asio::io_service svc;
spawn(svc, using_yield_ec);
spawn(svc, using_yield_catch);
std::thread work([] {
using_future();
using_handler();
});
svc.run();
work.join();
}
指纹:
using_yield_ec: Result: Success
using_yield_ec: Answer: 42
using_yield_ec: Result: Operation canceled
using_yield_ec: Answer: 0
using_future: Answer: 42
using_yield_catch: Answer: 42
using_yield_catch: Caught: Operation canceled
using_future: Caught: Operation canceled
using_handler: Result: Success
using_handler: Answer: 42
using_handler: Result: Operation canceled
using_handler: Answer: 0
注意:为简单起见,我没有添加输出同步,因此输出可能会根据运行时执行顺序混合在一起
¹ 例如,请参阅这个出色的演示,了解如何使用它来扩展库,并带有您自己的异步结果模式 boost::asio with boost::unique_future
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 提升 ASIO 无法识别计时器对象
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 缓慢提升ASIO
- 从 Boost ASIO 获取 epoll 描述符 io_service对象
- 如何在 Boost.Asio 中使用 Zero-copy sendmsg/receive
- C++ Boost::asio串行通信与Arduino无法写入
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?
- 提升 Asio TCP 服务器 处理多个客户端
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 提升 ASIO - io_service 不要等待连接到线程
- 执行时使用 boost::asio::d eadline_timer 时出错
- Boost.Asio/OpenSSL HTTPS GET certificate trouble
- C++ boost::asio::ip::tcp::acceptor 有时不接受连接器?
- boost::asio data owning `ConstBufferSequence`
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 如何将boost::asio::d eadline_timer 与Qt一起使用?