communication_message.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Created by huli on 2020/6/29.
  3. //
  4. #include "communication_message.h"
  5. Communication_message::Communication_message()
  6. {
  7. m_message_type = eBase_msg;
  8. m_receive_time = std::chrono::system_clock::now();
  9. m_timeout_ms = std::chrono::milliseconds(5000); //超时默认5秒
  10. m_sender = eEmpty;
  11. m_receiver = eEmpty;
  12. // m_message_buf = "";
  13. }
  14. Communication_message::Communication_message(std::string message_buf)
  15. {
  16. m_message_type = eBase_msg;
  17. m_receive_time = std::chrono::system_clock::now();
  18. m_timeout_ms = std::chrono::milliseconds(5000); //超时默认5秒
  19. m_sender = eEmpty;
  20. m_receiver = eEmpty;
  21. m_message_buf = message_buf;
  22. }
  23. Communication_message::Communication_message(char* p_buf, int size)
  24. {
  25. m_message_type = eBase_msg;
  26. m_receive_time = std::chrono::system_clock::now();
  27. m_timeout_ms = std::chrono::milliseconds(5000); //超时默认5秒
  28. m_sender = eEmpty;
  29. m_receiver = eEmpty;
  30. m_message_buf = std::string(p_buf, size);
  31. }
  32. Communication_message::~Communication_message()
  33. {
  34. }
  35. bool Communication_message::is_over_time()
  36. {
  37. std::cout<<"interval: "<<std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - m_receive_time).count()<<std::endl;
  38. std::cout<<"timeout: "<<m_timeout_ms.count()<<std::endl;
  39. if ( std::chrono::system_clock::now() - m_receive_time > m_timeout_ms)
  40. {
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48. void Communication_message::reset(const message::Base_info& base_info, std::string receive_string)
  49. {
  50. m_message_type = (Message_type)(base_info.msg_type());
  51. m_receive_time = std::chrono::system_clock::now();
  52. m_timeout_ms = std::chrono::milliseconds(base_info.timeout_ms());
  53. m_sender = (Communicator)(base_info.sender());
  54. m_receiver = (Communicator)(base_info.receiver());
  55. m_message_buf = receive_string;
  56. }
  57. Communication_message::Message_type Communication_message::get_message_type()
  58. {
  59. return m_message_type;
  60. }
  61. Communication_message::Communicator Communication_message::get_sender()
  62. {
  63. return m_sender;
  64. }
  65. Communication_message::Communicator Communication_message::get_receiver()
  66. {
  67. return m_receiver;
  68. }
  69. std::string& Communication_message::get_message_buf()
  70. {
  71. return m_message_buf;
  72. }