gpt4 book ai didi

c++ - 通过Socket发送protocol buffer数据并判断类

转载 作者:行者123 更新时间:2023-11-30 02:45:20 24 4
gpt4 key购买 nike

我现在正在研究 Google 的 Protocol Buffers,有一个问题。如果我有多个 .proto 文件,因此有多个类,当数据通过套接字发送时是否有可能确定它是哪种类型?

例如我有两个类,我们称它们为 person.proto 和 address.proto。现在我通过网络发送其中一个。接收方如何判断是人还是地址?

我正在用 C++ 执行此操作。

我的尝试是在消息周围添加一个框架,其中包含长度和类型。但我想知道是否已经有某种类型的实现,所以我不重新实现现有的东西。

最佳答案

是的,这是可能的。 Protobuf 支持使用所谓的反射 message descriptors .

但是(如其他答案所述)您需要一个可靠的众所周知的根消息类型。与其引入自己的消息鉴别机制,恕我直言,最好使用 protobufs extension mechanism

这是我们在生产中的示例

package Common.ConfigurationCommands;

message UcpConfiguration
{
optional uint32 componentIndex = 1;
optional ConfigCmdStatus configCmdResponseStatus = 2;
optional string configErrorDescription = 3;

extensions 100 to max;
}

扩展看起来像

import "Common/ConfigurationCommands.proto";

message AmplifierConfiguration
{
extend Common.ConfigurationCommands.UcpConfiguration
{
optional AmplifierConfiguration amplifierConfiguration = 108;
}
optional uint32 preemphasis = 1;
}

import "Common/ConfigurationCommands.proto";

message FrontendConfiguration
{
extend Common.ConfigurationCommands.UcpConfiguration
{
optional FrontendConfiguration frontendConfiguration = 100;
}
optional bool frontendActive = 1;
optional uint32 refInputComponentIndex = 2;

extensions 100 to max;
}

可以查看this part of the documentation了解如何处理 C++ 代码中的扩展。

关于c++ - 通过Socket发送protocol buffer数据并判断类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24553181/

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