posix_chat_client.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // posix_chat_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 <cstdlib>
  11. #include <cstring>
  12. #include <iostream>
  13. #include <boost/array.hpp>
  14. #include <boost/bind.hpp>
  15. #include <boost/asio.hpp>
  16. #include "chat_message.hpp"
  17. #if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  18. using boost::asio::ip::tcp;
  19. namespace posix = boost::asio::posix;
  20. class posix_chat_client
  21. {
  22. public:
  23. posix_chat_client(boost::asio::io_context& io_context,
  24. const tcp::resolver::results_type& endpoints)
  25. : socket_(io_context),
  26. input_(io_context, ::dup(STDIN_FILENO)),
  27. output_(io_context, ::dup(STDOUT_FILENO)),
  28. input_buffer_(chat_message::max_body_length)
  29. {
  30. boost::asio::async_connect(socket_, endpoints,
  31. boost::bind(&posix_chat_client::handle_connect, this,
  32. boost::asio::placeholders::error));
  33. }
  34. private:
  35. void handle_connect(const boost::system::error_code& error)
  36. {
  37. if (!error)
  38. {
  39. // Read the fixed-length header of the next message from the server.
  40. boost::asio::async_read(socket_,
  41. boost::asio::buffer(read_msg_.data(), chat_message::header_length),
  42. boost::bind(&posix_chat_client::handle_read_header, this,
  43. boost::asio::placeholders::error));
  44. // Read a line of input entered by the user.
  45. boost::asio::async_read_until(input_, input_buffer_, '\n',
  46. boost::bind(&posix_chat_client::handle_read_input, this,
  47. boost::asio::placeholders::error,
  48. boost::asio::placeholders::bytes_transferred));
  49. }
  50. }
  51. void handle_read_header(const boost::system::error_code& error)
  52. {
  53. if (!error && read_msg_.decode_header())
  54. {
  55. // Read the variable-length body of the message from the server.
  56. boost::asio::async_read(socket_,
  57. boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
  58. boost::bind(&posix_chat_client::handle_read_body, this,
  59. boost::asio::placeholders::error));
  60. }
  61. else
  62. {
  63. close();
  64. }
  65. }
  66. void handle_read_body(const boost::system::error_code& error)
  67. {
  68. if (!error)
  69. {
  70. // Write out the message we just received, terminated by a newline.
  71. static char eol[] = { '\n' };
  72. boost::array<boost::asio::const_buffer, 2> buffers = {{
  73. boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
  74. boost::asio::buffer(eol) }};
  75. boost::asio::async_write(output_, buffers,
  76. boost::bind(&posix_chat_client::handle_write_output, this,
  77. boost::asio::placeholders::error));
  78. }
  79. else
  80. {
  81. close();
  82. }
  83. }
  84. void handle_write_output(const boost::system::error_code& error)
  85. {
  86. if (!error)
  87. {
  88. // Read the fixed-length header of the next message from the server.
  89. boost::asio::async_read(socket_,
  90. boost::asio::buffer(read_msg_.data(), chat_message::header_length),
  91. boost::bind(&posix_chat_client::handle_read_header, this,
  92. boost::asio::placeholders::error));
  93. }
  94. else
  95. {
  96. close();
  97. }
  98. }
  99. void handle_read_input(const boost::system::error_code& error,
  100. std::size_t length)
  101. {
  102. if (!error)
  103. {
  104. // Write the message (minus the newline) to the server.
  105. write_msg_.body_length(length - 1);
  106. input_buffer_.sgetn(write_msg_.body(), length - 1);
  107. input_buffer_.consume(1); // Remove newline from input.
  108. write_msg_.encode_header();
  109. boost::asio::async_write(socket_,
  110. boost::asio::buffer(write_msg_.data(), write_msg_.length()),
  111. boost::bind(&posix_chat_client::handle_write, this,
  112. boost::asio::placeholders::error));
  113. }
  114. else if (error == boost::asio::error::not_found)
  115. {
  116. // Didn't get a newline. Send whatever we have.
  117. write_msg_.body_length(input_buffer_.size());
  118. input_buffer_.sgetn(write_msg_.body(), input_buffer_.size());
  119. write_msg_.encode_header();
  120. boost::asio::async_write(socket_,
  121. boost::asio::buffer(write_msg_.data(), write_msg_.length()),
  122. boost::bind(&posix_chat_client::handle_write, this,
  123. boost::asio::placeholders::error));
  124. }
  125. else
  126. {
  127. close();
  128. }
  129. }
  130. void handle_write(const boost::system::error_code& error)
  131. {
  132. if (!error)
  133. {
  134. // Read a line of input entered by the user.
  135. boost::asio::async_read_until(input_, input_buffer_, '\n',
  136. boost::bind(&posix_chat_client::handle_read_input, this,
  137. boost::asio::placeholders::error,
  138. boost::asio::placeholders::bytes_transferred));
  139. }
  140. else
  141. {
  142. close();
  143. }
  144. }
  145. void close()
  146. {
  147. // Cancel all outstanding asynchronous operations.
  148. socket_.close();
  149. input_.close();
  150. output_.close();
  151. }
  152. private:
  153. tcp::socket socket_;
  154. posix::stream_descriptor input_;
  155. posix::stream_descriptor output_;
  156. chat_message read_msg_;
  157. chat_message write_msg_;
  158. boost::asio::streambuf input_buffer_;
  159. };
  160. int main(int argc, char* argv[])
  161. {
  162. try
  163. {
  164. if (argc != 3)
  165. {
  166. std::cerr << "Usage: posix_chat_client <host> <port>\n";
  167. return 1;
  168. }
  169. boost::asio::io_context io_context;
  170. tcp::resolver resolver(io_context);
  171. tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]);
  172. posix_chat_client c(io_context, endpoints);
  173. io_context.run();
  174. }
  175. catch (std::exception& e)
  176. {
  177. std::cerr << "Exception: " << e.what() << "\n";
  178. }
  179. return 0;
  180. }
  181. #else // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
  182. int main() {}
  183. #endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)