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

时间:2017-09-04 06:37:12

标签: c protocol-buffers nanopb

*编辑:已更新* 我的消息定义为:

message Repeat {
  int32 inum = 1;
  float fnum = 2;
}
message NotSimpleMessage {
  repeated Repeat repeat = 1;
}

我正在尝试使用回调选项编写解码器和编码器。我认为我的编码工作正常,但我的解码器失败了。 我的代码是: 定义:

 typedef struct{
        Repeat rep[MAX_NUMBERS];
        int32_t numbers_count;
    }Messer;

typedef struct{
    Mess mess[MAX_NUMBERS];
    int32_t numbers_count;
}MessList;

void mess_add_number(MessList * list, int32_t inum, float fnum)
{
    if (list->numbers_count < MAX_NUMBERS)
    {
        (list->mess[list->numbers_count]).inumber = inum;
        (list->mess[list->numbers_count]).fnumber = fnum;
        list->numbers_count++;
    }
}

    void messer_add_number(Messer * list, int32_t inum, float fnum)
    {
        if (list->numbers_count < MAX_NUMBERS)
        {
            (list->rep[list->numbers_count]).inum = inum;
            (list->rep[list->numbers_count]).fnum = fnum;
            (list->rep[list->numbers_count]).has_inum = true;
            (list->rep[list->numbers_count]).has_fnum = true;
            list->numbers_count++;
        }
    }

编码器/解码器功能:

bool NestedMessage_encode_numbers(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg)
{
    Messer * source = (Messer*)(*arg);
        int i;
        // encode all numbers
        for ( i = 0; i < source->numbers_count; i++)
        {
            if (!pb_encode_tag_for_field(ostream, field))
            {
                const char * error = PB_GET_ERROR(ostream);
                printf("SimpleMessage_encode_numbers error: %s\n", error);
                return false;
            }

            if (!pb_encode_submessage(ostream, Repeat_fields, &(source->rep[i])))
            {
                const char * error = PB_GET_ERROR(ostream);
                printf("SimpleMessage_encode_numbers error: %s\n", error);
                return false;
            }
        }

        return true;
c}
bool NestedMessage_decode_numbers(pb_istream_t *istream, const pb_field_t *field, void **arg)
{
    MessList * dest = (MessList*)(*arg);
    Repeat rep;
    // decode single number
    Mess decmess;
    printf("decoding...\n");
    if (!pb_decode(istream, Repeat_fields ,&rep))
    {
        const char * error = PB_GET_ERROR(istream);
        printf("decode error: %s\n", error);
        return false;
    }

    // add to destination list
    mess_add_number(dest, rep.inum, rep.fnum);
    return true;
}

主要是:

int main(void) {


    uint8_t buffer[128];
    size_t total_bytes_encoded = 0;

    // encoding
    // prepare the actual "variable" array
    Messer actualData = { 0 };
    messer_add_number(&actualData, 123, 1.2);
    messer_add_number(&actualData, 456, 2.3);
    messer_add_number(&actualData, 789, 3.4);
    printf("Size: %d\n",actualData.numbers_count);
    printf("data to be encoded: %d - %f, %d-%f, %d-%f\n",actualData.rep[0].inum,actualData.rep[0].fnum,
            actualData.rep[1].inum, actualData.rep[1].fnum,
            actualData.rep[2].inum,actualData.rep[2].fnum);
    // prepare the nanopb ENCODING callback
    NotSimpleMessage msg = NotSimpleMessage_init_zero;
    msg.repeat.arg = &actualData;
    msg.repeat.funcs.encode = NestedMessage_encode_numbers;

    // call nanopb
    pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    if (!pb_encode(&ostream, NotSimpleMessage_fields, &msg))
    {
        const char * error = PB_GET_ERROR(&ostream);
        printf("pb_encode error: %s\n", error);
        return EXIT_FAILURE;
    }

    total_bytes_encoded = ostream.bytes_written;
    printf("Encoded size: %d\n", total_bytes_encoded);


    // decoding

    // empty array for decoding
    Messer decodedData = { 0 };

    // prepare the nanopb DECODING callback
    NotSimpleMessage msgdec = NotSimpleMessage_init_zero;
    msgdec.repeat.arg = &decodedData;
    msgdec.repeat.funcs.decode = NestedMessage_decode_numbers;

    // call nanopb
    pb_istream_t istream = pb_istream_from_buffer(buffer, total_bytes_encoded);
    if (!pb_decode(&istream, NotSimpleMessage_fields, &msgdec))
    {
        const char * error = PB_GET_ERROR(&istream);
        printf("pb_decode error: %s", error);
        return EXIT_FAILURE;
    }

    printf("Bytes decoded: %d\n", total_bytes_encoded - istream.bytes_left);
    printf("decoded data: %d - %f, %d-%f, %d-%f\n",decodedData.rep[0].inum,decodedData.rep[0].fnum,
            decodedData.rep[1].inum, decodedData.rep[1].fnum,
            decodedData.rep[2].inum,decodedData.rep[2].fnum);
}

我得到的输出是:

  

大小:3个要编码的数据:123 - 1.200000,456-2.300000,789-3.400000   编码大小:29字节解码:1个解码数据:0 - 0.000000,   0-0.000000,0-0.000000

打印编码缓冲区:

  

0a07087b15ffffff9affffff99ffffff993f0a0808ffffffc80315333313400a0808ffffff950615ffffff9affffff995940

我在解码器中尝试了一些不同的结构,但它不起作用。 我确实知道这是一件令人愚蠢的小事,但我对它一无所知。

1 个答案:

答案 0 :(得分:1)

啊,回调中的编码/解码子消息中存在一些小问题。

解码时,pb_decode()工作正常,因为子消息标记和长度已经被nanopb解析。但是,在编码时,需要单独计算和编码消息的长度。因此,您需要在pb_encode()使用pb_encode_submessage()而不是 if (!pb_encode_submessage(ostream, Repeat_fields, &(source->rep[i]))) { const char * error = PB_GET_ERROR(ostream); printf("SimpleMessage_encode_numbers error: %s\n", error); return false; }

0a07087b15ffffff9affffff99ffffff993f0a0808ffffffc80315333313400a0808ffffff950615ffffff9affffff995940

(供参考,这里是relevant part of an example。)

-

关于您的更新,此十六进制文本:

0a07087b159a99993f0a0808c80315333313400a08089506159a995940

有点损坏,因为你的打印功能似乎打印“ffffff9a”而不仅仅是“9a”。可能是签名到无签名的演员表现出乎意料。但这可以通过简单的搜索和修复来解决。替换,给出:

echo 0a07087b159a99993f0a0808c80315333313400a08089506159a995940 | xxd -r -p | protoc --decode=NotSimpleMessage test.proto

用protoc解码:

repeat {
  inum: 123
  fnum: 1.2
}
repeat {
  inum: 456
  fnum: 2.3
}
repeat {
  inum: 789
  fnum: 3.4
}

给出:

Imports Excel = Microsoft.Office.Interop.Excel
private sub OpenExcel()
Dim objApp As Object
        Me.Hide()
        'This path is independent of where the program is installed as it refers to program itself
        Dim ResourcePath As String = My.Application.Info.DirectoryPath &
            System.IO.Path.DirectorySeparatorChar & "Templates\Rate_Reach_ATT finished.xlsm"
        objApp = CreateObject("Excel.Application")
        objApp.WorkBooks.Open(ResourcePath)
        objApp.visible = True
        objApp.Run("MacroName", Var1, Var2)
        Me.Close()
End Sub

看来您的编码现在正常运行。

不确定是什么原因导致解码结束这么早(只读取1个字节)而没有错误消息。也许尝试使用调试器逐步完成它,看看发生了什么。一个原因可能是缓冲区中的数据在解码调用之前会以某种方式被破坏,但无法理解为什么会发生这种情况。