Fix client crash when historic is too big
Because you deserve it
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public const int TCP_CLIENT_PORT = 1403;
|
||||
|
||||
/// <summary>
|
||||
/// Bytes to read at a time in a <see cref="NetworkStream"/>
|
||||
/// </summary>
|
||||
public const int BUFFER_SIZE = 4096;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user