chat_message.cc 4.3 KB

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