From 5c643dd2c9074c44887fc22bd4874817c3353c47 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 16 Feb 2025 17:05:16 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=BB=D1=83=D1=87=D1=88=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D1=8C=20=D0=B2=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=B8=D1=8F=20=D0=B8=20=D0=BD=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B0=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D1=80=D0=B5=D0=BF=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=D0=B5=D0=BD=D1=82=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.Test/NextGenTest.cs | 1 + mROA/Abstract/IInteractionModule.cs | 17 +------ mROA/Abstract/ISerialisationModule.cs | 10 ++++- .../AsyncNetworkInteractionModule.cs | 33 -------------- mROA/Implementation/NetworkMessage.cs | 3 +- .../NextGenerationInteractionModule.cs | 32 +++++++------- .../Implementation/RemoteContextRepository.cs | 1 - mROA/Implementation/RepresentationModule.cs | 44 +++++++++++++++++++ 8 files changed, 73 insertions(+), 68 deletions(-) delete mode 100644 mROA/Implementation/AsyncNetworkInteractionModule.cs rename mROA/{ => Implementation}/NextGenerationInteractionModule.cs (58%) create mode 100644 mROA/Implementation/RepresentationModule.cs diff --git a/mROA.Test/NextGenTest.cs b/mROA.Test/NextGenTest.cs index f57dc5c..7293877 100644 --- a/mROA.Test/NextGenTest.cs +++ b/mROA.Test/NextGenTest.cs @@ -46,6 +46,7 @@ public class NextGenTest Task.WaitAll(tasks.ToArray()); Assert.Pass(); + } private async Task ReadStream(Guid current) diff --git a/mROA/Abstract/IInteractionModule.cs b/mROA/Abstract/IInteractionModule.cs index 5d49bf1..5ccc8e6 100644 --- a/mROA/Abstract/IInteractionModule.cs +++ b/mROA/Abstract/IInteractionModule.cs @@ -2,25 +2,10 @@ using mROA.Implementation; namespace mROA.Abstract; -public interface IInteractionModule : IInjectableModule -{ - void SendTo(int clientId, byte[] message); - void RegisterSource(Stream stream); - Stream GetSource(int clientId); - public interface IFrontendInteractionModule : IInjectableModule - { - int ClientId { get; } - public Task ReceiveMessage(); - public void PostMessage(byte[] message); - } -} - public interface INextGenerationInteractionModule : IInjectableModule { int ConntectionId { get; } public Stream? BaseStream { get; set; } Task GetNextMessageReceiving(); - void PostMessage(NetworkMessage message); - NetworkMessage[] UnhandledMessages { get; } - void HandleMessage(NetworkMessage msg); + Task PostMessage(NetworkMessage message); } \ No newline at end of file diff --git a/mROA/Abstract/ISerialisationModule.cs b/mROA/Abstract/ISerialisationModule.cs index 7ce2124..883265d 100644 --- a/mROA/Abstract/ISerialisationModule.cs +++ b/mROA/Abstract/ISerialisationModule.cs @@ -1,3 +1,4 @@ +using System.Windows.Input; using mROA.Implementation; namespace mROA.Abstract; @@ -12,7 +13,14 @@ public interface ISerialisationModule : IInjectableModule int ClientId { get; } Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution; Task> GetFinalCommandExecution(Guid requestId); - void PostCallRequest(ICallRequest callRequest); } +} + +public interface IRepresentationModule : IInjectableModule +{ + int Id { get; } + Task GetMessage(Guid? requestId, MessageType? messageType); + Task PostCallMessage(Guid id, MessageType messageType, T payload); + Task PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType); } \ No newline at end of file diff --git a/mROA/Implementation/AsyncNetworkInteractionModule.cs b/mROA/Implementation/AsyncNetworkInteractionModule.cs deleted file mode 100644 index d3c4a96..0000000 --- a/mROA/Implementation/AsyncNetworkInteractionModule.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace mROA.Implementation; - -public class AsyncNetworkInteractionModule -{ - private ISerializationToolkit _iSerializationToolkit; - private const int BufferSize = ushort.MaxValue; - private Stream? _stream; - private List _unhandledMessages = new(8); - private byte[] _buffer = new byte[BufferSize]; - private Task? _currentReadTask; - - - public Task ReceiveNext() - { - _currentReadTask ??= StartReceiving(); - return _currentReadTask; - } - - private async Task StartReceiving() - { - await _stream.ReadExactlyAsync(_buffer, 0, 2); - var len = BitConverter.ToUInt16(_buffer, 0); - await _stream.ReadExactlyAsync(_buffer, 0, len); - - } - - public NetworkMessage GetLastMessage() - { - return _unhandledMessages.Last(); - } - - -} \ No newline at end of file diff --git a/mROA/Implementation/NetworkMessage.cs b/mROA/Implementation/NetworkMessage.cs index 4a08543..bd70245 100644 --- a/mROA/Implementation/NetworkMessage.cs +++ b/mROA/Implementation/NetworkMessage.cs @@ -1,4 +1,5 @@ -using mROA.Abstract; +using System.Text.Json; +using mROA.Abstract; namespace mROA.Implementation; diff --git a/mROA/NextGenerationInteractionModule.cs b/mROA/Implementation/NextGenerationInteractionModule.cs similarity index 58% rename from mROA/NextGenerationInteractionModule.cs rename to mROA/Implementation/NextGenerationInteractionModule.cs index 07404da..e8657ad 100644 --- a/mROA/NextGenerationInteractionModule.cs +++ b/mROA/Implementation/NextGenerationInteractionModule.cs @@ -1,17 +1,15 @@ using mROA.Abstract; -using mROA.Implementation; -namespace mROA; +namespace mROA.Implementation; public class NextGenerationInteractionModule : INextGenerationInteractionModule { - private ISerializationToolkit _serialization; + private ISerializationToolkit? _serialization; public int ConntectionId { get; set; } - public Stream BaseStream { get; set; } + public Stream? BaseStream { get; set; } private Task? _currentReceiving; private const int BufferSize = ushort.MaxValue; - private byte[] _buffer = new byte[BufferSize]; - private List _unhandledMessages = new(8); + private readonly byte[] _buffer = new byte[BufferSize]; public void Inject(T dependency) { @@ -29,22 +27,24 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule return _currentReceiving; } - public void PostMessage(NetworkMessage message) + public async Task PostMessage(NetworkMessage message) { + if (BaseStream == null) + throw new NullReferenceException("BaseStream is null"); + var rawMessage = _serialization.Serialize(message); - BaseStream.Write(BitConverter.GetBytes((ushort)rawMessage.Length), 0, sizeof(ushort)); - BaseStream.Write(rawMessage, 0, rawMessage.Length); - } - - public NetworkMessage[] UnhandledMessages => _unhandledMessages.ToArray(); - - public void HandleMessage(NetworkMessage msg) - { - _unhandledMessages.Remove(msg); + await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort))); + await BaseStream.WriteAsync(rawMessage); } private async Task GetNextMessage() { + if (BaseStream == null) + throw new NullReferenceException("BaseStream is null"); + + if (_serialization == null) + throw new NullReferenceException("Serialization toolkit is null"); + await BaseStream.ReadExactlyAsync(_buffer, 0, 2); var len = BitConverter.ToUInt16(_buffer, 0); await BaseStream.ReadExactlyAsync(_buffer, 0, len); diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 8c30743..78faf9e 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -1,5 +1,4 @@ using System.Collections.Frozen; -using System.Collections.Immutable; using mROA.Abstract; namespace mROA.Implementation; diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs new file mode 100644 index 0000000..477f411 --- /dev/null +++ b/mROA/Implementation/RepresentationModule.cs @@ -0,0 +1,44 @@ +using mROA.Abstract; + +namespace mROA.Implementation; + +public class RepresentationModule : IRepresentationModule +{ + private ISerializationToolkit _serialization; + private INextGenerationInteractionModule _interaction; + + public void Inject(T dependency) + { + switch (dependency) + { + case ISerializationToolkit toolkit: + _serialization = toolkit; + break; + case INextGenerationInteractionModule interactionModule: + _interaction = interactionModule; + break; + } + } + + public int Id { get; set; } + + public async Task GetMessage(Guid? requestId, MessageType? messageType) + { + while (true) + { + var message = await _interaction.GetNextMessageReceiving(); + if ((requestId is null || message.Id == requestId) && (messageType is null || message.SchemaId == messageType)) + return _serialization.Deserialize(message.Data)!; + } + } + + public async Task PostCallMessage(Guid id, MessageType messageType, T payload) + { + await PostCallMessage(id, messageType, payload, typeof(T)); + } + + public async Task PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType) + { + await _interaction.PostMessage(new NetworkMessage {Id = id, SchemaId = messageType, Data = _serialization.Serialize(payload, payloadType)}); + } +} \ No newline at end of file