chat_client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // 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 <deque>
  12. #include <iostream>
  13. #include <boost/bind.hpp>
  14. #include <boost/asio.hpp>
  15. #include <boost/thread/thread.hpp>
  16. #include "chat_message.hpp"
  17. using boost::asio::ip::tcp;
  18. typedef std::deque<chat_message> chat_message_queue;
  19. class chat_client
  20. {
  21. public:
  22. chat_client(boost::asio::io_context& io_context,
  23. const tcp::resolver::results_type& endpoints)
  24. : io_context_(io_context),
  25. socket_(io_context)
  26. {
  27. boost::asio::async_connect(socket_, endpoints,
  28. boost::bind(&chat_client::handle_connect, this,
  29. boost::asio::placeholders::error));
  30. }
  31. void write(const chat_message& msg)
  32. {
  33. boost::asio::post(io_context_,
  34. boost::bind(&chat_client::do_write, this, msg));
  35. }
  36. void close()
  37. {
  38. boost::asio::post(io_context_,
  39. boost::bind(&chat_client::do_close, this));
  40. }
  41. private:
  42. void handle_connect(const boost::system::error_code& error)
  43. {
  44. if (!error)
  45. {
  46. boost::asio::async_read(socket_,
  47. boost::asio::buffer(read_msg_.data(), chat_message::header_length),
  48. boost::bind(&chat_client::handle_read_header, this,
  49. boost::asio::placeholders::error));
  50. }
  51. }
  52. void handle_read_header(const boost::system::error_code& error)
  53. {
  54. if (!error && read_msg_.decode_header())
  55. {
  56. boost::asio::async_read(socket_,
  57. boost::asio::buffer(read_msg_.body(), read_msg_.body_length()),
  58. boost::bind(&chat_client::handle_read_body, this,
  59. boost::asio::placeholders::error));
  60. }
  61. else
  62. {
  63. do_close();
  64. }
  65. }
  66. void handle_read_body(const boost::system::error_code& error)
  67. {
  68. if (!error)
  69. {
  70. std::cout.write(read_msg_.body(), read_msg_.body_length());
  71. std::cout << "\n";
  72. boost::asio::async_read(socket_,
  73. boost::asio::buffer(read_msg_.data(), chat_message::header_length),
  74. boost::bind(&chat_client::handle_read_header, this,
  75. boost::asio::placeholders::error));
  76. }
  77. else
  78. {
  79. do_close();
  80. }
  81. }
  82. void do_write(chat_message msg)
  83. {
  84. bool write_in_progress = !write_msgs_.empty();
  85. write_msgs_.push_back(msg);
  86. if (!write_in_progress)
  87. {
  88. boost::asio::async_write(socket_,
  89. boost::asio::buffer(write_msgs_.front().data(),
  90. write_msgs_.front().length()),
  91. boost::bind(&chat_client::handle_write, this,
  92. boost::asio::placeholders::error));
  93. }
  94. }
  95. void handle_write(const boost::system::error_code& error)
  96. {
  97. if (!error)
  98. {
  99. write_msgs_.pop_front();
  100. if (!write_msgs_.empty())
  101. {
  102. boost::asio::async_write(socket_,
  103. boost::asio::buffer(write_msgs_.front().data(),
  104. write_msgs_.front().length()),
  105. boost::bind(&chat_client::handle_write, this,
  106. boost::asio::placeholders::error));
  107. }
  108. }
  109. else
  110. {
  111. do_close();
  112. }
  113. }
  114. void do_close()
  115. {
  116. socket_.close();
  117. }
  118. private:
  119. boost::asio::io_context& io_context_;
  120. tcp::socket socket_;
  121. chat_message read_msg_;
  122. chat_message_queue write_msgs_;
  123. };
  124. int main(int argc, char* argv[])
  125. {
  126. try
  127. {
  128. if (argc != 3)
  129. {
  130. std::cerr << "Usage: chat_client <host> <port>\n";
  131. return 1;
  132. }
  133. boost::asio::io_context io_context;
  134. tcp::resolver resolver(io_context);
  135. tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]);
  136. chat_client c(io_context, endpoints);
  137. boost::thread t(boost::bind(&boost::asio::io_context::run, &io_context));
  138. char line[chat_message::max_body_length + 1];
  139. while (std::cin.getline(line, chat_message::max_body_length + 1))
  140. {
  141. using namespace std; // For strlen and memcpy.
  142. chat_message msg;
  143. msg.body_length(strlen(line));
  144. memcpy(msg.body(), line, msg.body_length());
  145. msg.encode_header();
  146. c.write(msg);
  147. }
  148. c.close();
  149. t.join();
  150. }
  151. catch (std::exception& e)
  152. {
  153. std::cerr << "Exception: " << e.what() << "\n";
  154. }
  155. return 0;
  156. }