mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
use streams
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Client;
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Core.Channel
|
||||
{
|
||||
public class BufferedCommunicationChannel : IMqttCommunicationChannel
|
||||
{
|
||||
private readonly IMqttCommunicationChannel _inner;
|
||||
private int _bufferSize;
|
||||
private int _bufferOffset;
|
||||
|
||||
public BufferedCommunicationChannel(IMqttCommunicationChannel inner)
|
||||
{
|
||||
_inner = inner;
|
||||
}
|
||||
|
||||
public Task ConnectAsync(MqttClientOptions options)
|
||||
{
|
||||
return _inner.ConnectAsync(options);
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
return _inner.DisconnectAsync();
|
||||
}
|
||||
|
||||
public int Peek()
|
||||
{
|
||||
return _inner.Peek();
|
||||
}
|
||||
|
||||
public async Task<ArraySegment<byte>> ReadAsync(int length, byte[] buffer)
|
||||
{
|
||||
//read from buffer
|
||||
if (_bufferSize > 0)
|
||||
{
|
||||
return ReadFomBuffer(length, buffer);
|
||||
}
|
||||
|
||||
var available = _inner.Peek();
|
||||
// if there are less or equal bytes available then requested then just read em
|
||||
if (available <= length)
|
||||
{
|
||||
return await _inner.ReadAsync(length, buffer);
|
||||
}
|
||||
|
||||
//if more bytes are available than requested do buffer them to reduce calls to network buffers
|
||||
await WriteToBuffer(available, buffer).ConfigureAwait(false);
|
||||
return ReadFomBuffer(length, buffer);
|
||||
}
|
||||
|
||||
private async Task WriteToBuffer(int available, byte[] buffer)
|
||||
{
|
||||
await _inner.ReadAsync(available, buffer).ConfigureAwait(false);
|
||||
_bufferSize = available;
|
||||
_bufferOffset = 0;
|
||||
}
|
||||
|
||||
private ArraySegment<byte> ReadFomBuffer(int length, byte[] buffer)
|
||||
{
|
||||
var result = new ArraySegment<byte>(buffer, _bufferOffset, length);
|
||||
_bufferSize -= length;
|
||||
_bufferOffset += length;
|
||||
|
||||
if (_bufferSize < 0)
|
||||
{
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task WriteAsync(byte[] buffer)
|
||||
{
|
||||
return _inner.WriteAsync(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Client;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace MQTTnet.Core.Channel
|
||||
{
|
||||
@@ -9,14 +9,7 @@ namespace MQTTnet.Core.Channel
|
||||
Task ConnectAsync(MqttClientOptions options);
|
||||
|
||||
Task DisconnectAsync();
|
||||
|
||||
Task WriteAsync(byte[] buffer);
|
||||
|
||||
/// <summary>
|
||||
/// get the currently available number of bytes without reading them
|
||||
/// </summary>
|
||||
int Peek();
|
||||
|
||||
Task<ArraySegment<byte>> ReadAsync(int length, byte[] buffer);
|
||||
|
||||
Stream Stream { get; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user