| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.IO.Pipelines; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels.Framing |
| | | 13 | | { |
| | | 14 | | public class RawStream : Stream |
| | | 15 | | { |
| | | 16 | | #if DEBUG |
| | | 17 | | #pragma warning disable IDE0052 // Remove unread private members |
| | | 18 | | private readonly FramingConnection _connection; |
| | | 19 | | #pragma warning restore IDE0052 // Remove unread private members |
| | | 20 | | #endif |
| | | 21 | | private readonly PipeReader _input; |
| | | 22 | | private readonly PipeWriter _output; |
| | | 23 | | private bool _canRead; |
| | | 24 | | private readonly object _thisLock; |
| | | 25 | | private TaskCompletionSource<object> _unwrapTcs; |
| | 0 | 26 | | private readonly SemaphoreSlim _readSemaphore = new SemaphoreSlim(1, 1); |
| | | 27 | | |
| | 0 | 28 | | public RawStream(FramingConnection connection) |
| | | 29 | | { |
| | | 30 | | #if DEBUG |
| | | 31 | | _connection = connection; |
| | | 32 | | #endif |
| | 0 | 33 | | _input = connection.Input; |
| | 0 | 34 | | _output = connection.Output; |
| | 0 | 35 | | _canRead = true; |
| | 0 | 36 | | _thisLock = new object(); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public override bool CanRead |
| | | 40 | | { |
| | | 41 | | get |
| | | 42 | | { |
| | 0 | 43 | | lock (_thisLock) |
| | | 44 | | { |
| | 0 | 45 | | return _canRead; |
| | | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | public override bool CanSeek => false; |
| | | 51 | | |
| | 0 | 52 | | public override bool CanWrite => true; |
| | | 53 | | |
| | | 54 | | public override long Length |
| | | 55 | | { |
| | | 56 | | get |
| | | 57 | | { |
| | 0 | 58 | | throw new NotSupportedException(); |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public override long Position |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | throw new NotSupportedException(); |
| | | 67 | | } |
| | | 68 | | set |
| | | 69 | | { |
| | 0 | 70 | | throw new NotSupportedException(); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 75 | | { |
| | 0 | 76 | | throw new NotSupportedException(); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public override void SetLength(long value) |
| | | 80 | | { |
| | 0 | 81 | | throw new NotSupportedException(); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 85 | | { |
| | | 86 | | // ValueTask uses .GetAwaiter().GetResult() if necessary |
| | | 87 | | // https://github.com/dotnet/corefx/blob/f9da3b4af08214764a51b2331f3595ffaf162abe/src/System.Threading.Tasks |
| | 0 | 88 | | return ReadAsyncInternal(new Memory<byte>(buffer, offset, count)).Result; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 92 | | { |
| | 0 | 93 | | return ReadAsyncInternal(new Memory<byte>(buffer, offset, count)).AsTask(); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // TODO: Uncomment once moved to netstandard2.1+ |
| | | 97 | | // public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = defa |
| | | 98 | | // { |
| | | 99 | | // return ReadAsyncInternal(destination); |
| | | 100 | | // } |
| | | 101 | | |
| | | 102 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 103 | | { |
| | 0 | 104 | | WriteAsync(buffer, offset, count).GetAwaiter().GetResult(); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 108 | | { |
| | 0 | 109 | | if (buffer != null) |
| | | 110 | | { |
| | 0 | 111 | | _output.Write(new ReadOnlySpan<byte>(buffer, offset, count)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | await _output.FlushAsync(cancellationToken); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | // TODO: Uncomment once moved to netstandard2.1+ |
| | | 118 | | //public override async ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = |
| | | 119 | | //{ |
| | | 120 | | // _output.Write(source.Span); |
| | | 121 | | // await _output.FlushAsync(cancellationToken); |
| | | 122 | | //} |
| | | 123 | | |
| | | 124 | | public override void Flush() |
| | | 125 | | { |
| | 0 | 126 | | FlushAsync(CancellationToken.None).GetAwaiter().GetResult(); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | | 130 | | { |
| | 0 | 131 | | return WriteAsync(null, 0, 0, cancellationToken); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination) |
| | | 135 | | { |
| | 0 | 136 | | await _readSemaphore.WaitAsync(); |
| | | 137 | | try |
| | | 138 | | { |
| | | 139 | | while (true) |
| | | 140 | | { |
| | 0 | 141 | | if (!CanRead) |
| | | 142 | | { |
| | 0 | 143 | | await _unwrapTcs.Task; |
| | 0 | 144 | | return 0; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | ReadResult result = await _input.ReadAsync(); |
| | 0 | 148 | | if (!CanRead) |
| | | 149 | | { |
| | 0 | 150 | | _input.AdvanceTo(result.Buffer.Start); |
| | 0 | 151 | | await _unwrapTcs.Task; |
| | 0 | 152 | | return 0; |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | ReadOnlySequence<byte> readableBuffer = result.Buffer; |
| | | 156 | | try |
| | | 157 | | { |
| | 0 | 158 | | if (!readableBuffer.IsEmpty) |
| | | 159 | | { |
| | | 160 | | // buffer.Count is int |
| | 0 | 161 | | int count = (int)Math.Min(readableBuffer.Length, destination.Length); |
| | 0 | 162 | | readableBuffer = readableBuffer.Slice(0, count); |
| | 0 | 163 | | readableBuffer.CopyTo(destination.Span); |
| | 0 | 164 | | return count; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | if (result.IsCompleted) |
| | | 168 | | { |
| | | 169 | | // Treat a closed PipeReader as end-of-stream instead of looping; |
| | | 170 | | // otherwise an empty + completed result would cause this read to |
| | | 171 | | // make no progress. |
| | 0 | 172 | | return 0; |
| | | 173 | | } |
| | 0 | 174 | | } |
| | | 175 | | finally |
| | | 176 | | { |
| | 0 | 177 | | _input.AdvanceTo(readableBuffer.End, readableBuffer.End); |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | finally |
| | | 182 | | { |
| | | 183 | | Fx.Assert(_readSemaphore.CurrentCount == 0, "_readSemaphore double release"); |
| | 0 | 184 | | _readSemaphore.Release(); |
| | | 185 | | } |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object stat |
| | | 189 | | { |
| | 0 | 190 | | Task<int> task = ReadAsync(buffer, offset, count, default, state); |
| | 0 | 191 | | if (callback != null) |
| | | 192 | | { |
| | 0 | 193 | | task.ContinueWith(t => callback.Invoke(t)); |
| | | 194 | | } |
| | 0 | 195 | | return task; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | public override int EndRead(IAsyncResult asyncResult) |
| | | 199 | | { |
| | 0 | 200 | | return ((Task<int>)asyncResult).GetAwaiter().GetResult(); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object st |
| | | 204 | | { |
| | 0 | 205 | | var tcs = new TaskCompletionSource<int>(state); |
| | 0 | 206 | | Task<int> task = ReadAsync(buffer, offset, count, cancellationToken); |
| | 0 | 207 | | task.ContinueWith((task2, state2) => |
| | 0 | 208 | | { |
| | 0 | 209 | | var tcs2 = (TaskCompletionSource<int>)state2; |
| | 0 | 210 | | if (task2.IsCanceled) |
| | 0 | 211 | | { |
| | 0 | 212 | | tcs2.SetCanceled(); |
| | 0 | 213 | | } |
| | 0 | 214 | | else if (task2.IsFaulted) |
| | 0 | 215 | | { |
| | 0 | 216 | | tcs2.SetException(task2.Exception); |
| | 0 | 217 | | } |
| | 0 | 218 | | else |
| | 0 | 219 | | { |
| | 0 | 220 | | tcs2.SetResult(task2.Result); |
| | 0 | 221 | | } |
| | 0 | 222 | | }, tcs, cancellationToken); |
| | 0 | 223 | | return tcs.Task; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object sta |
| | | 227 | | { |
| | 0 | 228 | | Task task = WriteAsync(buffer, offset, count, default, state); |
| | 0 | 229 | | if (callback != null) |
| | | 230 | | { |
| | 0 | 231 | | task.ContinueWith(t => callback.Invoke(t)); |
| | | 232 | | } |
| | 0 | 233 | | return task; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public override void EndWrite(IAsyncResult asyncResult) |
| | | 237 | | { |
| | 0 | 238 | | ((Task<object>)asyncResult).GetAwaiter().GetResult(); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | private Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state) |
| | | 242 | | { |
| | 0 | 243 | | var tcs = new TaskCompletionSource<object>(state); |
| | 0 | 244 | | Task task = WriteAsync(buffer, offset, count, cancellationToken); |
| | 0 | 245 | | task.ContinueWith((task2, state2) => |
| | 0 | 246 | | { |
| | 0 | 247 | | var tcs2 = (TaskCompletionSource<object>)state2; |
| | 0 | 248 | | if (task2.IsCanceled) |
| | 0 | 249 | | { |
| | 0 | 250 | | tcs2.SetCanceled(); |
| | 0 | 251 | | } |
| | 0 | 252 | | else if (task2.IsFaulted) |
| | 0 | 253 | | { |
| | 0 | 254 | | tcs2.SetException(task2.Exception); |
| | 0 | 255 | | } |
| | 0 | 256 | | else |
| | 0 | 257 | | { |
| | 0 | 258 | | tcs2.SetResult(null); |
| | 0 | 259 | | } |
| | 0 | 260 | | }, tcs, cancellationToken); |
| | 0 | 261 | | return tcs.Task; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | public void StartUnwrapRead() |
| | | 265 | | { |
| | | 266 | | // Upon sending the session end byte, the client can start another session immediately. |
| | | 267 | | // We need to stop those bytes from being consumed by the upgrade stream (e.g. NegotiateStream), |
| | | 268 | | // but we can't return a zero byte response to the pending read until after we've sent the session |
| | | 269 | | // end byte otherwise it will close the upgrade stream and prevent the session end byte from being |
| | | 270 | | // sent. Calling StartUnwrapRead prevents any reads from completing until FinisheUnwrapRead has |
| | | 271 | | // been called. This ensures any client bytes from the next session are not consumed and still |
| | | 272 | | // allows a write to be sent through the wrapping stream. |
| | | 273 | | |
| | 0 | 274 | | bool acquired = _readSemaphore.Wait(0); |
| | | 275 | | try |
| | | 276 | | { |
| | 0 | 277 | | lock (_thisLock) |
| | | 278 | | { |
| | 0 | 279 | | _unwrapTcs = new TaskCompletionSource<object>(); |
| | 0 | 280 | | _canRead = false; |
| | 0 | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | _input.CancelPendingRead(); |
| | 0 | 284 | | } |
| | | 285 | | finally |
| | | 286 | | { |
| | 0 | 287 | | if (acquired) |
| | | 288 | | { |
| | 0 | 289 | | _readSemaphore.Release(); |
| | | 290 | | } |
| | 0 | 291 | | } |
| | 0 | 292 | | } |
| | | 293 | | |
| | | 294 | | public async Task FinishUnwrapReadAsync() |
| | | 295 | | { |
| | | 296 | | Fx.Assert(_unwrapTcs != null, "StartUnwrapRead must be called first"); |
| | 0 | 297 | | _unwrapTcs.TrySetResult(null); |
| | | 298 | | // Ensure any reads have completed before continuing on to connection reuse |
| | 0 | 299 | | await _readSemaphore.WaitAsync(); |
| | 0 | 300 | | _readSemaphore.Release(); |
| | 0 | 301 | | } |
| | | 302 | | } |
| | | 303 | | } |