communication_message.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. if ( std::chrono::system_clock::now() - m_receive_time > m_timeout_ms)
  38. {
  39. return true;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. void Communication_message::reset(const message::Base_info& base_info, std::string receive_string)
  47. {
  48. m_message_type = (Message_type)(base_info.msg_type());
  49. m_receive_time = std::chrono::system_clock::now();
  50. m_timeout_ms = std::chrono::milliseconds(base_info.timeout_ms());
  51. m_sender = (Communicator)(base_info.sender());
  52. m_receiver = (Communicator)(base_info.receiver());
  53. m_message_buf = receive_string;
  54. }
  55. Communication_message::Message_type Communication_message::get_message_type()
  56. {
  57. return m_message_type;
  58. }
  59. Communication_message::Communicator Communication_message::get_sender()
  60. {
  61. return m_sender;
  62. }
  63. Communication_message::Communicator Communication_message::get_receiver()
  64. {
  65. return m_receiver;
  66. }
  67. std::string& Communication_message::get_message_buf()
  68. {
  69. return m_message_buf;
  70. }
  71. std::chrono::system_clock::time_point Communication_message::get_receive_time()
  72. {
  73. return m_receive_time;
  74. }