-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathStreamDemuxer.cs
304 lines (276 loc) · 11.9 KB
/
StreamDemuxer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System.Buffers;
using System.Diagnostics;
using System.Net.WebSockets;
namespace k8s
{
/// <summary>
/// <para>
/// The <see cref="StreamDemuxer"/> allows you to interact with processes running in a container in a Kubernetes pod. You can start an exec or attach command
/// by calling <see cref="Kubernetes.WebSocketNamespacedPodExecAsync(string, string, IEnumerable{string}, string, bool, bool, bool, bool, Dictionary{string, List{string}}, CancellationToken)"/>
/// or <see cref="Kubernetes.WebSocketNamespacedPodAttachAsync(string, string, string, bool, bool, bool, bool, Dictionary{string, List{string}}, CancellationToken)"/>. These methods
/// will return you a <see cref="WebSocket"/> connection.
/// </para>
/// <para>
/// Kubernetes 'multiplexes' multiple channels over this <see cref="WebSocket"/> connection, such as standard input, standard output and standard error. The <see cref="StreamDemuxer"/>
/// allows you to extract individual <see cref="Stream"/>s from this <see cref="WebSocket"/> class. You can then use these streams to send/receive data from that process.
/// </para>
/// </summary>
public class StreamDemuxer : IStreamDemuxer
{
private const int MAXFRAMESIZE = 15 * 1024 * 1024; // 15MB
private readonly WebSocket webSocket;
private readonly Dictionary<byte, ByteBuffer> buffers = new Dictionary<byte, ByteBuffer>();
private readonly CancellationTokenSource cts = new CancellationTokenSource();
private readonly StreamType streamType;
private readonly bool ownsSocket;
private Task runLoop;
private bool disposedValue;
/// <summary>
/// Initializes a new instance of the <see cref="StreamDemuxer"/> class.
/// </summary>
/// <param name="webSocket">
/// A <see cref="WebSocket"/> which contains a multiplexed stream, such as the <see cref="WebSocket"/> returned by the exec or attach commands.
/// </param>
/// <param name="streamType">
/// A <see cref="StreamType"/> specifies the type of the stream.
/// </param>
/// <param name="ownsSocket">
/// A value indicating whether this instance of the <see cref="StreamDemuxer"/> owns the underlying <see cref="WebSocket"/>,
/// and should dispose of it when this instance is disposed of.
/// </param>
public StreamDemuxer(WebSocket webSocket, StreamType streamType = StreamType.RemoteCommand,
bool ownsSocket = false)
{
this.streamType = streamType;
this.webSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket));
this.ownsSocket = ownsSocket;
}
public event EventHandler ConnectionClosed;
/// <summary>
/// Starts reading the data sent by the server.
/// </summary>
public void Start()
{
runLoop = Task.Run(async () => await RunLoop(cts.Token).ConfigureAwait(false));
}
/// <summary>
/// Gets a <see cref="Stream"/> which allows you to read to and/or write from a remote channel.
/// </summary>
/// <param name="inputIndex">
/// The index of the channel from which to read.
/// </param>
/// <param name="outputIndex">
/// The index of the channel to which to write.
/// </param>
/// <returns>
/// A <see cref="Stream"/> which allows you to read/write to the requested channels.
/// </returns>
public Stream GetStream(ChannelIndex? inputIndex, ChannelIndex? outputIndex)
{
return GetStream((byte?)inputIndex, (byte?)outputIndex);
}
/// <summary>
/// Gets a <see cref="Stream"/> which allows you to read to and/or write from a remote channel.
/// </summary>
/// <param name="inputIndex">
/// The index of the channel from which to read.
/// </param>
/// <param name="outputIndex">
/// The index of the channel to which to write.
/// </param>
/// <returns>
/// A <see cref="Stream"/> which allows you to read/write to the requested channels.
/// </returns>
public Stream GetStream(byte? inputIndex, byte? outputIndex)
{
lock (buffers)
{
if (inputIndex != null && !buffers.ContainsKey(inputIndex.Value))
{
var buffer = new ByteBuffer();
buffers.Add(inputIndex.Value, buffer);
}
}
var inputBuffer = inputIndex == null ? null : buffers[inputIndex.Value];
return new MuxedStream(this, inputBuffer, outputIndex);
}
/// <summary>
/// Directly writes data to a channel.
/// </summary>
/// <param name="index">
/// The index of the channel to which to write.
/// </param>
/// <param name="buffer">
/// The buffer from which to read data.
/// </param>
/// <param name="offset">
/// The offset at which to start reading.
/// </param>
/// <param name="count">
/// The number of bytes to read.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> which represents the asynchronous operation.
/// </returns>
public Task Write(ChannelIndex index, byte[] buffer, int offset, int count,
CancellationToken cancellationToken = default)
{
return Write((byte)index, buffer, offset, count, cancellationToken);
}
/// <summary>
/// Directly writes data to a channel.
/// </summary>
/// <param name="index">
/// The index of the channel to which to write.
/// </param>
/// <param name="buffer">
/// The buffer from which to read data.
/// </param>
/// <param name="offset">
/// The offset at which to start reading.
/// </param>
/// <param name="count">
/// The number of bytes to read.
/// </param>
/// <param name="cancellationToken">
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
/// </param>
/// <returns>
/// A <see cref="Task"/> which represents the asynchronous operation.
/// </returns>
public async Task Write(byte index, byte[] buffer, int offset, int count,
CancellationToken cancellationToken = default)
{
var writeBuffer = ArrayPool<byte>.Shared.Rent(Math.Min(count, MAXFRAMESIZE) + 1);
try
{
writeBuffer[0] = index;
for (var i = 0; i < count; i += MAXFRAMESIZE)
{
var c = Math.Min(count - i, MAXFRAMESIZE);
Buffer.BlockCopy(buffer, offset + i, writeBuffer, 1, c);
var segment = new ArraySegment<byte>(writeBuffer, 0, c + 1);
await webSocket.SendAsync(segment, WebSocketMessageType.Binary, false, cancellationToken)
.ConfigureAwait(false);
}
}
finally
{
ArrayPool<byte>.Shared.Return(writeBuffer);
}
}
protected async Task RunLoop(CancellationToken cancellationToken)
{
// Get a 1KB buffer
var buffer = ArrayPool<byte>.Shared.Rent(1024 * 1024);
// This maps remembers bytes skipped for each stream.
var streamBytesToSkipMap = new Dictionary<byte, int>();
try
{
var segment = new ArraySegment<byte>(buffer);
while (!cancellationToken.IsCancellationRequested && webSocket.CloseStatus == null)
{
// We always get data in this format:
// [stream index] (1 for stdout, 2 for stderr)
// [payload]
var result = await webSocket.ReceiveAsync(segment, cancellationToken).ConfigureAwait(false);
// Ignore empty messages
if (result.Count < 2)
{
continue;
}
var streamIndex = buffer[0];
var extraByteCount = 1;
while (true)
{
var bytesToSkip = 0;
if (!streamBytesToSkipMap.TryGetValue(streamIndex, out bytesToSkip))
{
// When used in port-forwarding, the first 2 bytes from the web socket is port bytes, skip.
// https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/server/portforward/websocket.go
bytesToSkip = streamType == StreamType.PortForward ? 2 : 0;
}
var bytesCount = result.Count - extraByteCount;
if (bytesToSkip > 0 && bytesToSkip >= bytesCount)
{
// skip the entire data.
bytesToSkip -= bytesCount;
extraByteCount += bytesCount;
bytesCount = 0;
}
else
{
bytesCount -= bytesToSkip;
extraByteCount += bytesToSkip;
bytesToSkip = 0;
if (buffers.ContainsKey(streamIndex))
{
buffers[streamIndex].Write(buffer, extraByteCount, bytesCount);
}
}
streamBytesToSkipMap[streamIndex] = bytesToSkip;
if (result.EndOfMessage == true)
{
break;
}
extraByteCount = 0;
result = await webSocket.ReceiveAsync(segment, cancellationToken).ConfigureAwait(false);
}
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
runLoop = null;
foreach (var b in buffers.Values)
{
b.WriteEnd();
}
ConnectionClosed?.Invoke(this, EventArgs.Empty);
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
try
{
if (runLoop != null)
{
cts.Cancel();
cts.Dispose();
runLoop.Wait();
}
}
catch (Exception ex)
{
// Dispose methods can never throw.
Debug.Write(ex);
}
if (ownsSocket)
{
webSocket.Dispose();
}
}
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~StreamDemuxer()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(true);
GC.SuppressFinalize(this);
}
}
}