2

I have message of the form:

message OuterMessage {
  repeated InnerMessage msg = 1;
}

I would like to compose an instance of OuterMessage by appending already encoded instances of InnerMessage. Is there a legit way to do this with nanopbs?

  • if you are able to change the schema, maybe something like repeated bytes? However, you will loose the advantages of type check. If not, I would recommend that you check if the implementations of protocol buffers you work with have a way to create an object from bytes. This is called marshalling and unmarshalling. – Clément Jean Feb 24 '22 at 04:59
  • You could look into using the any field type: https://developers.google.com/protocol-buffers/docs/proto3#any – Bart Feb 25 '22 at 07:50

1 Answers1

0

There are a few ways:

  1. Encode outer message manually by calling pb_encode_tag_for_field() and pb_encode_string(). In protobuf, submessages are equivalent to strings in the wire format.

  2. Use callbacks for the repeated field. In the callback you can do the same as 1. above, but also get automatic encoding of any other fields in the outer message.

  3. Encode just the header (tag and length) to a separate buffer, send it and then send your already encoded message. This avoids having to copy the data around.

  4. Set option (nanopb).type_override = TYPE_BYTES on the submessage field, to generate it as if was a bytes field instead. Then you can set the encoded content inside that. But this approach is quite inefficient, as it results in copying the message data multiple times.

jpa
  • 10,351
  • 1
  • 28
  • 45