ZMQCPP使用socket_t作为类变量



我正在创建一个类来处理ZMQCPP,我可以在几个不同的项目中使用它。我想让context_t和socket_t成为一个类变量,这样我就不必将它们作为参数传递给不同的函数(我目前所做的(。但我不断地犯错误,不确定这是否可能。

我浏览了zmq.hpp文件,没有看到一个";默认";sockett类的null构造函数,而不需要上下文和套接字类型。

任何意见或指导都将不胜感激。

这是我目前的课程设置

#include "zmq_addon.hpp"
#include <zmq.h>
#define DEFAULT_IP "tcp://127.0.0.1:"
#define DEFAULT_PORT 5555
class zmqClientTCP {
public:
zmqClientTCP();                     // Default Constructor
~zmqClientTCP();                    // Deconstructor
int setAddress(std::string);        // Changes the IP Address. 
int setPortNumber(int);             // Change port number explicitly
int setSocketType(int);             // Change the socket type
int connect();                      // Connects 
void disconnect();                  // Disconnects from active connection
void sendMessage(std::string*);     // Send Message over connection. 
private:
std::string ipAddress = "";         // Holds the IP Address
int portNum = -1;                   // Holds port number
int sockType = ZMQ_SUB;             // Set socket type for ZMQ connection
const zmq::context_t context;       // ZMQ Context for single thread
zmq::socket_t socket;               // ZMQ Socket 
bool isConnected = false;           // Bool for connection. 
};

类实现:(我想做的(

int zmqClientTCP::connect() {
socket(context, sockType);
socket.connect(ipAddress);
// MONITOR IMPLEMENTATION HERE
if(isConnected)return 1;
return 0
}
void sendMessage(std::string msg){
zmq::message_t zOut(msg);
socket.send(zOut, zmq::send_flags::none);
}
void zmqClientTCP::disconnect() {
socket.disconnect(ipAddress);
}

课堂实施(我目前所做的(:

int zmqClientTCP::connect() {
zmq::context_t context;
zmq::socket_t socket(context, sockType);
socket.connect(ipAddress);
std::string temp
/*   MONITOR IS HERE TO TRIGGER isConnected BOOL */
while(isConnected){
temp.clear();
std::getline(std::cin, temp);
sendMessage(socket, temp);
}
disconnect(socket);
return 1;
}
void sendMessage(zmq::socket_t &socket, std::string msg){
zmq::message_t zOut(msg);
socket.send(zOut, zmq::send_flags::none);
}
void zmqClientTCP::disconnect(zmq::socket_t &socket) {
socket.disconnect(ipAddress);
}

对于任何遇到此问题的人;我能够通过使用ZmqC++API功能来实现我的愿望。您将需要";zmq_addons.hpp";。

class zmqClientTCP {
public:
zmqClientTCP();                     // Default Constructor
~zmqClientTCP();                    // Deconstructor
void connect();                     // Connects to address
void disconnect();                  // Disconnects from active connection
void sendMessage(const std::string*);// Send string over connection. 
void sendMessage(const char*);      // Send c style string over connection. 
void recvMessage(std::string*);     // Recv message over connection. 
private:
void* context;                      // ZMQ Context for single thread
void* socket;                       // ZMQ Socket 
char buffer[256];                   // Buffer for message receiving
};

以下是该类附带的基本功能。

void zmqClientTCP::connect() {
context = zmq_ctx_new();
socket = zmq_socket(context, ZMQ_REQ);
zmq_connect(socket, ipAddress+PortNum);
}
void zmqClientTCP::sendMessage(const std::string* msg) {
zmq_send(socket, _strdup(msg->c_str()), strlen(msg->c_str()), 0);
}
void zmqClientTCP::sendMessage(const char* msg) {
zmq_send(socket, _strdup(msg), strlen(msg), 0);
}
void zmqClientTCP::recvMessage(std::string* msg) {
// Clear buffer 
memset(buffer, '', sizeof(buffer));
// Recv message from zmq into the buffer 
zmq_recv(socket, buffer, sizeof(buffer), 0);
// Convert buffer to string
std::string temp(buffer);
// Assign temp string to inbound string parameter. 
msg->assign(temp);
}
void zmqClientTCP::disconnect() {
zmq_close(socket);
zmq_term(context);
}

然后,您可以编写简单的类实现,如下所示:

zmqClientTCP zmqClient;
zmqClient.connect();
std::string msgOut = "Hello World";
zmqClient.sendMessage(&msgOut);
std::string msgIn = "";
zmqClient.recvMessage(&msgIn);
zmqClient.disconnect();

相关内容

  • 没有找到相关文章

最新更新