123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //
- // RUDataTransMgr.h
- // base_world_ios
- //
- // Created by ZhengSong on 2023/2/28.
- //
- #ifndef RUDataTransMgr_h
- #define RUDataTransMgr_h
- #include "RUDefine.h"
- #include "RedZMQ.h"
- NS_RU_BEGIN
- /**
- * Pub 和 Sub 是一对 (pub是发送, sub是接收)
- * Req 和 Rep 是一对 (req是发送, rep是接收)
- *
- * App_A 中可以放Pub 和 Rep
- * App_B 中可以放Sub 和 Req
- *
- * Req 发送消息会阻塞线程
- */
- class DataTransDelegate {
- public:
- ///处理从订阅者发送的消息
- virtual void handleReceiveMessageFromPub(const std::string& command, const std::string& data) {}
- ///处理请求-应答发送的消息
- virtual std::string handleReceiveMsgFromReq(const std::string& command, const std::string& data) {return "";}
- };
- class DataTransMgr {
- public:
- static DataTransMgr* getInstance();
-
- /// socket通信(Sub-Req),发送信息会阻塞
- /// @param sender 发送地址,例如"tcp://localhost:5556"
- /// @param receiver 接收地址,例如tcp://localhost:5555"
- void connectSync(const std::string& sender, const std::string& receiver);
-
- /// socket通信(Pub-Rep),发送信息不会阻塞
- /// @param sender 发送地址,例如"tcp://*:5555"
- /// @param receiver 接收地址,例如"tcp://*:5556"
- void connectAsync(const std::string& sender, const std::string& receiver);
-
- /// 向订阅者发送消息
- /// @param command 消息类型
- /// @param data 消息内容
- void sendMsgToSubscriber(const std::string& command, const std::string& data);
-
- /// 请求消息并返回应答消息(阻塞直到返回应答消息)
- /// @param command 消息类型
- /// @param data 消息内容
- std::string requestMsg(const std::string& command, const std::string& data);
-
- ///设置代理
- void setDataTransDelegate(DataTransDelegate* delegate);
- private:
- DataTransMgr();
- ~DataTransMgr();
- //处理远端发送的消息
- void _handlePubMessage(const std::string& msg);
- std::string _handleReqMessage(const std::string& msg);
- private:
- red_zmq::Req* _zmqReq = nullptr;
- red_zmq::Sub* _zmqSub = nullptr;
- red_zmq::Pub* _zmqPub = nullptr;
- red_zmq::Rep* _zmqRep = nullptr;
- DataTransDelegate* _dataTransDelegate = nullptr;
- };
- NS_RU_END
- #endif /* RUDataTransMgr_h */
|