如何将error_code设置为asio :: yield_context [英] How to set error_code to asio::yield_context

查看:142
本文介绍了如何将error_code设置为asio :: yield_context的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个异步函数,该函数采用最后一个参数boost :: asio :: yield_context。例如:

I'd like to create an asynchronous function which takes as it's last argument boost::asio::yield_context. E.g.:

int async_meaning_of_life(asio::yield_context yield);

我还希望与Asio返回错误代码的方式保持一致。也就是说,如果用户这样做:

I'd also like to be consistent with how Asio returns error codes. That is, if the user does:

int result = async_meaning_of_life(yield);

函数失败,然后抛出 system_error 例外。但是如果用户这样做:

and the function fails, then it throws the system_error exception. But if the user does:

boost::error_code ec;
int result = async_meaning_of_life(yield[ec]);

然后-而不是抛出-错误在 ec

Then - instead of throwing - the error is returned in ec.

问题是,在实现函数时,我似乎找不到一种干净的方法来检查是否使用了operator []并进行设置我们想出了这样的东西:

The problem is that when implementing the function, I can't seem to find a clean way to check whether the operator[] was used or not and set it if so. We came up with something like this:

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_ 声明为私有(尽管仅在文档中)。

But that's hacky, because yield_context::ec_ is declared private (although only in the documentation).

我可以想到的另一种方法是转换收益对象放入 asio :: handler_type 并执行。

One other way I can think of doing this is to convert the yield object into asio::handler_type and execute it. But this solution seems awkward at best.

还有另一种方式吗?

推荐答案

Asio使用 async_result 透明地提供 use_future yield_context 或API接口中的完成处理程序。¹

Asio uses async_result to transparently provide use_future, yield_context or completion handlers in its API interfaces.¹

以下是模式的运行方式:

Here's how the pattern goes:

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);




综合演示



显示如何与

Comprehensive Demo

Showing how to use it with with


  • 色度和产量[ec]

  • Coro和收益+例外

  • std :: future

  • 完成处理程序

  • coro's and yield[ec]
  • coro's and yield + exceptions
  • std::future
  • completion handlers

在Coliru上直播

#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




注意:为简单起见,我没有添加输出同步,因此输出可以根据运行时执行顺序混合在一起

Note: for simplicity I have not added output synchronization, so the output can become intermingled depending on runtime execution order






¹例如关于如何使用它以您自己的异步结果模式扩展库的出色演示增强: :asio with boost :: unique_future

这篇关于如何将error_code设置为asio :: yield_context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆