]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/timeouts/blocking_token_tcp_client.cpp
2 // blocking_token_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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 typedef boost::asio::basic_stream_socket
<tcp
,
27 boost::asio::io_context::executor_type
> tcp_socket
;
29 //----------------------------------------------------------------------
31 // A custom completion token that makes asynchronous operations behave as
32 // though they are blocking calls with a timeout.
35 close_after(boost::asio::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 boost::asio::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()(boost::system::error_code ec
, T t
)
76 friend class async_result
;
78 boost::system::error_code
* ec_
;
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
= socket_
.get_executor().context();
103 // Restart the io_context, as it may have been left in the "stopped" state
104 // by a previous operation.
105 io_context
.restart();
107 // Block until the asynchronous operation has completed, or timed out. If
108 // the pending asynchronous operation is a composed operation, the deadline
109 // applies to the entire operation, rather than individual operations on
111 io_context
.run_for(timeout_
);
113 // If the asynchronous operation completed successfully then the io_context
114 // would have been stopped due to running out of work. If it was not
115 // stopped, then the io_context::run_for call must have timed out and the
116 // operation is still incomplete.
117 if (!io_context
.stopped())
119 // Close the socket to cancel the outstanding asynchronous operation.
122 // Run the io_context again until the operation completes.
126 // If the operation failed, throw an exception. Otherwise return the result.
127 return ec_
? throw boost::system::system_error(ec_
) : t_
;
131 boost::asio::chrono::steady_clock::duration timeout_
;
133 boost::system::error_code ec_
;
140 //----------------------------------------------------------------------
142 int main(int argc
, char* argv
[])
148 std::cerr
<< "Usage: blocking_tcp_client <host> <port> <message>\n";
152 boost::asio::io_context io_context
;
154 // Resolve the host name and service to a list of endpoints.
155 tcp::resolver::results_type endpoints
=
156 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(boost::asio::chrono::seconds(10), socket
));
164 boost::asio::chrono::steady_clock::time_point time_sent
=
165 boost::asio::chrono::steady_clock::now();
167 // Run an asynchronous write operation with a timeout.
168 std::string msg
= argv
[3] + std::string("\n");
169 boost::asio::async_write(socket
, boost::asio::buffer(msg
),
170 close_after(boost::asio::chrono::seconds(10), socket
));
172 for (std::string input_buffer
;;)
174 // Run an asynchronous read operation with a timeout.
175 std::size_t n
= boost::asio::async_read_until(socket
,
176 boost::asio::dynamic_buffer(input_buffer
), '\n',
177 close_after(boost::asio::chrono::seconds(10), socket
));
179 std::string
line(input_buffer
.substr(0, n
- 1));
180 input_buffer
.erase(0, n
);
182 // Keep going until we get back the line that was sent.
187 boost::asio::chrono::steady_clock::time_point time_received
=
188 boost::asio::chrono::steady_clock::now();
190 std::cout
<< "Round trip time: ";
191 std::cout
<< boost::asio::chrono::duration_cast
<
192 boost::asio::chrono::microseconds
>(
193 time_received
- time_sent
).count();
194 std::cout
<< " microseconds\n";
196 catch (std::exception
& e
)
198 std::cerr
<< "Exception: " << e
.what() << "\n";