zeromq / azmq

C++ language binding library integrating ZeroMQ with Boost Asio
Boost Software License 1.0
318 stars 108 forks source link

Can not get option ZMQ_IDENTITY in ZMQ_STREAM #156

Open qtcdxxyuc opened 5 years ago

qtcdxxyuc commented 5 years ago

ZMQ

 int rv = 0;
auto context = zmq_init(1);
auto socket = zmq_socket(context,ZMQ_STREAM);
rv = zmq_connect(socket,ADDRESS);
assert(rv==0);
std::string id;
id.resize(5);
size_t sz = 5;
rv = zmq_getsockopt(socket,ZMQ_IDENTITY,id.data(),&sz);

can getZMQ_IDENTITY. e.g. k�Eg.


AZMQ

boost::asio::io_context ioc;
azmq::socket client(ioc,ZMQ_STREAM);
client.connect(ADDRESS);
azmq::socket::identity id;
client.get_option(id);
//throw Boost::system_error: Invalid argument

other

now ZMQ_IDENTITY renamed ZMQ_ROUTING_ID.

version

zmq:4.3.2

boost: 1.7.1

azmq:github-lasted: 19.11.1

qtcdxxyuc commented 5 years ago

I found the reason.

//main.cpp
azmq::socket::identity id;
//typename opt::binary<ZMQ_IDENTITY>
// it create with default constructor,so
// id.size = 0.

//socket_ops.hpp
//when it do this func:
template<typename Option>
static boost::system::error_code 
get_option(socket_type & socket,  Option & opt,  boost::system::error_code & ec) {
    BOOST_ASSERT_MSG(socket, "invalid socket");
    size_t size = opt.size();
    auto rc = zmq_getsockopt(socket.get(), opt.name(), opt.data(), &size);
    //if size = 0 ,rc = -1;
    if (rc < 0)
        ec = make_error_code();
    return ec;
}

solution

boost::asio::io_context ioc;
azmq::socket client(ioc,ZMQ_STREAM);
client.connect(ADDRESS);
string id_str('0',5);
azmq::socket::identity id(id_str);
client.get_option(id);

Is it possible to add a constructor for this option?

 binary(size_t sz) {
     data_.resize(sz);
     pv_ = data_.data();
    size_ = sz;
 }