chat_message.cc 4.3 KB

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