прокидывание айдишников

This commit is contained in:
2025-02-13 10:52:06 +03:00
parent 6b794e1cd9
commit cd184b9060
15 changed files with 102 additions and 57 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ public class Printer : IPrinter
return Name; return Name;
} }
public async Task<TransmittedSharedObject<IPage>> Print(string text, CancellationToken cancellationToken = default) public async Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken = default)
{ {
// throw new Exception("The method or operation is not implemented."); // throw new Exception("The method or operation is not implemented.");
return new Page {Text = text}; return new Page {Text = text};
+1 -1
View File
@@ -8,7 +8,7 @@ namespace Example.Backend;
[SharedObjectSingleton] [SharedObjectSingleton]
public class PrinterFactory : IPrinterFactory public class PrinterFactory : IPrinterFactory
{ {
public TransmittedSharedObject<IPrinter> Create(string printerName) public SharedObject<IPrinter> Create(string printerName)
{ {
return new Printer {Name = printerName}; return new Printer {Name = printerName};
} }
+4 -1
View File
@@ -19,9 +19,12 @@ 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();
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>();
mixer.GetModule<NetworkFrontendBridge>().Connect(); mixer.GetModule<NetworkFrontendBridge>().Connect();
Console.WriteLine(TransmissionConfig.ProcessOwnerId);
var context = mixer.GetModule<RemoteContextRepository>(); var context = mixer.GetModule<RemoteContextRepository>();
var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory;
+1 -1
View File
@@ -7,6 +7,6 @@ namespace Example.Shared;
public interface IPrinter public interface IPrinter
{ {
string GetName(); string GetName();
Task<TransmittedSharedObject<IPage>> Print(string text, CancellationToken cancellationToken); Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken);
} }
+1 -1
View File
@@ -7,6 +7,6 @@ namespace Example.Shared;
[SharedObjectInterface] [SharedObjectInterface]
public interface IPrinterFactory public interface IPrinterFactory
{ {
TransmittedSharedObject<IPrinter> Create(string printerName); SharedObject<IPrinter> Create(string printerName);
} }
+1
View File
@@ -6,6 +6,7 @@ public interface ISerialisationModule : IInjectableModule
{ {
void HandleIncomingRequest(int clientId, byte[] message); void HandleIncomingRequest(int clientId, byte[] message);
void PostResponse(NetworkMessage message, int clientId); void PostResponse(NetworkMessage message, int clientId);
void SendWelcomeMessage(int clientId);
public interface IFrontendSerialisationModule : IInjectableModule public interface IFrontendSerialisationModule : IInjectableModule
{ {
Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution; Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
@@ -44,6 +44,11 @@ public class JsonSerialisationModule : ISerialisationModule
_dataSource!.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(message)); _dataSource!.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(message));
} }
public void SendWelcomeMessage(int clientId)
{
_dataSource.SendTo(clientId, JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage { Data = JsonSerializer.SerializeToUtf8Bytes(new IdAssingnment { Id = clientId }), SchemaId = MessageType.IdAssigning}));
}
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
{ {
if (dependency is IInteractionModule interactionModule) if (dependency is IInteractionModule interactionModule)
@@ -4,6 +4,7 @@ namespace mROA.Implementation.Backend;
public class StreamBasedInteractionModule : IInteractionModule public class StreamBasedInteractionModule : IInteractionModule
{ {
private ISerialisationModule _serialisationModule;
private readonly Dictionary<int, Stream> _streams = new(); private readonly Dictionary<int, Stream> _streams = new();
private Action<int, byte[]>? _handler; private Action<int, byte[]>? _handler;
@@ -12,6 +13,7 @@ public class StreamBasedInteractionModule : IInteractionModule
var id = Random.Shared.Next(); var id = Random.Shared.Next();
_streams.Add(id, stream); _streams.Add(id, stream);
_ = ListenTo((id, stream), _handler!); _ = ListenTo((id, stream), _handler!);
_serialisationModule.SendWelcomeMessage(id);
} }
public void SendTo(int clientId, byte[] message) public void SendTo(int clientId, byte[] message)
@@ -51,6 +53,7 @@ public class StreamBasedInteractionModule : IInteractionModule
if (dependency is ISerialisationModule serialisationModule) if (dependency is ISerialisationModule serialisationModule)
{ {
_handler = serialisationModule.HandleIncomingRequest; _handler = serialisationModule.HandleIncomingRequest;
_serialisationModule = serialisationModule;
} }
} }
@@ -21,4 +21,9 @@ public class JsonFrontendCallbackSerializationModule : ISerialisationModule
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void SendWelcomeMessage(int clientId)
{
throw new NotImplementedException();
}
} }
@@ -1,5 +1,7 @@
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using mROA.Abstract; using mROA.Abstract;
namespace mROA.Implementation.Frontend; namespace mROA.Implementation.Frontend;
@@ -24,5 +26,13 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge
_tcpClient.Connect(ipEndPoint); _tcpClient.Connect(ipEndPoint);
_interactionModule.ServerStream = _tcpClient.GetStream(); _interactionModule.ServerStream = _tcpClient.GetStream();
var welcomeMessage = _interactionModule.ReceiveMessage().GetAwaiter().GetResult();
var message = JsonSerializer.Deserialize<NetworkMessage>(welcomeMessage);
if (message.SchemaId != MessageType.IdAssigning)
{
throw new Exception($"Incorrect message type. Must be IdAssigning, current : {message.SchemaId.ToString()}");
}
TransmissionConfig.ProcessOwnerId = JsonSerializer.Deserialize<IdAssingnment>(message.Data)!.Id;
} }
} }
+6
View File
@@ -0,0 +1,6 @@
namespace mROA.Implementation;
public class IdAssingnment
{
public int Id { get; set; }
}
+1 -2
View File
@@ -38,7 +38,6 @@ public class MethodRepository : IMethodRepository
} }
public void Inject<T>(T dependency) public void Inject<T>(T dependency)
{ {
}
} }
}
+1 -1
View File
@@ -11,5 +11,5 @@ public class NetworkMessage
public enum MessageType public enum MessageType
{ {
Unknown, FinishedCommandExecution, ErrorCommandExecution, AcyncCancelCommandExecution, CallRequest Unknown, FinishedCommandExecution, ErrorCommandExecution, AcyncCancelCommandExecution, CallRequest, IdAssigning
} }
+62
View File
@@ -0,0 +1,62 @@
using System.Text.Json.Serialization;
using mROA.Abstract;
namespace mROA.Implementation;
public static class TransmissionConfig
{
public static IContextRepository? RealContextRepository { get; set; }
public static IContextRepository? RemoteEndpointContextRepository { get; set; }
public static int ProcessOwnerId { get; set; }
public static Dictionary<int, int> ThreadsOwners { get; } = new();
}
public class SharedObject<T> where T : notnull
{
private IContextRepository GetDefaultContextRepository() =>
(OwnerId == TransmissionConfig.ProcessOwnerId
? TransmissionConfig.RealContextRepository
: TransmissionConfig.RemoteEndpointContextRepository) ??
throw new NullReferenceException(
"DefaultContextRepository was not defined");
private int _contextId = -1;
public int OwnerId { get; }
// ReSharper disable once MemberCanBePrivate.Global
public int ContextId
{
get => _contextId;
init
{
_contextId = value;
Value = GetDefaultContextRepository().GetObject<T>(_contextId)!;
}
}
[JsonIgnore] public T Value { get; private set; }
public SharedObject()
{
}
// ReSharper disable once UnusedMember.Global
public SharedObject(T value)
{
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;
public static implicit operator SharedObject<T>(T value) =>
new()
{
ContextId = value is IRemoteObject ro
? TransmissionConfig.RemoteEndpointContextRepository!.GetObjectIndex(ro)
: TransmissionConfig.RealContextRepository!.GetObjectIndex(value)
};
}
@@ -1,49 +0,0 @@
using System.Text.Json.Serialization;
using mROA.Abstract;
namespace mROA.Implementation;
public static class TransmissionConfig
{
public static IContextRepository? RealContextRepository { get; set; }
public static IContextRepository? RemoteEndpointContextRepository { get; set; }
public static int ProcessOwnerId { get; set; }
}
public class TransmittedSharedObject<T> where T : notnull
{
private IContextRepository GetDefaultContextRepository() => (OwnerId == TransmissionConfig.ProcessOwnerId ? TransmissionConfig.RealContextRepository : TransmissionConfig.RemoteEndpointContextRepository) ??
throw new NullReferenceException(
"DefaultContextRepository was not defined");
private int _contextId = -1;
public int OwnerId { get; init; }
// ReSharper disable once MemberCanBePrivate.Global
public int ContextId
{
get => _contextId;
init
{
_contextId = value;
Value = GetDefaultContextRepository().GetObject<T>(_contextId)!;
}
}
[JsonIgnore]
public T Value { get; private set; }
public TransmittedSharedObject()
{
}
// ReSharper disable once UnusedMember.Global
public TransmittedSharedObject(T value)
{
Value = value;
_contextId = GetDefaultContextRepository().GetObjectIndex(value);
}
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value;
public static implicit operator TransmittedSharedObject<T>(T value) =>
new() { ContextId = value is IRemoteObject ro ? TransmissionConfig.RemoteEndpointContextRepository!.GetObjectIndex(ro) : TransmissionConfig.RealContextRepository!.GetObjectIndex(value) };
}