-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSophonDownloadSpeedLimiter.cs
56 lines (49 loc) · 2.08 KB
/
SophonDownloadSpeedLimiter.cs
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
using System;
using System.Threading;
// ReSharper disable UnusedType.Global
// ReSharper disable InvalidXmlDocComment
// ReSharper disable IdentifierTypo
namespace Hi3Helper.Sophon
{
public class SophonDownloadSpeedLimiter
{
internal event EventHandler<int> CurrentChunkProcessingChangedEvent;
internal event EventHandler<long> DownloadSpeedChangedEvent;
// ReSharper disable once MemberCanBePrivate.Global
internal long? InitialRequestedSpeed { get; set; }
private EventHandler<long> InnerListener { get; set; }
internal int CurrentChunkProcessing;
private SophonDownloadSpeedLimiter(long initialRequestedSpeed)
{
InitialRequestedSpeed = initialRequestedSpeed;
}
/// <summary>
/// Create an instance by its initial speed to request.
/// </summary>
/// <param name="initialSpeed">The initial speed to be requested</param>
/// <returns>An instance of the speed limiter</returns>
public static SophonDownloadSpeedLimiter CreateInstance(long initialSpeed)
=> new(initialSpeed);
/// <summary>
/// Get the listener for the parent event
/// </summary>
/// <returns>The EventHandler of the listener.</returns>
/// <seealso cref="EventHandler"/>
public EventHandler<long> GetListener() => InnerListener ??= DownloadSpeedChangeListener;
private void DownloadSpeedChangeListener(object sender, long newRequestedSpeed)
{
DownloadSpeedChangedEvent?.Invoke(this, newRequestedSpeed);
InitialRequestedSpeed = newRequestedSpeed;
}
internal void IncrementChunkProcessedCount()
{
Interlocked.Increment(ref CurrentChunkProcessing);
CurrentChunkProcessingChangedEvent?.Invoke(this, CurrentChunkProcessing);
}
internal void DecrementChunkProcessedCount()
{
Interlocked.Decrement(ref CurrentChunkProcessing);
CurrentChunkProcessingChangedEvent?.Invoke(this, CurrentChunkProcessing);
}
}
}