From 99c938fb488985674fc592a6dedb4de5d05e5df7 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 1 Feb 2025 12:55:00 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=82=D0=BE=D0=BA=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F=20=D0=B2=D0=B7=D0=B0=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.Test/obj/mROA.Test.csproj.nuget.g.props | 8 +-- .../StreamBasedInteractionModule.cs | 54 +++++++++++++++++++ .../Test/ConsoleFrontendInteractionModule.cs | 2 +- mROA/obj/mROA.csproj.nuget.g.props | 6 +-- 4 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 mROA/Implementation/StreamBasedInteractionModule.cs diff --git a/mROA.Test/obj/mROA.Test.csproj.nuget.g.props b/mROA.Test/obj/mROA.Test.csproj.nuget.g.props index 3598713..92a05d6 100644 --- a/mROA.Test/obj/mROA.Test.csproj.nuget.g.props +++ b/mROA.Test/obj/mROA.Test.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference - 6.10.1 + 6.12.0 - + @@ -21,6 +21,6 @@ - C:\Users\Mimm\.nuget\packages\nunit.analyzers\4.6.0 + C:\Users\Mikhail\.nuget\packages\nunit.analyzers\4.6.0 \ No newline at end of file diff --git a/mROA/Implementation/StreamBasedInteractionModule.cs b/mROA/Implementation/StreamBasedInteractionModule.cs new file mode 100644 index 0000000..792aafb --- /dev/null +++ b/mROA/Implementation/StreamBasedInteractionModule.cs @@ -0,0 +1,54 @@ +namespace mROA.Implementation; + +public class StreamBasedInteractionModule : IInteractionModule +{ + private Dictionary _streams = new(); + private Action _handler; + + public void RegisterClient(Stream stream) + { + var id = Random.Shared.Next(); + _streams.Add(id, stream); + ListenTo((id, stream), _handler); + } + + public void SetMessageHandler(Action handler) + { + _handler = handler; + } + + 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) + { + 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); + + action(client.id, buffer[..len]); + } + } + catch (Exception e) + { + Console.WriteLine($"Client handling finished:{client.id}"); + _streams.Remove(client.id); + } + + } +} \ No newline at end of file diff --git a/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs b/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs index b59218b..40323c3 100644 --- a/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs +++ b/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs @@ -6,7 +6,7 @@ public class ConsoleFrontendInteractionModule : IInteractionModule.IFrontendInte { public byte[] ReceiveMessage() { - + return []; } public void PostMessage(byte[] message) diff --git a/mROA/obj/mROA.csproj.nuget.g.props b/mROA/obj/mROA.csproj.nuget.g.props index 566ee42..721d876 100644 --- a/mROA/obj/mROA.csproj.nuget.g.props +++ b/mROA/obj/mROA.csproj.nuget.g.props @@ -5,12 +5,12 @@ NuGet $(MSBuildThisFileDirectory)project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages + C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages PackageReference - 6.10.1 + 6.12.0 - + \ No newline at end of file