From b70da1148ed9d26d2453eef9121b897659d63574 Mon Sep 17 00:00:00 2001 From: shinichi Date: Wed, 16 Sep 2020 10:23:35 +0200 Subject: [PATCH] added ReadPrefMessage --- RH-Engine/Program.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index 8c2db92..bf90f8b 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -60,7 +60,28 @@ namespace RH_Engine return packetLength; } + public static string ReadPrefMessage(NetworkStream stream) + { + byte[] lengthBytes = new byte[4]; + stream.Read(lengthBytes, 0, 4); + + int length = (int)(lengthBytes[0] | (lengthBytes[1] << 8) | (lengthBytes[1] << 16) | (lengthBytes[1] << 24)); + + byte[] buffer = new byte[length]; + int totalRead = 0; + + //read bytes until stream indicates there are no more + do + { + int read = stream.Read(buffer, totalRead, buffer.Length - totalRead); + totalRead += read; + Console.WriteLine("ReadMessage: " + read); + } while (stream.DataAvailable); + + return Encoding.ASCII.GetString(buffer, 0, totalRead); + + } } }