charis charis - 4 months ago 11
C++ Question

What is the correct way to receive an object as a string through ZeroMQ and then send it with zero-copy through another socket?

I am receiving a large, serialized, binary object using

ZeroMQ
:

std::unique_ptr<Message> input(CreateMessage());

if (Receive(input, "data") > 0) {
std::string serialStr(static_cast<char*>(input->GetData()), input->GetSize());
}


How can I send this object through another socket avoiding the unnecessary
memcpy
:

std::unique_ptr<Message> output(CreateMessage(serialStr.length()));
memcpy(output->GetData(), serialStr.c_str(), serialStr.length());

fChannels.at("data2").at(0).Send(output);


Also, is it a good idea to use an
std::string
to contain a binary object, or should I use a
void*
instead?

Answer

If all you are doing is forwarding then you can use the same message, you can use something like :

std::unique_ptr<Message> input(CreateMessage());
if (Receive(input, "data") > 0)
{
    fChannels.at("data2").at(0).Send(input);
}

Alternatively you can look at the 4 argument message constructor to use an existing buffer as the message payload rather than copying.