123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- * communication_socket_base 通信模块的基类,
- * 用户从这个基类继承, 初始化之后, 便可以自动进行通信
- * 重载解析消息和封装消息,
- *
- *Thread_safe_list<Binary_buf*> 使用 Binary_buf , 而不是string
- * 主要是为了支持直接发送数字0
- *
- *
- * */
- #ifndef __COMMUNICATION_SOCKET_BASE__HH__
- #define __COMMUNICATION_SOCKET_BASE__HH__
- #include <nnxx/message>
- #include <nnxx/socket>
- #include <glog/logging.h>
- #include "../error_code/error_code.h"
- #include "../tool/binary_buf.h"
- #include "../tool/thread_safe_list.h"
- #include "../tool/thread_condition.h"
- class Communication_socket_base
- {
- //通信状态
- enum Communication_statu
- {
- COMMUNICATION_UNKNOW =0, //通信状态 未知
- COMMUNICATION_READY =1, //通信状态 正常
- COMMUNICATION_FAULT =3, //通信状态 错误
- };
- public:
- Communication_socket_base();
- Communication_socket_base(const Communication_socket_base& other)= delete;
- Communication_socket_base& operator =(const Communication_socket_base& other)= delete;
- ~Communication_socket_base();
- public://API functions
- //初始化 通信 模块。如下三选一
- virtual Error_manager communication_init();
- //初始化 通信 模块。从文件读取
- // Error_manager communication_init_from_protobuf(std::string prototxt_path);
- //初始化 通信 模块。从protobuf读取
- // Error_manager communication_init_from_protobuf(Laser_proto::Laser_parameter_all& laser_parameters);
- //初始化
- virtual Error_manager communication_init(std::string bind_string, std::vector<std::string>& connect_string_vector);
- //bind
- virtual Error_manager communication_bind(std::string bind_string);
- //connect
- virtual Error_manager communication_connect(std::vector<std::string>& connect_string_vector);
- //connect
- virtual Error_manager communication_connect(std::string connect_string);
- //启动通信, run thread
- virtual Error_manager communication_run();
- //反初始化 通信 模块。
- virtual Error_manager communication_uninit();
-
-
- public://get or set member variable
- protected:
- //mp_receive_data_thread 接受线程执行函数,
- //receive_data_thread 内部线程负责接受消息
- void receive_data_thread();
- //mp_analysis_data_thread 解析线程执行函数,
- //analysis_data_thread 内部线程负责解析消息
- void analysis_data_thread();
- //遍历接受链表, 解析消息,
- Error_manager analysis_receive_list();
- //检查消息是否可以被解析, 需要子类重载
- virtual Error_manager check_msg(Binary_buf* p_buf);
- //处理消息, 需要子类重载
- virtual Error_manager excute_msg(Binary_buf* p_buf);
- //mp_send_data_thread 发送线程执行函数,
- //send_data_thread 内部线程负责发送消息
- void send_data_thread();
- //mp_encapsulate_data_thread 封装线程执行函数,
- //encapsulate_data_thread 内部线程负责封装消息
- void encapsulate_data_thread();
- //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
- virtual Error_manager encapsulate_send_data();
- //封装消息, 需要子类重载
- virtual Error_manager encapsulate_msg(Binary_buf* p_buf);
- protected://member variable
- //通用的网络编程接口, 默认使用总线模式, (网状结构)
- nnxx::socket m_socket { nnxx::SP, nnxx::BUS };
- std::mutex m_mutex; //m_socket的锁
- //通信状态
- Communication_statu m_communication_statu; //通信状态
- //接受模块,
- Thread_safe_list<Binary_buf*> m_receive_data_list; //接受的list容器
- std::thread* mp_receive_data_thread; //接受的线程指针
- Thread_condition m_receive_condition; //接受的条件变量
- std::thread* mp_analysis_data_thread; //解析的线程指针
- Thread_condition m_analysis_data_condition; //解析的条件变量
- //发送模块,
- Thread_safe_list<Binary_buf*> m_send_data_list; //发送的list容器
- std::thread* mp_send_data_thread; //发送的线程指针
- Thread_condition m_send_data_condition; //发送的条件变量
- std::thread* mp_encapsulate_data_thread; //封装的线程指针
- Thread_condition m_encapsulate_data_condition; //封装的条件变量
-
- private:
- };
- #endif //__COMMUNICATION_SOCKET_BASE__HH__
|