简体   繁体   English

使用带有protobuf-csharp-port的文件记录和重播人类可读的protobuf消息

[英]Record and replay human-readable protobuf messages using a file with protobuf-csharp-port

I'm using protobuf-csharp-port and I need the ability to record some protobuf messages for replay later. 我正在使用protobuf-csharp-port,并且需要能够记录一些protobuf消息以供以后重播。 XML would be ideal for me, but I'm flexible as long as a human could go into the file, make changes, then replay the messages from the file. XML对我来说是理想的选择,但是我很灵活,只要有人可以进入文件,进行更改,然后重播文件中的消息即可。

Using this C# code: 使用此C#代码:

        MyRequest req =
            MyRequest.CreateBuilder().SetStr("Lafayette").Build();

        using (var stringWriter = new StringWriter())
        {
            var xmlWriterSettings = new XmlWriterSettings
                {
                    ConformanceLevel = ConformanceLevel.Fragment
                };

            var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings);

            ICodedOutputStream output = XmlFormatWriter.CreateInstance(xmlWriter);
            req.WriteTo(output);
            output.Flush();
            string xml = stringWriter.ToString();

            using (var streamWriter = new StreamWriter(@"Requests.txt"))
            {
                streamWriter.WriteLine(xml);
                streamWriter.WriteLine(xml);
                streamWriter.WriteLine(xml);
            }
        }

I produce the Requests.txt file containing: 我生成包含以下内容的Requests.txt文件:

<str>Lafayette</str>
<str>Lafayette</str>
<str>Lafayette</str>

However when I try to deserialize them back using: 但是,当我尝试使用以下方法反序列化它们时:

        var xmlReaderSettings = new XmlReaderSettings
        {
            ConformanceLevel = ConformanceLevel.Fragment
        };

        using (var xmlReader = XmlReader.Create(@"Requests.txt", xmlReaderSettings))
        {
            ICodedInputStream input = XmlFormatReader.CreateInstance(xmlReader);
            MyRequest reqFromFile;

            while(!input.IsAtEnd)
            {
                reqFromFile =
                    ReverseRequest.CreateBuilder().MergeFrom(input).Build();
            }
        }

Only one MyRequest gets deserialized and the other two are ignored. 只有一个MyRequest被反序列化,而另两个则被忽略。 (After reqFromFile is built, input.IsAtEnd == true .) (后reqFromFile建成, input.IsAtEnd == true )。

So back to my question: is there some way to read multiple human-readable protobuf messages from a file? 回到我的问题:有什么方法可以从文件中读取多个人类可读的protobuf消息?

So back to my question: is there some way to read multiple human-readable protobuf messages from a file? 回到我的问题:有什么方法可以从文件中读取多个人类可读的protobuf消息?

Well you're currently creating a single text file with multiple root elements - it's not a valid XML file. 好吧,您当前正在创建带有多个根元素的单个文本文件-它不是有效的XML文件。

The simplest approach would probably be to create a single XML document with all those requests in, then load it (eg with LINQ to XML) and create an XmlReader positioned at each of the root element's children. 最简单的方法可能是创建一个包含所有这些请求的XML文档,然后将其加载(例如,使用LINQ to XML),并在每个根元素的子元素中创建一个XmlReader Each of those XmlReader objects should be able to then deserialize to a single protobuf message. 然后,这些XmlReader对象中的每一个都应该能够反序列化为单个protobuf消息。

You could look at the Protocol buffer Editor 您可以看一下协议缓冲区编辑器

Alternatively there is Google own Text format which is a bit like JSon. 另外,还有Google自己的文本格式,有点像JSon。 You can convert between the 2 using the protoc command (look at encode / decode options, you also need the proto definition) 您可以使用protoc命令在2之间进行转换(查看编码/解码选项,还需要原始定义)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 protobuf的-CSHARP端口 - protobuf-csharp-port protobuf-csharp-port 和 protobuf-net 如何选择 - How to choose between protobuf-csharp-port and protobuf-net 如何在 Windows 下在 Mono 上构建 protobuf-csharp-port - How to build protobuf-csharp-port on Mono under Windows protobuf-csharp-port是否支持Windows RT? - Does protobuf-csharp-port support Windows RT? 是否可以在protobuf-csharp-port中检测消息类型? - Is it possible to detect message type in protobuf-csharp-port? protobuf-csharp-port-来自文件的流记录有点像LINQ-to-XML中的轴函数 - protobuf-csharp-port - streaming records from a file a bit like an axis function in LINQ-to-XML 有没有办法将protobuf-csharp-port生成的类与servicestack.ormlite一起使用? - Is there way to use protobuf-csharp-port generated classes with servicestack.ormlite? protobuf-csharp-port这些工具最适合今天使用什么版本? - protobuf-csharp-port what version(s) of these tools are best to use today? 协议缓冲区-protobuf-csharp-port:是否存在等效的JAVA API调用CodedInputStream.getBytesUntilLimit()? - Protocol Buffers - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream.getBytesUntilLimit() exist? 如何使用 .NET 获得以字节缩写形式的人类可读文件大小? - How do I get a human-readable file size in bytes abbreviation using .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM