From aa3606401bac873dd4d99335f0c81c10ff644524 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofaton Date: Tue, 1 Jul 2025 15:41:48 +0300 Subject: [PATCH] Rewrite reading length of message --- .../ChannelInteractionModule.cs | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index 5acef43..d8440c1 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -166,7 +166,7 @@ namespace mROA.Implementation private readonly Memory _buffer = new byte[BufferSize]; private bool _manualConnectionState = true; private readonly IEndPointContext _context; - + private readonly byte[] _lenBuffer; public StreamExtractor(Stream ioStream, IContextualSerializationToolKit serializationToolkit, IEndPointContext context) { @@ -174,23 +174,16 @@ namespace mROA.Implementation _serializationToolkit = serializationToolkit; _context = context; profiler.Start(); + _lenBuffer = new byte[2]; } public Action MessageReceived = _ => { }; - private ushort ReadMessageLength() + private async Task ReadMessageLength() { - var firstBit = _ioStream.ReadByte(); - if (firstBit == -1) - { - _manualConnectionState = false; - throw new EndOfStreamException(); - } - - _manualConnectionState = true; - var secondBit = (byte)_ioStream.ReadByte(); - - var len = BitConverter.ToUInt16(new[] { (byte)firstBit, secondBit }); + await _ioStream.ReadAsync(_lenBuffer, 0, 2); + + var len = BitConverter.ToUInt16(_lenBuffer); return len; } @@ -198,7 +191,7 @@ namespace mROA.Implementation public async Task SingleReceive(CancellationToken token = default) { Console.WriteLine($"Receive start. Time: {profiler.ElapsedMilliseconds}ms"); - var len = ReadMessageLength(); + var len = await ReadMessageLength(); Console.WriteLine($"Received len {len}. Time: {profiler.ElapsedMilliseconds}ms"); var localSpan = _buffer[..len];