19 Commits

Author SHA1 Message Date
Christopher Kohlhoff
1afbc5c12b Update copyright notices. 2025-03-04 22:57:26 +11:00
Christopher Kohlhoff
c36d3ef338 Update copyright notices. 2024-03-05 07:51:17 +11:00
Christopher Kohlhoff
35e93e4e90 Update copyright notices. 2023-03-01 23:03:03 +11: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
ff58013a23 Update copyright notices. 2022-03-02 21:23:52 +11: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
3f12308c03 Add overloads of co_spawn that launch an awaitable.
This change allows us to write:

 co_spawn(executor,
     echo(std::move(socket)),
     detached);

instead of:

 co_spawn(executor,
     [socket = std::move(socket)]() mutable
     {
       return echo(std::move(socket));
     },
     detached);
2020-06-22 20:49:30 +10:00
Christopher Kohlhoff
cb60b08ff5 Add source location support to handler tracking.
The BOOST_ASIO_HANDLER_LOCATION((file_name, line, function_name)) macro
may be used to inform the handler tracking mechanism of a source
location. This macro declares an object that is placed on the stack.

When an asynchronous operation is launched with location information, it
outputs lines using the <action> 'n^m', prior to the 'n*m' line that
signifies the beginning of the asynchronous operation. For example:

    @asio|1589423304.861944|>7|ec=system:0,bytes_transferred=5
    @asio|1589423304.861952|7^8|in 'async_write' (./../../../include/asio/impl/write.hpp:330)
    @asio|1589423304.861952|7^8|called from 'do_write' (handler_tracking/async_tcp_echo_server.cpp:62)
    @asio|1589423304.861952|7^8|called from 'operator()' (handler_tracking/async_tcp_echo_server.cpp:51)
    @asio|1589423304.861952|7*8|socket@0x7ff61c008230.async_send
    @asio|1589423304.861975|.8|non_blocking_send,ec=system:0,bytes_transferred=5
    @asio|1589423304.861980|<7|

If std::source_location or std::experimental::source_location are
available, the use_awaitable_t token (when default-constructed or used
as a default completion token) will also cause handler tracking to
output a source location for each newly created asynchronous operation.
A use_awaitable_t object may also be explicitly constructed with location
information.
2020-06-22 20:46:29 +10:00
Christopher Kohlhoff
4b552cfd5b Update copyright notices. 2020-04-07 11:15:42 +10:00
Christopher Kohlhoff
8e0762de49 Add default completion tokens.
Every I/O executor type now has an associated default completion token
type. This is specified via the `default_completion_token_type` trait.
This trait may be used in asynchronous operation declarations as
follows:

  template <
      typename IoObject,
      typename CompletionToken =
        typename default_completion_token_type<
          typename IoObject::executor_type
        >::type
    >
  auto async_foo(
      IoObject& io_object,
      CompletionToken&& token =
        typename default_completion_token_type<
          typename IoObject::executor_type
        >::type{}
    );

If not specialised, this trait type is `void`, meaning no default
completion token type is available for the given I/O executor.

The `default_completion_token_type` trait is specialised for the
`use_awaitable` completion token so that it may be used as shown in the
following example:

  auto socket = use_awaitable.as_default_on(tcp::socket(my_context));
  // ...
  co_await socket.async_connect(my_endpoint); // Defaults to use_awaitable.

In this example, type of the `socket` object is transformed from
`tcp::socket` to have an I/O executor with the default completion token
set to `use_awaitable`.

Alternatively, the socket type may be computed directly:

  using tcp_socket = use_awaitable_t<>::as_default_on_t<tcp::socket>;
  tcp_socket socket(my_context);
  // ...
  co_await socket.async_connect(my_endpoint); // Defaults to use_awaitable.
2019-10-30 20:51:58 +11:00
Christopher Kohlhoff
2b3c9a4693 Capture port by value to prevent dangling reference. 2019-02-28 20:15:59 +11:00
Christopher Kohlhoff
baca9a092f Promote coroutines TS support classes to asio namespace.
The awaitable<>, co_spawn(), this_coro, detached, and redirect_error
facilities have been moved from the asio::experimental namespace to
namespace asio. As part of this change, the this_coro::token() awaitable
has been superseded by the asio::use_awaitable completion token.

Please note that the use_awaitable and redirect_error completion tokens
work only with asynchronous operations that use the new form of
async_result with member function initiate(). Furthermore, when using
use_awaitable, please be aware that the asynchronous operation is not
initiated until co_await is applied to the awaitable<>.
2019-02-28 00:02:00 +11:00
Christopher Kohlhoff
ae04c26689 Update copyright notices. 2019-02-17 19:59:39 -10:00
Christopher Kohlhoff
59066d80b2 Add custom I/O executor support to I/O objects.
All I/O objects now have an additional Executor template parameter. This
template parameter defaults to the asio::executor type (the polymorphic
executor wrapper) but can be used to specify a user-defined executor
type.

I/O objects' constructors and functions that previously took an
asio::io_context& now accept either an Executor or a reference to a
concrete ExecutionContext (such as asio::io_context or
asio::thread_pool).

One potential point of breakage in existing user code is when reusing an
I/O object's io_context for constructing another I/O object, as in:

    asio::steady_timer my_timer(my_socket.get_executor().context());

To fix this, either construct the second I/O object using the first I/O
object's executor:

    asio::steady_timer my_timer(my_socket.get_executor());

or otherwise explicitly pass the io_context:

    asio::steady_timer my_timer(my_io_context);
2019-02-17 19:59:29 -10:00
Christopher Kohlhoff
c5da00630b Clean up some experimental::co_spawn examples. 2018-03-09 20:58:42 +11:00
Christopher Kohlhoff
77714ac82b Add some more experimental::co_spawn examples. 2018-03-07 17:29:34 +11:00
Christopher Kohlhoff
e3226bf6e0 Add experimental::co_spawn examples. 2018-03-04 23:09:04 +11:00