zmq,在类中重新创建套接字

问题描述 投票:0回答:1

我正在尝试在类中创建一个 zmq 请求客户端。我可以通过类初始化列表初始化套接字。 这是我的班级代码:

class request_client_t 
{

    private:
        zmq::socket_t sock;
        zmq::context_t& context;

    public:
        std::string endp;
        int retries = 3;
        int timeout_ms = 1000;

        request_client_t(zmq::context_t& ctx, std::string target, std::string port) : context(ctx),  sock(ctx,zmq::socket_type::req) {
            endp = "tcp://" + target + ":" + port;
            sock.connect(endp.c_str());
        };

        ~request_client_t(){
            sock.close();
        };

        /**
         * send a request to a reply socket
         * if the reply fails (timeout) we recreate the request socket and retry
         * if we have to retry to many times we abandon with error
        */
        int send(std::string command, std::string msg, std::vector<std::string>& reply){
            zmq::message_t zcommand(command.c_str(),command.size());
            zmq::message_t zmsg(msg.c_str(),msg.size());
            int retry = retries;
            zmq_pollitem_t items [] = { { sock, 0, ZMQ_POLLIN, 0 } };
            while(retry){
                //send single or dual frame message if the payload is not empty
                if(msg != ""){
                    auto res = sock.send(zcommand, zmq::send_flags::sndmore);
                    res = sock.send(zmsg, zmq::send_flags::none);     
                } else {
                    auto res = sock.send(zcommand, zmq::send_flags::none);
                }
                //pol the socket for the reply, else timeout and retry
                int rc = zmq_poll (items, 1, timeout_ms);
                if (rc == -1) break; // Interrupted
                if (items [0].revents & ZMQ_POLLIN) {
                    zmq::message_t zreply;
                    zmq::recv_result_t res = sock.recv(zreply,zmq::recv_flags::none);
                    reply.push_back(zreply.to_string());
                    while (zreply.more()){
                        res = sock.recv(zreply,zmq::recv_flags::none);    
                        reply.push_back(zreply.to_string());
                    }
                    return EXIT_SUCCESS;
                //timeout and too many retries
                } else if (--retry == 0) {
                    return EXIT_FAILURE;
                //timeout -> retry, but recreate socket to solve state machine conflicts
                } else {
                    
                    --????

                }                    
            }
            return EXIT_FAILURE;
        };

};

我想实现懒惰的海盗客户端,所以我需要销毁并重新创建套接字。 如何使用 zmq::socket_t 的新实例覆盖我的 sock 变量?

sock.close();
zmq::socket_t sock(context,zmq::socket_type::req);

没成功

sock.close();
zmq::socket_t * sockpntr = &sock;
sockpntr = new zmq::socket_t(context,zmq::socket_type::req);

没成功

class zeromq
1个回答
0
投票

找到了..可以通过移动分配来实现

                sock.close();
                zmq::socket_t newsock(context,zmq::socket_type::req);
                sock = std::move(newsock);
                sock.connect(endp.c_str());
© www.soinside.com 2019 - 2024. All rights reserved.