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