communication_socket_base.h 5.4 KB

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