From af65f22aa9bc7019326de20735982164dd96cf40 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 1 Mar 2025 15:54:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=B0=20Universal?= =?UTF-8?q?ObjectIdentifier.=20=D0=A7=D0=B0=D1=81=D1=82=D1=8C=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Backend/Example.Backend.csproj | 2 +- Example.Frontend/Example.Frontend.csproj | 2 +- mROA.Cbor/CborSerializationToolkit.cs | 2 ++ mROA.Test/UnSOization.cs | 26 +++++++++++++++ mROA/Implementation/RemoteObjectBase.cs | 16 ++++----- mROA/Implementation/SharedObject.cs | 41 ++++++++++++++++++++++++ mROA/mROA.csproj | 2 +- 7 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 mROA.Test/UnSOization.cs diff --git a/Example.Backend/Example.Backend.csproj b/Example.Backend/Example.Backend.csproj index d29fff5..06f0ed2 100644 --- a/Example.Backend/Example.Backend.csproj +++ b/Example.Backend/Example.Backend.csproj @@ -15,7 +15,7 @@ - TRACE; + diff --git a/Example.Frontend/Example.Frontend.csproj b/Example.Frontend/Example.Frontend.csproj index a12fa05..8ad6b28 100644 --- a/Example.Frontend/Example.Frontend.csproj +++ b/Example.Frontend/Example.Frontend.csproj @@ -14,7 +14,7 @@ - TRACE; + diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index 3b7da43..6c50427 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -12,6 +12,7 @@ namespace mROA.Cbor { public class CborSerializationToolkit : IContextualSerializationToolKit { + private List _remoteTypes; public byte[] Serialize(object objectToSerialize, IEndPointContext context) { var writer = new CborWriter(); @@ -352,6 +353,7 @@ namespace mROA.Cbor public void Inject(T dependency) { + _remoteTypes = RemoteContextRepository.RemoteTypes.Keys.ToList(); } public byte[] Serialize(T objectToSerialize) diff --git a/mROA.Test/UnSOization.cs b/mROA.Test/UnSOization.cs new file mode 100644 index 0000000..afdcb9e --- /dev/null +++ b/mROA.Test/UnSOization.cs @@ -0,0 +1,26 @@ +using mROA.Implementation; + +namespace mROA.Test; + +public class UnSOization +{ + private UniversalObjectIdentifier _uoi; + + [SetUp] + public void Setup() + { + _uoi = new UniversalObjectIdentifier + { + ContextId = -123, OwnerId = 123 + }; + } + + [Test] + public void FlatTest() + { + var flat = _uoi.Flat; + var next = new UniversalObjectIdentifier { Flat = flat }; + + Assert.That(_uoi, Is.EqualTo(next)); + } +} \ No newline at end of file diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index e1381dd..535d5ba 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -10,23 +10,23 @@ namespace mROA.Implementation { public abstract class RemoteObjectBase : IDisposable { - private readonly int _id; + private readonly UniversalObjectIdentifier _identifier; private readonly IRepresentationModule _representationModule; protected RemoteObjectBase(int id, IRepresentationModule representationModule) { - _id = id; + _identifier = new UniversalObjectIdentifier { ContextId = id, OwnerId = representationModule.Id }; _representationModule = representationModule; } - public int Id => _id; - public int OwnerId => _representationModule.Id; + public int Id => _identifier.ContextId; + public int OwnerId => _identifier.OwnerId; protected async Task GetResultAsync(int methodId, object? parameter = default, CancellationToken cancellationToken = default) { var request = new DefaultCallRequest - { CommandId = methodId, ObjectId = _id, Parameter = parameter + { CommandId = methodId, ObjectId = _identifier.ContextId, Parameter = parameter }; await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); @@ -80,7 +80,7 @@ namespace mROA.Implementation CancellationToken cancellationToken = default) { var request = new DefaultCallRequest - { CommandId = methodId, ObjectId = _id, Parameter = parameter + { CommandId = methodId, ObjectId = _identifier.ContextId, Parameter = parameter }; await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); @@ -124,14 +124,14 @@ namespace mROA.Implementation public void Dispose() { - if (_id == -1) + if (_identifier.IsStatic) return; CallAsync(-1).Wait(); } public override string ToString() { - return $"{{Id : {_id}, OwnerId : {OwnerId} }}"; + return _identifier.ToString(); } } } \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index af7847e..67c97d9 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -121,4 +121,45 @@ namespace mROA.Implementation public static implicit operator SharedObject(T value) => new(value); } + + public struct UniversalObjectIdentifier : IEquatable + { + public int ContextId; + public int OwnerId; + + public override string ToString() + { + return $"{nameof(ContextId)}: {ContextId}, {nameof(OwnerId)}: {OwnerId}"; + } + + public bool IsStatic => ContextId == -1; + + public ulong Flat + { + get + { + return (ulong)OwnerId << 32 | (uint)ContextId; + } + set + { + OwnerId = (int)(value >> 32); + ContextId = (int)(value & 0xFFFFFFFF); + } + } + + public bool Equals(UniversalObjectIdentifier other) + { + return ContextId == other.ContextId && OwnerId == other.OwnerId; + } + + public override bool Equals(object? obj) + { + return obj is UniversalObjectIdentifier other && Equals(other); + } + + public override int GetHashCode() + { + return HashCode.Combine(ContextId, OwnerId); + } + } } \ No newline at end of file diff --git a/mROA/mROA.csproj b/mROA/mROA.csproj index 5702597..e9d980c 100644 --- a/mROA/mROA.csproj +++ b/mROA/mROA.csproj @@ -21,7 +21,7 @@ - TRACE; +