]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp11/timeouts/blocking_token_tcp_client.cpp
2 // blocking_token_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #include <boost/asio/connect.hpp>
12 #include <boost/asio/io_context.hpp>
13 #include <boost/asio/ip/tcp.hpp>
14 #include <boost/asio/read_until.hpp>
15 #include <boost/asio/streambuf.hpp>
16 #include <boost/system/system_error.hpp>
17 #include <boost/asio/write.hpp>
23 using boost::asio::ip::tcp
;
25 // We will use our sockets only with an io_context.
26 using tcp_socket
= boost::asio::basic_stream_socket
<
27 tcp
, boost::asio::io_context::executor_type
>;
29 //----------------------------------------------------------------------
31 // A custom completion token that makes asynchronous operations behave as
32 // though they are blocking calls with a timeout.
35 close_after(std::chrono::steady_clock::duration t
, tcp_socket
& s
)
36 : timeout_(t
), socket_(s
)
40 // The maximum time to wait for an asynchronous operation to complete.
41 std::chrono::steady_clock::duration timeout_
;
43 // The socket to be closed if the operation does not complete in time.
50 // The async_result template is specialised to allow the close_after token to
51 // be used with asynchronous operations that have a completion signature of
52 // void(error_code, T). Generalising this for all completion signature forms is
53 // left as an exercise for the reader.
55 class async_result
<close_after
, void(boost::system::error_code
, T
)>
58 // An asynchronous operation's initiating function automatically creates an
59 // completion_handler_type object from the token. This function object is
60 // then called on completion of the asynchronous operation.
61 class completion_handler_type
64 completion_handler_type(const close_after
& token
)
69 void operator()(const boost::system::error_code
& error
, T t
)
76 friend class async_result
;
78 boost::system::error_code
* error_
;
82 // The async_result constructor associates the completion handler object with
83 // the result of the initiating function.
84 explicit async_result(completion_handler_type
& h
)
85 : timeout_(h
.token_
.timeout_
),
86 socket_(h
.token_
.socket_
)
92 // The return_type typedef determines the result type of the asynchronous
93 // operation's initiating function.
94 typedef T return_type
;
96 // The get() function is used to obtain the result of the asynchronous
97 // operation's initiating function. For the close_after completion token, we
98 // use this function to run the io_context until the operation is complete.
101 boost::asio::io_context
& io_context
= boost::asio::query(
102 socket_
.get_executor(), boost::asio::execution::context
);
104 // Restart the io_context, as it may have been left in the "stopped" state
105 // by a previous operation.
106 io_context
.restart();
108 // Block until the asynchronous operation has completed, or timed out. If
109 // the pending asynchronous operation is a composed operation, the deadline
110 // applies to the entire operation, rather than individual operations on
112 io_context
.run_for(timeout_
);
114 // If the asynchronous operation completed successfully then the io_context
115 // would have been stopped due to running out of work. If it was not
116 // stopped, then the io_context::run_for call must have timed out and the
117 // operation is still incomplete.
118 if (!io_context
.stopped())
120 // Close the socket to cancel the outstanding asynchronous operation.
123 // Run the io_context again until the operation completes.
127 // If the operation failed, throw an exception. Otherwise return the result.
128 return error_
? throw std::system_error(error_
) : t_
;
132 std::chrono::steady_clock::duration timeout_
;
134 boost::system::error_code error_
;
141 //----------------------------------------------------------------------
143 int main(int argc
, char* argv
[])
149 std::cerr
<< "Usage: blocking_tcp_client <host> <port> <message>\n";
153 boost::asio::io_context io_context
;
155 // Resolve the host name and service to a list of endpoints.
156 auto endpoints
= tcp::resolver(io_context
).resolve(argv
[1], argv
[2]);
158 tcp_socket
socket(io_context
);
160 // Run an asynchronous connect operation with a timeout.
161 boost::asio::async_connect(socket
, endpoints
,
162 close_after(std::chrono::seconds(10), socket
));
164 auto time_sent
= std::chrono::steady_clock::now();
166 // Run an asynchronous write operation with a timeout.
167 std::string msg
= argv
[3] + std::string("\n");
168 boost::asio::async_write(socket
, boost::asio::buffer(msg
),
169 close_after(std::chrono::seconds(10), socket
));
171 for (std::string input_buffer
;;)
173 // Run an asynchronous read operation with a timeout.
174 std::size_t n
= boost::asio::async_read_until(socket
,
175 boost::asio::dynamic_buffer(input_buffer
), '\n',
176 close_after(std::chrono::seconds(10), socket
));
178 std::string
line(input_buffer
.substr(0, n
- 1));
179 input_buffer
.erase(0, n
);
181 // Keep going until we get back the line that was sent.
186 auto time_received
= std::chrono::steady_clock::now();
188 std::cout
<< "Round trip time: ";
189 std::cout
<< std::chrono::duration_cast
<
190 std::chrono::microseconds
>(
191 time_received
- time_sent
).count();
192 std::cout
<< " microseconds\n";
194 catch (std::exception
& e
)
196 std::cerr
<< "Exception: " << e
.what() << "\n";