wasSharp – Diff between revs 20 and 27
?pathlinks?
Rev 20 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | |
8 | |
|
9 | namespace wasSharp.Timers |
9 | namespace wasSharp.Timers |
|
10 | { |
10 | { |
|
11 | /////////////////////////////////////////////////////////////////////////// |
11 | /////////////////////////////////////////////////////////////////////////// |
|
12 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
12 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
|
13 | /////////////////////////////////////////////////////////////////////////// |
13 | /////////////////////////////////////////////////////////////////////////// |
|
14 | /// <summary> |
14 | /// <summary> |
|
15 | /// Given a number of allowed events per seconds, this class allows you |
15 | /// Given a number of allowed events per seconds, this class allows you |
|
16 | /// to determine via the IsSafe property whether it is safe to trigger |
16 | /// to determine via the IsSafe property whether it is safe to trigger |
|
17 | /// another lined-up event. This is mostly used to check that throttles |
17 | /// another lined-up event. This is mostly used to check that throttles |
|
18 | /// are being respected. |
18 | /// are being respected. |
|
19 | /// </summary> |
19 | /// </summary> |
|
20 | public class TimedThrottle : IDisposable |
20 | public class TimedThrottle : IDisposable |
|
21 | { |
21 | { |
|
22 | private readonly uint EventsAllowed; |
22 | private readonly uint EventsAllowed; |
|
23 | private readonly object LockObject = new object(); |
23 | private readonly object LockObject = new object(); |
|
24 | private Timer timer; |
24 | private Timer timer; |
|
25 | public uint TriggeredEvents; |
25 | public uint TriggeredEvents; |
|
26 | |
26 | |
|
27 | public TimedThrottle(uint events, uint seconds) |
27 | public TimedThrottle(uint events, uint seconds) |
|
28 | { |
28 | { |
|
29 | EventsAllowed = events; |
29 | EventsAllowed = events; |
|
30 | if (timer == null) |
30 | if (timer == null) |
|
31 | { |
31 | { |
|
32 | timer = new Timer(() => |
32 | timer = new Timer(() => |
|
33 | { |
33 | { |
|
34 | lock (LockObject) |
34 | lock (LockObject) |
|
35 | { |
35 | { |
|
36 | TriggeredEvents = 0; |
36 | TriggeredEvents = 0; |
|
37 | } |
37 | } |
|
38 | }, seconds, seconds); |
38 | }, seconds, seconds); |
|
39 | } |
39 | } |
|
40 | } |
40 | } |
|
41 | |
41 | |
|
42 | public bool IsSafe |
42 | public bool IsSafe |
|
43 | { |
43 | { |
|
44 | get |
44 | get |
|
45 | { |
45 | { |
|
46 | lock (LockObject) |
46 | lock (LockObject) |
|
47 | { |
47 | { |
|
48 | return ++TriggeredEvents <= EventsAllowed; |
48 | return ++TriggeredEvents <= EventsAllowed; |
|
49 | } |
49 | } |
|
50 | } |
50 | } |
|
51 | } |
51 | } |
|
52 | |
52 | |
|
53 | public void Dispose() |
53 | public void Dispose() |
|
54 | { |
54 | { |
|
55 | Dispose(true); |
55 | Dispose(true); |
|
56 | GC.SuppressFinalize(this); |
56 | GC.SuppressFinalize(this); |
|
57 | } |
57 | } |
|
58 | |
58 | |
|
59 | ~TimedThrottle() |
59 | ~TimedThrottle() |
|
60 | { |
60 | { |
|
61 | Dispose(false); |
61 | Dispose(false); |
|
62 | } |
62 | } |
|
63 | |
63 | |
|
64 | private void Dispose(bool dispose) |
64 | private void Dispose(bool dispose) |
|
65 | { |
65 | { |
|
66 | if (timer != null) |
66 | if (timer != null) |
|
67 | { |
67 | { |
|
68 | timer.Dispose(); |
68 | timer.Dispose(); |
|
69 | timer = null; |
69 | timer = null; |
|
70 | } |
70 | } |
|
71 | } |
71 | } |
|
72 | } |
72 | } |
|
73 | } |
- | ||
74 | |
73 | } |
|
- | 74 | |
||
75 |
|
- | ||
76 | |
- | ||
77 | |
- | ||
78 | |
- |