Skip to content

Commit

Permalink
Feature: ISerializable, Newtonsoft, SystemText Serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Sep 7, 2019
1 parent e4d85d7 commit 9769d4c
Show file tree
Hide file tree
Showing 35 changed files with 451 additions and 79 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
# Packages
**Reloaded.Messaging:** <a href="https://www.nuget.org/packages/Reloaded.Messaging"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Interfaces:** <a href="https://www.nuget.org/packages/Reloaded.Messaging.Interfaces"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Interfaces.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Serializer.MessagePack**: <a href="https://www.nuget.org/packages/Reloaded.Messaging.Serializer.MessagePack"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Serializer.MessagePack.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Serializer.ReloadedMemory**: <a href="https://www.nuget.org/packages/Reloaded.Messaging.Serializer.ReloadedMemory"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Serializer.ReloadedMemory.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Serializer.SystemTextJson**: <a href="https://www.nuget.org/packages/Reloaded.Messaging.Serializer.SystemTextJson"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Serializer.SystemTextJson.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Serializer.NewtonsoftJson**: <a href="https://www.nuget.org/packages/Reloaded.Messaging.Serializer.NewtonsoftJson"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Serializer.NewtonsoftJson.svg" alt="NuGet" /></a>

**Reloaded.Messaging.Compressor.ZStandard**: <a href="https://www.nuget.org/packages/Reloaded.Messaging.Compressor.ZStandard"><img src="https://img.shields.io/nuget/v/Reloaded.Messaging.Compressor.ZStandard.svg" alt="NuGet" /></a>

# Introduction
Reloaded.Networking is [Reloaded II](https://github.com/Reloaded-Project/Reloaded-II/)'s extensible "event-like" solution for passing messages across a local or remote network that extends on the base functionality of [LiteNetLib](https://github.com/RevenantX/LiteNetLib) by Ruslan Pyrch (RevenantX) .
Reloaded.Networking is [Reloaded II](https://github.com/Reloaded-Project/Reloaded-II/)'s Networking and Serialization library. The main goal for the library is to provide an extensible "event-like" solution for passing messages across a local or remote network that extends on the base functionality of [LiteNetLib](https://github.com/RevenantX/LiteNetLib) by Ruslan Pyrch (RevenantX) .

It has been slightly extended in the hope of becoming more general purpose, perhaps to be reused in other projects.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl>
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.3</Version>
<Version>1.1.0</Version>

<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Expand All @@ -27,15 +27,15 @@
<PackageReference Include="ZstdNet" Version="1.3.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reloaded.Messaging\Reloaded.Messaging.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reloaded.Messaging.Interfaces\Reloaded.Messaging.Interfaces.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Reloaded.Messaging.Compression;
using Reloaded.Messaging.Interfaces;
using ZstdNet;

namespace Reloaded.Messaging.Compressor.ZStandard
Expand Down Expand Up @@ -30,11 +30,14 @@ public void Dispose()
GC.SuppressFinalize(this);
}


/// <inheritdoc />
public byte[] Compress(byte[] data)
{
return Compressor.Wrap(data);
}

/// <inheritdoc />
public byte[] Decompress(byte[] data)
{
return Decompressor.Unwrap(data);
Expand Down
20 changes: 20 additions & 0 deletions Source/Reloaded.Messaging.Interfaces/ICompressor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Reloaded.Messaging.Interfaces
{
/// <summary>
/// Defines the minimal interface necessary to bootstrap a 3rd party compressor.
/// </summary>
public interface ICompressor
{
/// <summary>
/// Compresses the provided byte array.
/// </summary>
/// <param name="data">The data to compress.</param>
byte[] Compress(byte[] data);

/// <summary>
/// Decompresses the provided byte array.
/// </summary>
/// <param name="data">The data to decompress.</param>
byte[] Decompress(byte[] data);
}
}
15 changes: 15 additions & 0 deletions Source/Reloaded.Messaging.Interfaces/IMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Reloaded.Messaging.Interfaces.Message;

namespace Reloaded.Messaging.Interfaces
{
/// <summary>
/// Common interface shared by individual messages.
/// </summary>
public interface IMessage<TMessageType> : ISerializable where TMessageType : unmanaged
{
/// <summary>
/// Returns the unique message type/id for this message.
/// </summary>
TMessageType GetMessageType();
}
}
22 changes: 22 additions & 0 deletions Source/Reloaded.Messaging.Interfaces/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Reloaded.Messaging.Interfaces
{
/// <summary>
/// Defines the minimal interface necessary to bootstrap a 3rd party serializer.
/// </summary>
public interface ISerializer
{
/// <summary>
/// Deserializes the provided byte array into a concrete type.
/// </summary>
/// <typeparam name="TStruct">The type of the structure to deserialize.</typeparam>
/// <param name="serialized">The data to deserialize.</param>
TStruct Deserialize<TStruct>(byte[] serialized);

/// <summary>
/// Serializes the provided item into a byte array.
/// </summary>
/// <param name="item">The item to serialize to bytes.</param>
/// <returns>Serialized item.</returns>
byte[] Serialize<TStruct>(ref TStruct item);
}
}
18 changes: 18 additions & 0 deletions Source/Reloaded.Messaging.Interfaces/Message/ISerializable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Reloaded.Messaging.Interfaces.Message
{
/// <summary>
/// An interface that provides serialization/deserialization and compression/decompression support for.
/// </summary>
public interface ISerializable
{
/// <summary>
/// Returns the serializer for this specific type.
/// </summary>
ISerializer GetSerializer();

/// <summary>
/// Returns the compressor for this specific type.
/// </summary>
ICompressor GetCompressor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>Contains all of the interfaces (and some extension functionality) used by the base Reloaded.Messaging library.
This package exists to allow you to use various features of the library, such as serializers without the need to import the dependencies of the base package.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageProjectUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</PackageProjectUrl>
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl>
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions Source/Reloaded.Messaging.Interfaces/Serializable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Runtime.CompilerServices;
using Reloaded.Messaging.Interfaces.Message;

namespace Reloaded.Messaging.Interfaces
{
/// <summary>
/// An extension class providing serialization support to implementers of <see cref="ISerializable"/>.
/// </summary>
public static class Serializable
{
/// <summary>
/// Serializes and compresses the current instance of the class or struct
/// using the serializer and compressor defined by the <see cref="ISerializable"/>.
/// </summary>
public static byte[] Serialize<TSerializable>(this TSerializable serializable) where TSerializable : ISerializable
{
var serializer = serializable.GetSerializer();
var compressor = serializable.GetCompressor();

byte[] serialized = serializer.Serialize(ref serializable);
if (compressor != null)
return compressor.Compress(serialized);

return serialized;
}

/// <summary>
/// Decompresses and deserializes the current instance of the class or struct using the
/// serializer and compressor defined by the <see cref="ISerializable"/>.
/// </summary>
public static ISerializable Deserialize<TType>(this TType serializable, byte[] bytes) where TType : ISerializable
{
var compressor = serializable.GetCompressor();
var serializer = serializable.GetSerializer();

byte[] decompressed = bytes;
if (compressor != null)
decompressed = compressor.Decompress(bytes);

return serializer.Deserialize<TType>(decompressed);
}

/// <summary>
/// Decompresses and deserializes the current instance of the class or struct using the
/// serializer and compressor defined by the <see cref="ISerializable"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ISerializable Deserialize<TType>(byte[] bytes) where TType : ISerializable, new()
{
var serializable = new TType();
return Deserialize(serializable, bytes);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using MessagePack;
using Reloaded.Messaging.Serialization;
using Reloaded.Messaging.Interfaces;

namespace Reloaded.Messaging.Serializer.MessagePack
{
Expand All @@ -8,13 +8,13 @@ public class MsgPackSerializer : ISerializer
/// <summary>
/// Uses LZ4 compression for serialization.
/// </summary>
public bool UseLZ4 { get; set; }
public bool UseLZ4 { get; private set; }

/// <summary>
/// Any custom resolver to pass to MessagePack.
/// Default is <see cref="MessagePack.Resolvers.ContractlessStandardResolver.Instance"/>
/// </summary>
public IFormatterResolver Resolver { get; set; } = global::MessagePack.Resolvers.ContractlessStandardResolver.Instance;
public IFormatterResolver Resolver { get; private set; } = global::MessagePack.Resolvers.ContractlessStandardResolver.Instance;

/// <summary>
/// Creates a new instance of the MessagePack serializer.
Expand All @@ -31,12 +31,16 @@ public MsgPackSerializer(bool useLz4, IFormatterResolver resolver = null)
Resolver = resolver;
}


/// <inheritdoc />
public TStruct Deserialize<TStruct>(byte[] serialized)
{
return UseLZ4 ? LZ4MessagePackSerializer.Deserialize<TStruct>(serialized, Resolver) :
MessagePackSerializer.Deserialize<TStruct>(serialized, Resolver);
}


/// <inheritdoc />
public byte[] Serialize<TStruct>(ref TStruct item)
{
return UseLZ4 ? LZ4MessagePackSerializer.Serialize(item, Resolver) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl>
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.3</Version>
<Version>1.1.0</Version>

<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
Expand All @@ -29,15 +29,15 @@
<PackageReference Include="MessagePack" Version="1.7.3.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reloaded.Messaging\Reloaded.Messaging.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reloaded.Messaging.Interfaces\Reloaded.Messaging.Interfaces.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Runtime.Serialization;
using System.Text;
using Newtonsoft.Json;
using Reloaded.Messaging.Interfaces;

namespace Reloaded.Messaging.Serializer.NewtonsoftJson
{
public class NewtonsoftJsonSerializer : ISerializer
{
/// <summary>
/// Serialization options.
/// </summary>
public JsonSerializerSettings Options { get; private set; }

/// <summary>
/// Creates the System.Text.Json based serializer.
/// </summary>
/// <param name="serializerOptions">Options to use for serialization/deserialization.</param>
public NewtonsoftJsonSerializer(JsonSerializerSettings serializerOptions)
{
Options = serializerOptions;
}

/// <inheritdoc />
public TStruct Deserialize<TStruct>(byte[] serialized)
{
return JsonConvert.DeserializeObject<TStruct>(Encoding.UTF8.GetString(serialized), Options);
}

/// <inheritdoc />
public byte[] Serialize<TStruct>(ref TStruct item)
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(item, Options));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Reloaded.Messaging.Serializer.NewtonsoftJson</PackageId>
<Authors>Sewer56</Authors>
<Company>Sewer56</Company>
<Description>Basic Json serialization implementation for Reloaded.Messaging based off of Newtonsoft.Json.</Description>
<Copyright>Sewer56</Copyright>
<PackageLicenseFile>README.md</PackageLicenseFile>
<PackageProjectUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</PackageProjectUrl>
<PackageIconUrl>https://avatars1.githubusercontent.com/u/45473408</PackageIconUrl>
<RepositoryUrl>https://github.com/Reloaded-Project/Reloaded.Messaging</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Reloaded.Messaging.Interfaces\Reloaded.Messaging.Interfaces.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
Loading

0 comments on commit 9769d4c

Please sign in to comment.