diff --git a/mROA.Cbor/CborSerializaitonToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs similarity index 94% rename from mROA.Cbor/CborSerializaitonToolkit.cs rename to mROA.Cbor/CborSerializationToolkit.cs index 3b8a171..f950fff 100644 --- a/mROA.Cbor/CborSerializaitonToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -11,7 +11,7 @@ using mROA.Implementation.Attributes; namespace mROA.Cbor { - public class CborSerializaitonToolkit : IContextualSerializationToolKit + public class CborSerializationToolkit : IContextualSerializationToolKit { public byte[] Serialize(object objectToSerialize, IEndPointContext context) { @@ -122,9 +122,6 @@ namespace mROA.Cbor private void WriteList(IList list, CborWriter writer, IEndPointContext context) { - - - writer.WriteStartArray(list.Count); foreach (var element in list) @@ -193,10 +190,10 @@ namespace mROA.Cbor if (type.IsSubclassOf(typeof(ISharedObject))) return ReadSharedObject(reader, type, context); - - if (type.IsSubclassOf(typeof(IList)) || type.IsArray) + + if (typeof(IList).IsAssignableFrom(type) || type.IsArray) return ReadList(reader, type, context); - + return ReadObject(reader, type, context); case CborReaderState.StartMap: @@ -216,21 +213,27 @@ namespace mROA.Cbor var length = reader.ReadStartArray(); if (length != null) { - var values = new object[length.Value]; - - Type elementType = typeof(object); + var elementType = typeof(object); if (type is { IsArray: true }) elementType = type.GetElementType(); + else if (typeof(IList).IsAssignableFrom(type)) + elementType = type.GetGenericArguments()[0]; + + Array values = Array.CreateInstance(elementType, length.Value); for (int i = 0; i < length; i++) { - values[i] = ReadData(reader, elementType, context); + values.SetValue(ReadData(reader, elementType, context), i); } + + reader.ReadEndArray(); return values; } + reader.ReadEndArray(); + return Array.Empty(); } diff --git a/mROA.Test/CborTest.cs b/mROA.Test/CborTest.cs index 0b8a68e..a6d1bf2 100644 --- a/mROA.Test/CborTest.cs +++ b/mROA.Test/CborTest.cs @@ -14,7 +14,9 @@ public class CborTest [SetUp] public void Setup() { - _complexTestObject = new () + _basicCollectionElement = new() { A = 567565, B = "test text", C = 2.781f }; + + _complexTestObject = new ComplexTestObject { IntValue = 123, DoubleValue = 3.14159, @@ -26,8 +28,7 @@ public class CborTest ], IntArray = [1, 4, 8, 16, 87] }; - _basicCollectionElement = new() { A = 567565, B = "test text", C = 2.781f }; - _serializationToolKit = new CborSerializaitonToolkit(); + _serializationToolKit = new CborSerializationToolkit(); } [Test] @@ -69,7 +70,7 @@ public class CborTest public double DoubleValue { get; set; } public string StringValue { get; set; } public int[] IntArray { get; set; } - public BasicCollectionElement[] CollectionElements { get; set; } + public List CollectionElements { get; set; } protected bool Equals(ComplexTestObject other) { diff --git a/mROA/Abstract/IEndPointContext.cs b/mROA/Abstract/IEndPointContext.cs index dbaf7d6..5d6e4c6 100644 --- a/mROA/Abstract/IEndPointContext.cs +++ b/mROA/Abstract/IEndPointContext.cs @@ -5,5 +5,6 @@ IContextRepository RealRepository { get; } IContextRepository RemoteRepository { get; } int HostId { get; } + int OwnerId { get; } } } \ No newline at end of file diff --git a/mROA/Implementation/EndPointContext.cs b/mROA/Implementation/EndPointContext.cs new file mode 100644 index 0000000..7ceb5df --- /dev/null +++ b/mROA/Implementation/EndPointContext.cs @@ -0,0 +1,20 @@ +using System; +using mROA.Abstract; + +namespace mROA.Implementation +{ + public class EndPointContext : IEndPointContext + { + public Func OwnerFunc; + public IContextRepository RealRepository { get; set; } + public IContextRepository RemoteRepository { get; set; } + public int HostId { get; set; } + public int OwnerId + { + get => OwnerFunc(); + set + { + OwnerFunc = () => value; + } } + } +} \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index 17f6d53..f99d420 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -1,6 +1,7 @@ using System; using System.Text.Json.Serialization; using mROA.Abstract; + // ReSharper disable UnusedMember.Global #pragma warning disable CS8618, CS9264 @@ -20,7 +21,8 @@ namespace mROA.Implementation public static IContextRepository RemoteEndpointContextRepository { - get => _remoteEndpointContextRepository ?? throw new NullReferenceException("RemoteEndpointContextRepository is null"); + get => _remoteEndpointContextRepository ?? + throw new NullReferenceException("RemoteEndpointContextRepository is null"); set => _remoteEndpointContextRepository = value; } @@ -29,14 +31,13 @@ namespace mROA.Implementation get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null"); set => _ownershipRepository = value; } - } public interface ISharedObject { IEndPointContext EndPointContext { get; set; } } - + public class SharedObject : ISharedObject where T : notnull { private IContextRepository GetDefaultContextRepository() => @@ -105,6 +106,12 @@ namespace mROA.Implementation public static implicit operator SharedObject(T value) => new(value); - public IEndPointContext EndPointContext { get; set; } + public IEndPointContext EndPointContext { get; set; } = new EndPointContext + { + RealRepository = TransmissionConfig.RealContextRepository, + RemoteRepository = TransmissionConfig.RemoteEndpointContextRepository, + HostId = TransmissionConfig.OwnershipRepository.GetHostOwnershipId(), + OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId + }; } } \ No newline at end of file