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