Nanopb 正确编码和解码子消息中的重复构造字段

标签 nanopb

对重复构造字段进行编码/解码的正确方法是什么? Nanopb 子消息?生成的输出表明解码操作确实 未检测到任何重复的构造字段。同样有趣的是, 编码回调被调用两次,并且也有问题。我错过了什么?

作为实验,如果将此示例修改为 for 则解码成功 编码和解码不是从 TopMessage 开始,而是从 SubMessage1 开始。还, 在这种情况下,编码回调按预期仅调用一次。

以下是原始定义;有问题的字段是 subMessage11 在 SubMessage1 下。

syntax = "proto2";
import 'nanopb.proto';

message SubMessage11
{
  required uint64 int64Val = 1;
};

message SubMessage1
{
  repeated SubMessage11 subMessage11 = 1;
};

message SubMessage2
{
  required uint32 intVal = 1;
};

message TopMessage
{
  oneof choice
  {
    SubMessage1 subMessage1 = 1;
    SubMessage2 subMessage2 = 2;
  }
};

使用proto定义的C++代码是:

#include "pb_encode.h"
#include "pb_decode.h"

#include "t.pb.h"
#include "debug.hpp"

bool subMessage11EncodeCb(pb_ostream_t *stream, const pb_field_t *field,
    void * const *arg)
{
  dprintf("called, field->tag=%d field->type=%d", field->tag, field->type);

  for(int i=0; i<4; i++)
  {
    if(pb_encode_tag_for_field(stream, field) == false)
    {
      dprintf("encode failed");
      return false;
    }

    SubMessage11 subMessage11 = SubMessage11_init_zero;
    subMessage11.int64Val = 0xaabbccddeef0 + i;

    if(pb_encode_submessage(stream, SubMessage11_fields, &subMessage11) == false)
    {
      dprintf("encode failed");
      return false;
    }
  }

  return true;
}

bool subMessage11DecodeCb(pb_istream_t *stream, const pb_field_t *field,
    void **arg)
{
  dprintf("called");

  SubMessage11 subMessage11 = SubMessage11_init_zero;
  if(pb_decode(stream, SubMessage11_fields, &subMessage11) == false)
  {
    dprintf("error decoding: %s", stream->errmsg);
    return false;
  }

  dprintf("int64Val=%lx", subMessage11.int64Val);

  return true;
}

bool encodeMsg(uint8_t buf[], size_t& bufsz)
{
  dprintf("begin encoding");
  pb_ostream_t stream = pb_ostream_from_buffer(buf, bufsz);

  TopMessage topMessage = TopMessage_init_zero;

  topMessage.which_choice = TopMessage_subMessage1_tag;
  SubMessage1& subMessage1 = topMessage.choice.subMessage1;
  subMessage1.subMessage11.funcs.encode = subMessage11EncodeCb;

  bool status = pb_encode(&stream, TopMessage_fields, &topMessage);
  if(status != true)
  {
    dprintf("error encoding: %s", stream.errmsg);
    bufsz = 0;
    return status;
  }

  bufsz = stream.bytes_written;

  dprintf("done encoding");
  return status;
}

bool decodeMsg(uint8_t buf[], size_t bufsz)
{
  dprintf("begin decoding");
  pb_istream_t stream = pb_istream_from_buffer(buf, bufsz);

  TopMessage topMessage;
  topMessage.which_choice = TopMessage_subMessage1_tag;
  SubMessage1& subMessage1 = topMessage.choice.subMessage1;
  int val;
  subMessage1.subMessage11.arg = (void *)&val;
  subMessage1.subMessage11.funcs.decode = &subMessage11DecodeCb;

  bool status = pb_decode(&stream, TopMessage_fields, &topMessage);
  if(status != true)
  {
    dprintf("error decoding: %s", stream.errmsg);
    return false;
  }

  dprintf("decoded fields: ");

  dprintf("done decoding");
  return status;
}

int main(int ac, char *av[])
{
  uint8_t encBuf[1024];
  size_t encSz = sizeof(encBuf);

  if(encodeMsg(encBuf, encSz) != true)
  {
    dprintf("Encode failed");
    return 1;
  }

  hexdump(encBuf, encSz);

  if(decodeMsg(encBuf, encSz) != true)
  {
    dprintf("Decode failed");
    return 1;
  }
}

产生的输出是:

c.cpp:55:encodeMsg: begin encoding
c.cpp:12:subMessage11EncodeCb: called, field->tag=1 field->type=103
c.cpp:12:subMessage11EncodeCb: called, field->tag=1 field->type=103
c.cpp:74:encodeMsg: done encoding

[0000]   0A 28 0A 08 08 F0 DD F7   E6 BC D7 2A 0A 08 08 F1   ........ ........
[0010]   DD F7 E6 BC D7 2A 0A 08   08 F2 DD F7 E6 BC D7 2A   ........ ........
[0020]   0A 08 08 F3 DD F7 E6 BC   D7 2A                     ........ ..
c.cpp:80:decodeMsg: begin decoding
c.cpp:97:decodeMsg: decoded fields:
c.cpp:99:decodeMsg: done decoding

最佳答案

对子消息多次调用的回调进行编码为 expected behaviour 。第一个调用用于计算大小,在写出子消息正文之前必须知道该大小:

If the callback is used in a submessage, it will be called multiple times during a single call to pb_encode. In this case, it must produce the same amount of data every time. If the callback is directly in the main message, it is called only once.

至于为什么你的解码不起作用,目前回调are not supported inside oneof constructs :

If a oneof contains a sub-message that has a string field, the encode callback is called twice, and the decode callback is never called.

关于Nanopb 正确编码和解码子消息中的重复构造字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854434/

相关文章:

python - 反序列化带有 header 和重复字段的流式 Protocol Buffer 消息

android - Protobuf可以和NanoPB通信吗

c - 当它是 pb_callback_t 类型时如何编码字符串

c - 使用 ProtoBuf 将数据流式传输到带有 header 的日志文件

c - 在 c 中使用 nanopb 在 protobuf 中使用嵌套和重复字段的回调

c - 如何使用nanopb编译.proto + .options文件

c - 输入字符串由空终止字符组成