corrade-vassal – Blame information for rev 14

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