Parsers written. Now 272 000 rps

This commit is contained in:
2025-07-09 17:15:55 +03:00
parent 151237a7b7
commit 5a66a05c5d
6 changed files with 160 additions and 19 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DefineConstants>JUST_LOAD</DefineConstants> <DefineConstants></DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -147,6 +147,6 @@ class Program
frontendBridge.Disconnect(); frontendBridge.Disconnect();
// DemoCheck.Show(); DemoCheck.Show();
} }
} }
+22 -10
View File
@@ -8,14 +8,16 @@ using System.Reflection;
using mROA.Abstract; using mROA.Abstract;
using mROA.Implementation; using mROA.Implementation;
using mROA.Implementation.Attributes; using mROA.Implementation.Attributes;
using mROA.Implementation.CommandExecution;
namespace mROA.Cbor namespace mROA.Cbor
{ {
public class CborSerializationToolkit : IContextualSerializationToolKit public class CborSerializationToolkit : IContextualSerializationToolKit
{ {
private Dictionary<Type, IOrdinaryStructureParser> _parsers = new(){{typeof(NetworkMessageHeader), new NetworkMessageHeaderParser()}, {typeof(DefaultCallRequest), new DefaultCallRequestParser()}, {typeof(FinalCommandExecution<object>), new FinalCommandExecutionParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionResultlessParser()}}; //
private Dictionary<Type, List<PropertyInfo>> _propertiesCache = new(); private Dictionary<Type, List<PropertyInfo>> _propertiesCache = new();
public static TimeSpan SerializationTime = TimeSpan.Zero; public static TimeSpan SerializationTime = TimeSpan.Zero;
private HashSet<int> a;
public byte[] Serialize(object objectToSerialize, IEndPointContext context) public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{ {
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
@@ -124,8 +126,14 @@ namespace mROA.Cbor
return Cast(nonCasted: nonCasted, type: type, context: null); 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) switch (obj)
{ {
case int i: 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); writer.WriteStartArray(list.Count);
@@ -197,7 +205,7 @@ namespace mROA.Cbor
writer.WriteEndArray(); writer.WriteEndArray();
} }
private void WriteDictionary(IDictionary dictionary, CborWriter writer, IEndPointContext? context) public void WriteDictionary(IDictionary dictionary, CborWriter writer, IEndPointContext? context)
{ {
writer.WriteStartMap(dictionary.Count); writer.WriteStartMap(dictionary.Count);
var keysEnumerator = dictionary.Keys.GetEnumerator(); var keysEnumerator = dictionary.Keys.GetEnumerator();
@@ -215,7 +223,7 @@ namespace mROA.Cbor
(valuesEnumerator as IDisposable)?.Dispose(); (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(); var type = obj.GetType();
@@ -238,8 +246,12 @@ namespace mROA.Cbor
WriteList(values, writer, context); 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(); var state = reader.PeekState();
switch (state) 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(); var length = reader.ReadStartArray();
if (length != null) if (length != null)
@@ -340,7 +352,7 @@ namespace mROA.Cbor
return null; 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 dictionaryInstance = (Activator.CreateInstance(type) as IDictionary)!;
var length = reader.ReadStartArray(); var length = reader.ReadStartArray();
@@ -357,7 +369,7 @@ namespace mROA.Cbor
return dictionaryInstance; return dictionaryInstance;
} }
private object ReadObject(CborReader reader, Type type, IEndPointContext? context) public object ReadObject(CborReader reader, Type type, IEndPointContext? context)
{ {
if (type == typeof(object)) if (type == typeof(object))
{ {
@@ -401,7 +413,7 @@ namespace mROA.Cbor
return sharedObject; 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 properties = GetAvailableProperties(type);
var length = reader.ReadStartArray(); var length = reader.ReadStartArray();
+135
View File
@@ -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<object>;
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<object>
{
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;
}
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.5</Version> <Version>2.0.6</Version>
<LangVersion>9</LangVersion> <LangVersion>9</LangVersion>
<PackageIcon>mroaLogo.png</PackageIcon> <PackageIcon>mroaLogo.png</PackageIcon>
</PropertyGroup> </PropertyGroup>
@@ -76,12 +76,6 @@ namespace mROA.Implementation
yield return (deserialized, message.MessageType)!; yield return (deserialized, message.MessageType)!;
} }
} }
// public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload,
// IEndPointContext? context) where T : notnull
// {
// await this.PostCallMessageAsync(id, eMessageType, payload, context);
// }
public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull public async Task PostCallMessageAsync<T>(Guid id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
{ {