| | | 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.Storage |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// An accumulator for request and response data transfers. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class AggregatingProgressIncrementer : IProgress<long> |
| | | 15 | | { |
| | | 16 | | private long _currentValue; |
| | | 17 | | private readonly IProgress<long> _innerHandler; |
| | | 18 | | |
| | 16 | 19 | | public Stream CreateProgressIncrementingStream(Stream stream) => _innerHandler != null && stream != null ? new P |
| | | 20 | | |
| | 96 | 21 | | public AggregatingProgressIncrementer(IProgress<long> innerHandler) => _innerHandler = innerHandler; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Increments the current value and reports it to the progress handler |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="bytes"></param> |
| | | 27 | | public void Report(long bytes) |
| | | 28 | | { |
| | 576 | 29 | | Interlocked.Add(ref _currentValue, bytes); |
| | | 30 | | |
| | 576 | 31 | | _innerHandler?.Report(Current); |
| | 576 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Zeroes out the current accumulation, and reports it to the progress handler |
| | | 36 | | /// </summary> |
| | | 37 | | public void Reset() |
| | | 38 | | { |
| | 0 | 39 | | Volatile.Write(ref _currentValue, 0); |
| | 0 | 40 | | Report(0); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns an instance that no-ops accumulation. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public static AggregatingProgressIncrementer None { get; } = new AggregatingProgressIncrementer(default); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns a long instance representing the current progress value. |
| | | 50 | | /// </summary> |
| | | 51 | | public long Current |
| | | 52 | | { |
| | | 53 | | get |
| | | 54 | | { |
| | 576 | 55 | | return Volatile.Read(ref _currentValue); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |