async_tcp_client.cpp 9.0 KB

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