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

35 lines
1.1 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using CliWrap;
namespace InnovEnergy.Lib.Channels.V2.Pipes;
public class ChannelPipeTarget : PipeTarget
{
private readonly IStreamChannel _StreamChannel;
private Byte[] _Buffer;
private ChannelPipeTarget(IStreamChannel streamChannel, UInt32 bufferSize = 64)
{
_StreamChannel = streamChannel;
_Buffer = new Byte[bufferSize];
}
public static ChannelPipeTarget FromStreamChannel(IStreamChannel streamChannel, UInt32 bufferSize)
{
return new ChannelPipeTarget(streamChannel, bufferSize);
}
public static PipeTarget FromStreamChannel(IStreamChannel streamChannel)
{
return new ChannelPipeTarget(streamChannel);
}
public override async Task CopyFromAsync(Stream source, CancellationToken cancellationToken = new CancellationToken())
{
var nRead = await source.ReadAsync(_Buffer, 0, _Buffer.Length, cancellationToken);
await _StreamChannel.Write(new ArraySegment<Byte>(_Buffer, 0, nRead));
if (nRead >= _Buffer.Length)
_Buffer = new Byte[_Buffer.Length * 2];
}
}