1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_TIMER_H |
---|
22 | #define TRINITY_TIMER_H |
---|
23 | |
---|
24 | #include "Platform/CompilerDefs.h" |
---|
25 | |
---|
26 | #if PLATFORM == PLATFORM_WINDOWS |
---|
27 | # include <ace/config-all.h> |
---|
28 | # include <mmsystem.h> |
---|
29 | # include <time.h> |
---|
30 | #else |
---|
31 | # if defined(__APPLE_CC__) |
---|
32 | # include <time.h> |
---|
33 | # endif |
---|
34 | # include <sys/time.h> |
---|
35 | # include <sys/timeb.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | #if PLATFORM == PLATFORM_WINDOWS |
---|
39 | inline uint32 getMSTime() { return GetTickCount(); } |
---|
40 | #else |
---|
41 | inline uint32 getMSTime() |
---|
42 | { |
---|
43 | struct timeval tv; |
---|
44 | struct timezone tz; |
---|
45 | gettimeofday( &tv, &tz ); |
---|
46 | return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); |
---|
47 | } |
---|
48 | #endif |
---|
49 | |
---|
50 | inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime) |
---|
51 | { |
---|
52 | // getMSTime() have limited data range and this is case when it overflow in this tick |
---|
53 | if (oldMSTime > newMSTime) |
---|
54 | return (0xFFFFFFFF - oldMSTime) + newMSTime; |
---|
55 | else |
---|
56 | return newMSTime - oldMSTime; |
---|
57 | } |
---|
58 | |
---|
59 | class IntervalTimer |
---|
60 | { |
---|
61 | public: |
---|
62 | IntervalTimer() : _interval(0), _current(0) {} |
---|
63 | |
---|
64 | void Update(time_t diff) { _current += diff; if(_current<0) _current=0;} |
---|
65 | bool Passed() { return _current >= _interval; } |
---|
66 | void Reset() { if(_current >= _interval) _current -= _interval; } |
---|
67 | |
---|
68 | void SetCurrent(time_t current) { _current = current; } |
---|
69 | void SetInterval(time_t interval) { _interval = interval; } |
---|
70 | time_t GetInterval() const { return _interval; } |
---|
71 | time_t GetCurrent() const { return _current; } |
---|
72 | |
---|
73 | private: |
---|
74 | time_t _interval; |
---|
75 | time_t _current; |
---|
76 | }; |
---|
77 | |
---|
78 | struct TimeTracker |
---|
79 | { |
---|
80 | TimeTracker(time_t expiry) : i_expiryTime(expiry) {} |
---|
81 | void Update(time_t diff) { i_expiryTime -= diff; } |
---|
82 | bool Passed(void) const { return (i_expiryTime <= 0); } |
---|
83 | void Reset(time_t interval) { i_expiryTime = interval; } |
---|
84 | time_t GetExpiry(void) const { return i_expiryTime; } |
---|
85 | time_t i_expiryTime; |
---|
86 | }; |
---|
87 | |
---|
88 | struct TimeTrackerSmall |
---|
89 | { |
---|
90 | TimeTrackerSmall(int32 expiry) : i_expiryTime(expiry) {} |
---|
91 | void Update(int32 diff) { i_expiryTime -= diff; } |
---|
92 | bool Passed(void) const { return (i_expiryTime <= 0); } |
---|
93 | void Reset(int32 interval) { i_expiryTime = interval; } |
---|
94 | int32 GetExpiry(void) const { return i_expiryTime; } |
---|
95 | int32 i_expiryTime; |
---|
96 | }; |
---|
97 | |
---|
98 | #endif |
---|