From 46cb938ea2736e816bcb26ade83440ecd5bf7d86 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Fri, 10 May 2019 20:27:37 +0200 Subject: [PATCH] Fix client crash when historic is too big Because you deserve it --- .../Communication/CommandStream.cs | 56 +++++++++++++++++-- PlantBox.Shared/Communication/Connection.cs | 6 ++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/PlantBox.Shared/Communication/CommandStream.cs b/PlantBox.Shared/Communication/CommandStream.cs index 2933287..3a57b0a 100644 --- a/PlantBox.Shared/Communication/CommandStream.cs +++ b/PlantBox.Shared/Communication/CommandStream.cs @@ -45,7 +45,16 @@ namespace PlantBox.Shared.Communication // Data buffer = new byte[length]; - _stream.Read(buffer, 0, buffer.Length); + int position = 0; + + while (position < length) + { + int bytesToRead = Math.Min(Connection.BUFFER_SIZE, (int)length - position); + + int bytesRead = _stream.Read(buffer, position, bytesToRead); + + position += bytesRead; + } string[] packet = Encoding.UTF8.GetString(buffer).Split('\n'); @@ -83,7 +92,16 @@ namespace PlantBox.Shared.Communication // Data buffer = new byte[length]; - await _stream.ReadAsync(buffer, 0, buffer.Length); + int position = 0; + + while (position < length) + { + int bytesToRead = Math.Min(Connection.BUFFER_SIZE, (int)length - position); + + int bytesRead = await _stream.ReadAsync(buffer, position, bytesToRead); + + position += bytesRead; + } string[] packet = Encoding.UTF8.GetString(buffer).Split('\n'); @@ -115,9 +133,22 @@ namespace PlantBox.Shared.Communication { string packet = command.ToString(); byte[] data = Encoding.UTF8.GetBytes(packet); + uint length = (uint)data.Length; - _stream.Write(BitConverter.GetBytes((uint)data.Length), 0, 4); - _stream.Write(data, 0, data.Length); + // Length + _stream.Write(BitConverter.GetBytes(length), 0, 4); + + // Data + int position = 0; + + while (position < length) + { + int bytesToWrite = Math.Min(Connection.BUFFER_SIZE, (int)length - position); + + _stream.Write(data, position, bytesToWrite); + + position += bytesToWrite; + } } /// @@ -129,9 +160,22 @@ namespace PlantBox.Shared.Communication { string packet = command.ToString(); byte[] data = Encoding.UTF8.GetBytes(packet); + uint length = (uint)data.Length; - await _stream.WriteAsync(BitConverter.GetBytes((uint)data.Length), 0, 4); - await _stream.WriteAsync(data, 0, data.Length); + // Length + await _stream.WriteAsync(BitConverter.GetBytes(length), 0, 4); + + // Data + int position = 0; + + while (position < length) + { + int bytesToWrite = Math.Min(Connection.BUFFER_SIZE, (int)length - position); + + await _stream.WriteAsync(data, position, bytesToWrite); + + position += bytesToWrite; + } } } } diff --git a/PlantBox.Shared/Communication/Connection.cs b/PlantBox.Shared/Communication/Connection.cs index 2240b52..c1b4b57 100644 --- a/PlantBox.Shared/Communication/Connection.cs +++ b/PlantBox.Shared/Communication/Connection.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.Net.Sockets; namespace PlantBox.Shared.Communication { @@ -32,5 +33,10 @@ namespace PlantBox.Shared.Communication /// Port used by the broker to accept clients /// public const int TCP_CLIENT_PORT = 1403; + + /// + /// Bytes to read at a time in a + /// + public const int BUFFER_SIZE = 4096; } }