PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hwcomponents/caros_kuka_robot/src/kuka_tcp_client.cpp

https://gitlab.com/caro-sdu/caros
C++ | 156 lines | 145 code | 8 blank | 3 comment | 14 complexity | 2a7d50b9c55bae5a24ce5217ee0c856d MD5 | raw file
  1. #include <caros/kuka_tcp_client.h>
  2. #include <string>
  3. tcp_client::tcp_client(boost::asio::io_service& io_service, boost::asio::ip::tcp::resolver::iterator endpoint_iterator,
  4. input_handler function, void* def_state)
  5. : io_service_(io_service),
  6. socket_(io_service),
  7. body_length_(0),
  8. running_(true),
  9. terminate_connection_string("bye"),
  10. input_handle(function),
  11. v_state(def_state)
  12. {
  13. do_connect(endpoint_iterator);
  14. }
  15. tcp_client::~tcp_client()
  16. {
  17. }
  18. void tcp_client::set_terminate_connection_string(std::string new_msg)
  19. {
  20. terminate_connection_string = new_msg;
  21. }
  22. void tcp_client::write(const std::string& msg)
  23. {
  24. if (msg != "hello")
  25. {
  26. std::cerr << "Writing: " << msg << std::endl;
  27. }
  28. io_service_.post([this, msg]()
  29. {
  30. bool write_in_progress = !write_msgs_.empty();
  31. write_msgs_.push_back(msg + "\n");
  32. if (!write_in_progress)
  33. {
  34. do_write();
  35. }
  36. }
  37. /*end of code*/);
  38. }
  39. void tcp_client::close()
  40. {
  41. std::cerr << "client is closed" << std::endl;
  42. io_service_.post([this]()
  43. {
  44. running_ = false;
  45. socket_.close();
  46. }
  47. /*end of code */);
  48. }
  49. void tcp_client::do_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
  50. {
  51. boost::asio::async_connect(socket_, endpoint_iterator,
  52. [this](boost::system::error_code err, boost::asio::ip::tcp::resolver::iterator)
  53. {
  54. if (!err)
  55. {
  56. do_read_header();
  57. }
  58. else
  59. {
  60. running_ = false;
  61. }
  62. std::cout << "[SOCKET CLIENT] connected to server" << std::endl;
  63. }
  64. /*end of code*/);
  65. }
  66. void tcp_client::do_read_header()
  67. {
  68. boost::asio::async_read(socket_, boost::asio::buffer(msg_len, tcp_client::header_length + 1),
  69. [this](boost::system::error_code ec, std::size_t /*length*/)
  70. {
  71. if (!ec)
  72. {
  73. // body_length_ =
  74. // atoi(std::string(msg_len,tcp_client::header_length-1).c_str());
  75. body_length_ = atoi(msg_len);
  76. if (body_length_ > 0 && body_length_ < tcp_client::max_msg_length - 2)
  77. {
  78. do_read_body();
  79. }
  80. else
  81. {
  82. std::cerr << "[SOCKET CLIENT] malformed header" << std::endl;
  83. // do_read_header();
  84. }
  85. }
  86. else
  87. {
  88. std::cerr << "[SOCKET CLIENT] error getting header, closing" << std::endl;
  89. close();
  90. }
  91. }
  92. /*end of code*/);
  93. }
  94. void tcp_client::do_read_body()
  95. {
  96. boost::asio::async_read(socket_, boost::asio::buffer(msg_txt, body_length_ + 2), // windows uses 2 chars for endl
  97. [this](boost::system::error_code ec, std::size_t /*length*/)
  98. {
  99. if (!ec)
  100. {
  101. read_msg_ = std::string(msg_txt, body_length_);
  102. if (read_msg_.find(terminate_connection_string) != std::string::npos)
  103. {
  104. std::cout << "\n[SOCKET CLIENT] closing server" << std::endl;
  105. close();
  106. }
  107. else
  108. {
  109. if (v_state != nullptr)
  110. {
  111. input_handle(read_msg_, v_state);
  112. }
  113. do_read_header();
  114. }
  115. }
  116. else
  117. {
  118. std::cerr << "[SOCKET CLIENT] error getting body, closing" << std::endl;
  119. close();
  120. }
  121. }
  122. /*end oc code*/);
  123. }
  124. void tcp_client::do_write()
  125. {
  126. boost::asio::async_write(socket_, boost::asio::buffer(write_msgs_.front().c_str(), write_msgs_.front().length()),
  127. [this](boost::system::error_code err, std::size_t /*length*/)
  128. {
  129. if (err)
  130. {
  131. std::cerr << "[SOCKET CLIENT] Writing failed\n";
  132. close();
  133. }
  134. else
  135. {
  136. write_msgs_.pop_front();
  137. if (!write_msgs_.empty())
  138. {
  139. do_write();
  140. }
  141. }
  142. }
  143. /*end of code*/);
  144. }
  145. void default_set_state(std::string, void*)
  146. {
  147. std::cout << "calling the wrong function" << std::endl;
  148. }