Parsers written. Now 272 000 rps
This commit is contained in:
@@ -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<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();
|
||||
public static TimeSpan SerializationTime = TimeSpan.Zero;
|
||||
|
||||
private HashSet<int> 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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>2.0.5</Version>
|
||||
<Version>2.0.6</Version>
|
||||
<LangVersion>9</LangVersion>
|
||||
<PackageIcon>mroaLogo.png</PackageIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
Reference in New Issue
Block a user