ZMQCPP 使用 socket_t 作为 class 变量

ZMQCPP using socket_t as class variable

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

我查看了 zmq.hpp 文件,但没有看到 socket_t class 的“默认”空构造函数,不需要立即使用上下文和套接字类型。

如有任何意见或指导,我们将不胜感激。

这是我当前的 class 设置

#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. 
};

Class实现:(我想做的)

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);
}

Class 实施(我目前所做的):

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);
}

对于遇到此问题的任何人;我能够通过使用 ZMQ C++ 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
};

这是 class 附带的基本功能。

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, '[=11=]', 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);

}

然后你可以像这样编写简单的 class 实现:

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