From 602d45c8948cd6b96ab5b11d24c12139762f0575 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 15 Feb 2025 13:58:25 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=B8=D1=80=D1=82=D1=83=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C?= =?UTF-8?q?=20=D0=B2=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20=D1=84=D1=80?= =?UTF-8?q?=D0=BE=D0=BD=D0=B5=D0=BD=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...eamBasedVirtualBackendInteractionModule.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs diff --git a/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs b/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs new file mode 100644 index 0000000..5c9cd96 --- /dev/null +++ b/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs @@ -0,0 +1,44 @@ +using mROA.Implementation.Backend; + +namespace mROA.Implementation.Frontend; + +public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule +{ + public Stream ServerStream { get; set; } + + public void RegisterSource(Stream stream) + { + ServerStream = 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) + { + } + } + +} \ No newline at end of file