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

25 lines
758 B
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
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];
}
}