diff options
author | 2024-04-22 08:04:58 +1000 | |
---|---|---|
committer | 2024-04-22 08:04:58 +1000 | |
commit | 184292b730f4236bd4840e780fdad97ee060ec84 (patch) | |
tree | 67902161a465fe92aec78ca502de07092b8cde0f /3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp | |
parent | 24154bc1f00790f344120b3a85175d6f616c5ad0 (diff) |
3rdparty/asio: Updated to 1.30.2
Diffstat (limited to '3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp b/3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp index 879e38ca0db..808a179e2d3 100644 --- a/3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp +++ b/3rdparty/asio/src/examples/cpp14/operations/composed_4.cpp @@ -2,13 +2,14 @@ // composed_4.cpp // ~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // 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) // #include <asio/bind_executor.hpp> +#include <asio/deferred.hpp> #include <asio/io_context.hpp> #include <asio/ip/tcp.hpp> #include <asio/use_future.hpp> @@ -38,12 +39,19 @@ template <typename CompletionToken> auto async_write_message(tcp::socket& socket, const char* message, CompletionToken&& token) // The return type of the initiating function is deduced from the combination - // of CompletionToken type and the completion handler's signature. When the - // completion token is a simple callback, the return type is always void. - // In this example, when the completion token is asio::yield_context - // (used for stackful coroutines) the return type would be also be void, as + // of: + // + // - the CompletionToken type, + // - the completion handler signature, and + // - the asynchronous operation's initiation function object. + // + // When the completion token is a simple callback, the return type is always + // void. In this example, when the completion token is asio::yield_context + // (used for stackful coroutines) the return type would also be void, as // there is no non-error argument to the completion handler. When the - // completion token is asio::use_future it would be std::future<void>. + // completion token is asio::use_future it would be std::future<void>. When + // the completion token is asio::deferred, the return type differs for each + // asynchronous operation. // // In C++14 we can omit the return type as it is automatically deduced from // the return type of asio::async_initiate. @@ -165,6 +173,38 @@ void test_callback() //------------------------------------------------------------------------------ +void test_deferred() +{ + asio::io_context io_context; + + tcp::acceptor acceptor(io_context, {tcp::v4(), 55555}); + tcp::socket socket = acceptor.accept(); + + // Test our asynchronous operation using the deferred completion token. This + // token causes the operation's initiating function to package up the + // operation with its arguments to return a function object, which may then be + // used to launch the asynchronous operation. + auto op = async_write_message(socket, "", asio::deferred); + + // Launch the operation using a lambda as a callback. + std::move(op)( + [](const std::error_code& error) + { + if (!error) + { + std::cout << "Message sent\n"; + } + else + { + std::cout << "Error: " << error.message() << "\n"; + } + }); + + io_context.run(); +} + +//------------------------------------------------------------------------------ + void test_future() { asio::io_context io_context; @@ -197,5 +237,6 @@ void test_future() int main() { test_callback(); + test_deferred(); test_future(); } |