communication_socket_base.h 5.7 KB

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