mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +08:00
use streams
This commit is contained in:
@@ -17,6 +17,9 @@ namespace MQTTnet.Implementations
|
||||
private Socket _socket;
|
||||
private SslStream _sslStream;
|
||||
|
||||
|
||||
public Stream Stream => _dataStream;
|
||||
|
||||
/// <summary>
|
||||
/// called on client sockets are created in connect
|
||||
/// </summary>
|
||||
@@ -78,45 +81,6 @@ namespace MQTTnet.Implementations
|
||||
}
|
||||
}
|
||||
|
||||
public async Task WriteAsync(byte[] buffer)
|
||||
{
|
||||
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
|
||||
|
||||
try
|
||||
{
|
||||
await _dataStream.WriteAsync(buffer, 0, buffer.Length);
|
||||
}
|
||||
catch (SocketException exception)
|
||||
{
|
||||
throw new MqttCommunicationException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ArraySegment<byte>> ReadAsync(int length, byte[] buffer)
|
||||
{
|
||||
try
|
||||
{
|
||||
var totalBytes = 0;
|
||||
|
||||
do
|
||||
{
|
||||
var read = await _dataStream.ReadAsync(buffer, totalBytes, length - totalBytes).ConfigureAwait(false);
|
||||
if (read == 0)
|
||||
{
|
||||
throw new MqttCommunicationException(new SocketException((int)SocketError.Disconnecting));
|
||||
}
|
||||
|
||||
totalBytes += read;
|
||||
}
|
||||
while (totalBytes < length);
|
||||
return new ArraySegment<byte>(buffer, 0, length);
|
||||
}
|
||||
catch (SocketException exception)
|
||||
{
|
||||
throw new MqttCommunicationException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_socket?.Dispose();
|
||||
|
||||
Reference in New Issue
Block a user