IPC使用Protobuf和内存映射文件C#

时间:2016-03-30 17:20:28

标签: c# stream ipc protobuf-net memory-mapped-files

我正在编写一个项目,将对象从父应用程序传递给子应用程序。我正在使用Protobuf来序列化和反序列化数据。我还使用非持久内存映射文件在序列化时写入(并在反序列化时读取)。这是代码:

[ProtoContract(SkipConstructor = true)]
public class Test
{
    [ProtoMember(1)]
    public int ID { get; private set; }
    [ProtoMember(2)]
    public bool TestBool { get; private set; }
    [ProtoMember(3)]
    public string MessageBody { get; private set; }

    public Test(int id, bool testBool, string messageBody)
    {
        this.ID = id;
        this.TestBool = testBool;
        this.MessageBody = messageBody;
    }


    public void Serialize()
    {
        MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 1000);
        using (MemoryMappedViewStream stream = mmf.CreateViewStream())
        {
            Serializer.SerializeWithLengthPrefix(stream, this, PrefixStyle.Base128);
            stream.Flush();
        }
    }

    public static Test Deserialize()
    {
        using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
        {
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                return Serializer.DeserializeWithLengthPrefix<Test>(stream, PrefixStyle.Base128);
            }
        }
    }
}

//On the parent application
var msg = new SharedLibrary.Test(1234, true, "Test message hahah");
msg.Serialize();
//spawn child application



//On the child application
Test result = Test.Deserialize();   

当我运行此代码时,我收到以下错误(当调用Serializer.Deserialize时):

抛出异常:protobuf-net.dll中的'ProtoBuf.ProtoException' 附加信息:源数据中的无效字段:0 我认为发生此错误是因为流大于它包含的实际数据。当流被反序列化时,我认为它开始超出实际数据。

抛出异常:protobuf-net.dll中的'ProtoBuf.ProtoException' 附加信息:找不到Test

的无参数构造函数

我不确定解决这个问题的最佳方法。有没有办法从流中读取字节,直到没有数据,然后停止?如果没有,我可以用另一种方式解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

  

我不确定解决此问题的最佳方法

  1. 添加无参数构造函数(如果您愿意,可以是private)或
  2. 在您的类型
  3. SkipConstructor = true上添加[ProtoContract(...)]作为参数
      

    但是。有没有办法从流中读取字节,直到没有数据然后停止?

    是的,这是协议缓冲区中的默认值,因为最外层的消息不包含长度标记或结束标记(它被设计为可附加)。但是,在你的情况下,这可能不是你想要的东西,因为在之后会有各种垃圾(可能全部为零,可能不是)您序列化的数据。您可能希望使用SerializeWithLengthPrefixDeserializeWithLengthPrefix代替。如果您的数据只有1000个字节,MemoryStream就可以了 - 不需要使用非托管内存。