]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/timeouts/blocking_token_tcp_client.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / timeouts / blocking_token_tcp_client.cpp
1 //
2 // blocking_token_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
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>
18 #include <cstdlib>
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 using boost::asio::ip::tcp;
24
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;
28
29 //----------------------------------------------------------------------
30
31 // A custom completion token that makes asynchronous operations behave as
32 // though they are blocking calls with a timeout.
33 struct close_after
34 {
35 close_after(boost::asio::chrono::steady_clock::duration t, tcp_socket& s)
36 : timeout_(t), socket_(s)
37 {
38 }
39
40 // The maximum time to wait for an asynchronous operation to complete.
41 boost::asio::chrono::steady_clock::duration timeout_;
42
43 // The socket to be closed if the operation does not complete in time.
44 tcp_socket& socket_;
45 };
46
47 namespace boost {
48 namespace asio {
49
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.
54 template <typename T>
55 class async_result<close_after, void(boost::system::error_code, T)>
56 {
57 public:
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
62 {
63 public:
64 completion_handler_type(const close_after& token)
65 : token_(token)
66 {
67 }
68
69 void operator()(boost::system::error_code ec, T t)
70 {
71 *ec_ = ec;
72 *t_ = t;
73 }
74
75 private:
76 friend class async_result;
77 close_after token_;
78 boost::system::error_code* ec_;
79 T* t_;
80 };
81
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_)
87 {
88 h.ec_ = &ec_;
89 h.t_ = &t_;
90 }
91
92 // The return_type typedef determines the result type of the asynchronous
93 // operation's initiating function.
94 typedef T return_type;
95
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.
99 return_type get()
100 {
101 boost::asio::io_context& io_context = socket_.get_executor().context();
102
103 // Restart the io_context, as it may have been left in the "stopped" state
104 // by a previous operation.
105 io_context.restart();
106
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
110 // the socket.
111 io_context.run_for(timeout_);
112
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())
118 {
119 // Close the socket to cancel the outstanding asynchronous operation.
120 socket_.close();
121
122 // Run the io_context again until the operation completes.
123 io_context.run();
124 }
125
126 // If the operation failed, throw an exception. Otherwise return the result.
127 return ec_ ? throw boost::system::system_error(ec_) : t_;
128 }
129
130 private:
131 boost::asio::chrono::steady_clock::duration timeout_;
132 tcp_socket& socket_;
133 boost::system::error_code ec_;
134 T t_;
135 };
136
137 } // namespace asio
138 } // namespace boost
139
140 //----------------------------------------------------------------------
141
142 int main(int argc, char* argv[])
143 {
144 try
145 {
146 if (argc != 4)
147 {
148 std::cerr << "Usage: blocking_tcp_client <host> <port> <message>\n";
149 return 1;
150 }
151
152 boost::asio::io_context io_context;
153
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]);
157
158 tcp_socket socket(io_context);
159
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));
163
164 boost::asio::chrono::steady_clock::time_point time_sent =
165 boost::asio::chrono::steady_clock::now();
166
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));
171
172 for (std::string input_buffer;;)
173 {
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));
178
179 std::string line(input_buffer.substr(0, n - 1));
180 input_buffer.erase(0, n);
181
182 // Keep going until we get back the line that was sent.
183 if (line == argv[3])
184 break;
185 }
186
187 boost::asio::chrono::steady_clock::time_point time_received =
188 boost::asio::chrono::steady_clock::now();
189
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";
195 }
196 catch (std::exception& e)
197 {
198 std::cerr << "Exception: " << e.what() << "\n";
199 }
200
201 return 0;
202 }