chat_client.cpp 4.4 KB

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