communication_socket_base.h 4.2 KB

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