簡體   English   中英

如何將 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返回錯誤。

問題是,在實現該功能時,我似乎無法找到一種干凈的方法來檢查是否使用了 operator[] 並在使用時進行設置。 我們想出了這樣的事情:

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_context::ec_聲明為私有的(盡管僅在文檔中)。

我能想到的另一種方法是將yield對象轉換為asio::handler_type並執行它。 但是這個解決方案充其量看起來很尷尬。

還有其他方法嗎?

Asio 使用async_result在其 API 接口中透明地提供use_futureyield_context或完成處理程序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 和 yield[ec]
  • coro 和 yield + 例外
  • 標准::未來
  • 完成處理程序

住在 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

注意:為簡單起見,我沒有添加輸出同步,因此輸出可以根據運行時執行順序混合


¹ 參見例如這個關於如何使用它通過您自己的異步結果模式boost::asio 和 boost::unique_future擴展庫的優秀演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM