2
0
mirror of https://github.com/boostorg/asio.git synced 2026-02-24 14:22:08 +00:00
Files
asio/test/experimental/coro/exception.cpp
klemens-morgenstern ecca8406c0 Added experimental::coro class template.
The coro type is a C++20 coroutine primitive for resumable functions,
with the ability to combine both asynchronous waiting (co_await) and
yielding (co_yield) into a single, stateful control flow. For example:

    #include <boostasio.hpp>
    #include <boostasio/experimental/coro.hpp>

    using boost::asio::ip::tcp;

    boost::asio::experimental::coro<std::string> reader(tcp::socket& sock)
    {
      std::string buf;
      while (sock.is_open())
      {
        std::size_t n = co_await boost::asio::async_read_until(
            sock, boost::asio::dynamic_buffer(buf), '\n',
            boost::asio::experimental::use_coro);
        co_yield buf.substr(0, n);
        buf.erase(0, n);
      }
    }

    boost::asio::awaitable<void> consumer(tcp::socket sock)
    {
      auto r = reader(sock);
      auto msg1 = co_await r.async_resume(boost::asio::use_awaitable);
      std::cout << "Message 1: " << msg1.value_or("\n");
      auto msg2 = co_await r.async_resume(boost::asio::use_awaitable);
      std::cout << "Message 2: " << msg2.value_or("\n");
    }

    boost::asio::awaitable<void> listen(tcp::acceptor& acceptor)
    {
      for (;;)
      {
        co_spawn(
            acceptor.get_executor(),
            consumer(co_await acceptor.async_accept(boost::asio::use_awaitable)),
            boost::asio::detached);
      }
    }

    int main()
    {
      boost::asio::io_context ctx;
      tcp::acceptor acceptor(ctx, {tcp::v4(), 54321});
      co_spawn(ctx, listen(acceptor), boost::asio::detached);
      ctx.run();
    }
2021-07-01 21:34:51 +10:00

171 lines
4.0 KiB
C++

//
// experimental/coro/exception.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2021 Klemens D. Morgenstern
// (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/asio/experimental/coro.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/awaitable.hpp>
#include "../../unit_test.hpp"
using namespace boost::asio::experimental;
namespace coro {
template <typename Func>
struct on_scope_exit
{
Func func;
static_assert(noexcept(func()));
on_scope_exit(const Func &f)
: func(static_cast< Func && >(f))
{
}
on_scope_exit(Func &&f)
: func(f)
{
}
on_scope_exit(const on_scope_exit &) = delete;
~on_scope_exit()
{
func();
}
};
boost::asio::experimental::coro<int> throwing_generator(
boost::asio::any_io_executor, int &last, bool &destroyed)
{
on_scope_exit x = [&]() noexcept { destroyed = true; };
(void)x;
int i = 0;
while (i < 3)
co_yield last = ++i;
throw std::runtime_error("throwing-generator");
}
boost::asio::awaitable<void> throwing_generator_test()
{
int val = 0;
bool destr = false;
{
auto gi = throwing_generator(
co_await boost::asio::this_coro::executor,
val, destr);
bool caught = false;
try
{
for (int i = 0; i < 10; i++)
{
BOOST_ASIO_CHECK(val == i);
const auto next = co_await gi.async_resume(boost::asio::use_awaitable);
BOOST_ASIO_CHECK(next);
BOOST_ASIO_CHECK(val == *next);
BOOST_ASIO_CHECK(val == i + 1);
}
}
catch (std::runtime_error &err)
{
caught = true;
using std::operator ""sv;
BOOST_ASIO_CHECK(err.what() == "throwing-generator"sv);
}
BOOST_ASIO_CHECK(val == 3);
BOOST_ASIO_CHECK(caught);
}
BOOST_ASIO_CHECK(destr);
};
void run_throwing_generator_test()
{
boost::asio::io_context ctx;
boost::asio::co_spawn(ctx, throwing_generator_test(), boost::asio::detached);
ctx.run();
}
boost::asio::experimental::coro<int(int)> throwing_stacked(
boost::asio::any_io_executor exec, int &val,
bool &destroyed_inner, bool &destroyed)
{
on_scope_exit x = [&]() noexcept { destroyed = true; };
(void)x;
auto gen = throwing_generator(exec, val, destroyed_inner);
while (auto next = co_await gen) // 1, 2, 4, 8, ...
BOOST_ASIO_CHECK(42 ==(co_yield *next)); // offset is delayed by one cycle
}
boost::asio::awaitable<void> throwing_generator_stacked_test()
{
int val = 0;
bool destr = false, destr_inner = false;
{
auto gi = throwing_stacked(
co_await boost::asio::this_coro::executor,
val, destr, destr_inner);
bool caught = false;
try
{
for (int i = 0; i < 10; i++)
{
BOOST_ASIO_CHECK(val == i);
const auto next =
co_await gi.async_resume(42, boost::asio::use_awaitable);
BOOST_ASIO_CHECK(next);
BOOST_ASIO_CHECK(val == *next);
BOOST_ASIO_CHECK(val == i + 1);
}
}
catch (std::runtime_error &err)
{
caught = true;
using std::operator ""sv;
BOOST_ASIO_CHECK(err.what() == "throwing-generator"sv);
}
BOOST_ASIO_CHECK(val == 3);
BOOST_ASIO_CHECK(caught);
}
BOOST_ASIO_CHECK(destr);
BOOST_ASIO_CHECK(destr_inner);
};
void run_throwing_generator_stacked_test()
{
boost::asio::io_context ctx;
boost::asio::co_spawn(ctx,
throwing_generator_stacked_test,
boost::asio::detached);
ctx.run();
}
} // namespace coro
BOOST_ASIO_TEST_SUITE
(
"coro/exception",
BOOST_ASIO_TEST_CASE(::coro::run_throwing_generator_stacked_test)
BOOST_ASIO_TEST_CASE(::coro::run_throwing_generator_test)
)