2 // async_tcp_client.cpp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2014 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/deadline_timer.hpp>
12 #include <boost/asio/io_service.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>
20 using boost::asio::deadline_timer;
21 using boost::asio::ip::tcp;
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
31 // | check_deadline |<---+
33 // +----------------+ | async_wait()
37 // If the deadline actor determines that the deadline has expired, the socket
38 // is closed and any outstanding operations are consequently cancelled.
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
47 // | start_connect |<---+
49 // +---------------+ |
51 // async_- | +----------------+
53 // +--->| handle_connect |
57 // Once a connection is :
58 // made, the connect :
59 // actor forks in two - :
61 // an actor for reading : and an actor for
62 // inbound messages: : sending heartbeats:
64 // +------------+ : +-------------+
65 // | |<- - - - -+- - - - ->| |
66 // | start_read | | start_write |<---+
68 // +------------+ | +-------------+ | async_wait()
70 // async_- | +-------------+ async_- | +--------------+
71 // read_- | | | write() | | |
72 // until() +--->| handle_read | +--->| handle_write |
74 // +-------------+ +--------------+
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.
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
86 client(boost::asio::io_service& io_service)
89 deadline_(io_service),
90 heartbeat_timer_(io_service)
94 // Called by the user of the client class to initiate the connection process.
95 // The endpoint iterator will have been obtained using a tcp::resolver.
96 void start(tcp::resolver::iterator endpoint_iter)
98 // Start the connect actor.
99 start_connect(endpoint_iter);
101 // Start the deadline actor. You will note that we're not setting any
102 // particular deadline here. Instead, the connect and input actors will
103 // update the deadline prior to each asynchronous operation.
104 deadline_.async_wait(boost::bind(&client::check_deadline, this));
107 // This function terminates all the actors to shut down the connection. It
108 // may be called by the user of the client class, or by the class itself in
109 // response to graceful termination or an unrecoverable error.
113 boost::system::error_code ignored_ec;
114 socket_.close(ignored_ec);
116 heartbeat_timer_.cancel();
120 void start_connect(tcp::resolver::iterator endpoint_iter)
122 if (endpoint_iter != tcp::resolver::iterator())
124 std::cout << "Trying " << endpoint_iter->endpoint() << "...\n";
126 // Set a deadline for the connect operation.
127 deadline_.expires_from_now(boost::posix_time::seconds(60));
129 // Start the asynchronous connect operation.
130 socket_.async_connect(endpoint_iter->endpoint(),
131 boost::bind(&client::handle_connect,
132 this, _1, endpoint_iter));
136 // There are no more endpoints to try. Shut down the client.
141 void handle_connect(const boost::system::error_code& ec,
142 tcp::resolver::iterator endpoint_iter)
147 // The async_connect() function automatically opens the socket at the start
148 // of the asynchronous operation. If the socket is closed at this time then
149 // the timeout handler must have run first.
150 if (!socket_.is_open())
152 std::cout << "Connect timed out\n";
154 // Try the next available endpoint.
155 start_connect(++endpoint_iter);
158 // Check if the connect operation failed before the deadline expired.
161 std::cout << "Connect error: " << ec.message() << "\n";
163 // We need to close the socket used in the previous connection attempt
164 // before starting a new one.
167 // Try the next available endpoint.
168 start_connect(++endpoint_iter);
171 // Otherwise we have successfully established a connection.
174 std::cout << "Connected to " << endpoint_iter->endpoint() << "\n";
176 // Start the input actor.
179 // Start the heartbeat actor.
186 // Set a deadline for the read operation.
187 deadline_.expires_from_now(boost::posix_time::seconds(30));
189 // Start an asynchronous operation to read a newline-delimited message.
190 boost::asio::async_read_until(socket_, input_buffer_, '\n',
191 boost::bind(&client::handle_read, this, _1));
194 void handle_read(const boost::system::error_code& ec)
201 // Extract the newline-delimited message from the buffer.
203 std::istream is(&input_buffer_);
204 std::getline(is, line);
206 // Empty messages are heartbeats and so ignored.
209 std::cout << "Received: " << line << "\n";
216 std::cout << "Error on receive: " << ec.message() << "\n";
227 // Start an asynchronous operation to send a heartbeat message.
228 boost::asio::async_write(socket_, boost::asio::buffer("\n", 1),
229 boost::bind(&client::handle_write, this, _1));
232 void handle_write(const boost::system::error_code& ec)
239 // Wait 10 seconds before sending the next heartbeat.
240 heartbeat_timer_.expires_from_now(boost::posix_time::seconds(10));
241 heartbeat_timer_.async_wait(boost::bind(&client::start_write, this));
245 std::cout << "Error on heartbeat: " << ec.message() << "\n";
251 void check_deadline()
256 // Check whether the deadline has passed. We compare the deadline against
257 // the current time since a new asynchronous operation may have moved the
258 // deadline before this actor had a chance to run.
259 if (deadline_.expires_at() <= deadline_timer::traits_type::now())
261 // The deadline has passed. The socket is closed so that any outstanding
262 // asynchronous operations are cancelled.
265 // There is no longer an active deadline. The expiry is set to positive
266 // infinity so that the actor takes no action until a new deadline is set.
267 deadline_.expires_at(boost::posix_time::pos_infin);
270 // Put the actor back to sleep.
271 deadline_.async_wait(boost::bind(&client::check_deadline, this));
277 boost::asio::streambuf input_buffer_;
278 deadline_timer deadline_;
279 deadline_timer heartbeat_timer_;
282 int main(int argc, char* argv[])
288 std::cerr << "Usage: client <host> <port>\n";
292 boost::asio::io_service io_service;
293 tcp::resolver r(io_service);
294 client c(io_service);
296 c.start(r.resolve(tcp::resolver::query(argv[1], argv[2])));
300 catch (std::exception& e)
302 std::cerr << "Exception: " << e.what() << "\n";