From dded1a5b24b112094a69fdc2e2a1d26fa888a978 Mon Sep 17 00:00:00 2001 From: shinichi Date: Fri, 25 Sep 2020 12:43:01 +0200 Subject: [PATCH] johan's code senior meeting --- Server/Client.cs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Server/Client.cs b/Server/Client.cs index 9dfc946..37a6800 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -14,6 +14,7 @@ namespace Server private byte[] buffer = new byte[1024]; private byte[] totalBuffer = new byte[1024]; private int bytesReceived; + private int totalBufferReceived = 0; public string Username { get; set; } @@ -31,6 +32,33 @@ namespace Server int receivedBytes = this.stream.EndRead(ar); byte[] lengthBytes = new byte[4]; + if (totalBufferReceived + receivedBytes > 1024) + { + throw new OutOfMemoryException("buffer too small"); + } + Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, receivedBytes); + totalBufferReceived += receivedBytes; + + + int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + while (totalBufferReceived >= expectedMessageLength) + { + //volledig packet binnen + byte[] packetBytes = new byte[expectedMessageLength]; + Array.Copy(totalBuffer, 6, packetBytes, 0, expectedMessageLength - 6); + + + Console.WriteLine(Encoding.ASCII.GetString(packetBytes) + " " + expectedMessageLength); + + + + Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength)); + totalBufferReceived -= expectedMessageLength; + expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0); + } + + /* + Array.Copy(this.buffer, 0, lengthBytes, 0, 4); int expectedMessageLength = BitConverter.ToInt32(lengthBytes); @@ -73,7 +101,7 @@ namespace Server this.bytesReceived = 0; } - + */ this.stream.BeginRead(this.buffer, 0, this.buffer.Length, new AsyncCallback(OnRead), null); }