From 14c74c577551ab54c12934dceffde3e993cef030 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 24 Feb 2025 12:48:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=B8=D1=81=D1=8C=20Cbor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.Benchmark/Program.cs | 7 +- mROA.Cbor/CborSerializaitonToolkit.cs | 144 +++++++++++++++++++ mROA.Cbor/IContextualSerializationToolKit.cs | 17 +++ mROA.Cbor/mROA.Cbor.csproj | 16 +++ mROA.sln | 6 + mROA/Abstract/IEndPointContext.cs | 9 ++ mROA/Implementation/SharedObject.cs | 4 + 7 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 mROA.Cbor/CborSerializaitonToolkit.cs create mode 100644 mROA.Cbor/IContextualSerializationToolKit.cs create mode 100644 mROA.Cbor/mROA.Cbor.csproj create mode 100644 mROA/Abstract/IEndPointContext.cs diff --git a/mROA.Benchmark/Program.cs b/mROA.Benchmark/Program.cs index c4ebb5e..a99f392 100644 --- a/mROA.Benchmark/Program.cs +++ b/mROA.Benchmark/Program.cs @@ -10,9 +10,8 @@ namespace mROA.Benchmark { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); - var summary = BenchmarkRunner.Run(); - + // Console.WriteLine("Hello, World!"); + // var summary = BenchmarkRunner.Run(); } } @@ -26,7 +25,7 @@ namespace mROA.Benchmark public CollectionsSpeed() { _array = Enumerable.Range(0, N).ToArray(); - _immutable = [.._array]; + // _immutable = [.._array]; } [Benchmark] diff --git a/mROA.Cbor/CborSerializaitonToolkit.cs b/mROA.Cbor/CborSerializaitonToolkit.cs new file mode 100644 index 0000000..07d4e8d --- /dev/null +++ b/mROA.Cbor/CborSerializaitonToolkit.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Formats.Cbor; +using System.Linq; +using mROA.Abstract; +using mROA.Implementation; + +namespace mROA.Cbor +{ + public class CborSerializaitonToolkit : IContextualSerializationToolKit + { + public byte[] Serialize(T objectToSerialize, IEndPointContext context) + { + return Serialize(objectToSerialize, typeof(T), context); + } + + public byte[] Serialize(object objectToSerialize, Type type, IEndPointContext context) + { + } + + public T Deserialize(byte[] rawData, IEndPointContext context) + { + return (T)Deserialize(rawData, typeof(T), context); + } + + public object? Deserialize(byte[] rawData, Type type, IEndPointContext context) + { + return Deserialize(rawData.AsSpan(), type, context); + } + + public T Deserialize(Span rawData, IEndPointContext context) + { + return (T)Deserialize(rawData, typeof(T), context); + } + + public object? Deserialize(Span rawData, Type type, IEndPointContext context) + { + return null; + } + + public T Cast(object nonCasted, IEndPointContext context) + { + return (T)Cast(nonCasted, typeof(T), context); + } + + public object Cast(object nonCasted, Type type, IEndPointContext context) + { + return null; + } + + private void WriteData(object? obj, CborWriter writer) + { + switch (obj) + { + case int i: + writer.WriteInt32(i); + break; + case long l: + writer.WriteInt64(l); + break; + case float f: + writer.WriteSingle(f); + break; + case double d: + writer.WriteDouble(d); + break; + case decimal dec: + writer.WriteDecimal(dec); + break; + case bool b: + writer.WriteBoolean(b); + break; + case string s: + writer.WriteTextString(s); + break; + case null: + writer.WriteNull(); + break; + case uint ui: + writer.WriteUInt32(ui); + break; + case ulong ul: + writer.WriteUInt64(ul); + break; + case DateTimeOffset dto: + writer.WriteDateTimeOffset(dto); + break; + case byte[] bytes: + writer.WriteByteString(bytes); + break; + case IDictionary dictionary: + WriteDictionary(dictionary, writer); + break; + case IEnumerable enumerable: + WriteEnumerable(enumerable, writer); + break; + case SharedObject sharedObject: + break; + default: + WriteObject(obj, writer); + break; + } + } + + private void WriteEnumerable(IEnumerable enumerable, CborWriter writer) + { + List list = new List(); + + foreach (var element in enumerable) + list.Add(element); + + writer.WriteStartArray(list.Count); + + foreach (var element in list) + WriteData(element, writer); + + writer.WriteEndArray(); + } + + private void WriteDictionary(IDictionary dictionary, CborWriter writer) + { + writer.WriteStartMap(dictionary.Count); + var keysEnumerator = dictionary.Keys.GetEnumerator(); + var valuesEnumerator = dictionary.Values.GetEnumerator(); + for (int i = 0; i < dictionary.Count; i++) + { + keysEnumerator.MoveNext(); + valuesEnumerator.MoveNext(); + WriteData(keysEnumerator.Current, writer); + WriteData(valuesEnumerator.Current, writer); + } + writer.WriteEndMap(); + } + + private void WriteObject(object obj, CborWriter writer) + { + var type = obj.GetType(); + var properties = type.GetProperties(); + var values = properties.Select(property => property.GetValue(obj)); + WriteEnumerable(values, writer); + } + } +} \ No newline at end of file diff --git a/mROA.Cbor/IContextualSerializationToolKit.cs b/mROA.Cbor/IContextualSerializationToolKit.cs new file mode 100644 index 0000000..b96b27e --- /dev/null +++ b/mROA.Cbor/IContextualSerializationToolKit.cs @@ -0,0 +1,17 @@ +using System; +using mROA.Abstract; + +namespace mROA.Cbor +{ + public interface IContextualSerializationToolKit + { + byte[] Serialize(T objectToSerialize, IEndPointContext context); + byte[] Serialize(object objectToSerialize, Type type, IEndPointContext context); + T Deserialize(byte[] rawData, IEndPointContext context); + object? Deserialize(byte[] rawData, Type type, IEndPointContext context); + T Deserialize(Span rawData, IEndPointContext context); + object? Deserialize(Span rawData, Type type, IEndPointContext context); + T Cast(object nonCasted, IEndPointContext context); + object Cast(object nonCasted, Type type, IEndPointContext context); + } +} \ No newline at end of file diff --git a/mROA.Cbor/mROA.Cbor.csproj b/mROA.Cbor/mROA.Cbor.csproj new file mode 100644 index 0000000..7f66dce --- /dev/null +++ b/mROA.Cbor/mROA.Cbor.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.1 + enable + + + + + + + + + + + diff --git a/mROA.sln b/mROA.sln index 7586eb3..34c47ba 100644 --- a/mROA.sln +++ b/mROA.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.Frontend", "Example EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Benchmark", "mROA.Benchmark\mROA.Benchmark.csproj", "{6868F42B-E30D-4040-AD4A-BC2A2E76D03A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Cbor", "mROA.Cbor\mROA.Cbor.csproj", "{6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -53,6 +55,10 @@ Global {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Debug|Any CPU.Build.0 = Debug|Any CPU {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.ActiveCfg = Release|Any CPU {6868F42B-E30D-4040-AD4A-BC2A2E76D03A}.Release|Any CPU.Build.0 = Release|Any CPU + {6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6211E4EA-13FD-4EB1-8E6A-C0173DD0784A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/mROA/Abstract/IEndPointContext.cs b/mROA/Abstract/IEndPointContext.cs new file mode 100644 index 0000000..dbaf7d6 --- /dev/null +++ b/mROA/Abstract/IEndPointContext.cs @@ -0,0 +1,9 @@ +namespace mROA.Abstract +{ + public interface IEndPointContext + { + IContextRepository RealRepository { get; } + IContextRepository RemoteRepository { get; } + int HostId { get; } + } +} \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index 8008b1f..8dcfcf9 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -32,6 +32,10 @@ namespace mROA.Implementation } + public class SharedObject : SharedObject + { + + } public class SharedObject where T : notnull { private IContextRepository GetDefaultContextRepository() =>