PipeStream.cs
7.12 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
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
using System;
using System.Buffers;
using System.IO;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using Microsoft;
namespace SoapCore.MessageEncoder
{
#pragma warning disable AvoidAsyncSuffix // Avoid Async suffix
/// <summary>
/// Wraps a <see cref="PipeReader"/> and/or <see cref="PipeWriter"/> as a <see cref="Stream"/> for
/// easier interop with existing APIs.
/// </summary>
internal class PipeStream : Stream, IDisposableObservable
{
/// <summary>
/// A value indicating whether the <see cref="UnderlyingPipeReader"/> should be completed when this instance is disposed.
/// </summary>
private readonly bool _ownsPipe;
/// <summary>
/// Indicates whether reading was completed.
/// </summary>
private bool _readingCompleted;
/// <summary>
/// Initializes a new instance of the <see cref="PipeStream"/> class.
/// </summary>
/// <param name="reader">The <see cref="PipeReader"/> to use when reading from this stream. May be null.</param>
/// <param name="ownsPipe"><c>true</c> to complete the underlying reader and writer when the <see cref="Stream"/> is disposed; <c>false</c> to keep them open.</param>
internal PipeStream(PipeReader reader, bool ownsPipe)
{
Requires.NotNull(reader, nameof(reader));
UnderlyingPipeReader = reader;
_ownsPipe = ownsPipe;
}
/// <inheritdoc />
public bool IsDisposed { get; private set; }
/// <inheritdoc />
public override bool CanRead => !IsDisposed && UnderlyingPipeReader != null;
/// <inheritdoc />
public override bool CanSeek => false;
/// <inheritdoc />
public override bool CanWrite => false;
/// <inheritdoc />
public override long Length => throw ThrowDisposedOr(new NotSupportedException());
/// <inheritdoc />
public override long Position
{
get => throw ThrowDisposedOr(new NotSupportedException());
set => throw ThrowDisposedOr(new NotSupportedException());
}
/// <summary>
/// Gets the underlying <see cref="PipeReader"/> (for purposes of unwrapping instead of stacking adapters).
/// </summary>
internal PipeReader UnderlyingPipeReader { get; }
/// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin) => throw ThrowDisposedOr(new NotSupportedException());
/// <inheritdoc />
public override void SetLength(long value) => throw ThrowDisposedOr(new NotSupportedException());
/// <inheritdoc />
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
/// <inheritdoc />
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Requires.NotNull(buffer, nameof(buffer));
Requires.Range(offset + count <= buffer.Length, nameof(count));
Requires.Range(offset >= 0, nameof(offset));
Requires.Range(count > 0, nameof(count));
Verify.NotDisposed(this);
if (UnderlyingPipeReader == null)
{
throw new NotSupportedException();
}
if (_readingCompleted)
{
return 0;
}
ReadResult readResult = await UnderlyingPipeReader.ReadAsync(cancellationToken).ConfigureAwait(false);
return ReadHelper(buffer.AsSpan(offset, count), readResult);
}
#if SPAN_BUILTIN
/// <inheritdoc />
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
Verify.NotDisposed(this);
if (UnderlyingPipeReader == null)
{
throw new NotSupportedException();
}
if (_readingCompleted)
{
return 0;
}
ReadResult readResult = await UnderlyingPipeReader.ReadAsync(cancellationToken).ConfigureAwait(false);
return ReadHelper(buffer.Span, readResult);
}
/// <inheritdoc />
public override int Read(Span<byte> buffer)
{
Verify.NotDisposed(this);
if (UnderlyingPipeReader == null)
{
throw new NotSupportedException();
}
if (_readingCompleted)
{
return 0;
}
if (UnderlyingPipeReader.TryRead(out var readResult))
{
return ReadHelper(buffer, readResult);
}
UnderlyingPipeReader.ReadAsync();
return 0;
}
/// <inheritdoc />
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
/// <inheritdoc />
public override void Write(ReadOnlySpan<byte> buffer)
{
throw new NotSupportedException();
}
#endif
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
/// <inheritdoc />
public override void Flush()
{
throw new NotSupportedException();
}
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
Requires.NotNull(buffer, nameof(buffer));
Requires.Range(offset + count <= buffer.Length, nameof(count));
Requires.Range(offset >= 0, nameof(offset));
Requires.Range(count > 0, nameof(count));
Verify.NotDisposed(this);
if (UnderlyingPipeReader == null)
{
throw new NotSupportedException();
}
if (_readingCompleted)
{
return 0;
}
return TryReadHelper(buffer.AsSpan(offset, count));
}
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
IsDisposed = true;
UnderlyingPipeReader?.CancelPendingRead();
if (_ownsPipe)
{
UnderlyingPipeReader?.Complete();
}
base.Dispose(disposing);
}
private Exception ThrowDisposedOr(Exception ex)
{
Verify.NotDisposed(this);
throw ex;
}
private int TryReadHelper(Span<byte> buffer)
{
if (UnderlyingPipeReader.TryRead(out var readResult))
{
var bytesRead = ReadHelper(buffer, readResult);
UnderlyingPipeReader.ReadAsync();
return bytesRead;
}
UnderlyingPipeReader.ReadAsync();
Thread.Yield();
if (UnderlyingPipeReader.TryRead(out readResult))
{
return ReadHelper(buffer, readResult);
}
return 0;
}
private int ReadHelper(Span<byte> buffer, ReadResult readResult)
{
if (readResult.IsCanceled && IsDisposed)
{
return 0;
}
long bytesToCopyCount = Math.Min(buffer.Length, readResult.Buffer.Length);
ReadOnlySequence<byte> slice = readResult.Buffer.Slice(0, bytesToCopyCount);
slice.CopyTo(buffer);
UnderlyingPipeReader.AdvanceTo(slice.End);
if (readResult.IsCompleted && slice.End.Equals(readResult.Buffer.End))
{
UnderlyingPipeReader.Complete();
_readingCompleted = true;
}
return (int)bytesToCopyCount;
}
}
}