3

我需要在 ZMQ 套接字上发送一个对象(用 GPB 序列化)。目前该代码有一个额外的副本。如何直接将序列化数组写入message_ts数据?

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);
4

1 回答 1

4

不要使用 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;
}
于 2013-05-24T13:15:08.563 回答