4

I need to send an object (serialized with GPB) on a ZMQ socket. Currently the code have an extra copy. How do I directly write serialized array into message_ts data?

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

1 Answer 1

5

Don't use zmq_send but 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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

With some modifications, I updated your code to meet C++ port of ZMQ, and it works well. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.