communication_socket_base.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * communication_socket_base 通信模块的基类,
  3. * 用户从这个基类继承, 初始化之后, 便可以自动进行通信
  4. * 重载解析消息和封装消息,
  5. *
  6. *Thread_safe_list<Binary_buf*> 使用 Binary_buf , 而不是string
  7. * 主要是为了支持直接发送数字0
  8. *
  9. *
  10. * */
  11. #ifndef __COMMUNICATION_SOCKET_BASE__HH__
  12. #define __COMMUNICATION_SOCKET_BASE__HH__
  13. #include <mutex>
  14. #include <thread>
  15. #include <nnxx/message>
  16. #include <nnxx/socket.h>
  17. #include <nnxx/bus.h>
  18. #include <glog/logging.h>
  19. #include "../error_code/error_code.h"
  20. #include "../tool/binary_buf.h"
  21. #include "../tool/thread_safe_list.h"
  22. #include "../tool/thread_condition.h"
  23. #include "../communication/communication.pb.h"
  24. #include "../communication/communication_message.h"
  25. #include "../message/message_base.pb.h"
  26. //#include "../message/measure_message.pb.h"
  27. #define COMMUNICATION_PARAMETER_PATH "../setting/communication.prototxt"
  28. class Communication_socket_base
  29. {
  30. //通信状态
  31. enum Communication_statu
  32. {
  33. COMMUNICATION_UNKNOW =0, //通信状态 未知
  34. COMMUNICATION_READY =1, //通信状态 正常
  35. COMMUNICATION_FAULT =3, //通信状态 错误
  36. };
  37. public:
  38. Communication_socket_base();
  39. Communication_socket_base(const Communication_socket_base& other)= delete;
  40. Communication_socket_base& operator =(const Communication_socket_base& other)= delete;
  41. ~Communication_socket_base();
  42. public://API functions
  43. //初始化 通信 模块。如下三选一
  44. virtual Error_manager communication_init();
  45. //初始化 通信 模块。从文件读取
  46. Error_manager communication_init_from_protobuf(std::string prototxt_path);
  47. //初始化 通信 模块。从protobuf读取
  48. Error_manager communication_init_from_protobuf(Communication_proto::Communication_parameter_all& communication_parameter_all);
  49. //初始化
  50. virtual Error_manager communication_init(std::string bind_string, std::vector<std::string>& connect_string_vector);
  51. //bind
  52. virtual Error_manager communication_bind(std::string bind_string);
  53. //connect
  54. virtual Error_manager communication_connect(std::vector<std::string>& connect_string_vector);
  55. //connect
  56. virtual Error_manager communication_connect(std::string connect_string);
  57. //启动通信, run thread
  58. virtual Error_manager communication_run();
  59. //反初始化 通信 模块。
  60. virtual Error_manager communication_uninit();
  61. public://get or set member variable
  62. void set_analysis_cycle_time(unsigned int analysis_cycle_time);
  63. void set_encapsulate_cycle_time(unsigned int encapsulate_cycle_time);
  64. protected:
  65. //mp_receive_data_thread 接受线程执行函数,
  66. //receive_data_thread 内部线程负责接受消息
  67. void receive_data_thread();
  68. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  69. virtual Error_manager check_msg(Communication_message* p_msg);
  70. //mp_analysis_data_thread 解析线程执行函数,
  71. //analysis_data_thread 内部线程负责解析消息
  72. void analysis_data_thread();
  73. //遍历接受链表, 解析消息,
  74. Error_manager analysis_receive_list();
  75. //检查执行者的状态, 判断能否处理这条消息, 需要子类重载
  76. virtual Error_manager check_executer(Communication_message* p_msg);
  77. //处理消息, 需要子类重载
  78. virtual Error_manager execute_msg(Communication_message* p_msg);
  79. //mp_send_data_thread 发送线程执行函数,
  80. //send_data_thread 内部线程负责发送消息
  81. void send_data_thread();
  82. //mp_encapsulate_data_thread 封装线程执行函数,
  83. //encapsulate_data_thread 内部线程负责封装消息
  84. void encapsulate_data_thread();
  85. //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
  86. virtual Error_manager encapsulate_send_data();
  87. public:
  88. //封装消息, 需要子类重载
  89. virtual Error_manager encapsulate_msg(std::string message);
  90. //封装消息, 需要子类重载
  91. virtual Error_manager encapsulate_msg(Communication_message* p_msg);
  92. protected://member variable
  93. //通用的网络编程接口, 默认使用总线模式, (网状结构)
  94. nnxx::socket m_socket { nnxx::SP, nnxx::BUS };
  95. std::mutex m_mutex; //m_socket的锁
  96. //通信状态
  97. Communication_statu m_communication_statu; //通信状态
  98. //接受模块,
  99. Thread_safe_list<Communication_message*> m_receive_data_list; //接受的list容器
  100. std::thread* mp_receive_data_thread; //接受的线程指针
  101. Thread_condition m_receive_condition; //接受的条件变量
  102. std::thread* mp_analysis_data_thread; //解析的线程指针
  103. Thread_condition m_analysis_data_condition; //解析的条件变量
  104. unsigned int m_analysis_cycle_time; //自动解析的时间周期
  105. //发送模块,
  106. Thread_safe_list<Communication_message*> m_send_data_list; //发送的list容器
  107. std::thread* mp_send_data_thread; //发送的线程指针
  108. Thread_condition m_send_data_condition; //发送的条件变量
  109. std::thread* mp_encapsulate_data_thread; //封装的线程指针
  110. Thread_condition m_encapsulate_data_condition; //封装的条件变量
  111. unsigned int m_encapsulate_cycle_time; //自动封装的时间周期
  112. private:
  113. };
  114. #endif //__COMMUNICATION_SOCKET_BASE__HH__