我需要在ZMQ套接字上发送一个对象(用GPB序列化)。目前代码有一个额外的副本。如何直接将序列化数组写入message_t
数据?
ABT_CommunicationProtocol introPacket;
// Fill the packet
message_t introMessage;
size_t dataLenght = introPacket.ByteSize();
char* temp = new char[dataLenght];
introPacket.SerializeToArray(temp, dataLenght); // write data to temp
memcpy(introMessage.data(), temp, dataLenght); // copy data to message
this->serverRquest.send(introMessage);
答案 0 :(得分:3)
不要使用zmq_send而是使用zmq_sendmsg
int cgi_msg_cnx_pool::PbToZmq(::google::protobuf::Message *src, zmq_msg_t *dest)
{
int size = src->ByteSize();
int rc = zmq_msg_init_size(dest, size);
if (rc==0)
{
try
{
rc = src->SerializeToArray(zmq_msg_data(dest), size)?0:-1;
}
catch (google::protobuf::FatalException fe)
{
std::cout << "PbToZmq " << fe.message() << std::endl;
}
}
return rc;
}
int cgi_msg_cnx_pool::ZmqToPb(zmq_msg_t *src, ::google::protobuf::Message *dest)
{
int rc = 0;
try
{
rc = dest->ParseFromArray(zmq_msg_data(src), zmq_msg_size(src))?0:-1;
}
catch (google::protobuf::FatalException fe)
{
std::cout << "ZmqToPb " << fe.message() << std::endl;
}
return rc;
}