2018-01-03 05:45:53 +01:00
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace k8s
|
|
|
|
|
{
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// A <see cref="Stream"/> which reads/writes from a specific channel using a <see cref="StreamDemuxer" />.
|
|
|
|
|
/// </summary>
|
2018-01-03 05:45:53 +01:00
|
|
|
public class MuxedStream : Stream
|
|
|
|
|
{
|
|
|
|
|
private ByteBuffer inputBuffer;
|
|
|
|
|
private byte? outputIndex;
|
|
|
|
|
private StreamDemuxer muxer;
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="MuxedStream"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="muxer">
|
|
|
|
|
/// The <see cref="StreamDemuxer"/> to use to read from/write to the underlying stream.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="inputBuffer">
|
|
|
|
|
/// The <see cref="inputBuffer"/> to read from.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="outputIndex">
|
|
|
|
|
/// The index of the channel to which to write.
|
|
|
|
|
/// </param>
|
2018-01-03 05:45:53 +01:00
|
|
|
public MuxedStream(StreamDemuxer muxer, ByteBuffer inputBuffer, byte? outputIndex)
|
|
|
|
|
{
|
|
|
|
|
this.inputBuffer = inputBuffer;
|
|
|
|
|
this.outputIndex = outputIndex;
|
|
|
|
|
|
|
|
|
|
if (this.inputBuffer == null && outputIndex == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("You must specify at least inputBuffer or outputIndex");
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 23:18:34 +02:00
|
|
|
if (outputIndex != null)
|
|
|
|
|
{
|
|
|
|
|
this.muxer = muxer ?? throw new ArgumentNullException(nameof(muxer));
|
|
|
|
|
}
|
2018-01-03 05:45:53 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2020-11-22 14:52:09 -08:00
|
|
|
public override bool CanRead => inputBuffer != null;
|
2018-01-03 05:45:53 +01:00
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override bool CanSeek => false;
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2020-11-22 14:52:09 -08:00
|
|
|
public override bool CanWrite => outputIndex != null;
|
2018-01-03 05:45:53 +01:00
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override long Length => throw new NotSupportedException();
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => throw new NotSupportedException();
|
|
|
|
|
set => throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
if (outputIndex == null)
|
2018-01-03 05:45:53 +01:00
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
muxer.Write(outputIndex.Value, buffer, offset, count).GetAwaiter().GetResult();
|
2018-01-03 05:45:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
if (inputBuffer == null)
|
2018-01-03 05:45:53 +01:00
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
return inputBuffer.Read(buffer, offset, count);
|
2018-01-03 05:45:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override void Flush()
|
|
|
|
|
{
|
2018-04-27 06:15:25 +02:00
|
|
|
// Whenever we call muxer.Write, a message is immediately sent over the wire, so we don't need/support flushing.
|
|
|
|
|
// Implement flushing as a no-op operation as opposed to throwing a NotSupportedException.
|
2018-01-03 05:45:53 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-27 06:14:38 +02:00
|
|
|
/// <inheritdoc/>
|
2018-01-03 05:45:53 +01:00
|
|
|
public override void SetLength(long value)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|