2
0
mirror of https://github.com/boostorg/asio.git synced 2026-02-24 14:22:08 +00:00
Files
asio/test/experimental/coro/use_coro.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

78 lines
1.7 KiB
C++

//
// experimental/coro/use_coro.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/use_coro.hpp>
#include <boost/asio/steady_timer.hpp>
#include <iostream>
#include "../../unit_test.hpp"
using namespace boost::asio::experimental;
namespace coro {
boost::asio::experimental::coro<void(), int>
awaiter(boost::asio::any_io_executor exec)
{
boost::asio::steady_timer timer{exec};
co_await timer.async_wait(use_coro);
co_return 42;
}
boost::asio::experimental::coro<void() noexcept, int>
awaiter_noexcept(boost::asio::any_io_executor exec)
{
boost::asio::steady_timer timer{exec};
auto ec = co_await timer.async_wait(use_coro);
BOOST_ASIO_CHECK(ec == boost::system::error_code{});
co_return 42;
}
void stack_test2()
{
bool done = false;
boost::asio::io_context ctx;
auto k = awaiter(ctx.get_executor());
auto k2 = awaiter_noexcept(ctx.get_executor());
k.async_resume(
[&](std::exception_ptr ex, int res)
{
BOOST_ASIO_CHECK(!ex);
BOOST_ASIO_CHECK(res == 42);
done = true;
});
k2.async_resume([&](int res)
{
BOOST_ASIO_CHECK(res == 42);
done = true;
});
ctx.run();
BOOST_ASIO_CHECK(done);
}
} // namespace coro
BOOST_ASIO_TEST_SUITE
(
"coro/use_coro",
BOOST_ASIO_TEST_CASE(::coro::stack_test2)
)