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

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
+13 -1
View File
@@ -8,8 +8,20 @@ namespace Example.Backend;
[SharedObjectSingleton]
public class PrinterFactory : IPrinterFactory
{
private List<IPrinter> _printers = new();
public SharedObject<IPrinter> Create(string printerName)
{
return new Printer {Name = printerName};
return new Printer { Name = printerName };
}
public void Register(SharedObject<IPrinter> printer)
{
_printers.Add(printer.Value);
}
public SharedObject<IPrinter> GetPrinterByName(string printerName)
{
return new SharedObject<IPrinter>(_printers.Find(i => i.GetName() == printerName)!);
}
}
+11 -9
View File
@@ -7,16 +7,18 @@ using mROA.Implementation.Backend;
using mROA.Implementation.Bootstrap;
var bootstrap = new FullMixBuilder();
bootstrap.UseJsonSerialisation();
bootstrap.UseNetworkGateway(new IPEndPoint(IPAddress.Loopback, 4567));
bootstrap.UseStreamInteraction();
bootstrap.UseBasicExecution();
bootstrap.UseCollectableContextRepository(typeof(PrinterFactory).Assembly);
bootstrap.SetupMethodsRepository(new CoCodegenMethodRepository());
bootstrap.Build();
var builder = new FullMixBuilder();
builder.UseJsonSerialisation();
builder.UseNetworkGateway(new IPEndPoint(IPAddress.Loopback, 4567));
builder.UseStreamInteraction();
builder.UseBasicExecution();
builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly);
builder.SetupMethodsRepository(new CoCodegenMethodRepository());
builder.Modules.Add(new StaticSerialisationModuleProducer());
builder.Build();
var gateway = bootstrap.GetModule<IGatewayModule>() ;
var gateway = builder.GetModule<IGatewayModule>() ;
gateway.Run();
+27
View File
@@ -0,0 +1,27 @@
using Example.Shared;
using mROA.Implementation;
namespace Example.Frontend;
public class ClientBasedPrinter : IPrinter
{
public string GetName()
{
return "ClientBasedPrinter";
}
public async Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken)
{
Console.WriteLine($"Printed: {text}");
await Task.Yield();
return new ClientBasedPage();
}
}
public class ClientBasedPage : IPage
{
public byte[] GetData()
{
return [1, 2, 3];
}
}
+11 -4
View File
@@ -4,9 +4,11 @@
using System.Diagnostics;
using System.Net;
using System.Text;
using Example.Frontend;
using Example.Shared;
using mROA.Codegen;
using mROA.Implementation;
using mROA.Implementation.Backend;
using mROA.Implementation.Bootstrap;
using mROA.Implementation.Frontend;
@@ -17,22 +19,26 @@ mixer.Modules.Add(new RemoteContextRepository());
mixer.Modules.Add(new JsonFrontendSerialisationModule());
mixer.Modules.Add(new StreamBasedFrontendInteractionModule());
mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567)));
mixer.Modules.Add(new StaticSerialisationModuleProducer());
mixer.UseCollectableContextRepository();
mixer.Build();
Thread.CurrentThread.ManagedThreadId
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
TransmissionConfig.RealContextRepository = mixer.GetModule<ContextRepository>();
TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>();
mixer.GetModule<NetworkFrontendBridge>().Connect();
Console.WriteLine(TransmissionConfig.ProcessOwnerId);
Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId());
var context = mixer.GetModule<RemoteContextRepository>();
var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory;
var printer = factory.Create("Test");
var name = printer.Value.GetName();
Console.WriteLine("Printer name : {0}", name);
factory.Register(new SharedObject<IPrinter>(new ClientBasedPrinter()));
var page = await printer.Value.Print("Test Page", new CancellationToken());
var data = page.Value.GetData();
Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data));
@@ -46,6 +52,7 @@ for (int i = 0; i < iterations; i++)
{
x = loadSingleton.Next(x);
}
timer.Stop();
Console.WriteLine("X is {0}", x);
Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds);
+2
View File
@@ -8,5 +8,7 @@ namespace Example.Shared;
public interface IPrinterFactory
{
SharedObject<IPrinter> Create(string printerName);
void Register(SharedObject<IPrinter> printer);
SharedObject<IPrinter> GetPrinterByName(string printerName);
}
+1
View File
@@ -187,6 +187,7 @@ namespace {namespaceName};
partial class {className} (int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule) : {originalName}, IRemoteObject
{{
public int Id => id;
public int OwnerId => serialisationModule.ClientId;
{string.Join("\r\n\t", methodsText)}
}}
+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,6 +6,8 @@ public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontend
{
public Stream? ServerStream { get; set; }
public int ClientId { get; set; }
public async Task<byte[]> ReceiveMessage()
{
if (ServerStream is null)
@@ -31,8 +33,8 @@ public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontend
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;
}
}