chat_client.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // chat_client.cpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 <thread>
  14. #include "asio.hpp"
  15. #include "chat_message.hpp"
  16. #include "protocol_examples_common.h"
  17. #include "esp_event.h"
  18. #include "nvs_flash.h"
  19. using asio::ip::tcp;
  20. typedef std::deque<chat_message> chat_message_queue;
  21. class chat_client
  22. {
  23. public:
  24. chat_client(asio::io_context& io_context,
  25. const tcp::resolver::results_type& endpoints)
  26. : io_context_(io_context),
  27. socket_(io_context)
  28. {
  29. do_connect(endpoints);
  30. }
  31. void write(const chat_message& msg)
  32. {
  33. asio::post(io_context_,
  34. [this, msg]()
  35. {
  36. bool write_in_progress = !write_msgs_.empty();
  37. write_msgs_.push_back(msg);
  38. if (!write_in_progress)
  39. {
  40. do_write();
  41. }
  42. });
  43. }
  44. void close()
  45. {
  46. asio::post(io_context_, [this]() { socket_.close(); });
  47. }
  48. private:
  49. void do_connect(const tcp::resolver::results_type& endpoints)
  50. {
  51. asio::async_connect(socket_, endpoints,
  52. [this](std::error_code ec, tcp::endpoint)
  53. {
  54. if (!ec)
  55. {
  56. do_read_header();
  57. }
  58. });
  59. }
  60. void do_read_header()
  61. {
  62. asio::async_read(socket_,
  63. asio::buffer(read_msg_.data(), chat_message::header_length),
  64. [this](std::error_code ec, std::size_t /*length*/)
  65. {
  66. if (!ec && read_msg_.decode_header())
  67. {
  68. do_read_body();
  69. }
  70. else
  71. {
  72. socket_.close();
  73. }
  74. });
  75. }
  76. void do_read_body()
  77. {
  78. asio::async_read(socket_,
  79. asio::buffer(read_msg_.body(), read_msg_.body_length()),
  80. [this](std::error_code ec, std::size_t /*length*/)
  81. {
  82. if (!ec)
  83. {
  84. std::cout.write(read_msg_.body(), read_msg_.body_length());
  85. std::cout << "\n";
  86. do_read_header();
  87. }
  88. else
  89. {
  90. socket_.close();
  91. }
  92. });
  93. }
  94. void do_write()
  95. {
  96. asio::async_write(socket_,
  97. asio::buffer(write_msgs_.front().data(),
  98. write_msgs_.front().length()),
  99. [this](std::error_code ec, std::size_t /*length*/)
  100. {
  101. if (!ec)
  102. {
  103. write_msgs_.pop_front();
  104. if (!write_msgs_.empty())
  105. {
  106. do_write();
  107. }
  108. }
  109. else
  110. {
  111. socket_.close();
  112. }
  113. });
  114. }
  115. private:
  116. asio::io_context& io_context_;
  117. tcp::socket socket_;
  118. chat_message read_msg_;
  119. chat_message_queue write_msgs_;
  120. };
  121. void read_line(char * line, int max_chars);
  122. extern "C" void app_main(void)
  123. {
  124. ESP_ERROR_CHECK(nvs_flash_init());
  125. esp_netif_init();
  126. ESP_ERROR_CHECK(esp_event_loop_create_default());
  127. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  128. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  129. * examples/protocols/README.md for more information about this function.
  130. */
  131. ESP_ERROR_CHECK(example_connect());
  132. /* This helper function configures blocking UART I/O */
  133. ESP_ERROR_CHECK(example_configure_stdin_stdout());
  134. std::string name(CONFIG_EXAMPLE_SERVER_IP);
  135. std::string port(CONFIG_EXAMPLE_PORT);
  136. char line[chat_message::max_body_length + 1] = { 0 };
  137. if (name == "FROM_STDIN") {
  138. std::cout << "Please enter ip address of chat server" << std::endl;
  139. if (std::cin.getline(line, chat_message::max_body_length + 1)) {
  140. name = line;
  141. std::cout << "Chat server IP:" << name << std::endl;
  142. }
  143. }
  144. asio::io_context io_context;
  145. tcp::resolver resolver(io_context);
  146. auto endpoints = resolver.resolve(name, port);
  147. chat_client c(io_context, endpoints);
  148. std::thread t([&io_context](){ io_context.run(); });
  149. while (std::cin.getline(line, chat_message::max_body_length + 1) && std::string(line) != "exit") {
  150. chat_message msg;
  151. msg.body_length(std::strlen(line));
  152. std::memcpy(msg.body(), line, msg.body_length());
  153. msg.encode_header();
  154. c.write(msg);
  155. }
  156. c.close();
  157. t.join();
  158. ESP_ERROR_CHECK(example_disconnect());
  159. }