| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace Azure.Core.Pipeline |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Read-only Stream that will throw a <see cref="OperationCanceledException"/> if it has to wait longer than a conf |
| | | 13 | | /// </summary> |
| | | 14 | | internal class ReadTimeoutStream : ReadOnlyStream |
| | | 15 | | { |
| | | 16 | | private readonly Stream _stream; |
| | | 17 | | private TimeSpan _readTimeout; |
| | | 18 | | private CancellationTokenSource _cancellationTokenSource = null!; |
| | | 19 | | |
| | 1620 | 20 | | public ReadTimeoutStream(Stream stream, TimeSpan readTimeout) |
| | | 21 | | { |
| | 1620 | 22 | | _stream = stream; |
| | 1620 | 23 | | _readTimeout = readTimeout; |
| | 1620 | 24 | | UpdateReadTimeout(); |
| | 1620 | 25 | | InitializeTokenSource(); |
| | 1620 | 26 | | } |
| | | 27 | | |
| | | 28 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 29 | | { |
| | 0 | 30 | | var source = StartTimeout(default, out bool dispose); |
| | | 31 | | try |
| | | 32 | | { |
| | 0 | 33 | | return _stream.Read(buffer, offset, count); |
| | | 34 | | } |
| | | 35 | | // We dispose stream on timeout so catch and check if cancellation token was cancelled |
| | 0 | 36 | | catch (ObjectDisposedException) |
| | | 37 | | { |
| | 0 | 38 | | source.Token.ThrowIfCancellationRequested(); |
| | 0 | 39 | | throw; |
| | | 40 | | } |
| | | 41 | | finally |
| | | 42 | | { |
| | 0 | 43 | | StopTimeout(source, dispose); |
| | 0 | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 48 | | { |
| | 6804 | 49 | | var source = StartTimeout(cancellationToken, out bool dispose); |
| | | 50 | | try |
| | | 51 | | { |
| | 6804 | 52 | | return await _stream.ReadAsync(buffer, offset, count, source.Token).ConfigureAwait(false); |
| | | 53 | | } |
| | | 54 | | // We dispose stream on timeout so catch and check if cancellation token was cancelled |
| | 0 | 55 | | catch (ObjectDisposedException) |
| | | 56 | | { |
| | 0 | 57 | | source.Token.ThrowIfCancellationRequested(); |
| | 0 | 58 | | throw; |
| | | 59 | | } |
| | | 60 | | finally |
| | | 61 | | { |
| | 6804 | 62 | | StopTimeout(source, dispose); |
| | | 63 | | } |
| | 6792 | 64 | | } |
| | | 65 | | |
| | | 66 | | private CancellationTokenSource StartTimeout(CancellationToken additionalToken, out bool dispose) |
| | | 67 | | { |
| | 6804 | 68 | | if (_cancellationTokenSource.IsCancellationRequested) |
| | | 69 | | { |
| | 0 | 70 | | InitializeTokenSource(); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | CancellationTokenSource source; |
| | 6804 | 74 | | if (additionalToken.CanBeCanceled) |
| | | 75 | | { |
| | 0 | 76 | | source = CancellationTokenSource.CreateLinkedTokenSource(additionalToken, _cancellationTokenSource.Token |
| | 0 | 77 | | dispose = true; |
| | | 78 | | } |
| | | 79 | | else |
| | | 80 | | { |
| | 6804 | 81 | | source = _cancellationTokenSource; |
| | 6804 | 82 | | dispose = false; |
| | | 83 | | } |
| | | 84 | | |
| | 6804 | 85 | | _cancellationTokenSource.CancelAfter(_readTimeout); |
| | | 86 | | |
| | 6804 | 87 | | return source; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private void InitializeTokenSource() |
| | | 91 | | { |
| | 1620 | 92 | | _cancellationTokenSource = new CancellationTokenSource(); |
| | 1632 | 93 | | _cancellationTokenSource.Token.Register(() => DisposeStream()); |
| | 1620 | 94 | | } |
| | | 95 | | |
| | | 96 | | private void DisposeStream() |
| | | 97 | | { |
| | 12 | 98 | | _stream.Dispose(); |
| | 12 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void StopTimeout(CancellationTokenSource source, bool dispose) |
| | | 102 | | { |
| | 6804 | 103 | | _cancellationTokenSource.CancelAfter(Timeout.InfiniteTimeSpan); |
| | 6804 | 104 | | if (dispose) |
| | | 105 | | { |
| | 0 | 106 | | source.Dispose(); |
| | | 107 | | } |
| | 6804 | 108 | | } |
| | | 109 | | |
| | | 110 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 111 | | { |
| | 0 | 112 | | return _stream.Seek(offset, origin); |
| | | 113 | | } |
| | | 114 | | |
| | 1204 | 115 | | public override bool CanRead => _stream.CanRead; |
| | 4012 | 116 | | public override bool CanSeek => _stream.CanSeek; |
| | 0 | 117 | | public override long Length => _stream.Length; |
| | | 118 | | |
| | | 119 | | public override long Position |
| | | 120 | | { |
| | 0 | 121 | | get => _stream.Position; |
| | 0 | 122 | | set => _stream.Position = value; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public override int ReadTimeout |
| | | 126 | | { |
| | 0 | 127 | | get => (int) _readTimeout.TotalMilliseconds; |
| | | 128 | | set |
| | | 129 | | { |
| | 0 | 130 | | _readTimeout = TimeSpan.FromMilliseconds(value); |
| | 0 | 131 | | UpdateReadTimeout(); |
| | 0 | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private void UpdateReadTimeout() |
| | | 136 | | { |
| | | 137 | | try |
| | | 138 | | { |
| | 1620 | 139 | | _stream.ReadTimeout = (int) _readTimeout.TotalMilliseconds; |
| | 12 | 140 | | } |
| | 1608 | 141 | | catch |
| | | 142 | | { |
| | | 143 | | // ignore |
| | 1608 | 144 | | } |
| | 1620 | 145 | | } |
| | | 146 | | |
| | | 147 | | public override void Close() |
| | | 148 | | { |
| | 1196 | 149 | | _stream.Close(); |
| | 1196 | 150 | | } |
| | | 151 | | |
| | | 152 | | protected override void Dispose(bool disposing) |
| | | 153 | | { |
| | 0 | 154 | | base.Dispose(disposing); |
| | 0 | 155 | | _stream.Dispose(); |
| | 0 | 156 | | _cancellationTokenSource.Dispose(); |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | } |