0

Well, I have message structure:

message SomeMessage
{

    enum MessageType
    {
        eRegisterQuery             = 1;
        eRegisterResponse          = 2;
        eConnectionControlQuery    = 3;
        eConnectionControlResponce = 4;
    }

    optional uint64                beginFlag        = 1;
    required uint32                type             = 2;

    oneof messages
    {
        optional RegisterQuery         registerQuery    = 3;
        optional RegisterResponce      registerResponce = 4;
        optional Control               control          = 5;
    }

    repeated uint32                buffer           = 6;
    optional uint64                endFlag          = 7;
}

And after serialization I must always get a packet of size 1010 bytes. Of course, I have more then 4 message types and their sizes are different. So I have to insert "repeated buffer" to get final size every time I have to serialize a message. How can I avoid it?

c4pQ
  • 884
  • 8
  • 28

1 Answers1

1

You should prefix the protobuf with its size, and then add zeros to the end of the packet to get it to 1010 bytes. On the receiving end, you first read the size and then you parse the message using e.g. ParseFromArray() passing that size rather than the full packet size.

You can simply encode the size as two bytes, or you can use the semi-standard protobuf "delimited format", which is usually meant for streaming multiple messages but also solves your problem. You can find it implemented in C++ here:

https://stackoverflow.com/a/22927149/2686899

It would also be possible to trick protobufs into ignoring bytes by inserting fake fields up to the size of the message, but this would be a rather convoluted and less-efficient solution.

Community
  • 1
  • 1
Kenton Varda
  • 41,353
  • 8
  • 121
  • 105