RandomEngy / PipeMethodCalls

Lightweight .NET Standard 2.0 library for method calls over named pipes for IPC. Supports two-way communication with callbacks.
https://www.nuget.org/packages/PipeMethodCalls
MIT License
145 stars 24 forks source link

Using ProtoBuf as binary serializer #14

Closed tobe83 closed 2 years ago

tobe83 commented 2 years ago

Hello, nice work with the PipeMethodCalls.

I want to use a binary serializer and so I made my own implementation with ProtoBuf:

public class ProtoBufPipeSerializer : IPipeSerializer
{
    public object Deserialize(byte[] data, Type type)
    {
        using (var memoryStream = new MemoryStream())
        {
            memoryStream.Write(data, 0, data.Length);
            return Serializer.Deserialize(type, memoryStream);
        }
    }

    public byte[] Serialize(object o)
    {
        using (var memoryStream = new MemoryStream())
        {
            Serializer.Serialize(memoryStream, o);
            return memoryStream.ToArray();
        }
    }
}

But this doesn't work. What binary serializer do you recommend?

Cheers, Tobias

RandomEngy commented 2 years ago

What protobuf NuGet package are you using? I think I remember seeing a fork that implemented a binary serializer but don't have time to check right now.

tobe83 commented 2 years ago

this one here: https://www.nuget.org/packages/protobuf-net/

RandomEngy commented 2 years ago

I tweaked your implementation a bit and got it working:

public object Deserialize(byte[] data, Type type)
{
    using (var memoryStream = new MemoryStream())
    {
        memoryStream.Write(data, 0, data.Length);
        memoryStream.Position = 0;
        return Serializer.Deserialize(type, memoryStream);
    }
}

You need to set position back to 0 in the MemoryStream for the deserializer to read the data.