繁体   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?

我正在使用ZeroMQ接收大型的序列化二进制对象:

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

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

我如何通过另一个套接字发送此对象,以避免不必要的memcpy

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

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

另外,使用std::string包含二进制对象是个好主意,还是应该使用void*代替?

如果您所做的只是转发,则可以使用同一条消息,也可以使用类似以下内容的消息:

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

或者,您可以查看4参数消息构造函数,以将现有缓冲区用作消息有效负载而不是复制。

暂无
暂无

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

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