diff options
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp b/3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp index cfec43c8e83..054dbdbbc88 100644 --- a/3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp +++ b/3rdparty/asio/src/examples/cpp11/operations/composed_1.cpp @@ -2,12 +2,13 @@ // composed_1.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/deferred.hpp> #include <asio/io_context.hpp> #include <asio/ip/tcp.hpp> #include <asio/use_future.hpp> @@ -31,14 +32,25 @@ 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 void. However, - // when the completion token is asio::yield_context (used for stackful - // coroutines) the return type would be std::size_t, and when the completion - // token is asio::use_future it would be std::future<std::size_t>. - -> typename asio::async_result< - typename std::decay<CompletionToken>::type, - void(std::error_code, std::size_t)>::return_type + // 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 void. + // However, when the completion token is asio::yield_context (used for + // stackful coroutines) the return type would be std::size_t, and when the + // completion token is asio::use_future it would be std::future<std::size_t>. + // When the completion token is asio::deferred, the return type differs for + // each asynchronous operation. + // + // In this example we are trivially delegating to an underlying asynchronous + // operation, so we can deduce the return type from that. + -> decltype( + asio::async_write(socket, + asio::buffer(message, std::strlen(message)), + std::forward<CompletionToken>(token))) { // When delegating to the underlying operation we must take care to perfectly // forward the completion token. This ensures that our operation works @@ -77,6 +89,39 @@ 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 and its arguments to return a function object, which may then be + // used to launch the asynchronous operation. + auto op = async_write_message(socket, + "Testing deferred\r\n", asio::deferred); + + // Launch the operation using a lambda as a callback. + std::move(op)( + [](const std::error_code& error, std::size_t n) + { + if (!error) + { + std::cout << n << " bytes transferred\n"; + } + else + { + std::cout << "Error: " << error.message() << "\n"; + } + }); + + io_context.run(); +} + +//------------------------------------------------------------------------------ + void test_future() { asio::io_context io_context; @@ -109,5 +154,6 @@ void test_future() int main() { test_callback(); + test_deferred(); test_future(); } |