From 5a66a05c5d51f86759f54f17236271fd025d87a6 Mon Sep 17 00:00:00 2001 From: Mitrofanov Mikhail Date: Wed, 9 Jul 2025 17:15:55 +0300 Subject: [PATCH] Parsers written. Now 272 000 rps --- Example.Frontend/Example.Frontend.csproj | 2 +- Example.Frontend/Program.cs | 2 +- mROA.Cbor/CborSerializationToolkit.cs | 32 +++-- mROA.Cbor/IOrdinaryStructureParser.cs | 135 ++++++++++++++++++++ mROA.Cbor/mROA.Cbor.csproj | 2 +- mROA/Implementation/RepresentationModule.cs | 6 - 6 files changed, 160 insertions(+), 19 deletions(-) create mode 100644 mROA.Cbor/IOrdinaryStructureParser.cs diff --git a/Example.Frontend/Example.Frontend.csproj b/Example.Frontend/Example.Frontend.csproj index 0c1a745..fdac1b1 100644 --- a/Example.Frontend/Example.Frontend.csproj +++ b/Example.Frontend/Example.Frontend.csproj @@ -14,7 +14,7 @@ - JUST_LOAD + diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 08c8b45..11501d4 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -147,6 +147,6 @@ class Program frontendBridge.Disconnect(); - // DemoCheck.Show(); + DemoCheck.Show(); } } \ No newline at end of file diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index eacc5f3..ff5d7dd 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -8,14 +8,16 @@ using System.Reflection; using mROA.Abstract; using mROA.Implementation; using mROA.Implementation.Attributes; +using mROA.Implementation.CommandExecution; namespace mROA.Cbor { public class CborSerializationToolkit : IContextualSerializationToolKit { + private Dictionary _parsers = new(){{typeof(NetworkMessageHeader), new NetworkMessageHeaderParser()}, {typeof(DefaultCallRequest), new DefaultCallRequestParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionResultlessParser()}}; // private Dictionary> _propertiesCache = new(); public static TimeSpan SerializationTime = TimeSpan.Zero; - + private HashSet a; public byte[] Serialize(object objectToSerialize, IEndPointContext context) { var sw = Stopwatch.StartNew(); @@ -124,8 +126,14 @@ namespace mROA.Cbor return Cast(nonCasted: nonCasted, type: type, context: null); } - private void WriteData(object? obj, CborWriter writer, IEndPointContext? context) + public void WriteData(object? obj, CborWriter writer, IEndPointContext? context) { + if (obj is not null && _parsers.TryGetValue(obj.GetType(), out var parser)) + { + parser.Write(writer, obj, context, this); + return; + } + switch (obj) { case int i: @@ -187,7 +195,7 @@ namespace mROA.Cbor } } - private void WriteList(IList list, CborWriter writer, IEndPointContext? context) + public void WriteList(IList list, CborWriter writer, IEndPointContext? context) { writer.WriteStartArray(list.Count); @@ -197,7 +205,7 @@ namespace mROA.Cbor writer.WriteEndArray(); } - private void WriteDictionary(IDictionary dictionary, CborWriter writer, IEndPointContext? context) + public void WriteDictionary(IDictionary dictionary, CborWriter writer, IEndPointContext? context) { writer.WriteStartMap(dictionary.Count); var keysEnumerator = dictionary.Keys.GetEnumerator(); @@ -215,7 +223,7 @@ namespace mROA.Cbor (valuesEnumerator as IDisposable)?.Dispose(); } - private void WriteObject(object obj, CborWriter writer, IEndPointContext? context) + public void WriteObject(object obj, CborWriter writer, IEndPointContext? context) { var type = obj.GetType(); @@ -238,8 +246,12 @@ namespace mROA.Cbor WriteList(values, writer, context); } - private object? ReadData(CborReader reader, Type? type, IEndPointContext? context) + public object? ReadData(CborReader reader, Type? type, IEndPointContext? context) { + if (type is not null && _parsers.TryGetValue(type, out var parser)) + { + return parser.Read(reader, context, this); + } var state = reader.PeekState(); switch (state) { @@ -296,7 +308,7 @@ namespace mROA.Cbor } - private IList ReadList(CborReader reader, Type? type, IEndPointContext? context) + public IList ReadList(CborReader reader, Type? type, IEndPointContext? context) { var length = reader.ReadStartArray(); if (length != null) @@ -340,7 +352,7 @@ namespace mROA.Cbor return null; } - private IDictionary ReadDictionary(CborReader reader, Type type, IEndPointContext? context) + public IDictionary ReadDictionary(CborReader reader, Type type, IEndPointContext? context) { var dictionaryInstance = (Activator.CreateInstance(type) as IDictionary)!; var length = reader.ReadStartArray(); @@ -357,7 +369,7 @@ namespace mROA.Cbor return dictionaryInstance; } - private object ReadObject(CborReader reader, Type type, IEndPointContext? context) + public object ReadObject(CborReader reader, Type type, IEndPointContext? context) { if (type == typeof(object)) { @@ -401,7 +413,7 @@ namespace mROA.Cbor return sharedObject; } - private void FillObject(object obj, Type type, CborReader reader, IEndPointContext? context) + public void FillObject(object obj, Type type, CborReader reader, IEndPointContext? context) { var properties = GetAvailableProperties(type); var length = reader.ReadStartArray(); diff --git a/mROA.Cbor/IOrdinaryStructureParser.cs b/mROA.Cbor/IOrdinaryStructureParser.cs new file mode 100644 index 0000000..6ea1545 --- /dev/null +++ b/mROA.Cbor/IOrdinaryStructureParser.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Formats.Cbor; +using mROA.Abstract; +using mROA.Implementation; +using mROA.Implementation.CommandExecution; + +namespace mROA.Cbor +{ + public interface IOrdinaryStructureParser + { + void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization); + object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization); + } + + public class NetworkMessageHeaderParser : IOrdinaryStructureParser + { + public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) + { + var v = value as NetworkMessageHeader; + writer.WriteStartArray(3); + writer.WriteByteString(v.Id.ToByteArray()); + writer.WriteInt32((int)v.MessageType); + writer.WriteByteString(v.Data); + writer.WriteEndArray(); + } + + public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization) + { + reader.ReadStartArray(); + var value = new NetworkMessageHeader + { + Id = new Guid(reader.ReadByteString()), + MessageType = (EMessageType)reader.ReadInt32(), + Data = reader.ReadByteString() + }; + return value; + } + } + + public class DefaultCallRequestParser : IOrdinaryStructureParser + { + public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) + { + var v = value as DefaultCallRequest; + writer.WriteStartArray(4); + writer.WriteByteString(v.Id.ToByteArray()); + writer.WriteInt32(v.CommandId); + writer.WriteStartArray(1); + writer.WriteUInt64(v.ObjectId.Flat); + writer.WriteEndArray(); + serialization.WriteData(v.Parameters, writer, context); + writer.WriteEndArray(); + } + + public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization) + { + reader.ReadStartArray(); + var value = new DefaultCallRequest + { + Id = new Guid(reader.ReadByteString()), + CommandId = reader.ReadInt32(), + ObjectId = (ComplexObjectIdentifier)ComplexObjectIdentifierParser.Instance.Read(reader, context, serialization), + Parameters = serialization.ReadData(reader, typeof(object[]), context) as object[] + }; + reader.ReadEndArray(); + return value; + } + } + + public class ComplexObjectIdentifierParser : IOrdinaryStructureParser + { + public static readonly ComplexObjectIdentifierParser Instance = new(); + public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) + { + writer.WriteStartArray(1); + writer.WriteUInt64(((ComplexObjectIdentifier)value).Flat); + writer.WriteEndArray(); + } + + public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization) + { + reader.ReadStartArray(); + var value = new ComplexObjectIdentifier { Flat = reader.ReadUInt64() }; + reader.ReadEndArray(); + return value; + } + } + + public class FinalCommandExecutionParser : IOrdinaryStructureParser + { + public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) + { + var v = value as FinalCommandExecution; + writer.WriteStartArray(2); + serialization.WriteData(v.Result, writer, context); + writer.WriteByteString(v.Id.ToByteArray()); + writer.WriteEndArray(); + } + + public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization) + { + reader.ReadStartArray(); + var result = new FinalCommandExecution + { + Result = serialization.ReadData(reader, typeof(object), context), + Id = new Guid(reader.ReadByteString()) + }; + reader.ReadEndArray(); + return result; + } + } + + public class FinalCommandExecutionResultlessParser : IOrdinaryStructureParser + { + public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) + { + var v = value as FinalCommandExecution; + writer.WriteStartArray(1); + writer.WriteByteString(v.Id.ToByteArray()); + writer.WriteEndArray(); + } + + public object Read(CborReader reader, IEndPointContext context, CborSerializationToolkit serialization) + { + reader.ReadStartArray(); + var result = new FinalCommandExecution + { + Id = new Guid(reader.ReadByteString()) + }; + reader.ReadEndArray(); + return result; + } + } +} \ No newline at end of file diff --git a/mROA.Cbor/mROA.Cbor.csproj b/mROA.Cbor/mROA.Cbor.csproj index ab207eb..293c5ca 100644 --- a/mROA.Cbor/mROA.Cbor.csproj +++ b/mROA.Cbor/mROA.Cbor.csproj @@ -4,7 +4,7 @@ netstandard2.1 enable true - 2.0.5 + 2.0.6 9 mroaLogo.png diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index ce38bf9..4bc64ba 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -76,12 +76,6 @@ namespace mROA.Implementation yield return (deserialized, message.MessageType)!; } } - - // public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload, - // IEndPointContext? context) where T : notnull - // { - // await this.PostCallMessageAsync(id, eMessageType, payload, context); - // } public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull {