communication_socket_base.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. protected:
  61. //mp_receive_data_thread 接受线程执行函数,
  62. //receive_data_thread 内部线程负责接受消息
  63. static void receive_data_thread(Communication_socket_base* communicator);
  64. //检查消息是否可以被解析, 需要子类重载
  65. virtual Error_manager check_msg(Communication_message* p_msg);
  66. //mp_analysis_data_thread 解析线程执行函数,
  67. //analysis_data_thread 内部线程负责解析消息
  68. void analysis_data_thread();
  69. //遍历接受链表, 解析消息,
  70. Error_manager analysis_receive_list();
  71. //检查消息是否可以被解析, 需要子类重载
  72. virtual Error_manager check_executer(Communication_message* p_msg);
  73. //处理消息, 需要子类重载
  74. virtual Error_manager execute_msg(Communication_message* p_msg);
  75. //mp_send_data_thread 发送线程执行函数,
  76. //send_data_thread 内部线程负责发送消息
  77. static void send_data_thread(Communication_socket_base* communicator);
  78. //mp_encapsulate_data_thread 封装线程执行函数,
  79. //encapsulate_data_thread 内部线程负责封装消息
  80. void encapsulate_data_thread();
  81. //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
  82. virtual Error_manager encapsulate_send_data();
  83. public:
  84. //封装消息, 需要子类重载
  85. virtual Error_manager encapsulate_msg(std::string message);
  86. //封装消息, 需要子类重载
  87. virtual Error_manager encapsulate_msg(Communication_message* p_msg);
  88. protected://member variable
  89. //通用的网络编程接口, 默认使用总线模式, (网状结构)
  90. nnxx::socket m_socket { nnxx::SP, nnxx::BUS };
  91. std::mutex m_mutex; //m_socket的锁
  92. //通信状态
  93. Communication_statu m_communication_statu; //通信状态
  94. //接受模块,
  95. Thread_safe_list<Communication_message*> m_receive_data_list; //接受的list容器
  96. std::thread* mp_receive_data_thread; //接受的线程指针
  97. Thread_condition m_receive_condition; //接受的条件变量
  98. std::thread* mp_analysis_data_thread; //解析的线程指针
  99. Thread_condition m_analysis_data_condition; //解析的条件变量
  100. //发送模块,
  101. Thread_safe_list<Communication_message*> m_send_data_list; //发送的list容器
  102. std::thread* mp_send_data_thread; //发送的线程指针
  103. Thread_condition m_send_data_condition; //发送的条件变量
  104. std::thread* mp_encapsulate_data_thread; //封装的线程指针
  105. Thread_condition m_encapsulate_data_condition; //封装的条件变量
  106. private:
  107. };
  108. #endif //__COMMUNICATION_SOCKET_BASE__HH__