给定一个协议缓冲区编码Stream
or byte[]
但不知道对象类型本身,我们如何打印消息的骨架?该用例用于调试基于 protobuf 的 IO,以进行根本原因分析。
如果有现有的工具可以从二进制文件中解析原始 Protocol Buffer 字节流——那就太好了!另一种方法是使用 ProtoBuf.NET 类ProtoReader()
继续前进,直到遇到错误,但 ProtoReader() 的用法尚不清楚。我从下面开始,但找不到关于如何使用的良好文档ProtoReader
类来真正做到这一点。该项目的源代码也不是很容易理解......所以希望得到一些提示/帮助
using (var fs = File.OpenRead(filePath))
{
using (var pr = new ProtoReader(fs, TypeModel.Create(), null))
{
// Use ProtoReader to march through the bytes
// Printing field number, type, size and payload values/bytes
}
}
首先,请注意,谷歌“protoc”命令行工具具有尝试反汇编没有架构信息的原始消息的选项。使用 protobuf-net,你可以执行如下操作 - 但我需要强调这一点没有模式,格式不明确:数据类型/格式比“线路类型”(实际编码格式)更多。这里我只是展示possible解释,但还有其他方法可以解析相同的数据。
static void WriteTree(ProtoReader reader)
{
while (reader.ReadFieldHeader() > 0)
{
Console.WriteLine(reader.FieldNumber);
Console.WriteLine(reader.WireType);
switch (reader.WireType)
{
case WireType.Variant:
// warning: this appear to be wrong if the
// value was written signed ("zigzag") - to
// read zigzag, add: pr.Hint(WireType.SignedVariant);
Console.WriteLine(reader.ReadInt64());
break;
case WireType.String:
// note: "string" here just means "some bytes"; could
// be UTF-8, could be a BLOB, could be a "packed array",
// or could be sub-object(s); showing UTF-8 for simplicity
Console.WriteLine(reader.ReadString());
break;
case WireType.Fixed32:
// could be an integer, but probably floating point
Console.WriteLine(reader.ReadSingle());
break;
case WireType.Fixed64:
// could be an integer, but probably floating point
Console.WriteLine(reader.ReadDouble());
break;
case WireType.StartGroup:
// one of 2 sub-object formats
var tok = ProtoReader.StartSubItem(reader);
WriteTree(reader);
ProtoReader.EndSubItem(tok, reader);
break;
default:
reader.SkipField();
break;
}
}
}
或者在 v3 中:https://stackoverflow.com/a/64621670/23354 https://stackoverflow.com/a/64621670/23354
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)