From 7fdff923a3f823a7cbdcb0a0be838912121b3e51 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 1 Feb 2025 13:40:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=20?= =?UTF-8?q?=D1=84=D1=80=D0=BE=D0=BD=D1=82=D0=B5=D0=BD=D0=B4=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BB=D1=8C=20json=20=D1=81=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8,=20mock=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D1=80=D0=B5=D0=B6?= =?UTF-8?q?=D0=B8=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.Test/StreamTest.cs | 28 +++++++++++++++++ .../JsonFrontendSerialisationModule.cs | 6 +++- .../StreamBasedFrontendInteractionModule.cs | 31 +++++++++++++++++++ mROA/Implementation/Test/MockExecModule.cs | 2 +- 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 mROA.Test/StreamTest.cs create mode 100644 mROA/Implementation/StreamBasedFrontendInteractionModule.cs diff --git a/mROA.Test/StreamTest.cs b/mROA.Test/StreamTest.cs new file mode 100644 index 0000000..e799071 --- /dev/null +++ b/mROA.Test/StreamTest.cs @@ -0,0 +1,28 @@ +using mROA.Implementation; + +namespace mROA.Test; + +public class StreamTest +{ + private StreamBasedInteractionModule _interactionModule; + private ISerialisationModule _serialisationModule; + private IExecuteModule _executeModule; + private IMethodRepository _methodRepository; + private IContextRepository _contextRepository; + [SetUp] + public void Setup() + { + _interactionModule = new StreamBasedInteractionModule(); + + _serialisationModule = new JsonSerialisationModule(_interactionModule, _methodRepository); + + _executeModule = new MockExecModule(); + _serialisationModule.SetExecuteModule(_executeModule); + } + + [Test] + public void StreamingTest() + { + + } +} \ No newline at end of file diff --git a/mROA/Implementation/JsonFrontendSerialisationModule.cs b/mROA/Implementation/JsonFrontendSerialisationModule.cs index 5f63f6f..080d423 100644 --- a/mROA/Implementation/JsonFrontendSerialisationModule.cs +++ b/mROA/Implementation/JsonFrontendSerialisationModule.cs @@ -1,6 +1,5 @@ using System.Text; using System.Text.Json; -using System.Windows.Input; namespace mROA.Implementation; @@ -11,6 +10,11 @@ public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInterac { var receiveMessage = interactionModule.ReceiveMessage(); var parsed = JsonSerializer.Deserialize(receiveMessage); + while (parsed.CallRequestId != requestId) + { + receiveMessage = interactionModule.ReceiveMessage(); + parsed = JsonSerializer.Deserialize(receiveMessage); + } return parsed; } diff --git a/mROA/Implementation/StreamBasedFrontendInteractionModule.cs b/mROA/Implementation/StreamBasedFrontendInteractionModule.cs new file mode 100644 index 0000000..e9c0b09 --- /dev/null +++ b/mROA/Implementation/StreamBasedFrontendInteractionModule.cs @@ -0,0 +1,31 @@ +namespace mROA.Implementation; + +public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule +{ + private Stream _serverStream; + + public StreamBasedFrontendInteractionModule(Stream serverStream) + { + _serverStream = serverStream; + } + + public byte[] ReceiveMessage() + { + const int bufferSize = ushort.MaxValue; + + byte[] buffer = new byte[bufferSize]; + if (!_serverStream.CanRead) throw new IOException("Server is not connected."); + + _serverStream.ReadExactly(buffer, 0, 2); + var len = BitConverter.ToUInt16(buffer, 0); + _serverStream.ReadExactly(buffer, 0, len); + + return buffer[..len]; + } + + public void PostMessage(byte[] message) + { + _serverStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); + _serverStream.Write(message, 0, message.Length); + } +} \ No newline at end of file diff --git a/mROA/Implementation/Test/MockExecModule.cs b/mROA/Implementation/Test/MockExecModule.cs index 697e1c2..928fb54 100644 --- a/mROA/Implementation/Test/MockExecModule.cs +++ b/mROA/Implementation/Test/MockExecModule.cs @@ -5,6 +5,6 @@ public class MockExecModule : IExecuteModule public ICommandExecution Execute(ICallRequest command) { return new FinalCommandExecution - { ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 } }; + { ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId }; } } \ No newline at end of file