| | | 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.IO; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | // Enforces an overall timeout based on the TimeoutHelper passed in |
| | | 12 | | internal class TimeoutStream : DelegatingStream |
| | | 13 | | { |
| | 0 | 14 | | private readonly byte[] _oneByteArray = new byte[1]; |
| | | 15 | | private CancellationToken _cancellationToken; |
| | | 16 | | |
| | | 17 | | public TimeoutStream(Stream stream, CancellationToken token) |
| | 0 | 18 | | : base(stream) |
| | | 19 | | { |
| | 0 | 20 | | if (!stream.CanTimeout) |
| | | 21 | | { |
| | 0 | 22 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(stream), SR.StreamDoesNotSupportTime |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | _cancellationToken = token; |
| | 0 | 26 | | ReadTimeout = (int)TimeoutHelper.GetOriginalTimeout(token).TotalMilliseconds; |
| | 0 | 27 | | WriteTimeout = ReadTimeout; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 31 | | { |
| | 0 | 32 | | return ReadAsyncInternal(buffer, offset, count, CancellationToken.None).WaitForCompletion(); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public override int ReadByte() |
| | | 36 | | { |
| | 0 | 37 | | int r = Read(_oneByteArray, 0, 1); |
| | 0 | 38 | | if (r == 0) |
| | | 39 | | { |
| | 0 | 40 | | return -1; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return _oneByteArray[0]; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 47 | | { |
| | | 48 | | // Supporting a passed in cancellationToken as well as honoring the timeout token in this class would requir |
| | | 49 | | // creating a linked token source on every call which is extra allocation and needs disposal. As this is an |
| | | 50 | | // internal class, it's okay to add this extra constraint to usage of this method. |
| | | 51 | | Fx.Assert(!cancellationToken.CanBeCanceled, "cancellationToken shouldn't be cancellable"); |
| | 0 | 52 | | return await base.ReadAsync(buffer, offset, count, _cancellationToken); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async Task<int> ReadAsyncInternal(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 56 | | { |
| | 0 | 57 | | await TaskHelpers.EnsureDefaultTaskScheduler(); |
| | 0 | 58 | | return await ReadAsync(buffer, offset, count, cancellationToken); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 62 | | { |
| | 0 | 63 | | WriteAsyncInternal(buffer, offset, count, CancellationToken.None).WaitForCompletion(); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public override void WriteByte(byte value) |
| | | 67 | | { |
| | 0 | 68 | | _oneByteArray[0] = value; |
| | 0 | 69 | | Write(_oneByteArray, 0, 1); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 73 | | { |
| | | 74 | | // Supporting a passed in cancellationToken as well as honoring the timeout token in this class would requir |
| | | 75 | | // creating a linked token source on every call which is extra allocation and needs disposal. As this is an |
| | | 76 | | // internal class, it's okay to add this extra constraint to usage of this method. |
| | | 77 | | Fx.Assert(!cancellationToken.CanBeCanceled, "cancellationToken shouldn't be cancellable"); |
| | 0 | 78 | | await base.WriteAsync(buffer, offset, count, _cancellationToken); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | private async Task WriteAsyncInternal(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 82 | | { |
| | 0 | 83 | | await TaskHelpers.EnsureDefaultTaskScheduler(); |
| | 0 | 84 | | await WriteAsync(buffer, offset, count, cancellationToken); |
| | 0 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |