репозитории обладания реализованы
This commit is contained in:
@@ -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<RemoteContextRepository>();
|
||||
TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>();
|
||||
mixer.GetModule<NetworkFrontendBridge>().Connect();
|
||||
|
||||
@@ -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()}");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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<int, int> ThreadsOwners { get; } = new();
|
||||
}
|
||||
|
||||
public class SharedObject<T> 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<T> where T : notnull
|
||||
|
||||
[JsonIgnore] public T Value { get; private set; }
|
||||
|
||||
// ReSharper disable once MemberCanBePrivate.Global
|
||||
public SharedObject()
|
||||
{
|
||||
}
|
||||
@@ -45,9 +47,6 @@ public class SharedObject<T> 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<T> value) => value.Value;
|
||||
|
||||
Reference in New Issue
Block a user