如何编码重复的Google.protobuf.

发布于 2025-01-31 20:18:35 字数 317 浏览 2 评论 0原文

我有一条消息,我想将其包装成任何重复的Google Proto Type :: 有没有办法编码重复任何消息类型?

我什至可以使用google.protobuf。

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}


/** Any Message **/
message RepeatedAny{
repeated google.protobuf.any sensors = 1;
}

我正在寻找一个示例,目前正在使用纳米布进行编码。

I have a message and I would like to package it into an any repeated google proto type::
Is there a way to encode an repeated any message type?

Can I even use repeated tag with google.protobuf.any?

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}


/** Any Message **/
message RepeatedAny{
repeated google.protobuf.any sensors = 1;
}

I am looking for an example, currently using nanopb to encode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆躺在深渊里 2025-02-07 20:18:35

当然,这只是一条常规消息。

https://github.com/nanapb/nanopb/nanopb/nanapb/nananopb/tree/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/master/tests/- Any_type 显示了如何编码单个消息,编码许多消息就像编码任何数组一样。您可以选择静态分配,动态分配或使用回调。或者,您可以一次将单个子字段编码到输出流中,因为串联编码的串联阵列以Protobuf格式进行。

Sure, it is just a regular message.

https://github.com/nanopb/nanopb/tree/master/tests/any_type shows how to encode a single Any message, encoding many is like encoding any array. You'll have a choice between allocating statically, allocating dynamically or using callbacks. Or you can just encode a single subfield at a time into output stream, because concatenating encoded concatenates arrays in protobuf format.

雨落□心尘 2025-02-07 20:18:35

我想我发现了我的问题,我无法使用(在google.protobuf.any上重复标签,因为我想在最后二进制中附加 repoyedan​​y 消息):

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}

message RepeatedAny{
repeated google.protobuf.any sensors = 1;
}

相反,我应该使用类似的东西:

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}

message SensorAny{
google.protobuf.any sensor = 1;
}

message RepeatedAny{
repeated SensorAny sensors = 1;
}

我不应该在google.protobuf..ony上使用重复标签。 ....(Sensorsn),一个或多个 Sensorany 消息。

以下是示例代码,如果某人将来找到了这个问题,则 nanopb

    /* First encode the SensorAny message by setting the value of the first field,
       The first field of this message is of type google.protobuf.any, so it should have
       1. sensor.type_url
       2. sensor.value
    */

    void* pBufAny = calloc(1, sBufSize);
    pb_ostream_t ostream_any = pb_ostream_from_buffer(pBufAny, sBufSize);
    SensorAny SensorAnyProto = SensorAny_init_default;
    SensorAnyProto.has_message = true;
    SensorAnyProto.sensor.type_url.arg = "type.googleapis.com/SensorAny.proto";
    SensorAnyProto.sensor.type_url.funcs.encode = Proto_encode_string;
    ProtoEncodeBufferInfo_t BufInfo = {
            .Buffer = pBuf, /* I have already filled and encoded Onesensor message previously as pBuf */
            .BufferSize = ostream.bytes_written,
    };
    SensorAnyProto.sensor.value.funcs.encode = Proto_encode_buffer;
    SensorAnyProto.sensor.value.arg = &BufInfo;
    pb_encode(&ostream_any, SensorAny_fields, &SensorAnyProto);
    free(pBuf);


    // Now Use the above encoded Any message buffer pBufAny to set the first repeated field in RepeatedAny

    RepeatedAny SensorAnyRepeated = RepeatedAny_init_default;
    ProtoEncodeBufferInfo_t AnyBufInfo = {
            .Buffer = pBufAny,
            .BufferSize = ostream_any.bytes_written,
    };

    AnyRepeated.sensors.arg=&AnyBufInfo;
    AnyRepeated.sensors.funcs.encode = Proto_encode_buffer;

    void* pBufAnyRepeated = calloc(1, sBufSize);
    pb_ostream_t ostream_repeated = pb_ostream_from_buffer(pBufAnyRepeated, sBufSize);
    !pb_encode(&ostream_repeated, RepeatedAny_fields, &AnyRepeated);
    free(pBufAny);

I think I found my issue, I cannot use(repeated tag on the google.protobuf.any, as I would like to append the RepeatedAny messages in the final binary):

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}

message RepeatedAny{
repeated google.protobuf.any sensors = 1;
}

Instead I should use something like this:

message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}

message SensorAny{
google.protobuf.any sensor = 1;
}

message RepeatedAny{
repeated SensorAny sensors = 1;
}

I should not use the repeated tag on the google.protobuf.any, I should be using it on a message that contains the google.protobuf.any instead, so that the protobinary can contain the format (sensors1), (sensors2).....(sensorsN), one or more SensorAny messages.

Below is the sample code, if someone finds this question in the future for nanopb:

    /* First encode the SensorAny message by setting the value of the first field,
       The first field of this message is of type google.protobuf.any, so it should have
       1. sensor.type_url
       2. sensor.value
    */

    void* pBufAny = calloc(1, sBufSize);
    pb_ostream_t ostream_any = pb_ostream_from_buffer(pBufAny, sBufSize);
    SensorAny SensorAnyProto = SensorAny_init_default;
    SensorAnyProto.has_message = true;
    SensorAnyProto.sensor.type_url.arg = "type.googleapis.com/SensorAny.proto";
    SensorAnyProto.sensor.type_url.funcs.encode = Proto_encode_string;
    ProtoEncodeBufferInfo_t BufInfo = {
            .Buffer = pBuf, /* I have already filled and encoded Onesensor message previously as pBuf */
            .BufferSize = ostream.bytes_written,
    };
    SensorAnyProto.sensor.value.funcs.encode = Proto_encode_buffer;
    SensorAnyProto.sensor.value.arg = &BufInfo;
    pb_encode(&ostream_any, SensorAny_fields, &SensorAnyProto);
    free(pBuf);


    // Now Use the above encoded Any message buffer pBufAny to set the first repeated field in RepeatedAny

    RepeatedAny SensorAnyRepeated = RepeatedAny_init_default;
    ProtoEncodeBufferInfo_t AnyBufInfo = {
            .Buffer = pBufAny,
            .BufferSize = ostream_any.bytes_written,
    };

    AnyRepeated.sensors.arg=&AnyBufInfo;
    AnyRepeated.sensors.funcs.encode = Proto_encode_buffer;

    void* pBufAnyRepeated = calloc(1, sBufSize);
    pb_ostream_t ostream_repeated = pb_ostream_from_buffer(pBufAnyRepeated, sBufSize);
    !pb_encode(&ostream_repeated, RepeatedAny_fields, &AnyRepeated);
    free(pBufAny);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文