установка хостового идентификатора владения при создании зашаренного объекта
This commit is contained in:
@@ -37,7 +37,7 @@ var printer = factory.Create("Test");
|
||||
var name = printer.Value.GetName();
|
||||
Console.WriteLine("Printer name : {0}", name);
|
||||
|
||||
factory.Register(new SharedObject<IPrinter>(new ClientBasedPrinter()));
|
||||
// factory.Register(new SharedObject<IPrinter>(new ClientBasedPrinter()));
|
||||
|
||||
var page = await printer.Value.Print("Test Page", new CancellationToken());
|
||||
var data = page.Value.GetData();
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
public interface IOwnershipRepository
|
||||
{
|
||||
int GetOwnershipId();
|
||||
int GetHostOwnershipId();
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation.Backend;
|
||||
@@ -13,11 +11,13 @@ public class JsonSerialisationModule : ISerialisationModule
|
||||
|
||||
public void HandleIncomingRequest(int clientId, byte[] message)
|
||||
{
|
||||
var ownership = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository ?? throw new Exception("Set ownership repository type is incorrect");
|
||||
ownership.RegisterOwnership(clientId);
|
||||
NetworkMessage input = JsonSerializer.Deserialize<NetworkMessage>(message)!;
|
||||
|
||||
if (input.SchemaId == MessageType.CallRequest)
|
||||
{
|
||||
var command = JsonSerializer.Deserialize<DefaultCallRequest>(input.Data);
|
||||
var command = JsonSerializer.Deserialize<DefaultCallRequest>(input.Data)!;
|
||||
if (command.Parameter is not null)
|
||||
{
|
||||
var parameter = _methodRepository!.GetMethod(command.CommandId).GetParameters().First().ParameterType;
|
||||
@@ -37,6 +37,7 @@ public class JsonSerialisationModule : ISerialisationModule
|
||||
Data = JsonSerializer.SerializeToUtf8Bytes(response, response.GetType())
|
||||
}, clientId);
|
||||
}
|
||||
ownership.FreeOwnership();
|
||||
}
|
||||
|
||||
public void PostResponse(NetworkMessage message, int clientId)
|
||||
@@ -46,7 +47,7 @@ public class JsonSerialisationModule : ISerialisationModule
|
||||
|
||||
public void SendWelcomeMessage(int clientId)
|
||||
{
|
||||
_dataSource.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage { Data = JsonSerializer.SerializeToUtf8Bytes(new IdAssingnment { Id = clientId }), SchemaId = MessageType.IdAssigning}));
|
||||
_dataSource!.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage { Data = JsonSerializer.SerializeToUtf8Bytes(new IdAssingnment { Id = clientId }), SchemaId = MessageType.IdAssigning}));
|
||||
}
|
||||
|
||||
public void Inject<T>(T dependency)
|
||||
|
||||
@@ -11,13 +11,18 @@ public class MultiClientOwnershipRepository : IOwnershipRepository
|
||||
return _ownerships.GetValueOrDefault(Environment.CurrentManagedThreadId, 0);
|
||||
}
|
||||
|
||||
public void RegisterOwnership(int ownershipId, int threadId)
|
||||
public int GetHostOwnershipId()
|
||||
{
|
||||
_ownerships.TryAdd(ownershipId, threadId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void FreeOwnership(int ownershipId)
|
||||
public void RegisterOwnership(int ownershipId)
|
||||
{
|
||||
_ownerships.Remove(ownershipId);
|
||||
_ownerships.TryAdd(Environment.CurrentManagedThreadId, ownershipId);
|
||||
}
|
||||
|
||||
public void FreeOwnership()
|
||||
{
|
||||
_ownerships.Remove(Environment.CurrentManagedThreadId);
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,9 @@ public class StaticOwnershipRepository(int id) : IOwnershipRepository
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public int GetHostOwnershipId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ namespace mROA.Implementation;
|
||||
|
||||
public class RemoteContextRepository : IContextRepository
|
||||
{
|
||||
private ISerialisationModuleProducer _serialisationModule;
|
||||
private ISerialisationModuleProducer _producer;
|
||||
private ISerialisationModuleProducer _serialisationProducer;
|
||||
public static FrozenDictionary<Type, Type> RemoteTypes;
|
||||
public int ResisterObject(object o)
|
||||
{
|
||||
@@ -28,7 +27,7 @@ public class RemoteContextRepository : IContextRepository
|
||||
{
|
||||
if (RemoteTypes.TryGetValue(typeof(T), out var remoteType))
|
||||
{
|
||||
var remote = (T)Activator.CreateInstance(remoteType, id, _serialisationModule.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!;
|
||||
var remote = (T)Activator.CreateInstance(remoteType, id, _serialisationProducer.Produce(TransmissionConfig.OwnershipRepository!.GetOwnershipId()))!;
|
||||
return remote;
|
||||
}
|
||||
throw new NotSupportedException();
|
||||
@@ -36,7 +35,7 @@ public class RemoteContextRepository : IContextRepository
|
||||
|
||||
public object GetSingleObject(Type type)
|
||||
{
|
||||
return Activator.CreateInstance(RemoteTypes[type], -1, _serialisationModule.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!;
|
||||
return Activator.CreateInstance(RemoteTypes[type], -1, _serialisationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!;
|
||||
}
|
||||
|
||||
public int GetObjectIndex(object o)
|
||||
@@ -51,6 +50,6 @@ public class RemoteContextRepository : IContextRepository
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
if (dependency is ISerialisationModuleProducer serialisationModule)
|
||||
_serialisationModule = serialisationModule;
|
||||
_serialisationProducer = serialisationModule;
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,8 @@ public class SharedObject<T> where T : notnull
|
||||
_ownerId = ro.OwnerId;
|
||||
_contextId = ro.Id;
|
||||
}
|
||||
|
||||
_ownerId = TransmissionConfig.OwnershipRepository!.GetHostOwnershipId();
|
||||
}
|
||||
|
||||
public static implicit operator T(SharedObject<T> value) => value.Value;
|
||||
|
||||
Reference in New Issue
Block a user