From 3fb0a525fa767b6c98d8847aa672552125ca39aa Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 13 Feb 2025 15:41:44 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=B8=20=D0=BE=D0=B1=D0=BB=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Frontend/Program.cs | 3 +-- mROA/Abstract/IOwnershipRepository.cs | 6 +++++ .../Backend/MultiClientOwnershipRepository.cs | 23 +++++++++++++++++++ .../Frontend/NetworkFrontendBridge.cs | 2 +- .../Frontend/StaticOwnershipRepository.cs | 11 +++++++++ mROA/Implementation/SharedObject.cs | 11 ++++----- 6 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 mROA/Abstract/IOwnershipRepository.cs create mode 100644 mROA/Implementation/Backend/MultiClientOwnershipRepository.cs create mode 100644 mROA/Implementation/Frontend/StaticOwnershipRepository.cs diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 02b758f..48d04c0 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -18,8 +18,7 @@ mixer.Modules.Add(new JsonFrontendSerialisationModule()); mixer.Modules.Add(new StreamBasedFrontendInteractionModule()); mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); mixer.Build(); - - +Thread.CurrentThread.ManagedThreadId TransmissionConfig.RealContextRepository = mixer.GetModule(); TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule(); mixer.GetModule().Connect(); diff --git a/mROA/Abstract/IOwnershipRepository.cs b/mROA/Abstract/IOwnershipRepository.cs new file mode 100644 index 0000000..81735af --- /dev/null +++ b/mROA/Abstract/IOwnershipRepository.cs @@ -0,0 +1,6 @@ +namespace mROA.Abstract; + +public interface IOwnershipRepository +{ + int GetOwnershipId(); +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs b/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs new file mode 100644 index 0000000..aea7194 --- /dev/null +++ b/mROA/Implementation/Backend/MultiClientOwnershipRepository.cs @@ -0,0 +1,23 @@ +using mROA.Abstract; + +namespace mROA.Implementation.Backend; + +public class MultiClientOwnershipRepository : IOwnershipRepository +{ + private Dictionary _ownerships = new(); + + public int GetOwnershipId() + { + return _ownerships.GetValueOrDefault(Environment.CurrentManagedThreadId, -1); + } + + public void RegisterOwnership(int ownershipId, int threadId) + { + _ownerships.TryAdd(ownershipId, threadId); + } + + public void FreeOwnership(int ownershipId) + { + _ownerships.Remove(ownershipId); + } +} \ No newline at end of file diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index a77c147..ccf42cb 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -33,6 +33,6 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge throw new Exception($"Incorrect message type. Must be IdAssigning, current : {message.SchemaId.ToString()}"); } - TransmissionConfig.ProcessOwnerId = JsonSerializer.Deserialize(message.Data)!.Id; + TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(JsonSerializer.Deserialize(message.Data)!.Id); } } \ No newline at end of file diff --git a/mROA/Implementation/Frontend/StaticOwnershipRepository.cs b/mROA/Implementation/Frontend/StaticOwnershipRepository.cs new file mode 100644 index 0000000..2963087 --- /dev/null +++ b/mROA/Implementation/Frontend/StaticOwnershipRepository.cs @@ -0,0 +1,11 @@ +using mROA.Abstract; + +namespace mROA.Implementation.Frontend; + +public class StaticOwnershipRepository(int id) : IOwnershipRepository +{ + public int GetOwnershipId() + { + return id; + } +} \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index 931e5b2..ed218d3 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -7,25 +7,26 @@ public static class TransmissionConfig { public static IContextRepository? RealContextRepository { get; set; } public static IContextRepository? RemoteEndpointContextRepository { get; set; } - public static int ProcessOwnerId { get; set; } + public static IOwnershipRepository? OwnershipRepository { get; set; } public static Dictionary ThreadsOwners { get; } = new(); } public class SharedObject where T : notnull { private IContextRepository GetDefaultContextRepository() => - (OwnerId == TransmissionConfig.ProcessOwnerId + (OwnerId == TransmissionConfig.OwnershipRepository!.GetOwnershipId() ? TransmissionConfig.RealContextRepository : TransmissionConfig.RemoteEndpointContextRepository) ?? throw new NullReferenceException( "DefaultContextRepository was not defined"); private int _contextId = -1; - public int OwnerId { get; } + public int OwnerId => TransmissionConfig.OwnershipRepository!.GetOwnershipId(); // ReSharper disable once MemberCanBePrivate.Global public int ContextId { + // ReSharper disable once UnusedMember.Global get => _contextId; init { @@ -36,6 +37,7 @@ public class SharedObject where T : notnull [JsonIgnore] public T Value { get; private set; } + // ReSharper disable once MemberCanBePrivate.Global public SharedObject() { } @@ -45,9 +47,6 @@ public class SharedObject where T : notnull { Value = value; _contextId = GetDefaultContextRepository().GetObjectIndex(value); - OwnerId = TransmissionConfig.ThreadsOwners.TryGetValue(Environment.CurrentManagedThreadId, out var ownerId) - ? ownerId - : TransmissionConfig.ProcessOwnerId; } public static implicit operator T(SharedObject value) => value.Value;