50 lines
1.2 KiB
C
50 lines
1.2 KiB
C
|
|
#pragma once
|
|||
|
|
#include <mutex>
|
|||
|
|
#include <string>
|
|||
|
|
#include "zmq.hpp"
|
|||
|
|
#include "IVrZeroMQClient.h"
|
|||
|
|
|
|||
|
|
#ifdef _DEBUG
|
|||
|
|
#pragma comment(lib, "libzmq-v142-mt-gd-4_3_5.lib")
|
|||
|
|
#else
|
|||
|
|
#pragma comment(lib, "libzmq-v142-mt-4_3_5.lib")
|
|||
|
|
#endif // _DEBUG
|
|||
|
|
|
|||
|
|
|
|||
|
|
#define TCP_TRANSFER_PORT 8800
|
|||
|
|
#define MAX_BUF_LEN 1024
|
|||
|
|
|
|||
|
|
class CVrZeroMQClient : public IVrZeroMQClient
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
CVrZeroMQClient();
|
|||
|
|
~CVrZeroMQClient();
|
|||
|
|
|
|||
|
|
// 初始化
|
|||
|
|
int Init(const char* sIP, int nPort) override;
|
|||
|
|
|
|||
|
|
// 发送数据
|
|||
|
|
int SendAndWaitBack(const char* pdata, const int nLen, char** ppRecvData, size_t& nRecvLen) override;
|
|||
|
|
|
|||
|
|
// 关闭通信
|
|||
|
|
int UnInit() override;
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
// 创建并配置一个新的 REQ socket(统一所有低延迟 + 超时选项)。调用方需持有 m_mutexObj。
|
|||
|
|
void createSocketLocked();
|
|||
|
|
// 关闭并重建 REQ socket,若已 Init 过则重新连接。
|
|||
|
|
// REQ 是严格 send-recv 交替状态机,recv 超时后必须重建才能恢复后续命令。调用方需持有 m_mutexObj。
|
|||
|
|
void rebuildSocketLocked();
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
zmq::context_t* m_pContext{nullptr};
|
|||
|
|
zmq::socket_t* m_pSocket{nullptr};
|
|||
|
|
|
|||
|
|
zmq::message_t m_recvmsg;
|
|||
|
|
std::mutex m_mutexObj;
|
|||
|
|
|
|||
|
|
std::string m_sAddr; // 已连接的 tcp://ip:port,供超时重建后重连使用
|
|||
|
|
|
|||
|
|
};
|
|||
|
|
|