summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2024-04-22 08:04:58 +1000
committer Vas Crabb <vas@vastheman.com>2024-04-22 08:04:58 +1000
commit184292b730f4236bd4840e780fdad97ee060ec84 (patch)
tree67902161a465fe92aec78ca502de07092b8cde0f /3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp
parent24154bc1f00790f344120b3a85175d6f616c5ad0 (diff)
3rdparty/asio: Updated to 1.30.2
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp')
-rw-r--r--3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp79
1 files changed, 26 insertions, 53 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp b/3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp
index 6379642665a..085570d8b78 100644
--- a/3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp
+++ b/3rdparty/asio/src/examples/cpp11/timeouts/blocking_token_tcp_client.cpp
@@ -2,7 +2,7 @@
// blocking_token_tcp_client.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)
@@ -22,6 +22,10 @@
using asio::ip::tcp;
+// NOTE: This example uses the new form of the asio::async_result trait.
+// For an example that works with the Networking TS style of completion tokens,
+// please see an older version of asio.
+
// We will use our sockets only with an io_context.
using tcp_socket = asio::basic_stream_socket<
tcp, asio::io_context::executor_type>;
@@ -54,51 +58,26 @@ template <typename T>
class async_result<close_after, void(std::error_code, T)>
{
public:
- // An asynchronous operation's initiating function automatically creates an
- // completion_handler_type object from the token. This function object is
- // then called on completion of the asynchronous operation.
- class completion_handler_type
- {
- public:
- completion_handler_type(const close_after& token)
- : token_(token)
- {
- }
-
- void operator()(const std::error_code& error, T t)
- {
- *error_ = error;
- *t_ = t;
- }
-
- private:
- friend class async_result;
- close_after token_;
- std::error_code* error_;
- T* t_;
- };
-
- // The async_result constructor associates the completion handler object with
- // the result of the initiating function.
- explicit async_result(completion_handler_type& h)
- : timeout_(h.token_.timeout_),
- socket_(h.token_.socket_)
- {
- h.error_ = &error_;
- h.t_ = &t_;
- }
-
- // The return_type typedef determines the result type of the asynchronous
- // operation's initiating function.
- typedef T return_type;
-
- // The get() function is used to obtain the result of the asynchronous
- // operation's initiating function. For the close_after completion token, we
- // use this function to run the io_context until the operation is complete.
- return_type get()
+ // The initiate() function is used to launch the asynchronous operation by
+ // calling the operation's initiation function object. For the close_after
+ // completion token, we use this function to run the io_context until the
+ // operation is complete.
+ template <typename Init, typename... Args>
+ static T initiate(Init init, close_after token, Args&&... args)
{
asio::io_context& io_context = asio::query(
- socket_.get_executor(), asio::execution::context);
+ token.socket_.get_executor(), asio::execution::context);
+
+ // Call the operation's initiation function object to start the operation.
+ // A lambda is supplied as the completion handler, to be called when the
+ // operation completes.
+ std::error_code error;
+ T result;
+ init([&](std::error_code e, T t)
+ {
+ error = e;
+ result = t;
+ }, std::forward<Args>(args)...);
// Restart the io_context, as it may have been left in the "stopped" state
// by a previous operation.
@@ -108,7 +87,7 @@ public:
// the pending asynchronous operation is a composed operation, the deadline
// applies to the entire operation, rather than individual operations on
// the socket.
- io_context.run_for(timeout_);
+ io_context.run_for(token.timeout_);
// If the asynchronous operation completed successfully then the io_context
// would have been stopped due to running out of work. If it was not
@@ -117,21 +96,15 @@ public:
if (!io_context.stopped())
{
// Close the socket to cancel the outstanding asynchronous operation.
- socket_.close();
+ token.socket_.close();
// Run the io_context again until the operation completes.
io_context.run();
}
// If the operation failed, throw an exception. Otherwise return the result.
- return error_ ? throw std::system_error(error_) : t_;
+ return error ? throw std::system_error(error) : result;
}
-
-private:
- std::chrono::steady_clock::duration timeout_;
- tcp_socket& socket_;
- std::error_code error_;
- T t_;
};
} // namespace asio