]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/timeouts/async_tcp_client.cpp
2134e6395d5ffc9c753ee5866947dba96532b3c1
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / timeouts / async_tcp_client.cpp
1 //
2 // async_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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/deadline_timer.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/asio/write.hpp>
17 #include <boost/bind.hpp>
18 #include <iostream>
19
20 using boost::asio::deadline_timer;
21 using boost::asio::ip::tcp;
22
23 //
24 // This class manages socket timeouts by applying the concept of a deadline.
25 // Some asynchronous operations are given deadlines by which they must complete.
26 // Deadlines are enforced by an "actor" that persists for the lifetime of the
27 // client object:
28 //
29 // +----------------+
30 // | |
31 // | check_deadline |<---+
32 // | | |
33 // +----------------+ | async_wait()
34 // | |
35 // +---------+
36 //
37 // If the deadline actor determines that the deadline has expired, the socket
38 // is closed and any outstanding operations are consequently cancelled.
39 //
40 // Connection establishment involves trying each endpoint in turn until a
41 // connection is successful, or the available endpoints are exhausted. If the
42 // deadline actor closes the socket, the connect actor is woken up and moves to
43 // the next endpoint.
44 //
45 // +---------------+
46 // | |
47 // | start_connect |<---+
48 // | | |
49 // +---------------+ |
50 // | |
51 // async_- | +----------------+
52 // connect() | | |
53 // +--->| handle_connect |
54 // | |
55 // +----------------+
56 // :
57 // Once a connection is :
58 // made, the connect :
59 // actor forks in two - :
60 // :
61 // an actor for reading : and an actor for
62 // inbound messages: : sending heartbeats:
63 // :
64 // +------------+ : +-------------+
65 // | |<- - - - -+- - - - ->| |
66 // | start_read | | start_write |<---+
67 // | |<---+ | | |
68 // +------------+ | +-------------+ | async_wait()
69 // | | | |
70 // async_- | +-------------+ async_- | +--------------+
71 // read_- | | | write() | | |
72 // until() +--->| handle_read | +--->| handle_write |
73 // | | | |
74 // +-------------+ +--------------+
75 //
76 // The input actor reads messages from the socket, where messages are delimited
77 // by the newline character. The deadline for a complete message is 30 seconds.
78 //
79 // The heartbeat actor sends a heartbeat (a message that consists of a single
80 // newline character) every 10 seconds. In this example, no deadline is applied
81 // message sending.
82 //
83 class client
84 {
85 public:
86 client(boost::asio::io_context& io_context)
87 : stopped_(false),
88 socket_(io_context),
89 deadline_(io_context),
90 heartbeat_timer_(io_context)
91 {
92 }
93
94 // Called by the user of the client class to initiate the connection process.
95 // The endpoints will have been obtained using a tcp::resolver.
96 void start(tcp::resolver::results_type endpoints)
97 {
98 // Start the connect actor.
99 endpoints_ = endpoints;
100 start_connect(endpoints_.begin());
101
102 // Start the deadline actor. You will note that we're not setting any
103 // particular deadline here. Instead, the connect and input actors will
104 // update the deadline prior to each asynchronous operation.
105 deadline_.async_wait(boost::bind(&client::check_deadline, this));
106 }
107
108 // This function terminates all the actors to shut down the connection. It
109 // may be called by the user of the client class, or by the class itself in
110 // response to graceful termination or an unrecoverable error.
111 void stop()
112 {
113 stopped_ = true;
114 boost::system::error_code ignored_ec;
115 socket_.close(ignored_ec);
116 deadline_.cancel();
117 heartbeat_timer_.cancel();
118 }
119
120 private:
121 void start_connect(tcp::resolver::results_type::iterator endpoint_iter)
122 {
123 if (endpoint_iter != endpoints_.end())
124 {
125 std::cout << "Trying " << endpoint_iter->endpoint() << "...\n";
126
127 // Set a deadline for the connect operation.
128 deadline_.expires_from_now(boost::posix_time::seconds(60));
129
130 // Start the asynchronous connect operation.
131 socket_.async_connect(endpoint_iter->endpoint(),
132 boost::bind(&client::handle_connect,
133 this, _1, endpoint_iter));
134 }
135 else
136 {
137 // There are no more endpoints to try. Shut down the client.
138 stop();
139 }
140 }
141
142 void handle_connect(const boost::system::error_code& ec,
143 tcp::resolver::results_type::iterator endpoint_iter)
144 {
145 if (stopped_)
146 return;
147
148 // The async_connect() function automatically opens the socket at the start
149 // of the asynchronous operation. If the socket is closed at this time then
150 // the timeout handler must have run first.
151 if (!socket_.is_open())
152 {
153 std::cout << "Connect timed out\n";
154
155 // Try the next available endpoint.
156 start_connect(++endpoint_iter);
157 }
158
159 // Check if the connect operation failed before the deadline expired.
160 else if (ec)
161 {
162 std::cout << "Connect error: " << ec.message() << "\n";
163
164 // We need to close the socket used in the previous connection attempt
165 // before starting a new one.
166 socket_.close();
167
168 // Try the next available endpoint.
169 start_connect(++endpoint_iter);
170 }
171
172 // Otherwise we have successfully established a connection.
173 else
174 {
175 std::cout << "Connected to " << endpoint_iter->endpoint() << "\n";
176
177 // Start the input actor.
178 start_read();
179
180 // Start the heartbeat actor.
181 start_write();
182 }
183 }
184
185 void start_read()
186 {
187 // Set a deadline for the read operation.
188 deadline_.expires_from_now(boost::posix_time::seconds(30));
189
190 // Start an asynchronous operation to read a newline-delimited message.
191 boost::asio::async_read_until(socket_, input_buffer_, '\n',
192 boost::bind(&client::handle_read, this, _1));
193 }
194
195 void handle_read(const boost::system::error_code& ec)
196 {
197 if (stopped_)
198 return;
199
200 if (!ec)
201 {
202 // Extract the newline-delimited message from the buffer.
203 std::string line;
204 std::istream is(&input_buffer_);
205 std::getline(is, line);
206
207 // Empty messages are heartbeats and so ignored.
208 if (!line.empty())
209 {
210 std::cout << "Received: " << line << "\n";
211 }
212
213 start_read();
214 }
215 else
216 {
217 std::cout << "Error on receive: " << ec.message() << "\n";
218
219 stop();
220 }
221 }
222
223 void start_write()
224 {
225 if (stopped_)
226 return;
227
228 // Start an asynchronous operation to send a heartbeat message.
229 boost::asio::async_write(socket_, boost::asio::buffer("\n", 1),
230 boost::bind(&client::handle_write, this, _1));
231 }
232
233 void handle_write(const boost::system::error_code& ec)
234 {
235 if (stopped_)
236 return;
237
238 if (!ec)
239 {
240 // Wait 10 seconds before sending the next heartbeat.
241 heartbeat_timer_.expires_from_now(boost::posix_time::seconds(10));
242 heartbeat_timer_.async_wait(boost::bind(&client::start_write, this));
243 }
244 else
245 {
246 std::cout << "Error on heartbeat: " << ec.message() << "\n";
247
248 stop();
249 }
250 }
251
252 void check_deadline()
253 {
254 if (stopped_)
255 return;
256
257 // Check whether the deadline has passed. We compare the deadline against
258 // the current time since a new asynchronous operation may have moved the
259 // deadline before this actor had a chance to run.
260 if (deadline_.expires_at() <= deadline_timer::traits_type::now())
261 {
262 // The deadline has passed. The socket is closed so that any outstanding
263 // asynchronous operations are cancelled.
264 socket_.close();
265
266 // There is no longer an active deadline. The expiry is set to positive
267 // infinity so that the actor takes no action until a new deadline is set.
268 deadline_.expires_at(boost::posix_time::pos_infin);
269 }
270
271 // Put the actor back to sleep.
272 deadline_.async_wait(boost::bind(&client::check_deadline, this));
273 }
274
275 private:
276 bool stopped_;
277 tcp::resolver::results_type endpoints_;
278 tcp::socket socket_;
279 boost::asio::streambuf input_buffer_;
280 deadline_timer deadline_;
281 deadline_timer heartbeat_timer_;
282 };
283
284 int main(int argc, char* argv[])
285 {
286 try
287 {
288 if (argc != 3)
289 {
290 std::cerr << "Usage: client <host> <port>\n";
291 return 1;
292 }
293
294 boost::asio::io_context io_context;
295 tcp::resolver r(io_context);
296 client c(io_context);
297
298 c.start(r.resolve(argv[1], argv[2]));
299
300 io_context.run();
301 }
302 catch (std::exception& e)
303 {
304 std::cerr << "Exception: " << e.what() << "\n";
305 }
306
307 return 0;
308 }