1

I try to write my first *.proto file. This is my try:

syntax = "proto3";
package Message;

message Message {
    string name = 1;
    string serial = 2;
    int32 command = 3;

    enum Status {
        GOOD = 0;
        BAD = 1;
    }

    Status status = 4;
    int32 length = 5;

    // end of header
    // start of payload

    int32 data = 6;

    string address = 7;
}

The header has the field length. This value defines the length of the data field in the payload. And that is my problem: The data field is dynamic, I can't know the size right now. I could split the header and the payload in 2 separate *.proto files. But then still I don't know how to dynamically set the length of one of the fields.

Thanks in advance for the help!

selmaohneh
  • 553
  • 2
  • 17
  • Protobuf already deals with details like payload lengths - you shouldn't need to. Your payload could be a string, a structured sub-messages, or just `bytes`: protobuf deals with the details. Am I misunderstanding the question? – Marc Gravell Jul 09 '18 at 18:52
  • If I would design the protocol myself, I would not use the length field, correct. But I try to implement an existing protocol which has this length field. So can Protobuffer know the size of the data without using the length field? – selmaohneh Jul 09 '18 at 19:18
  • Protobuf isn't intended for mapping to existing protocols. It is "opinionated", meaning: it has its own protocol and that's *all it does*. If you are expecting to fit protobuf onto something else: you may be frustrated – Marc Gravell Jul 09 '18 at 19:51
  • Not what I wanted to hear. This means I have to write the parsing myself. This is such a common task... I can't believe that there are no libraries/frameworks to make life easier. Protocol buffer was my hope. Thanks for the info! – selmaohneh Jul 10 '18 at 03:38

1 Answers1

0

Protocol buffer does not allow to check dependencies between fields. You have to check if the length matches the length of data yourself.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958