communication_socket_base.h 5.1 KB

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