прокидывание айдишников
This commit is contained in:
@@ -12,7 +12,7 @@ public class Printer : IPrinter
|
||||
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.");
|
||||
return new Page {Text = text};
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Example.Backend;
|
||||
[SharedObjectSingleton]
|
||||
public class PrinterFactory : IPrinterFactory
|
||||
{
|
||||
public TransmittedSharedObject<IPrinter> Create(string printerName)
|
||||
public SharedObject<IPrinter> Create(string printerName)
|
||||
{
|
||||
return new Printer {Name = printerName};
|
||||
}
|
||||
|
||||
@@ -19,9 +19,12 @@ mixer.Modules.Add(new StreamBasedFrontendInteractionModule());
|
||||
mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567)));
|
||||
mixer.Build();
|
||||
|
||||
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
|
||||
|
||||
TransmissionConfig.RealContextRepository = mixer.GetModule<RemoteContextRepository>();
|
||||
TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule<RemoteContextRepository>();
|
||||
mixer.GetModule<NetworkFrontendBridge>().Connect();
|
||||
|
||||
Console.WriteLine(TransmissionConfig.ProcessOwnerId);
|
||||
var context = mixer.GetModule<RemoteContextRepository>();
|
||||
|
||||
var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory;
|
||||
|
||||
@@ -7,6 +7,6 @@ namespace Example.Shared;
|
||||
public interface IPrinter
|
||||
{
|
||||
string GetName();
|
||||
Task<TransmittedSharedObject<IPage>> Print(string text, CancellationToken cancellationToken);
|
||||
Task<SharedObject<IPage>> Print(string text, CancellationToken cancellationToken);
|
||||
|
||||
}
|
||||
@@ -7,6 +7,6 @@ namespace Example.Shared;
|
||||
[SharedObjectInterface]
|
||||
public interface IPrinterFactory
|
||||
{
|
||||
TransmittedSharedObject<IPrinter> Create(string printerName);
|
||||
SharedObject<IPrinter> Create(string printerName);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ public interface ISerialisationModule : IInjectableModule
|
||||
{
|
||||
void HandleIncomingRequest(int clientId, byte[] message);
|
||||
void PostResponse(NetworkMessage message, int clientId);
|
||||
void SendWelcomeMessage(int clientId);
|
||||
public interface IFrontendSerialisationModule : IInjectableModule
|
||||
{
|
||||
Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution;
|
||||
|
||||
@@ -44,6 +44,11 @@ public class JsonSerialisationModule : ISerialisationModule
|
||||
_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)
|
||||
{
|
||||
if (dependency is IInteractionModule interactionModule)
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace mROA.Implementation.Backend;
|
||||
|
||||
public class StreamBasedInteractionModule : IInteractionModule
|
||||
{
|
||||
private ISerialisationModule _serialisationModule;
|
||||
private readonly Dictionary<int, Stream> _streams = new();
|
||||
private Action<int, byte[]>? _handler;
|
||||
|
||||
@@ -12,6 +13,7 @@ public class StreamBasedInteractionModule : IInteractionModule
|
||||
var id = Random.Shared.Next();
|
||||
_streams.Add(id, stream);
|
||||
_ = ListenTo((id, stream), _handler!);
|
||||
_serialisationModule.SendWelcomeMessage(id);
|
||||
}
|
||||
|
||||
public void SendTo(int clientId, byte[] message)
|
||||
@@ -51,6 +53,7 @@ public class StreamBasedInteractionModule : IInteractionModule
|
||||
if (dependency is ISerialisationModule serialisationModule)
|
||||
{
|
||||
_handler = serialisationModule.HandleIncomingRequest;
|
||||
_serialisationModule = serialisationModule;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,4 +21,9 @@ public class JsonFrontendCallbackSerializationModule : ISerialisationModule
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendWelcomeMessage(int clientId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using mROA.Abstract;
|
||||
|
||||
namespace mROA.Implementation.Frontend;
|
||||
@@ -24,5 +26,13 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge
|
||||
|
||||
_tcpClient.Connect(ipEndPoint);
|
||||
_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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class IdAssingnment
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
@@ -38,7 +38,6 @@ public class MethodRepository : IMethodRepository
|
||||
}
|
||||
public void Inject<T>(T dependency)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,5 +11,5 @@ public class NetworkMessage
|
||||
|
||||
public enum MessageType
|
||||
{
|
||||
Unknown, FinishedCommandExecution, ErrorCommandExecution, AcyncCancelCommandExecution, CallRequest
|
||||
Unknown, FinishedCommandExecution, ErrorCommandExecution, AcyncCancelCommandExecution, CallRequest, IdAssigning
|
||||
}
|
||||
@@ -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) };
|
||||
}
|
||||
Reference in New Issue
Block a user