mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 02:53:06 +08:00
propper buffering in place
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user