wasSharp – Diff between revs 6 and 7
?pathlinks?
Rev 6 | Rev 7 | |||
---|---|---|---|---|
Line 12... | Line 12... | |||
12 | using System.Threading.Tasks; |
12 | using System.Threading.Tasks; |
|
13 | using System.Xml.Serialization; |
13 | using System.Xml.Serialization; |
|
Line 14... | Line 14... | |||
14 | |
14 | |
|
15 | namespace wasSharp |
15 | namespace wasSharp |
|
16 | { |
16 | { |
|
17 | public class Time |
17 | public static class Time |
|
18 | { |
18 | { |
|
Line 19... | Line 19... | |||
19 | public delegate void TimerCallback(object state); |
19 | public delegate void TimerCallback(object state); |
|
20 | |
20 | |
|
Line 61... | Line 61... | |||
61 | public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) |
61 | public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) |
|
62 | : this(callback, state, (int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds) |
62 | : this(callback, state, (int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds) |
|
63 | { |
63 | { |
|
64 | } |
64 | } |
|
Line -... | Line 65... | |||
- | 65 | |
||
- | 66 | public Timer(TimerCallback callback) : this(callback, null, TimeSpan.Zero, TimeSpan.Zero) |
||
- | 67 | { |
||
- | 68 | } |
||
65 | |
69 | |
|
66 | public void Dispose() |
70 | public void Dispose() |
|
67 | { |
71 | { |
|
68 | Dispose(true); |
72 | Dispose(true); |
|
69 | GC.SuppressFinalize(this); |
73 | GC.SuppressFinalize(this); |
|
Line 85... | Line 89... | |||
85 | { |
89 | { |
|
86 | Period = period; |
90 | Period = period; |
|
87 | Reset(dueTime); |
91 | Reset(dueTime); |
|
88 | } |
92 | } |
|
Line -... | Line 93... | |||
- | 93 | |
||
- | 94 | public void Change(uint dueTime, int period) |
||
- | 95 | { |
||
- | 96 | Period = period; |
||
- | 97 | Change((int) dueTime, period); |
||
- | 98 | } |
||
89 | |
99 | |
|
90 | public void Change(TimeSpan dueTime, TimeSpan period) |
100 | public void Change(TimeSpan dueTime, TimeSpan period) |
|
91 | { |
101 | { |
|
92 | Change((int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds); |
102 | Change((int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds); |
|
Line 93... | Line 103... | |||
93 | } |
103 | } |
|
94 | |
104 | |
|
95 | private void Reset(int due) |
105 | private void Reset(int due) |
|
96 | { |
106 | { |
|
- | 107 | Cancel(); |
||
- | 108 | if (due <= 0) |
||
- | 109 | return; |
||
- | 110 | TokenSource = new CancellationTokenSource(); |
||
97 | Cancel(); |
111 | Action tick = null; |
|
98 | if (due >= 0) |
112 | tick = () => |
|
99 | { |
- | ||
100 | TokenSource = new CancellationTokenSource(); |
113 | { |
|
101 | Action tick = null; |
114 | Task.Run(() => Callback(State)); |
|
102 | tick = () => |
- | ||
103 | { |
- | ||
104 | Task.Run(() => Callback(State)); |
115 | if (Disposed) |
|
105 | if (Disposed || Period < 0) return; |
116 | return; |
|
106 | Delay = Period > 0 ? Task.Delay(Period, TokenSource.Token) : CompletedTask; |
117 | Delay = Period > 0 ? Task.Delay(Period, TokenSource.Token) : CompletedTask; |
|
107 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
- | ||
108 | }; |
118 | if (Delay.IsCompleted) |
|
109 | Delay = due > 0 ? Task.Delay(due, TokenSource.Token) : CompletedTask; |
119 | return; |
|
- | 120 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
||
- | 121 | }; |
||
- | 122 | Delay = due > 0 ? Task.Delay(due, TokenSource.Token) : CompletedTask; |
||
- | 123 | if (Delay.IsCompleted) |
||
- | 124 | return; |
||
- | 125 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
||
- | 126 | } |
||
- | 127 | |
||
- | 128 | public void Stop() |
||
110 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
129 | { |
|
Line 111... | Line 130... | |||
111 | } |
130 | Change(0, 0); |
|
112 | } |
131 | } |
|
113 | |
132 | |
|
114 | private void Cancel() |
133 | private void Cancel() |
|
115 | { |
134 | { |
|
116 | if (TokenSource != null) |
135 | if (TokenSource == null) |
|
117 | { |
136 | return; |
|
118 | TokenSource.Cancel(); |
- | ||
119 | TokenSource.Dispose(); |
137 | TokenSource.Cancel(); |
|
120 | TokenSource = null; |
138 | TokenSource.Dispose(); |
|
Line 121... | Line 139... | |||
121 | } |
139 | TokenSource = null; |
|
122 | } |
140 | } |
|
Line 129... | Line 147... | |||
129 | /// Given a number of allowed events per seconds, this class allows you |
147 | /// Given a number of allowed events per seconds, this class allows you |
|
130 | /// to determine via the IsSafe property whether it is safe to trigger |
148 | /// to determine via the IsSafe property whether it is safe to trigger |
|
131 | /// another lined-up event. This is mostly used to check that throttles |
149 | /// another lined-up event. This is mostly used to check that throttles |
|
132 | /// are being respected. |
150 | /// are being respected. |
|
133 | /// </summary> |
151 | /// </summary> |
|
134 | public class TimedThrottle : IDisposable |
152 | public sealed class TimedThrottle : IDisposable |
|
135 | { |
153 | { |
|
136 | private readonly uint EventsAllowed; |
154 | private readonly uint EventsAllowed; |
|
137 | private readonly object LockObject = new object(); |
155 | private readonly object LockObject = new object(); |
|
138 | private Timer timer; |
156 | private Timer timer; |
|
139 | private uint TriggeredEvents; |
157 | public uint TriggeredEvents; |
|
Line 140... | Line 158... | |||
140 | |
158 | |
|
141 | public TimedThrottle(uint events, uint seconds) |
159 | public TimedThrottle(uint events, uint seconds) |
|
142 | { |
160 | { |
|
143 | EventsAllowed = events; |
161 | EventsAllowed = events; |
|
Line 168... | Line 186... | |||
168 | { |
186 | { |
|
169 | Dispose(true); |
187 | Dispose(true); |
|
170 | GC.SuppressFinalize(this); |
188 | GC.SuppressFinalize(this); |
|
171 | } |
189 | } |
|
Line -... | Line 190... | |||
- | 190 | |
||
- | 191 | ~TimedThrottle() |
||
- | 192 | { |
||
- | 193 | Dispose(false); |
||
- | 194 | } |
||
172 | |
195 | |
|
173 | protected virtual void Dispose(bool dispose) |
196 | private void Dispose(bool dispose) |
|
174 | { |
197 | { |
|
175 | if (timer != null) |
198 | if (timer != null) |
|
176 | { |
199 | { |
|
177 | timer.Dispose(); |
200 | timer.Dispose(); |
|
Line 188... | Line 211... | |||
188 | /// of a decaying timer that tracks the time between rescheduling. |
211 | /// of a decaying timer that tracks the time between rescheduling. |
|
189 | /// </summary> |
212 | /// </summary> |
|
190 | /// <remarks> |
213 | /// <remarks> |
|
191 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
214 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
|
192 | /// </remarks> |
215 | /// </remarks> |
|
193 | public class DecayingAlarm : IDisposable |
216 | public sealed class DecayingAlarm : IDisposable |
|
194 | { |
217 | { |
|
195 | [Flags] |
218 | [Flags] |
|
196 | public enum DECAY_TYPE |
219 | public enum DECAY_TYPE |
|
197 | { |
220 | { |
|
198 | [XmlEnum(Name = "none")] NONE = 0, |
221 | [Reflection.NameAttribute("none")] [XmlEnum(Name = "none")] NONE = 0, |
|
199 | [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
222 | [Reflection.NameAttribute("arithmetic")] [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
|
200 | [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
223 | [Reflection.NameAttribute("geometric")] [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
|
201 | [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
224 | [Reflection.NameAttribute("harmonic")] [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
|
202 | [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
225 | [Reflection.NameAttribute("weighted")] [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
|
203 | } |
226 | } |
|
Line 204... | Line 227... | |||
204 | |
227 | |
|
205 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
228 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
|
206 | private readonly Stopwatch elapsed = new Stopwatch(); |
229 | private readonly Stopwatch elapsed = new Stopwatch(); |
|
Line 232... | Line 255... | |||
232 | { |
255 | { |
|
233 | Dispose(true); |
256 | Dispose(true); |
|
234 | GC.SuppressFinalize(this); |
257 | GC.SuppressFinalize(this); |
|
235 | } |
258 | } |
|
Line -... | Line 259... | |||
- | 259 | |
||
- | 260 | ~DecayingAlarm() |
||
- | 261 | { |
||
- | 262 | Dispose(false); |
||
- | 263 | } |
||
236 | |
264 | |
|
237 | public void Alarm(double deadline) |
265 | public void Alarm(double deadline) |
|
238 | { |
266 | { |
|
239 | lock (LockObject) |
267 | lock (LockObject) |
|
240 | { |
268 | { |
|
Line 262... | Line 290... | |||
262 | case DECAY_TYPE.ARITHMETIC: |
290 | case DECAY_TYPE.ARITHMETIC: |
|
263 | alarm?.Change( |
291 | alarm?.Change( |
|
264 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
292 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
|
265 | break; |
293 | break; |
|
266 | case DECAY_TYPE.GEOMETRIC: |
294 | case DECAY_TYPE.GEOMETRIC: |
|
267 | alarm?.Change((int) (Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
295 | alarm?.Change((int) Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
|
268 | 1f/(1f + times.Count))), 0); |
296 | 1f/(1f + times.Count)), 0); |
|
269 | break; |
297 | break; |
|
270 | case DECAY_TYPE.HARMONIC: |
298 | case DECAY_TYPE.HARMONIC: |
|
271 | alarm?.Change((int) ((1f + times.Count)/ |
299 | alarm?.Change((int) ((1f + times.Count)/ |
|
272 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
300 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
|
273 | break; |
301 | break; |
|
274 | case DECAY_TYPE.WEIGHTED: |
302 | case DECAY_TYPE.WEIGHTED: |
|
275 | HashSet<double> d = new HashSet<double>(times) {deadline}; |
303 | var d = new HashSet<double>(times) {deadline}; |
|
276 | double total = d.Aggregate((a, b) => b + a); |
304 | var total = d.Aggregate((a, b) => b + a); |
|
277 | alarm?.Change( |
305 | alarm?.Change( |
|
278 | (int) (d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total)), 0); |
306 | (int) d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total), 0); |
|
279 | break; |
307 | break; |
|
280 | default: |
308 | default: |
|
281 | alarm?.Change((int) deadline, 0); |
309 | alarm?.Change((int) deadline, 0); |
|
282 | break; |
310 | break; |
|
283 | } |
311 | } |
|
Line 286... | Line 314... | |||
286 | break; |
314 | break; |
|
287 | } |
315 | } |
|
288 | } |
316 | } |
|
289 | } |
317 | } |
|
Line 290... | Line 318... | |||
290 | |
318 | |
|
291 | protected virtual void Dispose(bool dispose) |
319 | private void Dispose(bool dispose) |
|
292 | { |
320 | { |
|
293 | if (alarm != null) |
321 | if (alarm != null) |
|
294 | { |
322 | { |
|
295 | alarm.Dispose(); |
323 | alarm.Dispose(); |
|
296 | alarm = null; |
324 | alarm = null; |
|
297 | } |
325 | } |
|
- | 326 | } |
||
- | 327 | |
||
- | 328 | public DecayingAlarm Clone() |
||
- | 329 | { |
||
- | 330 | return new DecayingAlarm(decay); |
||
298 | } |
331 | } |
|
299 | } |
332 | } |
|
300 | } |
333 | } |
|
301 | } |
334 | } |