首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对重复的google.protobuf.any进行编码?

如何对重复的google.protobuf.any进行编码?
EN

Stack Overflow用户
提问于 2022-05-23 00:30:00
回答 2查看 420关注 0票数 1

我有一条消息,我想把它打包成一个重复的谷歌proto类型::有方法编码一个重复的任何消息类型吗?

我甚至可以在google.protobuf.any中使用重复标记吗?

代码语言:javascript
复制
message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}


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

我正在寻找一个例子,目前使用纳米粒子编码。

EN

回答 2

Stack Overflow用户

发布于 2022-05-23 05:53:22

当然,这只是一个常规的信息。

https://github.com/nanopb/nanopb/tree/master/tests/any_type展示了如何对单个任意消息进行编码,编码很多就像编码任何数组一样。您可以选择静态分配、动态分配还是使用回调。或者,您可以一次将一个子字段编码成输出流,因为将编码后的数组以protobuf格式连接起来。

票数 0
EN

Stack Overflow用户

发布于 2022-05-23 13:28:12

我想我找到了我的问题,我不能使用(在google.protobuf.any上重复标记,因为我想在最后的二进制文件中附加RepeatedAny消息):

代码语言:javascript
复制
message Onesensor{
    string name=1
    string type=2
    int32_t reading=3
}

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

相反,我应该使用这样的东西:

代码语言:javascript
复制
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.any上使用重复的标记,我应该在包含google.protobuf.any的消息上使用它,以便原型可以包含格式(sensors1)、(Sensors2).(SensorsN)、一个或多个SensorAny消息。

下面是示例代码,如果有人在未来发现纳米粒子的这个问题

代码语言:javascript
复制
    /* 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72342189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档