From 327822d48ccf994fb0a6926f3e68ebe3ca2e3a6b Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 17 Feb 2025 12:46:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BB=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=20=D0=B0=D1=80=D1=85=D0=B8=D1=82=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D1=83=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA/Abstract/IConnectionHub.cs | 12 ++ mROA/Abstract/IIdentityGenerator.cs | 6 + .../Backend/BackendIdentityGenerator.cs | 13 ++ mROA/Implementation/Backend/ConntectionHub.cs | 21 +++ .../Backend/NetworkGatewayModule.cs | 44 +++-- .../Backend/StreamBasedInteractionModule.cs | 132 +++++++-------- .../CreativeSerializationModuleProducer.cs | 2 +- ...JsonFrontendCallbackSerializationModule.cs | 29 ---- .../JsonFrontendSerialisationModule.cs | 152 +++++++++--------- .../Frontend/NetworkFrontendBridge.cs | 23 +-- .../StreamBasedFrontendInteractionModule.cs | 94 +++++------ ...eamBasedVirtualBackendInteractionModule.cs | 60 ------- .../NextGenerationInteractionModule.cs | 16 +- 13 files changed, 300 insertions(+), 304 deletions(-) create mode 100644 mROA/Abstract/IConnectionHub.cs create mode 100644 mROA/Abstract/IIdentityGenerator.cs create mode 100644 mROA/Implementation/Backend/BackendIdentityGenerator.cs create mode 100644 mROA/Implementation/Backend/ConntectionHub.cs delete mode 100644 mROA/Implementation/Frontend/Callback/JsonFrontendCallbackSerializationModule.cs delete mode 100644 mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs diff --git a/mROA/Abstract/IConnectionHub.cs b/mROA/Abstract/IConnectionHub.cs new file mode 100644 index 0000000..ab62bdc --- /dev/null +++ b/mROA/Abstract/IConnectionHub.cs @@ -0,0 +1,12 @@ +namespace mROA.Abstract; + +public delegate void ConnectionHandler(INextGenerationInteractionModule interactionModule); +public delegate void DisconnectionHandler(INextGenerationInteractionModule interactionModule); + +public interface IConnectionHub +{ + void RegisterInteracion(INextGenerationInteractionModule interaction); + INextGenerationInteractionModule GetInteracion(int id); + event ConnectionHandler? OnConnectied; + event DisconnectionHandler? OnDisconnected; +} \ No newline at end of file diff --git a/mROA/Abstract/IIdentityGenerator.cs b/mROA/Abstract/IIdentityGenerator.cs new file mode 100644 index 0000000..29f0f2c --- /dev/null +++ b/mROA/Abstract/IIdentityGenerator.cs @@ -0,0 +1,6 @@ +namespace mROA.Abstract; + +public interface IIdentityGenerator +{ + int GetNextIdentity(); +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/BackendIdentityGenerator.cs b/mROA/Implementation/Backend/BackendIdentityGenerator.cs new file mode 100644 index 0000000..add087e --- /dev/null +++ b/mROA/Implementation/Backend/BackendIdentityGenerator.cs @@ -0,0 +1,13 @@ +using mROA.Abstract; + +namespace mROA.Implementation.Backend; + +public class BackendIdentityGenerator : IIdentityGenerator +{ + private int _currentId; + + public int GetNextIdentity() + { + return ++_currentId; + } +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/ConntectionHub.cs b/mROA/Implementation/Backend/ConntectionHub.cs new file mode 100644 index 0000000..bcf3dd0 --- /dev/null +++ b/mROA/Implementation/Backend/ConntectionHub.cs @@ -0,0 +1,21 @@ +using mROA.Abstract; + +namespace mROA.Implementation.Backend; + +public class ConntectionHub : IConnectionHub +{ + private Dictionary _connections = new(); + public void RegisterInteracion(INextGenerationInteractionModule interaction) + { + + OnConnectied?.Invoke(interaction); + } + + public INextGenerationInteractionModule GetInteracion(int id) + { + + } + + public event ConnectionHandler? OnConnectied; + public event DisconnectionHandler? OnDisconnected; +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 48ae7fc..c73b944 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -4,18 +4,29 @@ using mROA.Abstract; namespace mROA.Implementation.Backend; -public class NetworkGatewayModule(IPEndPoint endpoint) : IGatewayModule +public class NetworkGatewayModule() : IGatewayModule { - private readonly TcpListener _tcpListener = new(endpoint); - private IInteractionModule? _interactionModule; + private readonly IPEndPoint? _endpoint; + private readonly Type? _interactionModuleType; + private readonly IInjectableModule[]? _injectableModules; + private readonly TcpListener? _tcpListener; + private IConnectionHub? _hub; + public NetworkGatewayModule(IPEndPoint endpoint, Type interactionModuleType, IInjectableModule[] injectableModules) + { + _endpoint = endpoint; + _tcpListener = new(_endpoint); + + _interactionModuleType = interactionModuleType; + _injectableModules = injectableModules; + } public void Run() { _tcpListener.Start(); Console.WriteLine($"Listening on {_tcpListener.LocalEndpoint}"); Console.WriteLine("Enter Backspace to stop"); - + Task.Run(HandleIncomingConnections); while (true) @@ -26,8 +37,6 @@ public class NetworkGatewayModule(IPEndPoint endpoint) : IGatewayModule } Console.WriteLine("Stopping"); - - } public void Dispose() @@ -38,22 +47,33 @@ public class NetworkGatewayModule(IPEndPoint endpoint) : IGatewayModule private void HandleIncomingConnections() { - if (_interactionModule is null) - throw new NullReferenceException("Interaction module is null"); + if (_hub is null) + throw new NullReferenceException("Hub module is null"); + + if (_tcpListener == null) + throw new NullReferenceException("TcpListener is null"); + + if (_injectableModules is null) + throw new NullReferenceException("InjectableModules is null"); + + if (_interactionModuleType is null) + throw new NullReferenceException("InteractionModuleType is null"); while (true) { var client = _tcpListener.AcceptTcpClient(); Console.WriteLine($"Client connected from {client.Client.RemoteEndPoint}"); - _interactionModule.RegisterSource(client.GetStream()); + var interacton = Activator.CreateInstance(_interactionModuleType) as INextGenerationInteractionModule; + foreach (var injectableModule in _injectableModules) + interacton.Inject(injectableModule); + _hub.RegisterInteracion(new NextGenerationInteractionModule()); Console.WriteLine("Client registered"); } } public void Inject(T dependency) { - if (dependency is IInteractionModule interactionModule) - _interactionModule = interactionModule; + if (dependency is IConnectionHub interactionModule) + _hub = interactionModule; } - } \ No newline at end of file diff --git a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs index 1d25548..0427209 100644 --- a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs +++ b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs @@ -1,66 +1,66 @@ -using mROA.Abstract; - -namespace mROA.Implementation.Backend; - -public class StreamBasedInteractionModule : IInteractionModule -{ - internal ISerialisationModule _serialisationModule; - private readonly Dictionary _streams = new(); - internal Action? _handler; - - public void RegisterSource(Stream stream) - { - var id = Random.Shared.Next(); - _streams.Add(id, stream); - _ = ListenTo((id, stream), _handler!); - _serialisationModule.SendWelcomeMessage(id); - } - - public Stream GetSource(int clientId) - { - return _streams.GetValueOrDefault(clientId, Stream.Null); - } - - public void SendTo(int clientId, byte[] message) - { - if (!_streams.TryGetValue(clientId, out var stream)) - { - throw new KeyNotFoundException($"Client {clientId} not found"); - } - - stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); - stream.Write(message, 0, message.Length); - } - - private async Task ListenTo((int id, Stream stream) client, Action action) - { - TransmissionConfig.OwnershipRepository = new MultiClientOwnershipRepository(); - const int bufferSize = ushort.MaxValue; - try - { - byte[] buffer = new byte[bufferSize]; - while (client.stream.CanRead) - { - await client.stream.ReadExactlyAsync(buffer, 0, 2); - var len = BitConverter.ToUInt16(buffer, 0); - await client.stream.ReadExactlyAsync(buffer, 0, len); - _ = Task.Run(() => action(client.id, buffer[..len])); - } - } - catch (Exception) - { - Console.WriteLine($"Client handling finished:{client.id}"); - _streams.Remove(client.id); - } - } - - public void Inject(T dependency) - { - if (dependency is ISerialisationModule serialisationModule) - { - _handler = serialisationModule.HandleIncomingRequest; - _serialisationModule = serialisationModule; - } - } - -} \ No newline at end of file +// using mROA.Abstract; +// +// namespace mROA.Implementation.Backend; +// +// public class StreamBasedInteractionModule : IInteractionModule +// { +// internal ISerialisationModule _serialisationModule; +// private readonly Dictionary _streams = new(); +// internal Action? _handler; +// +// public void RegisterSource(Stream stream) +// { +// var id = Random.Shared.Next(); +// _streams.Add(id, stream); +// _ = ListenTo((id, stream), _handler!); +// _serialisationModule.SendWelcomeMessage(id); +// } +// +// public Stream GetSource(int clientId) +// { +// return _streams.GetValueOrDefault(clientId, Stream.Null); +// } +// +// public void SendTo(int clientId, byte[] message) +// { +// if (!_streams.TryGetValue(clientId, out var stream)) +// { +// throw new KeyNotFoundException($"Client {clientId} not found"); +// } +// +// stream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); +// stream.Write(message, 0, message.Length); +// } +// +// private async Task ListenTo((int id, Stream stream) client, Action action) +// { +// TransmissionConfig.OwnershipRepository = new MultiClientOwnershipRepository(); +// const int bufferSize = ushort.MaxValue; +// try +// { +// byte[] buffer = new byte[bufferSize]; +// while (client.stream.CanRead) +// { +// await client.stream.ReadExactlyAsync(buffer, 0, 2); +// var len = BitConverter.ToUInt16(buffer, 0); +// await client.stream.ReadExactlyAsync(buffer, 0, len); +// _ = Task.Run(() => action(client.id, buffer[..len])); +// } +// } +// catch (Exception) +// { +// Console.WriteLine($"Client handling finished:{client.id}"); +// _streams.Remove(client.id); +// } +// } +// +// public void Inject(T dependency) +// { +// if (dependency is ISerialisationModule serialisationModule) +// { +// _handler = serialisationModule.HandleIncomingRequest; +// _serialisationModule = serialisationModule; +// } +// } +// +// } \ No newline at end of file diff --git a/mROA/Implementation/CreativeSerializationModuleProducer.cs b/mROA/Implementation/CreativeSerializationModuleProducer.cs index c7e7a81..2ed20aa 100644 --- a/mROA/Implementation/CreativeSerializationModuleProducer.cs +++ b/mROA/Implementation/CreativeSerializationModuleProducer.cs @@ -10,7 +10,7 @@ public class CreativeSerializationModuleProducer : ISerialisationModuleProducer private IInjectableModule[] _creationModules; private StreamBasedInteractionModule? _interactionModule; - public CreativeSerializationModuleProducer(IInjectableModule[] creationModules, Type serializationModuleType) + public CreativeSerializationModuleProducer(IInjectableModule[] creationModules, Type serializationModuleType)а { _creationModules = creationModules; _serializationModuleType = serializationModuleType; diff --git a/mROA/Implementation/Frontend/Callback/JsonFrontendCallbackSerializationModule.cs b/mROA/Implementation/Frontend/Callback/JsonFrontendCallbackSerializationModule.cs deleted file mode 100644 index 9759e20..0000000 --- a/mROA/Implementation/Frontend/Callback/JsonFrontendCallbackSerializationModule.cs +++ /dev/null @@ -1,29 +0,0 @@ -using mROA.Abstract; - -namespace mROA.Implementation.Frontend; - -public class JsonFrontendCallbackSerializationModule : ISerialisationModule -{ - private IInteractionModule.IFrontendInteractionModule? _interactionModule; - - public void Inject(T dependency) - { - if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule) - _interactionModule = interactionModule; - } - - public void HandleIncomingRequest(int clientId, byte[] message) - { - - } - - public void PostResponse(NetworkMessage message, int clientId) - { - throw new NotImplementedException(); - } - - public void SendWelcomeMessage(int clientId) - { - throw new NotImplementedException(); - } -} \ No newline at end of file diff --git a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs index 1c98a9b..5ef07e7 100644 --- a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs +++ b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs @@ -4,82 +4,82 @@ using mROA.Abstract; namespace mROA.Implementation.Frontend; -public class JsonFrontendSerialisationModule - : ISerialisationModule.IFrontendSerialisationModule -{ - private IInteractionModule.IFrontendInteractionModule? _interactionModule; - public int ClientId => _interactionModule!.ClientId; - - public async Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution - { - if (_interactionModule is null) - throw new Exception("Interaction module not initialized"); - - var receiveMessage = await _interactionModule.ReceiveMessage(); - var message = JsonSerializer.Deserialize(receiveMessage)!; - - while (message.Id != requestId) - { - receiveMessage = await _interactionModule.ReceiveMessage(); - message = JsonSerializer.Deserialize(receiveMessage)!; - } - - var parsed = JsonSerializer.Deserialize(message.Data)!; - - if (message.SchemaId == MessageType.ErrorCommandExecution) - { - throw new RemoteException(JsonSerializer.Deserialize(message.Data)!.Exception) - { CallRequestId = requestId }; - } - - return parsed; - } - - public async Task> GetFinalCommandExecution(Guid requestId) - { - if (_interactionModule is null) - throw new Exception("Interaction module not initialized"); - - var receiveMessage = await _interactionModule.ReceiveMessage(); - - var message = JsonSerializer.Deserialize(receiveMessage)!; - while (message.Id != requestId) - { - receiveMessage = await _interactionModule.ReceiveMessage(); - - message = JsonSerializer.Deserialize(receiveMessage)!; - } - - if (message.SchemaId == MessageType.ErrorCommandExecution) - { - throw new RemoteException(JsonSerializer.Deserialize(message.Data)!.Exception) - { CallRequestId = requestId }; - } - - return JsonSerializer.Deserialize>(message.Data)!; - } - - public void PostCallRequest(ICallRequest callRequest) - { - if (_interactionModule is null) - throw new Exception("Interaction module not initialized"); - - - var post = JsonSerializer.SerializeToUtf8Bytes(callRequest, callRequest.GetType()); - _interactionModule.PostMessage(JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage - { - Id = callRequest.CallRequestId, - Data = post, - SchemaId = MessageType.CallRequest - })); - } - - public void Inject(T dependency) - { - if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule) - _interactionModule = interactionModule; - } -} +// public class JsonFrontendSerialisationModule +// : ISerialisationModule.IFrontendSerialisationModule +// { +// private IInteractionModule.IFrontendInteractionModule? _interactionModule; +// public int ClientId => _interactionModule!.ClientId; +// +// public async Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution +// { +// if (_interactionModule is null) +// throw new Exception("Interaction module not initialized"); +// +// var receiveMessage = await _interactionModule.ReceiveMessage(); +// var message = JsonSerializer.Deserialize(receiveMessage)!; +// +// while (message.Id != requestId) +// { +// receiveMessage = await _interactionModule.ReceiveMessage(); +// message = JsonSerializer.Deserialize(receiveMessage)!; +// } +// +// var parsed = JsonSerializer.Deserialize(message.Data)!; +// +// if (message.SchemaId == MessageType.ErrorCommandExecution) +// { +// throw new RemoteException(JsonSerializer.Deserialize(message.Data)!.Exception) +// { CallRequestId = requestId }; +// } +// +// return parsed; +// } +// +// public async Task> GetFinalCommandExecution(Guid requestId) +// { +// if (_interactionModule is null) +// throw new Exception("Interaction module not initialized"); +// +// var receiveMessage = await _interactionModule.ReceiveMessage(); +// +// var message = JsonSerializer.Deserialize(receiveMessage)!; +// while (message.Id != requestId) +// { +// receiveMessage = await _interactionModule.ReceiveMessage(); +// +// message = JsonSerializer.Deserialize(receiveMessage)!; +// } +// +// if (message.SchemaId == MessageType.ErrorCommandExecution) +// { +// throw new RemoteException(JsonSerializer.Deserialize(message.Data)!.Exception) +// { CallRequestId = requestId }; +// } +// +// return JsonSerializer.Deserialize>(message.Data)!; +// } +// +// public void PostCallRequest(ICallRequest callRequest) +// { +// if (_interactionModule is null) +// throw new Exception("Interaction module not initialized"); +// +// +// var post = JsonSerializer.SerializeToUtf8Bytes(callRequest, callRequest.GetType()); +// _interactionModule.PostMessage(JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage +// { +// Id = callRequest.CallRequestId, +// Data = post, +// SchemaId = MessageType.CallRequest +// })); +// } +// +// public void Inject(T dependency) +// { +// if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule) +// _interactionModule = interactionModule; +// } +// } public class RemoteException(string error) : Exception { diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index ccf42cb..7ef68c1 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -9,13 +9,19 @@ namespace mROA.Implementation.Frontend; public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge { private readonly TcpClient _tcpClient = new(); - private StreamBasedFrontendInteractionModule? _interactionModule; + private NextGenerationInteractionModule? _interactionModule; + private ISerializationToolkit? _serialization; public void Inject(T dependency) { - if (dependency is StreamBasedFrontendInteractionModule interactionModule) + switch (dependency) { - _interactionModule = interactionModule; + case NextGenerationInteractionModule interactionModule: + _interactionModule = interactionModule; + break; + case ISerializationToolkit toolkit: + _serialization = toolkit; + break; } } @@ -25,14 +31,13 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge throw new Exception("Interaction module was not injected"); _tcpClient.Connect(ipEndPoint); - _interactionModule.ServerStream = _tcpClient.GetStream(); - var welcomeMessage = _interactionModule.ReceiveMessage().GetAwaiter().GetResult(); - var message = JsonSerializer.Deserialize(welcomeMessage); - if (message.SchemaId != MessageType.IdAssigning) + _interactionModule.BaseStream = _tcpClient.GetStream(); + var welcomeMessage = _interactionModule.GetNextMessageReceiving().GetAwaiter().GetResult(); + if (welcomeMessage.SchemaId != MessageType.IdAssigning) { - throw new Exception($"Incorrect message type. Must be IdAssigning, current : {message.SchemaId.ToString()}"); + throw new Exception($"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.SchemaId.ToString()}"); } - TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(JsonSerializer.Deserialize(message.Data)!.Id); + TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_serialization.Deserialize(welcomeMessage.Data)!.Id); } } \ No newline at end of file diff --git a/mROA/Implementation/Frontend/StreamBasedFrontendInteractionModule.cs b/mROA/Implementation/Frontend/StreamBasedFrontendInteractionModule.cs index cb6fcb9..36fe157 100644 --- a/mROA/Implementation/Frontend/StreamBasedFrontendInteractionModule.cs +++ b/mROA/Implementation/Frontend/StreamBasedFrontendInteractionModule.cs @@ -2,50 +2,50 @@ using mROA.Abstract; namespace mROA.Implementation.Frontend; -public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule -{ - public Stream? ServerStream { get; set; } - public int ClientId { get; set; } - - public NetworkMessage[] UnhandledMessages() - { - return Array.Empty(); - } - - public NetworkMessage LastMessage() - { - return null; - } - - - - public async Task ReceiveMessage() - { - if (ServerStream is null) - throw new IOException("Server is not connected."); - - const int bufferSize = ushort.MaxValue; - - var buffer = new byte[bufferSize]; - if (!ServerStream.CanRead) throw new IOException("Server is not connected."); - - await ServerStream.ReadExactlyAsync(buffer, 0, 2); - var len = BitConverter.ToUInt16(buffer, 0); - await ServerStream.ReadExactlyAsync(buffer, 0, len); - - return buffer[..len]; - } - - public void PostMessage(byte[] message) - { - 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 dependency) - { - } -} \ No newline at end of file +// public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule +// { +// public Stream? ServerStream { get; set; } +// public int ClientId { get; set; } +// +// public NetworkMessage[] UnhandledMessages() +// { +// return Array.Empty(); +// } +// +// public NetworkMessage LastMessage() +// { +// return null; +// } +// +// +// +// public async Task ReceiveMessage() +// { +// if (ServerStream is null) +// throw new IOException("Server is not connected."); +// +// const int bufferSize = ushort.MaxValue; +// +// var buffer = new byte[bufferSize]; +// if (!ServerStream.CanRead) throw new IOException("Server is not connected."); +// +// await ServerStream.ReadExactlyAsync(buffer, 0, 2); +// var len = BitConverter.ToUInt16(buffer, 0); +// await ServerStream.ReadExactlyAsync(buffer, 0, len); +// +// return buffer[..len]; +// } +// +// public void PostMessage(byte[] message) +// { +// 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 dependency) +// { +// } +// } \ No newline at end of file diff --git a/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs b/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs deleted file mode 100644 index c68dda0..0000000 --- a/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs +++ /dev/null @@ -1,60 +0,0 @@ -using mROA.Abstract; -using mROA.Implementation.Backend; - -namespace mROA.Implementation.Frontend; - -public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule, IInteractionModule -{ - private Stream? _serverStream { get; set; } - private StreamBasedFrontendInteractionModule? _interactionModule; - public void RegisterSource(Stream stream) - { - - } - - public Stream GetSource(int clientId) - { - return _serverStream; - } - - public void SendTo(int clientId, byte[] message) - { - _serverStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); - _serverStream.Write(message, 0, message.Length); - } - - private async Task ListenTo((int id, Stream stream) client, Action action) - { - const int bufferSize = ushort.MaxValue; - try - { - byte[] buffer = new byte[bufferSize]; - while (client.stream.CanRead) - { - await client.stream.ReadExactlyAsync(buffer, 0, 2); - var len = BitConverter.ToUInt16(buffer, 0); - await client.stream.ReadExactlyAsync(buffer, 0, len); - _ = Task.Run(() => action(client.id, buffer[..len])); - } - } - catch (Exception) - { - } - } - - public void Inject(T dependency) - { - base.Inject(dependency); - if (dependency is StreamBasedFrontendInteractionModule interactionModule) - { - _interactionModule = interactionModule; - } - } - - - public void StartVirtualInteraction() - { - _serverStream = _interactionModule.ServerStream; - _ = ListenTo((0, _serverStream), _handler!); - } -} \ No newline at end of file diff --git a/mROA/Implementation/NextGenerationInteractionModule.cs b/mROA/Implementation/NextGenerationInteractionModule.cs index e8657ad..3477340 100644 --- a/mROA/Implementation/NextGenerationInteractionModule.cs +++ b/mROA/Implementation/NextGenerationInteractionModule.cs @@ -1,11 +1,12 @@ -using mROA.Abstract; +using System.Security.Cryptography; +using mROA.Abstract; namespace mROA.Implementation; public class NextGenerationInteractionModule : INextGenerationInteractionModule { private ISerializationToolkit? _serialization; - public int ConntectionId { get; set; } + public int ConntectionId { get; private set; } public Stream? BaseStream { get; set; } private Task? _currentReceiving; private const int BufferSize = ushort.MaxValue; @@ -13,8 +14,15 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule public void Inject(T dependency) { - if (dependency is ISerializationToolkit toolkit) - _serialization = toolkit; + switch (dependency) + { + case ISerializationToolkit toolkit: + _serialization = toolkit; + break; + case IIdentityGenerator identityGenerator: + ConntectionId = identityGenerator.GetNextIdentity(); + break; + } }