203 Commits

Author SHA1 Message Date
Christopher Kohlhoff
1afbc5c12b Update copyright notices. 2025-03-04 22:57:26 +11:00
Christopher Kohlhoff
6169ff92cb Use snprintf instead of sprintf in example. 2024-12-03 08:40:02 +11:00
Christopher Kohlhoff
7e5ab33105 Fix spelling errors. 2024-12-03 08:39:39 +11:00
Rene Rivera
a31cca5ec1 Add support for modular build structure. 2024-10-31 21:42:07 +11:00
Christopher Kohlhoff
df973a85ed Remove support for boost.coroutine-based spawn().
The spawn() function now works only with the fiber support in boost.context.
2024-10-23 21:08:21 +11:00
Christopher Kohlhoff
f5651f9765 Fix truncated tutorial code snippet. 2024-08-07 21:08:54 +10:00
Christopher Kohlhoff
6df0537864 Update operations/composed_6 exaple to also free delay_timer_.
Also the delay_timer_ member should be freed before calling the
user-supplied completion handler (as in the composed_7 and composed_8
examples).
2024-07-08 23:24:05 +10:00
Christopher Kohlhoff
c83ee18458 Clean up spurious white space. 2024-06-27 23:01:17 +10:00
Christopher Kohlhoff
561e752d92 Update examples to use deferred as the default. 2024-06-26 22:42:54 +10:00
Christopher Kohlhoff
c36d3ef338 Update copyright notices. 2024-03-05 07:51:17 +11:00
Christopher Kohlhoff
dc3eb7c1aa Migrate remaining c++03 examples to c++11. 2023-12-07 00:23:31 +11:00
Christopher Kohlhoff
28ff1e7c2b Fix example file header comment. 2023-08-02 23:52:50 +10:00
Christopher Kohlhoff
447e8f0b34 Add a custom completion executor example. 2023-08-02 23:34:48 +10:00
Christopher Kohlhoff
2ca27525ba Add examples showing how to use channels for mutual exclusion. 2023-08-01 22:04:28 +10:00
Christopher Kohlhoff
b4ed3f67ff Add missing c++14 echo examples. 2023-07-05 21:38:20 +10:00
Christopher Kohlhoff
35e93e4e90 Update copyright notices. 2023-03-01 23:03:03 +11:00
Christopher Kohlhoff
9e03478ba1 Add range-based experimental::make_parallel_group().
Added new overloads of experimental::make_parallel_group that may be used
to launch a dynamically-sized set of asynchronous operations, where all
operations are the same type. For example:

  using op_type = decltype(
      socket1.async_read_some(
        boost::asio::buffer(data1),
        boost::asio::deferred
      )
    );

  std::vector<op_type> ops;

  ops.push_back(
      socket1.async_read_some(
        boost::asio::buffer(data1),
        boost::asio::deferred
      )
    );

  ops.push_back(
      socket2.async_read_some(
        boost::asio::buffer(data2),
        boost::asio::deferred
      )
    );

  boost::asio::experimental::make_parallel_group(ops).async_wait(
      boost::asio::experimental::wait_for_all(),
      [](
          std::vector<std::size_t> completion_order,
          std::vector<boost::system::error_code> e,
          std::vector<std::size_t> n
        )
      {
        for (std::size_t i = 0; i < completion_order.size(); ++i)
        {
          std::size_t idx = completion_order[i];
          std::cout << "socket " << idx << " finished: ";
          std::cout << e[idx] << ", " << n[idx] << "\n";
        }
      }
    );

Thanks go to Klemens Morgenstern for supplying part of this implementation.
2022-11-03 00:48:45 +11:00
Christopher Kohlhoff
64448e6a19 Add any_completion_handler<>.
The any_completion_handler<> template can be used to type-erase
completion handlers. A typical use case is to enable separate
compilation of asynchronous operation implementations. For example:

  // Header file:

  void async_sleep_impl(
      boost::asio::any_completion_handler<void(boost::system::error_code)> handler,
      boost::asio::any_io_executor ex, std::chrono::nanoseconds duration);

  template <typename CompletionToken>
  inline auto async_sleep(boost::asio::any_io_executor ex,
      std::chrono::nanoseconds duration, CompletionToken&& token)
  {
    return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
        async_sleep_impl, token, std::move(ex), duration);
  }

  // Separately compiled source file:

  void async_sleep_impl(
      boost::asio::any_completion_handler<void(boost::system::error_code)> handler,
      boost::asio::any_io_executor ex, std::chrono::nanoseconds duration)
  {
    auto timer = std::make_shared<boost::asio::steady_timer>(ex, duration);
    timer->async_wait(boost::asio::consign(std::move(handler), timer));
  }
2022-11-01 11:00:16 +11:00
Christopher Kohlhoff
5b4106c1a6 Add C++11 parallel_group example. 2022-11-01 11:00:16 +11:00
Christopher Kohlhoff
316250e5be Don't use deprecated conversion to endpoint. 2022-11-01 11:00:15 +11:00
Christopher Kohlhoff
17e08c23fe Deprecate execution::execute member function.
Use execute as a member function.
2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
689f94b9ab Re-throw exception from top-level spawn()-ed function. 2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
e1704e6fc4 Add coroutines examples for C++20. 2022-07-05 20:53:19 +10:00
Christopher Kohlhoff
64289a9111 Add composed operation examples for c++20. 2022-07-05 20:43:13 +10:00
Christopher Kohlhoff
488ff8b582 Update C++11 timeouts example to use new form of async_result. 2022-07-05 20:25:57 +10:00
Christopher Kohlhoff
e857ceba97 Ensure all operations examples use the new async_result form. 2022-07-05 20:25:09 +10:00
Christopher Kohlhoff
5bbdc9b709 Change spawn() to be a completion token-based async operation.
Added new spawn() overloads that conform to the requirements for
asynchronous operations. These overloads also support cancellation. When
targeting C++11 and later these functions are implemented in terms of
Boost.Context directly.

The existing overloads have been retained but are deprecated.
2022-06-30 01:18:45 +10:00
Christopher Kohlhoff
4c216747dc Move deferred to the asio namespace.
This is no longer an experimental facility. The names deferred and
deferred_t have been temporarily retained as deprecated entities under
the asio::experimental namespace, for backwards compatibility.
2022-06-30 01:08:13 +10:00
Christopher Kohlhoff
f7356fbe90 Move append to the asio namespace.
This is no longer an experimental facility. The names append and
append_t have been temporarily retained as deprecated entities under
the asio::experimental namespace, for backwards compatibility.
2022-06-30 00:43:16 +10:00
Christopher Kohlhoff
a312a46715 Move as_tuple to the asio namespace.
This is no longer an experimental facility. The names as_tuple and
as_tuple_t have been temporarily retained as deprecated entities under
the asio::experimental namespace, for backwards compatibility.
2022-06-30 00:43:16 +10:00
Christopher Kohlhoff
4b25ab9fe1 Note that I/O object constructors are passed executors or execution contexts. 2022-04-06 23:27:35 +10:00
Christopher Kohlhoff
e618fd4353 Work around code formatting problem in doc generation. 2022-04-06 23:24:39 +10:00
Christopher Kohlhoff
5cda4165b6 Add completion token cross-references to tutorial. 2022-04-06 23:21:35 +10:00
Christopher Kohlhoff
5898a5279c Add C++14 examples of wrapping callback-based APIs. 2022-03-07 23:32:16 +11:00
Christopher Kohlhoff
36e93b79bd Fix deprecated enum usage warning. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
59bce59911 Add example showing file descriptor passing over local sockets.
Thanks to Heiko Hund for providing this example.
2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
64faf0a6d2 Enable executor_work_guard<> even when ASIO_NO_TS_EXECUTORS is defined. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
3cd04eee90 Add bind_allocator. 2022-03-02 21:57:41 +11:00
Christopher Kohlhoff
ff58013a23 Update copyright notices. 2022-03-02 21:23:52 +11:00
Christopher Kohlhoff
3cf596f0b2 Regenerate certificates for ssl examples. 2021-11-17 08:39:22 +11:00
Christopher Kohlhoff
b428c745b9 Add channel-based proxy example. 2021-11-17 08:37:08 +11:00
Christopher Kohlhoff
c6b9f33dcf Add support for files.
This change adds support for stream-oriented and random-access files.
For example, to write to a newly created stream-oriented file:

  asio::stream_file file(
      my_io_context, "/path/to/file",
      asio::stream_file::write_only
        | asio::stream_file::create
        | asio::stream_file::truncate);

  file.async_write_some(my_buffer,
      [](error_code e, size_t n)
      {
        // ...
      });

or to read from a random-access file:

  asio::random_access_file file(
      my_io_context, "/path/to/file",
      asio::random_access_file::read_only);

  file.async_read_some_at(1234, my_buffer,
      [](error_code e, size_t n)
      {
        // ...
      });

This feature currently supports I/O completion ports on Windows, and
io_uring on Linux (define BOOST_ASIO_HAS_IO_URING to enable).
2021-10-25 12:15:08 +11:00
Christopher Kohlhoff
b6bd05ae45 Fixes compiler error "error: 'asio::posix' has not been declared" on Windows 10 with MSYS Mingw64 10.3.0 2021-10-17 11:02:22 +11:00
Christopher Kohlhoff
3a585b615e Add missing C++14 examples. 2021-08-04 21:16:59 +10:00
Christopher Kohlhoff
1c3c80d778 Make experimental::parallel_group compatible with C++14. 2021-07-01 10:52:01 +10:00
Christopher Kohlhoff
0c340c786a Add experimental::deferred completion token.
The experimental::deferred completion token takes a call to an
asynchronous operation's initiating function and turns it into a
function object that accepts a completion token. For example:

  auto deferred_op =
    timer.async_wait(
      boost::asio::experimental::deferred);
  ...
  std::move(deferred_op)(
      [](std::error_code ec){ ... });

or

  auto deferred_op =
    timer.async_wait(
      boost::asio::experimental::deferred);
  ...
  std::future<void> =
    std::move(deferred_op)(
      boost::asio::use_future);

The deferred token also supports chaining, to create simple
compositions:

  auto deferred_op =
    timer.async_wait(
      boost::asio::experimental::deferred(
        [&](std::error_code ec)
        {
          timer.expires_after(
              std::chrono::seconds(1));

          return timer.async_wait(
              boost::asio::experimental::deferred);
        });
  ...
  std::future<void> = std::move(deferred_op)(boost::asio::use_future);
2021-07-01 10:52:01 +10:00
Christopher Kohlhoff
aa311e118c Add experimental::as_tuple completion token adapter.
The as_tuple completion token adapter can be used to specify that the
completion handler arguments should be combined into a single tuple
argument.

The as_tuple adapter may be used in conjunction with use_awaitable and
structured bindings as follows:

    auto [e, n] = co_await socket.async_read_some(
        asio::buffer(data), as_tuple(use_awaitable));

Alternatively, it may be used as a default completion token like so:

    using default_token = as_tuple_t<use_awaitable_t<>>;
    using tcp_socket = default_token::as_default_on_t<tcp::socket>;
    // ...
    awaitable<void> do_read(tcp_socket socket)
    {
      // ...
      auto [e, n] = co_await socket.async_read_some(asio::buffer(data));
      // ...
    }
2021-06-05 17:43:31 +10:00
Christopher Kohlhoff
723982b867 Update copyright notices. 2021-02-25 08:29:05 +11:00
Christopher Kohlhoff
311d355ab4 Add experimental::as_single completion token adapter.
The as_single completion token adapter can be used to specify that the
completion handler arguments should be combined into a single argument.
For completion signatures with a single parameter, the argument is
passed through as-is. For signatures with two or more parameters, the
arguments are combined into a tuple.

The as_single adapter may be used in conjunction with use_awaitable and
structured bindings as follows:

    auto [e, n] = co_await socket.async_read_some(
        boost::asio::buffer(data), as_single(use_awaitable));

Alternatively, it may be used as a default completion token like so:

    using default_token = as_single_t<use_awaitable_t<>>;
    using tcp_socket = default_token::as_default_on_t<tcp::socket>;
    // ...
    awaitable<void> do_read(tcp_socket socket)
    {
      // ...
      auto [e, n] = co_await socket.async_read_some(boost::asio::buffer(data));
      // ...
    }
2020-11-02 14:03:09 +11:00
Christopher Kohlhoff
18deb3dc5f Document that there are multiple types of I/O execution context. 2020-08-05 22:54:41 +10:00