propper buffering in place

This commit is contained in:
JanEggers
2017-09-10 18:20:25 +02:00
parent 644bcfba27
commit 9cde49f5f8
14 changed files with 227 additions and 131 deletions
@@ -93,17 +93,15 @@ namespace MQTTnet.Implementations
}
}
public async Task ReadAsync(byte[] buffer)
public async Task<ArraySegment<byte>> ReadAsync(int length, byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
try
{
var totalBytes = 0;
do
{
var read = await _dataStream.ReadAsync(buffer, totalBytes, buffer.Length - totalBytes).ConfigureAwait(false);
var read = await _dataStream.ReadAsync(buffer, totalBytes, length - totalBytes).ConfigureAwait(false);
if (read == 0)
{
throw new MqttCommunicationException(new SocketException((int)SocketError.Disconnecting));
@@ -111,7 +109,8 @@ namespace MQTTnet.Implementations
totalBytes += read;
}
while (totalBytes < buffer.Length);
while (totalBytes < length);
return new ArraySegment<byte>(buffer, 0, length);
}
catch (SocketException exception)
{
@@ -143,5 +142,10 @@ namespace MQTTnet.Implementations
return certificates;
}
public int Peek()
{
return _socket.Available;
}
}
}