communication_socket_base.h 4.7 KB

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