2
0
mirror of https://github.com/boostorg/asio.git synced 2026-02-22 01:32:08 +00:00
Christopher Kohlhoff 1c0f3c36be Add experimental::co_composed.
The following example illustrates a simple asynchronous operation that
implements an echo protocol in terms of a coroutine:

  template <typename CompletionToken>
  auto async_echo(tcp::socket& socket,
      CompletionToken&& token)
  {
    return boost::asio::async_initiate<
      CompletionToken, void(boost::system::error_code)>(
        boost::asio::experimental::co_composed<
          void(boost::system::error_code)>(
            [](auto state, tcp::socket& socket) -> void
            {
              try
              {
                state.throw_if_cancelled(true);
                state.reset_cancellation_state(
                  boost::asio::enable_terminal_cancellation());

                for (;;)
                {
                  char data[1024];
                  std::size_t n = co_await socket.async_read_some(
                      boost::asio::buffer(data), boost::asio::deferred);

                  co_await boost::asio::async_write(socket,
                      boost::asio::buffer(data, n), boost::asio::deferred);
                }
              }
              catch (const boost::system::system_error& e)
              {
                co_return {e.code()};
              }
            }, socket),
        token, std::ref(socket));
  }
2022-11-01 11:35:07 +11:00
2022-11-01 11:35:07 +11:00
2022-11-01 11:00:16 +11:00
2021-03-04 09:02:43 +11:00
2022-11-01 11:35:07 +11:00
2022-03-02 21:23:52 +11:00
2006-06-14 22:26:36 +00:00
2021-11-01 21:19:46 +11:00
Description
Mirrored via gitea-mirror
24 MiB
Languages
C++ 99.6%
HTML 0.2%
Perl 0.2%