简体   繁体   English

通过ZeroMQ接收对象作为字符串然后通过另一个套接字以零拷贝发送的正确方法是什么?

[英]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 : 我正在使用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 : 我如何通过另一个套接字发送此对象,以避免不必要的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? 另外,使用std::string包含二进制对象是个好主意,还是应该使用void*代替?

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. 或者,您可以查看4参数消息构造函数,以将现有缓冲区用作消息有效负载而不是复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM