This repository was archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathPipe.cs
More file actions
384 lines (331 loc) · 11.9 KB
/
Copy pathPipe.cs
File metadata and controls
384 lines (331 loc) · 11.9 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Docker.PowerShell.Support
{
internal class Pipe
{
// The ring buffer.
private byte[] _buffer;
// The current insertion and removal pointers in the ring buffer.
int _in, _out;
// The endpoints.
Endpoint _reader, _writer;
bool _eof;
// The lock protecting all the internal state.
object _mutex = new object();
private struct Endpoint
{
public TaskCompletionSource<int> PendingTask;
public ArraySegment<byte> PendingBuffer;
public Exception Failure;
public TaskCompletionSource<int> ReleaseTask()
{
var task = PendingTask;
PendingTask = null;
PendingBuffer = new ArraySegment<byte>();
return task;
}
}
public Pipe(int bufferSize = 65536)
{
if (bufferSize == 0)
{
// SpaceLeft() needs a buffer size of at least 1.
bufferSize = 1;
}
_buffer = new byte[bufferSize];
}
// Number of free bytes in the ring.
private int SpaceLeft()
{
// The ring buffer cannot be filled completely since that state would
// be indistinguishable from an empty ring, so subtract one byte from
// the total capacity.
return _buffer.Length - DataLeft() - 1;
}
// Number of data bytes in the ring.
private int DataLeft()
{
return (_in >= _out) ? (_in - _out) : (_buffer.Length - (_out - _in));
}
// Copy between two array segments and return the number of bytes copied.
private static int Copy(ArraySegment<byte> dest, ArraySegment<byte> source)
{
int count = Math.Min(dest.Count, source.Count);
for (int i = 0; i < count; i++)
{
dest.Array[dest.Offset + i] = source.Array[source.Offset + i];
}
return count;
}
private static ArraySegment<byte> Advance(ArraySegment<byte> segment, int count)
{
return new ArraySegment<byte>(segment.Array, segment.Offset + count, segment.Count - count);
}
// Copies data into the ring and returns the number of bytes copied.
private int CopyIn(ArraySegment<byte> source)
{
int i = 0;
var inp = _in;
var outp = _out;
if (inp >= outp)
{
while (inp < _buffer.Length && i < source.Count)
{
_buffer[inp] = source.Array[source.Offset + i];
i++;
inp++;
}
if (inp == _buffer.Length)
{
inp = 0;
}
}
while (inp + 1 < outp && i < source.Count)
{
_buffer[inp] = source.Array[source.Offset + i];
i++;
inp++;
}
_in = inp;
return i;
}
// Copy bytes out of the ring and return the number of bytes copied.
private int CopyOut(ArraySegment<byte> dest)
{
int i = 0;
var inp = _in;
var outp = _out;
if (outp > inp)
{
while (outp < _buffer.Length && i < dest.Count)
{
dest.Array[dest.Offset + i] = _buffer[outp];
i++;
outp++;
}
if (outp == _buffer.Length)
{
outp = 0;
}
}
while (outp < inp && i < dest.Count)
{
dest.Array[dest.Offset + i] = _buffer[outp];
i++;
outp++;
}
_out = outp;
return i;
}
private Task WriteInternal(ArraySegment<byte> data, CancellationToken cancellationToken, out TaskCompletionSource<int> signaledTask, out int bytesCopied)
{
signaledTask = null;
bytesCopied = 0;
lock (_mutex)
{
if (_reader.Failure != null)
{
throw _reader.Failure;
}
if (_writer.Failure != null || _eof)
{
throw new InvalidOperationException("pipe already closed");
}
if (_writer.PendingTask != null)
{
throw new InvalidOperationException("multiple concurrent writes not supported");
}
// Satisfy the pending reader first.
if (_reader.PendingTask != null)
{
int copied = Copy(_reader.PendingBuffer, data);
signaledTask = _reader.ReleaseTask();
bytesCopied = copied;
if (copied == data.Count)
{
return Task.CompletedTask;
}
// There is more data than fit in the reader's buffer.
data = Advance(data, copied);
}
// Copy data into the ring only if all of it fits. There is no sense buffering if
// we have to block anyway.
if (SpaceLeft() >= data.Count)
{
CopyIn(data);
return Task.CompletedTask;
}
// We must block. Allocate a new task to do the blocking.
var taskSource = new TaskCompletionSource<int>();
// Register a cancellation callback that dequeues the task.
if (cancellationToken.CanBeCanceled)
{
cancellationToken.Register(() => CancelTask(ref _writer, taskSource));
}
_writer.PendingTask = taskSource;
_writer.PendingBuffer = data;
return taskSource.Task;
}
}
public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
int copied;
TaskCompletionSource<int> signaledTask;
var data = new ArraySegment<byte>(buffer, offset, count);
var task = WriteInternal(data, cancellationToken, out signaledTask, out copied);
if (signaledTask != null)
{
signaledTask.SetResult(copied);
}
return task;
}
private Task<int> ReadInternal(ArraySegment<byte> data, CancellationToken cancellationToken, out TaskCompletionSource<int> signaledTask)
{
signaledTask = null;
lock (_mutex)
{
if (_writer.Failure != null)
{
throw _writer.Failure;
}
if (_reader.Failure != null)
{
throw new InvalidOperationException("pipe already closed");
}
if (_reader.PendingTask != null)
{
throw new InvalidOperationException("multiple concurrent reads not supported");
}
// If there is data in the ring, consume it directly. For simplicity, do not
// consume writer data if there is ring data; the caller can call back again.
if (_in != _out)
{
return Task.FromResult(CopyOut(data));
}
if (_eof)
{
// Return 0 to indicate EOF.
return Task.FromResult(0);
}
// If there is a writer, copy its data into the buffer.
if (_writer.PendingTask != null)
{
int copied = Copy(data, _writer.PendingBuffer);
if (copied == _writer.PendingBuffer.Count)
{
signaledTask = _writer.ReleaseTask();
}
else
{
// The writer still has data left. Copy it into the ring buffer if
// doing so would unblock the writer.
var remainingData = Advance(_writer.PendingBuffer, copied);
if (remainingData.Count <= SpaceLeft())
{
CopyIn(remainingData);
signaledTask = _writer.ReleaseTask();
}
else
{
_writer.PendingBuffer = remainingData;
}
}
return Task.FromResult(copied);
}
var taskSource = new TaskCompletionSource<int>();
// Register a cancellation callback that dequeues the task.
if (cancellationToken.CanBeCanceled)
{
cancellationToken.Register(() => CancelTask(ref _reader, taskSource));
}
_reader.PendingTask = taskSource;
_reader.PendingBuffer = data;
return taskSource.Task;
}
}
public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
var data = new ArraySegment<byte>(buffer, offset, count);
TaskCompletionSource<int> signaledTask;
var task = ReadInternal(data, cancellationToken, out signaledTask);
if (signaledTask != null)
{
signaledTask.SetResult(0);
}
return task;
}
public void CloseReader(Exception e = null)
{
// Any writes after the reader has closed should cause a failure.
if (e == null)
{
e = new InvalidOperationException("pipe closed");
}
TaskCompletionSource<int> readTask, writeTask;
lock (_mutex)
{
readTask = _reader.ReleaseTask();
writeTask = _writer.ReleaseTask();
if (_reader.Failure == null)
{
_reader.Failure = e;
}
}
if (writeTask != null)
{
writeTask.SetException(e);
}
if (readTask != null)
{
readTask.SetException(new InvalidOperationException("pipe closed"));
}
}
public void CloseWriter(Exception e = null)
{
TaskCompletionSource<int> readTask, writeTask;
lock (_mutex)
{
readTask = _reader.ReleaseTask();
writeTask = _writer.ReleaseTask();
if (_writer.Failure == null)
{
_writer.Failure = e;
}
_eof = true;
}
if (readTask != null)
{
if (e != null)
{
readTask.SetException(e);
}
else
{
readTask.SetResult(0);
}
}
if (writeTask != null)
{
writeTask.SetException(new InvalidOperationException("pipe closed"));
}
}
private void CancelTask(ref Endpoint endpoint, TaskCompletionSource<int> taskSource)
{
bool cancel = false;
lock (_mutex)
{
if (taskSource == endpoint.PendingTask)
{
endpoint.ReleaseTask();
cancel = true;
}
}
if (cancel)
{
taskSource.SetCanceled();
}
}
}
}