OregonCore  revision 3611e8a-git
Your Favourite TBC server
Timer.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_TIMER_H
19 #define OREGON_TIMER_H
20 
21 #include "Platform/CompilerDefs.h"
22 #include "Common.h"
23 
24 #if PLATFORM == PLATFORM_WINDOWS
25 # include <mmsystem.h>
26 #else
27 # include <sys/time.h>
28 # include <sys/timeb.h>
29 #endif
30 
31 #if PLATFORM == PLATFORM_WINDOWS
32 inline uint32 getMSTime()
33 {
34  return GetTickCount();
35 }
36 
38 {
39  #if _WIN32_WINNT >= 0x0600 // Vista and higher
40  return GetTickCount64();
41  #else // Backwards compatibility for XP and lower
42  static uint32 previous = 0;
43  static uint64 total = 0;
44  uint32 now = GetTickCount();
45  total += (previous > now) ? (0xFFFFFFFF - previous) + now : (now - previous);
46  previous = now;
47  return total;
48  #endif
49 }
50 #else
51 inline uint32 getMSTime()
52 {
53  #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L
54  struct timespec tp;
55  clock_gettime(CLOCK_MONOTONIC, &tp);
56  return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000 / 1000);
57  #else // Backwards compatibility
58  struct timeval tv;
59  gettimeofday( &tv, NULL );
60  return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
61  #endif
62 }
63 
64 inline uint64 getMSTime64()
65 {
66  #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L
67  struct timespec tp;
68  clock_gettime(CLOCK_MONOTONIC, &tp);
69  return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000 / 1000);
70  #else // Backwards compatibility
71  struct timeval tv;
72  gettimeofday(&tv, NULL);
73  return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
74  #endif
75 }
76 #endif
77 
78 inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
79 {
80  // getMSTime() have limited data range and this is case when it overflow in this tick
81  if (oldMSTime > newMSTime)
82  return (0xFFFFFFFF - oldMSTime) + newMSTime;
83  else
84  return newMSTime - oldMSTime;
85 }
86 
88 {
89  if (t1 > t2)
90  return t1 - t2;
91  return t2 - t1;
92 }
93 
94 inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
95 {
96  return getMSTimeDiff(oldMSTime, getMSTime());
97 }
98 
100 {
101  public:
103 
104  void Update(time_t diff)
105  {
106  _current += diff;
107  if (_current < 0) _current = 0;
108  }
109  bool Passed()
110  {
111  return _current >= _interval;
112  }
113  void Reset()
114  {
116  }
117 
118  void SetCurrent(time_t current)
119  {
120  _current = current;
121  }
122  void SetInterval(time_t interval)
123  {
124  _interval = interval;
125  }
126  time_t GetInterval() const
127  {
128  return _interval;
129  }
130  time_t GetCurrent() const
131  {
132  return _current;
133  }
134 
135  private:
136 
137  time_t _interval;
138  time_t _current;
139 };
140 
142 {
143  TimeTracker(time_t expiry) : i_expiryTime(expiry) {}
144  void Update(time_t diff)
145  {
146  i_expiryTime -= diff;
147  }
148  bool Passed(void) const
149  {
150  return (i_expiryTime <= 0);
151  }
152  void Reset(time_t interval)
153  {
154  i_expiryTime = interval;
155  }
156  time_t GetExpiry(void) const
157  {
158  return i_expiryTime;
159  }
160  time_t i_expiryTime;
161 };
162 
164 {
165  TimeTrackerSmall(int32 expiry) : i_expiryTime(expiry) {}
166  void Update(int32 diff)
167  {
168  i_expiryTime -= diff;
169  }
170  bool Passed(void) const
171  {
172  return (i_expiryTime <= 0);
173  }
174  void Reset(int32 interval)
175  {
176  i_expiryTime = interval;
177  }
178  int32 GetExpiry(void) const
179  {
180  return i_expiryTime;
181  }
183 };
184 
186 {
187  public:
188 
189  PeriodicTimer(int32 period, int32 start_time)
190  : i_expireTime(start_time), i_period(period)
191  {
192  }
193 
194  bool Update(const uint32& diff)
195  {
196  if ((i_expireTime -= diff) > 0)
197  return false;
198 
199  i_expireTime += i_period > int32(diff) ? i_period : diff;
200  return true;
201  }
202 
203  void SetPeriodic(int32 period, int32 start_time)
204  {
205  i_expireTime = start_time;
206  i_period = period;
207  }
208 
209  // Tracker interface
210  void TUpdate(int32 diff)
211  {
212  i_expireTime -= diff;
213  }
214  bool TPassed() const
215  {
216  return i_expireTime <= 0;
217  }
218  void TReset(int32 diff, int32 period)
219  {
220  i_expireTime += period > diff ? period : diff;
221  }
222 
223  private:
224 
227 };
228 
229 #endif
230 
void Reset(int32 interval)
Definition: Timer.h:174
time_t GetInterval() const
Definition: Timer.h:126
time_t _interval
Definition: Timer.h:137
int32 GetExpiry(void) const
Definition: Timer.h:178
TimeTrackerSmall(int32 expiry)
Definition: Timer.h:165
void TUpdate(int32 diff)
Definition: Timer.h:210
int32 i_period
Definition: Timer.h:226
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition: Timer.h:78
time_t _current
Definition: Timer.h:138
bool Passed(void) const
Definition: Timer.h:148
time_t GetCurrent() const
Definition: Timer.h:130
uint32 getMSTime()
Definition: Timer.h:32
IntervalTimer()
Definition: Timer.h:102
void Update(time_t diff)
Definition: Timer.h:104
ACE_INT32 int32
Definition: Define.h:67
bool Passed()
Definition: Timer.h:109
TimeTracker(time_t expiry)
Definition: Timer.h:143
void Reset(time_t interval)
Definition: Timer.h:152
void SetInterval(time_t interval)
Definition: Timer.h:122
time_t i_expiryTime
Definition: Timer.h:160
void Update(int32 diff)
Definition: Timer.h:166
void SetPeriodic(int32 period, int32 start_time)
Definition: Timer.h:203
int32 i_expireTime
Definition: Timer.h:225
void Update(time_t diff)
Definition: Timer.h:144
time_t GetExpiry(void) const
Definition: Timer.h:156
int32 i_expiryTime
Definition: Timer.h:182
void TReset(int32 diff, int32 period)
Definition: Timer.h:218
ACE_UINT64 uint64
Definition: Define.h:70
uint64 getMSTime64()
Definition: Timer.h:37
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:94
void Reset()
Definition: Timer.h:113
void SetCurrent(time_t current)
Definition: Timer.h:118
bool Update(const uint32 &diff)
Definition: Timer.h:194
ACE_UINT32 uint32
Definition: Define.h:71
bool Passed(void) const
Definition: Timer.h:170
uint64 getMSTimeDiff64(uint64 t1, uint64 t2)
Definition: Timer.h:87
PeriodicTimer(int32 period, int32 start_time)
Definition: Timer.h:189
bool TPassed() const
Definition: Timer.h:214