Rewrite dictionary to if and using structs
This commit is contained in:
@@ -14,10 +14,43 @@ 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 readonly IOrdinaryStructureParser[] _parsers = {
|
||||||
|
new NetworkMessageHeaderParser(), new DefaultCallRequestParser(), new FinalCommandExecutionParser(),
|
||||||
|
new FinalCommandExecutionResultlessParser()
|
||||||
|
};
|
||||||
|
// 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;
|
|
||||||
|
private bool FindParser(Type t, out IOrdinaryStructureParser parser)
|
||||||
|
{
|
||||||
|
if (t == typeof(NetworkMessageHeader))
|
||||||
|
{
|
||||||
|
parser = _parsers[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t == typeof(DefaultCallRequest))
|
||||||
|
{
|
||||||
|
parser = _parsers[1];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t == typeof(FinalCommandExecution<object>))
|
||||||
|
{
|
||||||
|
parser = _parsers[2];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t == typeof(FinalCommandExecution))
|
||||||
|
{
|
||||||
|
parser = _parsers[3];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
parser = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
||||||
{
|
{
|
||||||
var sw = Stopwatch.StartNew();
|
var sw = Stopwatch.StartNew();
|
||||||
@@ -128,7 +161,7 @@ namespace mROA.Cbor
|
|||||||
|
|
||||||
public 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))
|
if (obj is not null && FindParser(obj.GetType(), out var parser))
|
||||||
{
|
{
|
||||||
parser.Write(writer, obj, context, this);
|
parser.Write(writer, obj, context, this);
|
||||||
return;
|
return;
|
||||||
@@ -251,7 +284,7 @@ namespace mROA.Cbor
|
|||||||
|
|
||||||
public 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))
|
if (type is not null && FindParser(type, out var parser))
|
||||||
{
|
{
|
||||||
return parser.Read(reader, context, this);
|
return parser.Read(reader, context, this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
||||||
{
|
{
|
||||||
var v = value as NetworkMessageHeader;
|
var v = (NetworkMessageHeader)value;
|
||||||
writer.WriteStartArray(3);
|
writer.WriteStartArray(3);
|
||||||
writer.WriteByteString(v.Id.ToByteArray());
|
writer.WriteByteString(v.Id.ToByteArray());
|
||||||
writer.WriteInt32((int)v.MessageType);
|
writer.WriteInt32((int)v.MessageType);
|
||||||
@@ -91,10 +91,10 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
||||||
{
|
{
|
||||||
var v = value as FinalCommandExecution<object>;
|
var v = (FinalCommandExecution<object>)value;
|
||||||
writer.WriteStartArray(2);
|
writer.WriteStartArray(2);
|
||||||
serialization.WriteData(v.Result, writer, context);
|
|
||||||
writer.WriteByteString(v.Id.ToByteArray());
|
writer.WriteByteString(v.Id.ToByteArray());
|
||||||
|
serialization.WriteData(v.Result, writer, context);
|
||||||
writer.WriteEndArray();
|
writer.WriteEndArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,8 +103,8 @@ namespace mROA.Cbor
|
|||||||
reader.ReadStartArray();
|
reader.ReadStartArray();
|
||||||
var result = new FinalCommandExecution<object>
|
var result = new FinalCommandExecution<object>
|
||||||
{
|
{
|
||||||
|
Id = new Guid(reader.ReadByteString()),
|
||||||
Result = serialization.ReadData(reader, typeof(object), context),
|
Result = serialization.ReadData(reader, typeof(object), context),
|
||||||
Id = new Guid(reader.ReadByteString())
|
|
||||||
};
|
};
|
||||||
reader.ReadEndArray();
|
reader.ReadEndArray();
|
||||||
return result;
|
return result;
|
||||||
@@ -115,7 +115,7 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
|
||||||
{
|
{
|
||||||
var v = value as FinalCommandExecution;
|
var v = (FinalCommandExecution)value;
|
||||||
writer.WriteStartArray(1);
|
writer.WriteStartArray(1);
|
||||||
writer.WriteByteString(v.Id.ToByteArray());
|
writer.WriteByteString(v.Id.ToByteArray());
|
||||||
writer.WriteEndArray();
|
writer.WriteEndArray();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace mROA.Implementation
|
|||||||
{
|
{
|
||||||
public class CancellationRepository : ICancellationRepository
|
public class CancellationRepository : ICancellationRepository
|
||||||
{
|
{
|
||||||
private ConcurrentDictionary<Guid, CancellationTokenSource> _cancellations = new();
|
private readonly ConcurrentDictionary<Guid, CancellationTokenSource> _cancellations = new();
|
||||||
|
|
||||||
public void RegisterCancellation(Guid id, CancellationTokenSource cts)
|
public void RegisterCancellation(Guid id, CancellationTokenSource cts)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ namespace mROA.Implementation
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await _trustedWriter.WriteAsync(new NetworkMessageHeader());
|
await _trustedWriter.WriteAsync(new NetworkMessageHeader(){Data = Array.Empty<byte>()});
|
||||||
}
|
}
|
||||||
|
|
||||||
PassReconnection();
|
PassReconnection();
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ using mROA.Abstract;
|
|||||||
|
|
||||||
namespace mROA.Implementation.CommandExecution
|
namespace mROA.Implementation.CommandExecution
|
||||||
{
|
{
|
||||||
public class FinalCommandExecution : ICommandExecution
|
public struct FinalCommandExecution : ICommandExecution
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public EMessageType MessageType => EMessageType.FinishedCommandExecution;
|
public EMessageType MessageType => EMessageType.FinishedCommandExecution;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FinalCommandExecution<T> : FinalCommandExecution
|
public struct FinalCommandExecution<T> : ICommandExecution
|
||||||
{
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public EMessageType MessageType => EMessageType.FinishedCommandExecution;
|
||||||
public T? Result { get; set; }
|
public T? Result { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ using mROA.Abstract;
|
|||||||
|
|
||||||
namespace mROA.Implementation
|
namespace mROA.Implementation
|
||||||
{
|
{
|
||||||
public class NetworkMessageHeader
|
public struct NetworkMessageHeader
|
||||||
{
|
{
|
||||||
private bool Equals(NetworkMessageHeader other)
|
private bool Equals(NetworkMessageHeader other)
|
||||||
{
|
{
|
||||||
@@ -21,12 +21,10 @@ namespace mROA.Implementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static readonly NetworkMessageHeader Null = new();
|
public static readonly NetworkMessageHeader Null = new();
|
||||||
public NetworkMessageHeader()
|
// public NetworkMessageHeader()
|
||||||
{
|
// {
|
||||||
Id = Guid.NewGuid();
|
// Data = Array.Empty<byte>();
|
||||||
MessageType = EMessageType.Unknown;
|
// }
|
||||||
Data = Array.Empty<byte>();
|
|
||||||
}
|
|
||||||
public NetworkMessageHeader(IContextualSerializationToolKit serializationToolkit,
|
public NetworkMessageHeader(IContextualSerializationToolKit serializationToolkit,
|
||||||
INetworkMessage networkMessage, IEndPointContext? context)
|
INetworkMessage networkMessage, IEndPointContext? context)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,9 +71,17 @@ namespace mROA.Implementation
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = converter.Select(i => i(message)).First(i => i != null)!;
|
|
||||||
var deserialized = _serialization.Deserialize(message.Data, type, context);
|
for (int i = 0; i < converter.Length; i++)
|
||||||
|
{
|
||||||
|
var func = converter[i];
|
||||||
|
if (func(message) is {} t)
|
||||||
|
{
|
||||||
|
var deserialized = _serialization.Deserialize(message.Data, t, context);
|
||||||
yield return (deserialized, message.MessageType)!;
|
yield return (deserialized, message.MessageType)!;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user