- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的代码中,我想在 C# 中使用预定义的 protobuf 消息。我发现我能够编写并使用该方法来获取已创建的方法并生成 byte[]
:
ContainerMessage containerMessage = new ContainerMessage();
containerMessage.Type = CommandType.Connect;
containerMessage.Connect = new Connect();
containerMessage.Connect.ClientName = "TemporaryClientName";
byte[] stream = new byte[containerMessage.CalculateSize()];
using (Google.Protobuf.CodedOutputStream outstream = new Google.Protobuf.CodedOutputStream(stream))
{
containerMessage.WriteTo(outstream);
}
这按预期工作,我可以检查消息,并且值与 byte[]
中的值一样符合预期。但是,如果我尝试反序列化我刚刚创建的这个简单的 byte[]
:
using (Google.Protobuf.CodedInputStream instream = new Google.Protobuf.CodedInputStream(stream))
{
instream.ReadMessage(containerMessage);
}
它失败了:
An unhandled exception of type 'Google.Protobuf.InvalidProtocolBufferException' occurred in Google.Protobuf.dll
Additional information: Protocol message contained an invalid tag (zero).
这种从 byte[]
反序列化的方式对 protobuf 是否正确?
Protobuf 定义是:
message ContainerMessage {
CommandType type = 1;
bool commandSuccess = 2;
oneof message {
Connect connect = 3;
}
}
enum CommandType {
START = 0;
CONNECT = 2;
}
message Connect {
string ClientName = 1;
uint32 PushPullPort = 2;
}
然后用命令行生成CS文件:
protoc.exe -I=%CD% --csharp_out=..\GeneratedCsMessaging\ Connect.proto
最佳答案
CodedOutputStream
和CodedInputStream
主要供编译后的原型(prototype)类使用。 The API for CodedOutputStream声明这样并提到如果你想要手动编写代码调用这两个类中的任何一个,你需要在每个值之前使用它们的 WriteTag
方法。
但是,由于您希望使用 Google Protobuf 来序列化和解析任何 System.IO.Stream,因此可以按预期完成工作。 Protocol Buffer Basics for C# 的解析和序列化部分对此进行了很好的记录和描述。 . examples which can be found in Google Protobuf's Github对于快速掌握 Google Protobuf 的窍门非常有帮助。在那里您可以看到 MemoryStream
用于序列化对象,而 Parse.ParseFrom
方法可用于从序列化数据中解析对象。
正如您在问题的评论中提到的,using Google.Protobuf;
是能够使用 Google Protobuf 功能的重要部分。
编辑:您案例中的示例用法看起来像这样
byte[] serializedBytes;
ContainerMessage containerMessage = new ContainerMessage()
{
Connect = new Connect()
{
ClientName = "TemporaryClientName",
},
Type = CommandType.Connect,
};
using( MemoryStream stream = new MemoryStream())
{
containerMessage.WriteTo(stream);
serializedBytes = stream.ToArray();
}
ContainerMessage parsedCopy = ContainerMessage.Parser.ParseFrom(serializedBytes);
关于c# - 使用 protobuf CodedInputStream 读取 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48558451/
我正在尝试实现与 java 版本兼容的 protobuf 发送/接收,它首先包含一个 varint32 前缀。 我几乎让它工作了,但由于某些原因,一些消息变得不完整并且无法通过 assert()。 /
这是演示该问题的示例代码。 ByteArrayOutputStream bos = new ByteArrayOutputStream(); CodedOutputStream cos
所以我有一个用于 hololens 的 dotNet CoreCLR 应用程序,据我了解,CoreCLR 不支持标准套接字,推荐的途径是使用 StreamSocket。 据我所知,CodedInput
val people = Person() people.mergeFrom(new FileInputStream("path_of_file")) Person是一个在scala中使用protob
在下面的代码中,我想在 C# 中使用预定义的 protobuf 消息。我发现我能够编写并使用该方法来获取已创建的方法并生成 byte[]: ContainerMessage containerMes
我在数据节点的日志中看到了这一点。这可能是因为我正在将 500 万个文件复制到 HDFS 中: java.lang.IllegalStateException: com.google.protobuf
我正在使用我最新项目中的 Protocol Buffer (proto3)。但是我遇到以下问题。从 python 生成的编码消息,我正在尝试从 Scala 解码它(我正在使用 SPARK)。您可以看到
我正在通过 Windows 命名管道将( Protocol Buffer )序列化消息从 Java 应用程序发送到 .net C# 应用程序。 我已经为这两种环境编译了一个 .proto 文件。在 C
我是一名优秀的程序员,十分优秀!