corrade-vassal – Diff between revs 14 and 16
?pathlinks?
Rev 14 | Rev 16 | |||
---|---|---|---|---|
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 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
9 | using System.Diagnostics; |
9 | using System.Diagnostics; |
|
10 | using System.Linq; |
10 | using System.Linq; |
|
11 | using System.Threading; |
11 | using System.Threading; |
|
12 | using System.Threading.Tasks; |
12 | using System.Threading.Tasks; |
|
13 | using System.Xml.Serialization; |
13 | using System.Xml.Serialization; |
|
14 | |
14 | |
|
15 | namespace wasSharp |
15 | namespace wasSharp |
|
16 | { |
16 | { |
|
17 | public class Time |
17 | public static class Time |
|
18 | { |
18 | { |
|
19 | public delegate void TimerCallback(object state); |
19 | public delegate void TimerCallback(object state); |
|
20 | |
20 | |
|
21 | /// <summary> |
21 | /// <summary> |
|
22 | /// Convert an Unix timestamp to a DateTime structure. |
22 | /// Convert an Unix timestamp to a DateTime structure. |
|
23 | /// </summary> |
23 | /// </summary> |
|
24 | /// <param name="unixTimestamp">the Unix timestamp to convert</param> |
24 | /// <param name="unixTimestamp">the Unix timestamp to convert</param> |
|
25 | /// <returns>the DateTime structure</returns> |
25 | /// <returns>the DateTime structure</returns> |
|
26 | /// <remarks>the function assumes UTC time</remarks> |
26 | /// <remarks>the function assumes UTC time</remarks> |
|
27 | public static DateTime UnixTimestampToDateTime(uint unixTimestamp) |
27 | public static DateTime UnixTimestampToDateTime(uint unixTimestamp) |
|
28 | { |
28 | { |
|
29 | return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimestamp).ToUniversalTime(); |
29 | return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimestamp).ToUniversalTime(); |
|
30 | } |
30 | } |
|
31 | |
31 | |
|
32 | /// <summary> |
32 | /// <summary> |
|
33 | /// Convert a DateTime structure to a Unix timestamp. |
33 | /// Convert a DateTime structure to a Unix timestamp. |
|
34 | /// </summary> |
34 | /// </summary> |
|
35 | /// <param name="dateTime">the DateTime structure to convert</param> |
35 | /// <param name="dateTime">the DateTime structure to convert</param> |
|
36 | /// <returns>the Unix timestamp</returns> |
36 | /// <returns>the Unix timestamp</returns> |
|
37 | /// <remarks>the function assumes UTC time</remarks> |
37 | /// <remarks>the function assumes UTC time</remarks> |
|
38 | public static uint DateTimeToUnixTimestamp(DateTime dateTime) |
38 | public static uint DateTimeToUnixTimestamp(DateTime dateTime) |
|
39 | { |
39 | { |
|
40 | return (uint) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; |
40 | return (uint) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; |
|
41 | } |
41 | } |
|
42 | |
42 | |
|
43 | public sealed class Timer : IDisposable |
43 | public sealed class Timer : IDisposable |
|
44 | { |
44 | { |
|
45 | private static readonly Task CompletedTask = Task.FromResult(false); |
45 | private static readonly Task CompletedTask = Task.FromResult(false); |
|
46 | private readonly TimerCallback Callback; |
46 | private readonly TimerCallback Callback; |
|
47 | private readonly object State; |
47 | private readonly object State; |
|
48 | private Task Delay; |
48 | private Task Delay; |
|
49 | private bool Disposed; |
49 | private bool Disposed; |
|
50 | private int Period; |
50 | private int Period; |
|
51 | private CancellationTokenSource TokenSource; |
51 | private CancellationTokenSource TokenSource; |
|
52 | |
52 | |
|
53 | public Timer(TimerCallback callback, object state, int dueTime, int period) |
53 | public Timer(TimerCallback callback, object state, int dueTime, int period) |
|
54 | { |
54 | { |
|
55 | Callback = callback; |
55 | Callback = callback; |
|
56 | State = state; |
56 | State = state; |
|
57 | Period = period; |
57 | Period = period; |
|
58 | Reset(dueTime); |
58 | Reset(dueTime); |
|
59 | } |
59 | } |
|
60 | |
60 | |
|
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 | } |
|
65 | |
65 | |
|
66 | public void Dispose() |
66 | public void Dispose() |
|
67 | { |
67 | { |
|
68 | Dispose(true); |
68 | Dispose(true); |
|
69 | GC.SuppressFinalize(this); |
69 | GC.SuppressFinalize(this); |
|
70 | } |
70 | } |
|
71 | |
71 | |
|
72 | ~Timer() |
72 | ~Timer() |
|
73 | { |
73 | { |
|
74 | Dispose(false); |
74 | Dispose(false); |
|
75 | } |
75 | } |
|
76 | |
76 | |
|
77 | private void Dispose(bool cleanUpManagedObjects) |
77 | private void Dispose(bool cleanUpManagedObjects) |
|
78 | { |
78 | { |
|
79 | if (cleanUpManagedObjects) |
79 | if (cleanUpManagedObjects) |
|
80 | Cancel(); |
80 | Cancel(); |
|
81 | Disposed = true; |
81 | Disposed = true; |
|
82 | } |
82 | } |
|
83 | |
83 | |
|
84 | public void Change(int dueTime, int period) |
84 | public void Change(int dueTime, int period) |
|
85 | { |
85 | { |
|
86 | Period = period; |
86 | Period = period; |
|
87 | Reset(dueTime); |
87 | Reset(dueTime); |
|
88 | } |
88 | } |
|
89 | |
89 | |
|
90 | public void Change(TimeSpan dueTime, TimeSpan period) |
90 | public void Change(TimeSpan dueTime, TimeSpan period) |
|
91 | { |
91 | { |
|
92 | Change((int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds); |
92 | Change((int) dueTime.TotalMilliseconds, (int) period.TotalMilliseconds); |
|
93 | } |
93 | } |
|
94 | |
94 | |
|
95 | private void Reset(int due) |
95 | private void Reset(int due) |
|
96 | { |
96 | { |
|
97 | Cancel(); |
97 | Cancel(); |
|
98 | if (due >= 0) |
98 | if (due >= 0) |
|
99 | { |
99 | { |
|
100 | TokenSource = new CancellationTokenSource(); |
100 | TokenSource = new CancellationTokenSource(); |
|
101 | Action tick = null; |
101 | Action tick = null; |
|
102 | tick = () => |
102 | tick = () => |
|
103 | { |
103 | { |
|
104 | Task.Run(() => Callback(State)); |
104 | Task.Run(() => Callback(State)); |
|
105 | if (Disposed || Period < 0) return; |
105 | if (Disposed || Period < 0) return; |
|
106 | Delay = Period > 0 ? Task.Delay(Period, TokenSource.Token) : CompletedTask; |
106 | Delay = Period > 0 ? Task.Delay(Period, TokenSource.Token) : CompletedTask; |
|
107 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
107 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
|
108 | }; |
108 | }; |
|
109 | Delay = due > 0 ? Task.Delay(due, TokenSource.Token) : CompletedTask; |
109 | Delay = due > 0 ? Task.Delay(due, TokenSource.Token) : CompletedTask; |
|
110 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
110 | Delay.ContinueWith(t => tick(), TokenSource.Token); |
|
111 | } |
111 | } |
|
112 | } |
112 | } |
|
113 | |
113 | |
|
114 | private void Cancel() |
114 | private void Cancel() |
|
115 | { |
115 | { |
|
116 | if (TokenSource != null) |
116 | if (TokenSource != null) |
|
117 | { |
117 | { |
|
118 | TokenSource.Cancel(); |
118 | TokenSource.Cancel(); |
|
119 | TokenSource.Dispose(); |
119 | TokenSource.Dispose(); |
|
120 | TokenSource = null; |
120 | TokenSource = null; |
|
121 | } |
121 | } |
|
122 | } |
122 | } |
|
123 | } |
123 | } |
|
124 | |
124 | |
|
125 | /////////////////////////////////////////////////////////////////////////// |
125 | /////////////////////////////////////////////////////////////////////////// |
|
126 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
126 | // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
|
127 | /////////////////////////////////////////////////////////////////////////// |
127 | /////////////////////////////////////////////////////////////////////////// |
|
128 | /// <summary> |
128 | /// <summary> |
|
129 | /// Given a number of allowed events per seconds, this class allows you |
129 | /// 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 |
130 | /// 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 |
131 | /// another lined-up event. This is mostly used to check that throttles |
|
132 | /// are being respected. |
132 | /// are being respected. |
|
133 | /// </summary> |
133 | /// </summary> |
|
134 | public class TimedThrottle : IDisposable |
134 | public sealed class TimedThrottle : IDisposable |
|
135 | { |
135 | { |
|
136 | private readonly uint EventsAllowed; |
136 | private readonly uint EventsAllowed; |
|
137 | private readonly object LockObject = new object(); |
137 | private readonly object LockObject = new object(); |
|
138 | private Timer timer; |
138 | private Timer timer; |
|
139 | private uint TriggeredEvents; |
139 | private uint TriggeredEvents; |
|
140 | |
140 | |
|
141 | public TimedThrottle(uint events, uint seconds) |
141 | public TimedThrottle(uint events, uint seconds) |
|
142 | { |
142 | { |
|
143 | EventsAllowed = events; |
143 | EventsAllowed = events; |
|
144 | if (timer == null) |
144 | if (timer == null) |
|
145 | { |
145 | { |
|
146 | timer = new Timer(o => |
146 | timer = new Timer(o => |
|
147 | { |
147 | { |
|
148 | lock (LockObject) |
148 | lock (LockObject) |
|
149 | { |
149 | { |
|
150 | TriggeredEvents = 0; |
150 | TriggeredEvents = 0; |
|
151 | } |
151 | } |
|
152 | }, null, (int) seconds, (int) seconds); |
152 | }, null, (int) seconds, (int) seconds); |
|
153 | } |
153 | } |
|
154 | } |
154 | } |
|
155 | |
155 | |
|
156 | public bool IsSafe |
156 | public bool IsSafe |
|
157 | { |
157 | { |
|
158 | get |
158 | get |
|
159 | { |
159 | { |
|
160 | lock (LockObject) |
160 | lock (LockObject) |
|
161 | { |
161 | { |
|
162 | return ++TriggeredEvents <= EventsAllowed; |
162 | return ++TriggeredEvents <= EventsAllowed; |
|
163 | } |
163 | } |
|
164 | } |
164 | } |
|
165 | } |
165 | } |
|
166 | |
166 | |
|
167 | public void Dispose() |
167 | public void Dispose() |
|
168 | { |
168 | { |
|
169 | Dispose(true); |
169 | Dispose(true); |
|
170 | GC.SuppressFinalize(this); |
170 | GC.SuppressFinalize(this); |
|
171 | } |
171 | } |
|
- | 172 | |
||
- | 173 | ~TimedThrottle() |
||
- | 174 | { |
||
- | 175 | Dispose(false); |
||
- | 176 | } |
||
172 | |
177 | |
|
173 | protected virtual void Dispose(bool dispose) |
178 | private void Dispose(bool dispose) |
|
174 | { |
179 | { |
|
175 | if (timer != null) |
180 | if (timer != null) |
|
176 | { |
181 | { |
|
177 | timer.Dispose(); |
182 | timer.Dispose(); |
|
178 | timer = null; |
183 | timer = null; |
|
179 | } |
184 | } |
|
180 | } |
185 | } |
|
181 | } |
186 | } |
|
182 | |
187 | |
|
183 | /////////////////////////////////////////////////////////////////////////// |
188 | /////////////////////////////////////////////////////////////////////////// |
|
184 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
189 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
185 | /////////////////////////////////////////////////////////////////////////// |
190 | /////////////////////////////////////////////////////////////////////////// |
|
186 | /// <summary> |
191 | /// <summary> |
|
187 | /// An alarm class similar to the UNIX alarm with the added benefit |
192 | /// An alarm class similar to the UNIX alarm with the added benefit |
|
188 | /// of a decaying timer that tracks the time between rescheduling. |
193 | /// of a decaying timer that tracks the time between rescheduling. |
|
189 | /// </summary> |
194 | /// </summary> |
|
190 | /// <remarks> |
195 | /// <remarks> |
|
191 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
196 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
|
192 | /// </remarks> |
197 | /// </remarks> |
|
193 | public class DecayingAlarm : IDisposable |
198 | public sealed class DecayingAlarm : IDisposable |
|
194 | { |
199 | { |
|
195 | [Flags] |
200 | [Flags] |
|
196 | public enum DECAY_TYPE |
201 | public enum DECAY_TYPE |
|
197 | { |
202 | { |
|
198 | [XmlEnum(Name = "none")] NONE = 0, |
203 | [Reflection.NameAttribute("none")] [XmlEnum(Name = "none")] NONE = 0, |
|
199 | [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
204 | [Reflection.NameAttribute("arithmetic")] [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
|
200 | [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
205 | [Reflection.NameAttribute("geometric")] [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
|
201 | [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
206 | [Reflection.NameAttribute("harmonic")] [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
|
202 | [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
207 | [Reflection.NameAttribute("weighted")] [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
|
203 | } |
208 | } |
|
204 | |
209 | |
|
205 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
210 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
|
206 | private readonly Stopwatch elapsed = new Stopwatch(); |
211 | private readonly Stopwatch elapsed = new Stopwatch(); |
|
207 | private readonly object LockObject = new object(); |
212 | private readonly object LockObject = new object(); |
|
208 | private readonly HashSet<double> times = new HashSet<double>(); |
213 | private readonly HashSet<double> times = new HashSet<double>(); |
|
209 | private Timer alarm; |
214 | private Timer alarm; |
|
210 | |
215 | |
|
211 | /// <summary> |
216 | /// <summary> |
|
212 | /// The default constructor using no decay. |
217 | /// The default constructor using no decay. |
|
213 | /// </summary> |
218 | /// </summary> |
|
214 | public DecayingAlarm() |
219 | public DecayingAlarm() |
|
215 | { |
220 | { |
|
216 | Signal = new ManualResetEvent(false); |
221 | Signal = new ManualResetEvent(false); |
|
217 | } |
222 | } |
|
218 | |
223 | |
|
219 | /// <summary> |
224 | /// <summary> |
|
220 | /// The constructor for the DecayingAlarm class taking as parameter a decay type. |
225 | /// The constructor for the DecayingAlarm class taking as parameter a decay type. |
|
221 | /// </summary> |
226 | /// </summary> |
|
222 | /// <param name="decay">the type of decay: arithmetic, geometric, harmonic, heronian or quadratic</param> |
227 | /// <param name="decay">the type of decay: arithmetic, geometric, harmonic, heronian or quadratic</param> |
|
223 | public DecayingAlarm(DECAY_TYPE decay) |
228 | public DecayingAlarm(DECAY_TYPE decay) |
|
224 | { |
229 | { |
|
225 | Signal = new ManualResetEvent(false); |
230 | Signal = new ManualResetEvent(false); |
|
226 | this.decay = decay; |
231 | this.decay = decay; |
|
227 | } |
232 | } |
|
228 | |
233 | |
|
229 | public ManualResetEvent Signal { get; set; } |
234 | public ManualResetEvent Signal { get; set; } |
|
230 | |
235 | |
|
231 | public void Dispose() |
236 | public void Dispose() |
|
232 | { |
237 | { |
|
233 | Dispose(true); |
238 | Dispose(true); |
|
234 | GC.SuppressFinalize(this); |
239 | GC.SuppressFinalize(this); |
|
235 | } |
240 | } |
|
- | 241 | |
||
- | 242 | ~DecayingAlarm() |
||
- | 243 | { |
||
- | 244 | Dispose(false); |
||
- | 245 | } |
||
236 | |
246 | |
|
237 | public void Alarm(double deadline) |
247 | public void Alarm(double deadline) |
|
238 | { |
248 | { |
|
239 | lock (LockObject) |
249 | lock (LockObject) |
|
240 | { |
250 | { |
|
241 | switch (alarm == null) |
251 | switch (alarm == null) |
|
242 | { |
252 | { |
|
243 | case true: |
253 | case true: |
|
244 | elapsed.Start(); |
254 | elapsed.Start(); |
|
245 | alarm = new Timer(o => |
255 | alarm = new Timer(o => |
|
246 | { |
256 | { |
|
247 | lock (LockObject) |
257 | lock (LockObject) |
|
248 | { |
258 | { |
|
249 | Signal.Set(); |
259 | Signal.Set(); |
|
250 | elapsed.Stop(); |
260 | elapsed.Stop(); |
|
251 | times.Clear(); |
261 | times.Clear(); |
|
252 | alarm.Dispose(); |
262 | alarm.Dispose(); |
|
253 | alarm = null; |
263 | alarm = null; |
|
254 | } |
264 | } |
|
255 | }, null, (int) deadline, 0); |
265 | }, null, (int) deadline, 0); |
|
256 | return; |
266 | return; |
|
257 | case false: |
267 | case false: |
|
258 | elapsed.Stop(); |
268 | elapsed.Stop(); |
|
259 | times.Add(elapsed.ElapsedMilliseconds); |
269 | times.Add(elapsed.ElapsedMilliseconds); |
|
260 | switch (decay) |
270 | switch (decay) |
|
261 | { |
271 | { |
|
262 | case DECAY_TYPE.ARITHMETIC: |
272 | case DECAY_TYPE.ARITHMETIC: |
|
263 | alarm?.Change( |
273 | alarm?.Change( |
|
264 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
274 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
|
265 | break; |
275 | break; |
|
266 | case DECAY_TYPE.GEOMETRIC: |
276 | case DECAY_TYPE.GEOMETRIC: |
|
267 | alarm?.Change((int) (Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
277 | alarm?.Change((int) Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
|
268 | 1f/(1f + times.Count))), 0); |
278 | 1f/(1f + times.Count)), 0); |
|
269 | break; |
279 | break; |
|
270 | case DECAY_TYPE.HARMONIC: |
280 | case DECAY_TYPE.HARMONIC: |
|
271 | alarm?.Change((int) ((1f + times.Count)/ |
281 | alarm?.Change((int) ((1f + times.Count)/ |
|
272 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
282 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
|
273 | break; |
283 | break; |
|
274 | case DECAY_TYPE.WEIGHTED: |
284 | case DECAY_TYPE.WEIGHTED: |
|
275 | HashSet<double> d = new HashSet<double>(times) {deadline}; |
285 | var d = new HashSet<double>(times) {deadline}; |
|
276 | double total = d.Aggregate((a, b) => b + a); |
286 | var total = d.Aggregate((a, b) => b + a); |
|
277 | alarm?.Change( |
287 | alarm?.Change( |
|
278 | (int) (d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total)), 0); |
288 | (int) d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total), 0); |
|
279 | break; |
289 | break; |
|
280 | default: |
290 | default: |
|
281 | alarm?.Change((int) deadline, 0); |
291 | alarm?.Change((int) deadline, 0); |
|
282 | break; |
292 | break; |
|
283 | } |
293 | } |
|
284 | elapsed.Reset(); |
294 | elapsed.Reset(); |
|
285 | elapsed.Start(); |
295 | elapsed.Start(); |
|
286 | break; |
296 | break; |
|
287 | } |
297 | } |
|
288 | } |
298 | } |
|
289 | } |
299 | } |
|
290 | |
300 | |
|
291 | protected virtual void Dispose(bool dispose) |
301 | private void Dispose(bool dispose) |
|
292 | { |
302 | { |
|
293 | if (alarm != null) |
303 | if (alarm != null) |
|
294 | { |
304 | { |
|
295 | alarm.Dispose(); |
305 | alarm.Dispose(); |
|
296 | alarm = null; |
306 | alarm = null; |
|
297 | } |
307 | } |
|
298 | } |
308 | } |
|
- | 309 | |
||
- | 310 | public DecayingAlarm Clone() |
||
- | 311 | { |
||
- | 312 | return new DecayingAlarm(decay); |
||
- | 313 | } |
||
299 | } |
314 | } |
|
300 | } |
315 | } |
|
301 | } |
316 | } |
|
302 | |
317 | |
|
303 |
|
318 |
|
|
304 | |
319 | |
|
305 | |
320 | |
|
306 | |
321 | |