// // RedZMQ.h // rebolt // // Created by zhuge on 2022/11/7. // Red ZMQ的一层封装,屏蔽zmq的使用细节,继承相对应的类,然后设置对应的endpoint即可 #ifndef RedZMQ_h #define RedZMQ_h #include #include namespace red_zmq { /** 广播消息 例: class Editor : Pub { Editor(): Pub("tcp://:5555") {} } Editor()->pubMsg("{}"); */ class Pub { public: Pub(std::string endPoint); /** 广播信息,不会阻塞,还没有连接的sub收不到 return 发送状态 */ int pubMsg(const char* sendMsg); private: std::string _endPoint; void *_context; void *_socket; void _bind(); }; /// 注: onMsg为接收消息的子线程 class Sub { public: Sub(const std::string& endPoint, const std::function& handleMsgCb); private: std::string _endPoint; std::function _handleMsgCb; void *_context; void *_socket; void _connect(); void _threadReceiveMsg(); }; class Req { public: Req(std::string endPoint); /** 发送信息,如果无rep,会阻塞!!! */ std::string sendMsg(const char* sendMsg); private: std::string _endPoint; void *_context; void *_socket; void _connect(); }; /// 注: onMsg为接收消息的子线程 class Rep { public: Rep(const std::string& endPoint, const std::function& handleMsgCb); private: std::string _endPoint; std::function _handleMsgCb; void *_context; void *_socket; void _bind(); void _threadReceiveMsg(); }; }; #endif /* RedZMQ_h */