aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Utilities/Timer.h
diff options
context:
space:
mode:
authorXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
committerXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
commit57f64f1f5a8eccff388211e0d08bf9d630d48ada (patch)
treed2136a7d198b667d9cbda7c845da112c4aa577db /src/server/shared/Utilities/Timer.h
parent51121d0769619b1690fb140079244c121a39afc8 (diff)
* Restructuring shared.
--HG-- branch : trunk
Diffstat (limited to 'src/server/shared/Utilities/Timer.h')
-rw-r--r--src/server/shared/Utilities/Timer.h215
1 files changed, 215 insertions, 0 deletions
diff --git a/src/server/shared/Utilities/Timer.h b/src/server/shared/Utilities/Timer.h
new file mode 100644
index 00000000000..3e0a369b655
--- /dev/null
+++ b/src/server/shared/Utilities/Timer.h
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef TRINITY_TIMER_H
+#define TRINITY_TIMER_H
+
+#include "Platform/CompilerDefs.h"
+
+#if PLATFORM == PLATFORM_WINDOWS
+# include <ace/config-all.h>
+# include <mmsystem.h>
+# include <time.h>
+#else
+# if defined(__APPLE_CC__)
+# include <time.h>
+# endif
+# include <sys/time.h>
+# include <sys/timeb.h>
+#endif
+
+#if PLATFORM == PLATFORM_WINDOWS
+inline uint32 getMSTime() { return GetTickCount(); }
+#else
+inline uint32 getMSTime()
+{
+ struct timeval tv;
+ struct timezone tz;
+ gettimeofday( &tv, &tz );
+ return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+}
+#endif
+
+inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
+{
+ // getMSTime() have limited data range and this is case when it overflow in this tick
+ if (oldMSTime > newMSTime)
+ return (0xFFFFFFFF - oldMSTime) + newMSTime;
+ else
+ return newMSTime - oldMSTime;
+}
+
+struct IntervalTimer
+{
+ public:
+
+ IntervalTimer()
+ : _interval(0), _current(0)
+ {
+ }
+
+ void Update(time_t diff)
+ {
+ _current += diff;
+ if (_current < 0)
+ _current = 0;
+ }
+
+ bool Passed()
+ {
+ return _current >= _interval;
+ }
+
+ void Reset()
+ {
+ if (_current >= _interval)
+ _current -= _interval;
+ }
+
+ void SetCurrent(time_t current)
+ {
+ _current = current;
+ }
+
+ void SetInterval(time_t interval)
+ {
+ _interval = interval;
+ }
+
+ time_t GetInterval() const
+ {
+ return _interval;
+ }
+
+ time_t GetCurrent() const
+ {
+ return _current;
+ }
+
+ private:
+
+ time_t _interval;
+ time_t _current;
+};
+
+struct TimeTracker
+{
+ public:
+
+ TimeTracker(time_t expiry)
+ : i_expiryTime(expiry)
+ {
+ }
+
+ void Update(time_t diff)
+ {
+ i_expiryTime -= diff;
+ }
+
+ bool Passed() const
+ {
+ return i_expiryTime <= 0;
+ }
+
+ void Reset(time_t interval)
+ {
+ i_expiryTime = interval;
+ }
+
+ time_t GetExpiry() const
+ {
+ return i_expiryTime;
+ }
+
+ private:
+
+ time_t i_expiryTime;
+};
+
+struct TimeTrackerSmall
+{
+ public:
+
+ TimeTrackerSmall(uint32 expiry)
+ : i_expiryTime(expiry)
+ {
+ }
+
+ void Update(int32 diff)
+ {
+ i_expiryTime -= diff;
+ }
+
+ bool Passed() const
+ {
+ return i_expiryTime <= 0;
+ }
+
+ void Reset(uint32 interval)
+ {
+ i_expiryTime = interval;
+ }
+
+ int32 GetExpiry() const
+ {
+ return i_expiryTime;
+ }
+
+ private:
+
+ int32 i_expiryTime;
+};
+
+struct PeriodicTimer
+{
+ public:
+
+ PeriodicTimer(int32 period, int32 start_time)
+ : i_expireTime(start_time), i_period(period)
+ {
+ }
+
+ bool Update(const uint32 &diff)
+ {
+ if ((i_expireTime -= diff) > 0)
+ return false;
+
+ i_expireTime += i_period > diff ? i_period : diff;
+ return true;
+ }
+
+ void SetPeriodic(int32 period, int32 start_time)
+ {
+ i_expireTime = start_time;
+ i_period = period;
+ }
+
+ // Tracker interface
+ void TUpdate(int32 diff) { i_expireTime -= diff; }
+ bool TPassed() const { return i_expireTime <= 0; }
+ void TReset(int32 diff, int32 period) { i_expireTime += period > diff ? period : diff; }
+
+ private:
+
+ int32 i_period;
+ int32 i_expireTime;
+};
+
+#endif