ZeroMQ + Protocol Buffers

前端 未结 4 805

ZeroMQ FAQ page suggest use of Google\'s protobuf as a way to serialise message content.

Has anyone see a good usage example?

I also need to get the answer to \"

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 17:36

    Here is a sample which send and receive messages through java and in C++:

    Serializing in java:

    Person person = Person.newBuilder().setName("chand")
        .setEmail("chand@test.com").setId(55555).build();
    socket.send(person.toByteArray(), 0);
    

    De-serialize in java:

    byte[] reply = socket.recv(0);
    Person person2 = Person.parseFrom(reply);
    

    Serializing in C++:

    Person p = Person();
    std::string str;
    p.SerializeToString(&str);
    int sz = str.length();
    zmq::message_t *query = new message_t(sz);
    memcpy(query->data (), str.c_str(), sz);
    socket->send (*query);
    

    De-serializign in C++

    zmq::message_t resultset(100);
    socket->recv (&resultset);
    
    Person p = Person();
    p.ParseFromArray(resultset.data(), resultset.size());
    printf("\n Server : %s", p.name().c_str());
    

提交回复
热议问题