gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:03:31 25 4
gpt4 key购买 nike

我正在编写一个项目以将对象从父应用程序传递到子应用程序。我正在使用 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”附加信息:未找到用于测试的无参数构造函数

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

最佳答案

I'm not sure the best way to fix this

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

however. Is there a way to read the bytes from a stream until the is no data left and then stop?

是的,这是 Protocol Buffer 中的默认设置,因为最外层的消息不包含长度标记或结束标记(它被设计为可追加的)。但是,在您的情况下,这可能不是您想要的,因为在之后会有各种垃圾(可能全为零,也可能不是)您序列化的数据。您可能想改用 SerializeWithLengthPrefixDeserializeWithLengthPrefix。如果您的数据只有 1000 字节,MemoryStream 就可以了——无需使用非托管内存。

关于c# - IPC 使用 Protobuf 和内存映射文件 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36315600/

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