Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MarshalSerializer for structs #20

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Hyperion.Tests/Hyperion.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="IlCompilerTests.cs" />
<Compile Include="ImmutableCollectionsTests.cs" />
<Compile Include="InterfaceTests.cs" />
<Compile Include="MarshalSerializerTests.cs" />
<Compile Include="UnsupportedTypeSerializerTests.cs" />
<Compile Include="ISerializableTests.cs" />
<Compile Include="PrimitivesTests.cs" />
Expand Down
47 changes: 47 additions & 0 deletions Hyperion.Tests/MarshalSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#region copyright
// -----------------------------------------------------------------------
// <copyright file="MarshaledValueSerializerFactory.cs" company="Akka.NET Team">
// Copyright (C) 2017-2017 Akka.NET Team <https://github.com/akkadotnet>
// </copyright>
// -----------------------------------------------------------------------
#endregion

using Hyperion.SerializerFactories;
using Xunit;

namespace Hyperion.Tests
{
public class MarshalSerializerTests : TestBase
{
public struct MyStruct
{
public readonly int X;
public readonly int Y;
public readonly int Z;

public MyStruct(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
}

public MarshalSerializerTests()
{
CustomInit(new Serializer(new SerializerOptions(
serializerFactories: new[] { new MarshalSerializerFactory(), })));
}

[Fact]
public void CanSerializerFullyValuedTypesUsingMarshaller()
{
var expected = new MyStruct(1337, 293256, 235034634);

Serialize(expected);
Reset();
var actual = Deserialize<MyStruct>();
Assert.Equal(expected, actual);
}
}
}
1 change: 1 addition & 0 deletions Hyperion/Hyperion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="SerializerFactories\ImmutableCollectionsSerializerFactory.cs" />
<Compile Include="SerializerFactories\ISerializableSerializerFactory.cs" />
<Compile Include="SerializerFactories\LinkedListSerializerFactory.cs" />
<Compile Include="SerializerFactories\MarshalSerializerFactory.cs" />
<Compile Include="SerializerFactories\MethodInfoSerializerFactory.cs" />
<Compile Include="SerializerFactories\ToSurrogateSerializerFactory.cs" />
<Compile Include="SerializerFactories\ValueSerializerFactory.cs" />
Expand Down
81 changes: 81 additions & 0 deletions Hyperion/SerializerFactories/MarshalSerializerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#region copyright
// -----------------------------------------------------------------------
// <copyright file="MarshaledValueSerializerFactory.cs" company="Akka.NET Team">
// Copyright (C) 2017-2017 Akka.NET Team <https://github.com/akkadotnet>
// </copyright>
// -----------------------------------------------------------------------
#endregion

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Hyperion.Extensions;
using Hyperion.ValueSerializers;

namespace Hyperion.SerializerFactories
{
public class MarshalSerializerFactory : ValueSerializerFactory
{
public override bool CanSerialize(Serializer serializer, Type type) =>
!serializer.Options.VersionTolerance && IsFullValueType(type, new HashSet<Type>());

public override bool CanDeserialize(Serializer serializer, Type type) =>
!serializer.Options.VersionTolerance && IsFullValueType(type, new HashSet<Type>());

private bool IsFullValueType(Type type, HashSet<Type> acknowledged)
{
if (!type.IsValueType) return false;

acknowledged.Add(type);

var fields = type.GetFieldInfosForType();

var result = true;
for (int i = 0; i < fields.Length; i++)
{
var fieldType = fields[i].FieldType;
result &= acknowledged.Contains(fieldType) || IsFullValueType(fieldType, acknowledged);
}

return result;
}

public override ValueSerializer BuildSerializer(Serializer serializer, Type type, ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var ser = new ObjectSerializer(type);
typeMapping.TryAdd(type, ser);

var size = Marshal.SizeOf(type);
ObjectWriter writer = (stream, value, session) =>
{
var bin = new byte[size];

var handle = GCHandle.Alloc(bin, GCHandleType.Pinned);
var ptr = handle.AddrOfPinnedObject();
Marshal.StructureToPtr(value, ptr, true);
Marshal.Copy(ptr, bin, 0, size);

stream.Write(bin, 0, size);
handle.Free();
};

ObjectReader reader = (stream, session) =>
{
var bin = new byte[size];
stream.Read(bin, 0, size);
var handle = GCHandle.Alloc(bin, GCHandleType.Pinned);
var ptr = handle.AddrOfPinnedObject();

Marshal.Copy(bin, 0, ptr, size);

var value = Marshal.PtrToStructure(ptr, type);
handle.Free();
return value;
};
ser.Initialize(reader, writer);

return ser;
}
}
}