репозитории обладания реализованы

This commit is contained in:
2025-02-13 15:41:44 +03:00
parent cd184b9060
commit 3fb0a525fa
6 changed files with 47 additions and 9 deletions
+1 -2
View File
@@ -18,8 +18,7 @@ mixer.Modules.Add(new JsonFrontendSerialisationModule());
mixer.Modules.Add(new StreamBasedFrontendInteractionModule()); mixer.Modules.Add(new StreamBasedFrontendInteractionModule());
mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567)));
mixer.Build(); mixer.Build();
Thread.CurrentThread.ManagedThreadId
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>(); TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>(); TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>();
mixer.GetModule<NetworkFrontendBridge>().Connect(); mixer.GetModule<NetworkFrontendBridge>().Connect();
+6
View File
@@ -0,0 +1,6 @@
namespace mROA.Abstract;
public interface IOwnershipRepository
{
int GetOwnershipId();
}
@@ -0,0 +1,23 @@
using mROA.Abstract;
namespace mROA.Implementation.Backend;
public class MultiClientOwnershipRepository : IOwnershipRepository
{
private Dictionary<int, int> _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);
}
}
@@ -33,6 +33,6 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge
throw new Exception($"Incorrect message type. Must be IdAssigning, current : {message.SchemaId.ToString()}"); throw new Exception($"Incorrect message type. Must be IdAssigning, current : {message.SchemaId.ToString()}");
} }
TransmissionConfig.ProcessOwnerId = JsonSerializer.Deserialize<IdAssingnment>(message.Data)!.Id; TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(JsonSerializer.Deserialize<IdAssingnment>(message.Data)!.Id);
} }
} }
@@ -0,0 +1,11 @@
using mROA.Abstract;
namespace mROA.Implementation.Frontend;
public class StaticOwnershipRepository(int id) : IOwnershipRepository
{
public int GetOwnershipId()
{
return id;
}
}
+5 -6
View File
@@ -7,25 +7,26 @@ public static class TransmissionConfig
{ {
public static IContextRepository? RealContextRepository { get; set; } public static IContextRepository? RealContextRepository { get; set; }
public static IContextRepository? RemoteEndpointContextRepository { get; set; } public static IContextRepository? RemoteEndpointContextRepository { get; set; }
public static int ProcessOwnerId { get; set; } public static IOwnershipRepository? OwnershipRepository { get; set; }
public static Dictionary<int, int> ThreadsOwners { get; } = new(); public static Dictionary<int, int> ThreadsOwners { get; } = new();
} }
public class SharedObject<T> where T : notnull public class SharedObject<T> where T : notnull
{ {
private IContextRepository GetDefaultContextRepository() => private IContextRepository GetDefaultContextRepository() =>
(OwnerId == TransmissionConfig.ProcessOwnerId (OwnerId == TransmissionConfig.OwnershipRepository!.GetOwnershipId()
? TransmissionConfig.RealContextRepository ? TransmissionConfig.RealContextRepository
: TransmissionConfig.RemoteEndpointContextRepository) ?? : TransmissionConfig.RemoteEndpointContextRepository) ??
throw new NullReferenceException( throw new NullReferenceException(
"DefaultContextRepository was not defined"); "DefaultContextRepository was not defined");
private int _contextId = -1; private int _contextId = -1;
public int OwnerId { get; } public int OwnerId => TransmissionConfig.OwnershipRepository!.GetOwnershipId();
// ReSharper disable once MemberCanBePrivate.Global // ReSharper disable once MemberCanBePrivate.Global
public int ContextId public int ContextId
{ {
// ReSharper disable once UnusedMember.Global
get => _contextId; get => _contextId;
init init
{ {
@@ -36,6 +37,7 @@ public class SharedObject<T> where T : notnull
[JsonIgnore] public T Value { get; private set; } [JsonIgnore] public T Value { get; private set; }
// ReSharper disable once MemberCanBePrivate.Global
public SharedObject() public SharedObject()
{ {
} }
@@ -45,9 +47,6 @@ public class SharedObject<T> where T : notnull
{ {
Value = value; Value = value;
_contextId = GetDefaultContextRepository().GetObjectIndex(value); _contextId = GetDefaultContextRepository().GetObjectIndex(value);
OwnerId = TransmissionConfig.ThreadsOwners.TryGetValue(Environment.CurrentManagedThreadId, out var ownerId)
? ownerId
: TransmissionConfig.ProcessOwnerId;
} }
public static implicit operator T(SharedObject<T> value) => value.Value; public static implicit operator T(SharedObject<T> value) => value.Value;