gpt4 book ai didi

c - nanopb( Protocol Buffer 库)重复子消息编码

转载 作者:行者123 更新时间:2023-11-30 14:38:12 48 4
gpt4 key购买 nike

我们正在使用nanopb库作为我们的 Protocol Buffer 库。我们定义了以下消息:

simple.proto:

syntax = "proto2";

message repField {
required float x = 1;
required float y = 2;
required float z = 3;
}

message SimpleMessage {
required float lucky_number = 1;
repeated repField vector = 2;
}

simple.options

SimpleMessage.vector        max_count:300

因此我们知道 repField 的固定大小为 300,因此可以这样定义它。

生成的部分如下所示:

simple.pb.c:


const pb_field_t repField_fields[4] = {
PB_FIELD( 1, FLOAT , REQUIRED, STATIC , FIRST, repField, x, x, 0),
PB_FIELD( 2, FLOAT , REQUIRED, STATIC , OTHER, repField, y, x, 0),
PB_FIELD( 3, FLOAT , REQUIRED, STATIC , OTHER, repField, z, y, 0),
PB_LAST_FIELD
};

const pb_field_t SimpleMessage_fields[3] = {
PB_FIELD( 1, FLOAT , REQUIRED, STATIC , FIRST, SimpleMessage, lucky_number, lucky_number, 0),
PB_FIELD( 2, MESSAGE , REPEATED, STATIC , OTHER, SimpleMessage, vector, lucky_number, &repField_fields),
PB_LAST_FIELD
};

以及simple.pb.h的一部分:

/* Struct definitions */
typedef struct _repField {
float x;
float y;
float z;
/* @@protoc_insertion_point(struct:repField) */
} repField;

typedef struct _SimpleMessage {
float lucky_number;
pb_size_t vector_count;
repField vector[300];
/* @@protoc_insertion_point(struct:SimpleMessage) */
} SimpleMessage;

我们尝试通过以下方式对消息进行编码:

// Init message
SimpleMessage message = SimpleMessage_init_zero;
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

// Fill in message
[...]

// Encode message
status = pb_encode(&stream, SimpleMessage_fields, &message);

// stream.bytes_written is wrong!

但是stream.bytes_writing是错误的,这意味着它没有正确编码,尽管status=1

In the documentation对于pb_encode()它说:

[...] However, submessages must be serialized twice: first to calculate their size and then to actually write them to output. This causes some constraints for callback fields, which must return the same data on every call.

但是,我们不确定如何解释这句话 - 到底要遵循哪些步骤才能实现这一目标。

所以我们的问题是:

  • 使用 nanopb 对包含固定大小(重复)子消息的消息进行编码的正确方法是什么?图书馆?

谢谢!

最佳答案

您在这里没有使用回调字段,因此该引用对您来说并不重要。但如果是的话,这仅意味着在某些情况下您的回调将被多次调用。

你和on the forum是同一个人吗?您的堆栈溢出问题没有显示它,但论坛上的人也有类似的问题,似乎是由于未设置 vector_count 造成的。然后它将保持为 0 长度数组。所以尝试添加:

message.vector_count = 300;

今后,请等待几天后再在多个地方发布相同的问题。多次回答同一问题是浪费志愿者时间。

关于c - nanopb( Protocol Buffer 库)重复子消息编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56739667/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com