How use Protobuf + ZMQ in Rust


#1

I’m trying to retrieve data from a server written in C++ which I used Protobuf + ZMQ, the program written in Rust receive messages from server, but I’m having trouble to interpret the message, seriously, I don’t have any idea how to do it. It’s used the Protobuf (githut: stepancheg/rust-protobuf) and the ZMQ (github: erickt/rust-zmq).

Here is the code.

In src/main.rs there is the code. In src/state.proto there is the proto. In image/working.png there is a image with the working, on the left has the package sent and on the right has the package received.
May you help me? I’m not asking you to implement, only how may I do this.


#2

I recently started working on a Riak Client that uses Riak’s protobuf API, and I had similar trouble finding information on how to use the protobuf crate.

For me the answer was in the Protobuf Tests.

This function in particular should prove helpful:

pub fn test_serialize_deserialize_no_hex<M : Message + MessageStatic>(msg: &M) {
    let serialized_bytes = msg.write_to_bytes().unwrap();
    let parsed = parse_from_bytes::<M>(&serialized_bytes).unwrap();
    assert!(*msg == parsed);
}

At it’s simplest:

  • write_to_bytes() will allow you to take a protobuf Message and turn it into bytes that can be sent over the wire (make sure you use protobuf::Message;)
  • parse_from_bytes::<M>(&bytes) will take the bytes you pulled over the wire and render you an M. (use protobuf::parse_from_bytes;)

Hope this is helpful!


#3

Thank you very much. It Works :slight_smile: