1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //
- // RedZMQ.h
- // rebolt
- //
- // Created by zhuge on 2022/11/7.
- // Red ZMQ的一层封装,屏蔽zmq的使用细节,继承相对应的类,然后设置对应的endpoint即可
- #ifndef RedZMQ_h
- #define RedZMQ_h
- #include <string>
- #include <functional>
- 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<void(const std::string&)>& handleMsgCb);
- private:
- std::string _endPoint;
- std::function<void(const std::string&)> _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<std::string(const std::string&)>& handleMsgCb);
- private:
- std::string _endPoint;
- std::function<std::string(const std::string&)> _handleMsgCb;
- void *_context;
- void *_socket;
-
- void _bind();
- void _threadReceiveMsg();
- };
- };
- #endif /* RedZMQ_h */
|