Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send / receive binary data serialized with Protocol Buffers using ZMQ

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);
like image 867
sorush-r Avatar asked Oct 21 '25 14:10

sorush-r


1 Answers

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;
}
like image 60
Remi THOMAS Avatar answered Oct 23 '25 03:10

Remi THOMAS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!