通过 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 参数消息构造函数以使用现有缓冲区作为消息负载而不是复制。
我正在使用 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 参数消息构造函数以使用现有缓冲区作为消息负载而不是复制。