еще один шаг до последнего шага

This commit is contained in:
2025-02-13 19:38:54 +03:00
parent 59c77bc9df
commit e5253138db
17 changed files with 125 additions and 31 deletions
+1
View File
@@ -6,6 +6,7 @@ public interface IInteractionModule : IInjectableModule
void RegisterSource(Stream stream);
public interface IFrontendInteractionModule : IInjectableModule
{
int ClientId { get; }
public Task<byte[]> ReceiveMessage();
public void PostMessage(byte[] message);
}
+1
View File
@@ -3,4 +3,5 @@
public interface IRemoteObject
{
public int Id { get; }
public int OwnerId { get; }
}
+1
View File
@@ -9,6 +9,7 @@ public interface ISerialisationModule : IInjectableModule
void SendWelcomeMessage(int clientId);
public interface IFrontendSerialisationModule : IInjectableModule
{
int ClientId { get; }
Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
Task<FinalCommandExecution<T>> GetFinalCommandExecution<T>(Guid requestId);
@@ -0,0 +1,8 @@
using mROA.Abstract;
namespace mROA.Implementation;
interface ISerialisationModuleProducer : IInjectableModule
{
ISerialisationModule.IFrontendSerialisationModule Produce(int ownership);
}
@@ -8,7 +8,7 @@ public class MultiClientOwnershipRepository : IOwnershipRepository
public int GetOwnershipId()
{
return _ownerships.GetValueOrDefault(Environment.CurrentManagedThreadId, -1);
return _ownerships.GetValueOrDefault(Environment.CurrentManagedThreadId, 0);
}
public void RegisterOwnership(int ownershipId, int threadId)
@@ -29,6 +29,7 @@ public class StreamBasedInteractionModule : IInteractionModule
private async Task ListenTo((int id, Stream stream) client, Action<int, byte[]> action)
{
TransmissionConfig.OwnershipRepository = new MultiClientOwnershipRepository();
const int bufferSize = ushort.MaxValue;
try
{
@@ -9,6 +9,8 @@ public class JsonFrontendSerialisationModule
{
private IInteractionModule.IFrontendInteractionModule? _interactionModule;
public int ClientId => _interactionModule!.ClientId;
public async Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution
{
if (_interactionModule is null)
@@ -6,11 +6,13 @@ public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontend
{
public Stream? ServerStream { get; set; }
public int ClientId { get; set; }
public async Task<byte[]> ReceiveMessage()
{
if (ServerStream is null)
throw new IOException("Server is not connected.");
const int bufferSize = ushort.MaxValue;
var buffer = new byte[bufferSize];
@@ -27,12 +29,12 @@ public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontend
{
if (ServerStream is null)
throw new IOException("Server is not connected.");
ServerStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort));
ServerStream.Write(message, 0, message.Length);
}
public void Inject<T>(T dependency)
{
}
}
@@ -6,7 +6,8 @@ namespace mROA.Implementation;
public class RemoteContextRepository : IContextRepository
{
private ISerialisationModule.IFrontendSerialisationModule _serialisationModule;
private ISerialisationModuleProducer _serialisationModule;
private ISerialisationModuleProducer _producer;
public static FrozenDictionary<Type, Type> RemoteTypes;
public int ResisterObject(object o)
{
@@ -27,7 +28,7 @@ public class RemoteContextRepository : IContextRepository
{
if (RemoteTypes.TryGetValue(typeof(T), out var remoteType))
{
var remote = (T)Activator.CreateInstance(remoteType, id, _serialisationModule)!;
var remote = (T)Activator.CreateInstance(remoteType, id, _serialisationModule.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!;
return remote;
}
throw new NotSupportedException();
@@ -35,7 +36,7 @@ public class RemoteContextRepository : IContextRepository
public object GetSingleObject(Type type)
{
return Activator.CreateInstance(RemoteTypes[type], -1, _serialisationModule)!;
return Activator.CreateInstance(RemoteTypes[type], -1, _serialisationModule.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!;
}
public int GetObjectIndex(object o)
@@ -49,7 +50,7 @@ public class RemoteContextRepository : IContextRepository
public void Inject<T>(T dependency)
{
if (dependency is ISerialisationModule.IFrontendSerialisationModule serialisationModule)
if (dependency is ISerialisationModuleProducer serialisationModule)
_serialisationModule = serialisationModule;
}
}
+16 -9
View File
@@ -8,7 +8,6 @@ public static class TransmissionConfig
public static IContextRepository? RealContextRepository { get; set; }
public static IContextRepository? RemoteEndpointContextRepository { get; set; }
public static IOwnershipRepository? OwnershipRepository { get; set; }
public static Dictionary<int, int> ThreadsOwners { get; } = new();
}
public class SharedObject<T> where T : notnull
@@ -41,7 +40,8 @@ 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()
@@ -52,16 +52,23 @@ public class SharedObject<T> where T : notnull
public SharedObject(T value)
{
Value = value;
_contextId = GetDefaultContextRepository().GetObjectIndex(value);
if (value is IRemoteObject ro)
{
_ownerId = ro.OwnerId;
_contextId = ro.Id;
}
else
{
_contextId = TransmissionConfig.RealContextRepository!.GetObjectIndex(value);
_ownerId = TransmissionConfig.OwnershipRepository!.GetOwnershipId();
}
}
public static implicit operator T(SharedObject<T> value) => value.Value;
public static implicit operator SharedObject<T>(T value) =>
new()
{
ContextId = value is IRemoteObject ro
? TransmissionConfig.RemoteEndpointContextRepository!.GetObjectIndex(ro)
: TransmissionConfig.RealContextRepository!.GetObjectIndex(value)
};
new(value);
}
@@ -0,0 +1,19 @@
using mROA.Abstract;
namespace mROA.Implementation;
public class StaticSerialisationModuleProducer : ISerialisationModuleProducer
{
private ISerialisationModule.IFrontendSerialisationModule _serialisationModule;
public ISerialisationModule.IFrontendSerialisationModule Produce(int ownership)
{
return _serialisationModule;
}
public void Inject<T>(T dependency)
{
if (dependency is ISerialisationModule.IFrontendSerialisationModule serialisationModule)
_serialisationModule = serialisationModule;
}
}