2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-27 06:32:08 +00:00
Commit Graph

286 Commits

Author SHA1 Message Date
Christopher Kohlhoff
4ef2cd0054 Add converting move construction/assignment to serial ports. 2022-06-30 12:17:39 +10:00
Christopher Kohlhoff
ca858d3ecd Add tests for converting move construction/assignment of files. 2022-06-30 12:17:16 +10:00
Christopher Kohlhoff
de2d299455 Add converting move construction/assignment to Windows stream and random-access handles. 2022-06-30 12:17:04 +10:00
Christopher Kohlhoff
5ce5e7e8ff Add converting move construction/assignment to Windows object_handle. 2022-06-30 12:16:44 +10:00
Christopher Kohlhoff
15e9e956b7 Add converting move construction/assignment to posix descriptors. 2022-06-30 12:16:25 +10:00
Christopher Kohlhoff
af93ac1ca9 Add buffer() overloads for contiguous containers, such as std::span. 2022-06-30 01:08:13 +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
34f5627723 Move prepend to the asio namespace.
This is no longer an experimental facility. The names prepend and
prepend_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
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
45bd682d9a Make experimental::prepend compatible with C++11. 2022-06-30 00:43:16 +10:00
Christopher Kohlhoff
6f8e8c847d Make experimental::append compatible with C++11. 2022-06-30 00:43:16 +10:00
Christopher Kohlhoff
2117b3ee7e Expose recycling_allocator as part of public interface. 2022-03-04 20:56:45 +11:00
Christopher Kohlhoff
001d6213ca Work around shutdown ordering issue in coro/executor test. 2022-03-04 20:56:45 +11:00
Christopher Kohlhoff
31f93ed401 Fix "'this' pointer is null" warnings. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
fc021c78b2 Fix bind_cancellation_slot compatibility with legacy completion tokens. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
784575a3d3 Fix bind_executor compatibility with legacy completion tokens. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
0bfa387d2c Add missing implementation of basic_file::release(). 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
600cf024f1 Add unit test for experimental::as_tuple. 2022-03-02 21:57:42 +11:00
Christopher Kohlhoff
3cd04eee90 Add bind_allocator. 2022-03-02 21:57:41 +11:00
Christopher Kohlhoff
20ed628d43 Fix associator specialisations for append and prepend. 2022-03-02 21:24:01 +11:00
Christopher Kohlhoff
ff58013a23 Update copyright notices. 2022-03-02 21:23:52 +11:00
Christopher Kohlhoff
495e6367af Add experimental support for channels.
This adds experimental::channel and experimental::concurrent_channel.
Channels may be used to send completions as messages. For example:

  // Create a channel with no buffer space.
  channel<void(error_code, size_t)> ch(ctx);

  // The call to try_send fails as there is no buffer
  // space and no waiting receive operations.
  bool ok = ch.try_send(asio::error::eof, 123);
  assert(!ok);

  // The async_send operation blocks until a receive
  // operation consumes the message.
  ch.async_send(asio::error::eof, 123,
      [](error_code ec)
      {
        // ...
      });

  // The async_receive consumes the message. Both the
  // async_send and async_receive operations complete
  // immediately.
  ch.async_receive(
      [](error_code ec, size_t n)
      {
        // ...
      });
2021-11-04 01:56:32 +11:00
Christopher Kohlhoff
5b72b72b72 Improvements to asio::experimental::coro.
* Added overload so member functions can provide an explicit executor.
* Added co_spawn for coro tasks.
* Added reference and overview documentation.
* Adopted awaitable cancellation model.
* Refactored implementation.
2021-11-04 01:41:25 +11:00
Christopher Kohlhoff
5a706991b5 Add missing file synchronisation functions. 2021-11-04 01:34:52 +11:00
Christopher Kohlhoff
d8359719e1 Add support for registered buffers.
The mutable_registered_buffer and const_registered_buffer classes are
buffer sequence types that represented registered buffers. These buffers
are obtained by first performing a buffer registration:

  auto my_registration =
    asio::register_buffers(
        my_execution_context,
        my_buffer_sequence);

The registration object must be maintained for as long as the buffer
registration is required. The supplied buffer sequence represents the
memory location or locations that will be registered, and the caller
must ensure they remain valid for as long as they are registered. The
registration is automatically removed when the registration object is
destroyed. There can be at most one active registration per execution
context.

The registration object is a container of registered buffers. Buffers
may be obtained from it by iterating over the container, or via direct
index access:

  asio::mutable_registered_buffer my_buffer
    = my_registration[i];

The registered buffers may then be passed directly to operations:

  asio::async_read(my_socket, my_buffer,
      [](error_code ec, size_t n)
      {
        // ...
      });

Buffer registration supports the io_uring backend when used with read
and write operations on descriptors, files, pipes, and sockets.
2021-10-29 20:54:56 +11:00
Christopher Kohlhoff
028630b0f8 Add support for portable pipes.
This change add supports for pipes on POSIX and Windows (when I/O
completion ports are available). For example, to create and use a
connected pair of pipe objects:

  asio::readable_pipe read_end;
  asio::writable_pipe write_end;
  asio::connect_pipe(read_end, write_end);

  write_end.async_write_some(my_write_buffer,
      [](error_code e, size_t n)
      {
        // ...
      });

  read_end.async_read_some(my_read_buffer,
      [](error_code e, size_t n)
      {
        // ...
      });
2021-10-27 13:47:03 +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
4ddfe0bfb1 Remove inclusion of boost/scope_exit.hpp. 2021-10-17 11:10:35 +11:00
Klemens
9369dbb269 Gcc fixes for promise & coro. 2021-08-03 17:56:56 +10:00
Christopher Kohlhoff
a51b9b8198 Ensure un-cancelled ops are correctly placed back in the queue. 2021-07-11 22:03:29 +10:00
Christopher Kohlhoff
c1fd78e785 Fix handling of move-only results with awaitable operators && and ||. 2021-07-11 10:20:19 +10:00
Christopher Kohlhoff
ef4e3d873c Add missing #includes for when using separate compilation. 2021-07-08 13:14:30 +10:00
Christopher Kohlhoff
fc486d6530 Fix failure in coro simple_test. 2021-07-07 22:47:10 +10:00
Christopher Kohlhoff
4ca5e6a13a Make promise test more resistant to a heavily loaded test system. 2021-07-05 18:45:03 +10:00
Christopher Kohlhoff
769dc0e149 Add this_coro::throw_if_cancelled.
By default, awaitable<>-based coroutines now throw an exception if they
have been previously cancelled, and then try to perform a co_await
against another awaitable<>.

To disable this behaviour for the current awaitable<>-based "thread",
perform:

    co_await boost::asio::this_coro::throw_if_error(false);

It is then the responsibility of the coroutine implementation to ensure
that it checks the cancellation state of the coroutine manually, by
doing something like:

    auto cs = boost::asio::this_coro::cancellation_state;
    // ...
    if (cs.cancelled() != cancellation_type::none)
    {
      // ... handle cancellation ...
    }
2021-07-04 13:10:27 +10:00
Christopher Kohlhoff
d2ab7b7669 Fix yield input in experimental::coro. 2021-07-04 13:10:27 +10:00
Christopher Kohlhoff
01ca2be880 Ensure experimental::deferred header is self-contained. 2021-07-04 13:10:27 +10:00
Christopher Kohlhoff
f486b43c16 Add operator&& and operator|| for awaitable<>.
The logical operators || and && have been overloaded for awaitable<>, to
allow coroutines to be trivially awaited in parallel.

When awaited using &&, the await expression waits until both operations
have completed successfully. As a "short-circuit" evaluation, if one
operation fails with an exception, the other is immediately cancelled.
For example:

    std::tuple<std::size_t, std::size_t> results =
      co_await (
        async_read(socket, input_buffer, use_awaitable)
          && async_write(socket, output_buffer, use_awaitable)
      );

When awaited using ||, the await expression waits until either operation
succceeds. As a "short-circuit" evaluation, if one operation succeeds
without throwing an exception, the other is immediately cancelled. For
example:

    std::variant<std::size_t, std::monostate> results =
      co_await (
        async_read(socket, input_buffer, use_awaitable)
          || timer.async_wait(use_awaitable)
      );

The operators may be enabled by adding the #include:

    #include <asio/experimental/awaitable_operators.hpp>

and then bringing the contents of the experimental::awaitable_operators
namespace into scope:

    using namespace boost::asio::experimental::awaitable_operators;
2021-07-04 13:05:46 +10:00
Christopher Kohlhoff
9b3fc2ef83 Add move assignment to ssl::stream<>. 2021-07-02 12:29:57 +10:00
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
klemens-morgenstern
03ba758437 Added experimental::promise.
The experimental::promise type allows eager execution and
synchronisation of async operations.

    auto promise = async_read(
        stream, asio::buffer(my_buffer),
        asio::experimental::use_promise);

    ... do other stuff while the read is going on ...

    promise.async_wait( // completion the operation
        [](error_code ec, std::size_t bytes_read)
        {
          ...
        });

Promises can be safely disregarded if the result is no longer required.

Different operations can be combined to either wait for all to complete
or for one to complete (and cancel the rest). For example, to wait for
one to complete:

    auto timeout_promise =
      timer.async_wait(
        asio::experimental::use_promise);

    auto read_promise = async_read(
        stream, asio::buffer(my_buffer),
        asio::experimental::use_promise);

    auto promise =
      asio::experimental::promise<>::race(
        timeout_promise, read_promise);

    promise.async_wait(
        [](std::variant<error_code, std::tuple<error_code, std::size_t>> v)
        {
          if (v.index() == 0) {} //timed out
          else if (v.index() == 1) // completed in time
        });

or to wait for all to complete:

    auto write_promise = async_write(
        stream, asio::buffer(my_write_buffer),
        asio::experimental::use_promise);

    auto read_promise = async_read(
        stream, asio::buffer(my_buffer),
        asio::experimental::use_promise);

    auto promise =
      asio::experimental::promise<>::all(
        write_promise, read_promise);

    promise.async_wait(
        [](std::tuple<error_code, std::size_t> write_result,
          std::tuple<error_code, std::size_t> read_result)
        {
        });
2021-07-01 15:40:28 +10:00
Christopher Kohlhoff
5145c8f83d Add self-container-header checks for cancellation functionality. 2021-07-01 10:52:01 +10:00
Christopher Kohlhoff
4467d996b9 Add cancellation_slot support to async_compose. 2021-06-28 10:11:36 +10:00
Christopher Kohlhoff
f8b37f0c90 Add bind_cancellation_slot function and cancellation_slot_binder adapter. 2021-06-28 10:10:07 +10:00
Christopher Kohlhoff
059c40e935 Add initiate-based completion token support to bind_executor. 2021-06-05 17:43:31 +10:00
Christopher Kohlhoff
0b0b904883 Add std::hash specialisations for ip::basic_endpoint<>. 2021-02-25 08:37:47 +11:00
Christopher Kohlhoff
eed38fbe97 Add std::hash specialisations for IP addresses. 2021-02-25 08:35:32 +11:00
Christopher Kohlhoff
723982b867 Update copyright notices. 2021-02-25 08:29:05 +11:00
Christopher Kohlhoff
fc4546c8d3 Fix thread_pool test to work with BOOST_ASIO_NO_TYPEID (i.e. no RTTI). 2020-11-02 14:03:09 +11:00