mirror of
https://github.com/boostorg/asio.git
synced 2026-02-27 15:12:12 +00:00
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();
}
158 lines
4.0 KiB
C++
158 lines
4.0 KiB
C++
//
|
|
// experimental/coro/cancel.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 <iostream>
|
|
#include <boost/asio/io_context.hpp>
|
|
#include <boost/asio/steady_timer.hpp>
|
|
#include <boost/scope_exit.hpp>
|
|
#include "../../unit_test.hpp"
|
|
|
|
using namespace boost::asio::experimental;
|
|
namespace this_coro = boost::asio::this_coro;
|
|
|
|
namespace coro {
|
|
|
|
void coro_simple_cancel()
|
|
{
|
|
boost::asio::io_context ctx;
|
|
|
|
auto k =
|
|
[](boost::asio::io_context& ) noexcept
|
|
-> boost::asio::experimental::coro<
|
|
void() noexcept, boost::system::error_code>
|
|
{
|
|
boost::asio::steady_timer timer{
|
|
co_await this_coro::executor,
|
|
std::chrono::seconds(1)};
|
|
|
|
BOOST_ASIO_CHECK(
|
|
!(co_await this_coro::cancellation_state).cancelled());
|
|
|
|
auto ec = co_await timer;
|
|
|
|
BOOST_ASIO_CHECK(
|
|
(co_await this_coro::cancellation_state).cancelled());
|
|
|
|
co_return ec;
|
|
}(ctx);
|
|
|
|
boost::system::error_code res_ec;
|
|
k.async_resume([&](boost::system::error_code ec) {res_ec = ec;});
|
|
boost::asio::post(ctx, [&]{k.cancel();});
|
|
|
|
BOOST_ASIO_CHECK(!res_ec);
|
|
|
|
ctx.run();
|
|
|
|
BOOST_ASIO_CHECK(res_ec == boost::asio::error::operation_aborted);
|
|
}
|
|
|
|
void coro_throw_cancel()
|
|
{
|
|
boost::asio::io_context ctx;
|
|
|
|
auto k = [](boost::asio::io_context& )
|
|
-> boost::asio::experimental::coro<void() , void>
|
|
{
|
|
boost::asio::steady_timer timer{
|
|
co_await this_coro::executor,
|
|
std::chrono::seconds(1)};
|
|
co_await timer;
|
|
}(ctx);
|
|
|
|
std::exception_ptr res_ex;
|
|
k.async_resume([&](std::exception_ptr ex) {res_ex = ex;});
|
|
boost::asio::post(ctx, [&]{k.cancel();});
|
|
|
|
BOOST_ASIO_CHECK(!res_ex);
|
|
|
|
ctx.run();
|
|
|
|
BOOST_ASIO_CHECK(res_ex);
|
|
try
|
|
{
|
|
std::rethrow_exception(res_ex);
|
|
}
|
|
catch (boost::system::system_error& se)
|
|
{
|
|
BOOST_ASIO_CHECK(se.code() == boost::asio::error::operation_aborted);
|
|
}
|
|
}
|
|
|
|
void coro_simple_cancel_nested()
|
|
{
|
|
boost::asio::io_context ctx;
|
|
|
|
auto k = [](boost::asio::io_context&, int& cnt) noexcept
|
|
-> boost::asio::experimental::coro<
|
|
void() noexcept, boost::system::error_code>
|
|
{
|
|
boost::asio::steady_timer timer{
|
|
co_await this_coro::executor,
|
|
std::chrono::milliseconds(100)};
|
|
|
|
BOOST_ASIO_CHECK(!(co_await this_coro::cancellation_state).cancelled());
|
|
auto ec = co_await timer;
|
|
cnt++;
|
|
BOOST_ASIO_CHECK((co_await this_coro::cancellation_state).cancelled());
|
|
|
|
co_return ec;
|
|
};
|
|
|
|
int cnt = 0;
|
|
auto kouter = [&](boost::asio::io_context& ctx, int& cnt) noexcept
|
|
-> boost::asio::experimental::coro<
|
|
boost::system::error_code() noexcept,
|
|
boost::system::error_code>
|
|
{
|
|
BOOST_ASIO_CHECK(cnt == 0);
|
|
co_yield co_await k(ctx, cnt);
|
|
BOOST_ASIO_CHECK(cnt == 1);
|
|
auto ec = co_await k(ctx, cnt);
|
|
BOOST_ASIO_CHECK(cnt == 2);
|
|
co_return ec;
|
|
}(ctx, cnt);
|
|
|
|
boost::system::error_code res_ec;
|
|
kouter.async_resume([&](boost::system::error_code ec) {res_ec = ec;});
|
|
boost::asio::post(ctx, [&]{kouter.cancel();});
|
|
BOOST_ASIO_CHECK(!res_ec);
|
|
ctx.run();
|
|
BOOST_ASIO_CHECK(res_ec == boost::asio::error::operation_aborted);
|
|
|
|
ctx.restart();
|
|
res_ec = {};
|
|
kouter.async_resume([&](boost::system::error_code ec) {res_ec = ec;});
|
|
boost::asio::post(ctx, [&]{kouter.cancel();});
|
|
BOOST_ASIO_CHECK(!res_ec);
|
|
ctx.run();
|
|
BOOST_ASIO_CHECK(res_ec == boost::asio::error::operation_aborted);
|
|
BOOST_ASIO_CHECK(cnt == 2);
|
|
}
|
|
|
|
} // namespace coro
|
|
|
|
BOOST_ASIO_TEST_SUITE
|
|
(
|
|
"coro/cancel",
|
|
BOOST_ASIO_TEST_CASE(::coro::coro_simple_cancel)
|
|
BOOST_ASIO_TEST_CASE(::coro::coro_throw_cancel)
|
|
BOOST_ASIO_TEST_CASE(::coro::coro_simple_cancel_nested)
|
|
)
|