Innovenergy_trunk/csharp/Lib/Channels/V2/Pipes/AsyncPipeTarget.cs

25 lines
758 B
C#

using CliWrap;
namespace InnovEnergy.Lib.Channels.V2.Pipes;
public class AsyncPipeTarget : PipeTarget
{
private readonly Func<IReadOnlyList<Byte>, Task> _Write;
private Byte[] _Buffer;
public AsyncPipeTarget(Func<IReadOnlyList<Byte>, Task> write, UInt32 bufferSize = 64)
{
_Write = write;
_Buffer = new Byte[bufferSize];
}
public override async Task CopyFromAsync(Stream source, CancellationToken cancellationToken = new CancellationToken())
{
var nRead = await source.ReadAsync(_Buffer, 0, _Buffer.Length, cancellationToken);
await _Write(new ArraySegment<Byte>(_Buffer, 0, nRead));
if (nRead >= _Buffer.Length)
_Buffer = new Byte[_Buffer.Length * 2];
}
}