From ecc2253758c536753036ae4ab5ed5e7b1d891c8d Mon Sep 17 00:00:00 2001 From: Philipp Sehmisch Date: Sun, 30 Apr 2006 03:57:58 +0000 Subject: fixed some problems of the timer class --- src/main.cpp | 12 +++++-- src/utils/timer.cpp | 35 ++++++++++--------- src/utils/timer.h | 24 ++++++++----- src/utils/wingettimeofday.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ src/utils/wingettimeofday.h | 38 +++++++++++++++++++++ 5 files changed, 162 insertions(+), 26 deletions(-) create mode 100644 src/utils/wingettimeofday.cpp create mode 100644 src/utils/wingettimeofday.h (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 951d9b43..a0922202 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -334,6 +334,8 @@ void parseOptions(int argc, char *argv[]) */ int main(int argc, char *argv[]) { + int elapsedWorldTicks; + LOG_INFO("The Mana World Server v" << PACKAGE_VERSION, 0) // Parse Command Line Options @@ -398,8 +400,14 @@ int main(int argc, char *argv[]) worldTimer.start(); while (running) { - if (worldTimer.poll()) { - worldTime++; + elapsedWorldTicks = worldTimer.poll(); + if (elapsedWorldTicks > 0) { + worldTime += elapsedWorldTicks; + + if (elapsedWorldTicks > 1) + { + LOG_WARN(elapsedWorldTicks -1 <<" World Tick(s) skipped because of insufficient time. please buy a faster machine ;-)", 0); + }; // Print world time at 10 second intervals to show we're alive if (worldTime % 100 == 0) { diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp index eecbb7f1..25d96cbc 100644 --- a/src/utils/timer.cpp +++ b/src/utils/timer.cpp @@ -29,29 +29,25 @@ namespace utils Timer::Timer(signed int ms, bool createActive) { active = createActive; - interval = (ms * CLOCKS_PER_SEC) / 1000; - nextpulse = clock() + interval; + interval = ms; + lastpulse = getTimeInMillisec(); }; -bool Timer::poll() +int Timer::poll() { - if (!active) return false; - - if (nextpulse < clock()) + int elapsed = 0; + if (active) { - nextpulse += interval; - return true; - } - else { - return false; + elapsed = (getTimeInMillisec() - lastpulse) / interval; + lastpulse += interval * elapsed; }; - + return elapsed; }; void Timer::start() { active = true; - nextpulse = clock() + interval; + lastpulse = getTimeInMillisec(); }; void Timer::stop() @@ -59,13 +55,20 @@ void Timer::stop() active = false; }; -void Timer::changeInterval(signed int ms) +void Timer::changeInterval(signed int newinterval) { - signed int newinterval = ms * CLOCKS_PER_SEC / 1000; - nextpulse = nextpulse - interval + newinterval; interval = newinterval; }; +signed long long int Timer::getTimeInMillisec() +{ + signed long long int timeInMillisec; + timeval time; + + gettimeofday(&time, 0); + timeInMillisec = (signed long long int)time.tv_sec * 1000 + time.tv_usec / 1000; + return timeInMillisec; +}; } // ::utils } // ::tmwserv diff --git a/src/utils/timer.h b/src/utils/timer.h index 14f0638a..1fdf7af5 100644 --- a/src/utils/timer.h +++ b/src/utils/timer.h @@ -22,7 +22,11 @@ #ifndef _TMWSERV_TIMER_H_ #define _TMWSERV_TIMER_H_ -#include +#include + +#ifdef _WIN32 +#include "wingettimeofday.h" +#endif namespace tmwserv { @@ -43,10 +47,9 @@ class Timer Timer(signed int ms, bool createActive = true); /** - * checks if the desired time has passed - * returns true if yes and false if not + * returns the number of elapsed tics since last call */ - bool poll(); + int poll(); /** * activates the timer @@ -61,18 +64,23 @@ class Timer /** * changes the interval between two pulses */ - void changeInterval (signed int ms); + void changeInterval (signed int newinterval); private: + /** + * calls gettimeofday() and converts it into milliseconds + */ + signed long long int getTimeInMillisec(); + /** * interval between two pulses */ - signed int interval; + signed int interval; /** - * time for next pulse + * the time the last pulse occured */ - signed int nextpulse; + signed long long int lastpulse; /** * activity status of the timer diff --git a/src/utils/wingettimeofday.cpp b/src/utils/wingettimeofday.cpp new file mode 100644 index 00000000..baaa433b --- /dev/null +++ b/src/utils/wingettimeofday.cpp @@ -0,0 +1,79 @@ +#ifdef _WIN32 + +#include "wingettimeofday.h" + +int gettimeofday(struct timeval* tv, void *tz) +/*--------------------------------------------------------------- + * Copyright (c) 1999,2000,2001,2002,2003 + * The Board of Trustees of the University of Illinois + * All Rights Reserved. + *--------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software (Iperf) and associated + * documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and + * the following disclaimers. + * + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimers in the documentation and/or other materials + * provided with the distribution. + * + * + * Neither the names of the University of Illinois, NCSA, + * nor the names of its contributors may be used to endorse + * or promote products derived from this Software without + * specific prior written permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * ________________________________________________________________ + * National Laboratory for Applied Network Research + * National Center for Supercomputing Applications + * University of Illinois at Urbana-Champaign + * http://www.ncsa.uiuc.edu + * ________________________________________________________________ + * + * gettimeofday.c + * by Mark Gates + * ------------------------------------------------------------------- + * A (hack) implementation of gettimeofday for Windows. + * Since I send sec/usec in UDP packets, this made the most sense. + * ------------------------------------------------------------------- */ +{ + FILETIME time; + double timed; + + GetSystemTimeAsFileTime( &time ); + + // Apparently Win32 has units of 1e-7 sec (tenths of microsecs) + // 4294967296 is 2^32, to shift high word over + // 11644473600 is the number of seconds between + // the Win32 epoch 1601-Jan-01 and the Unix epoch 1970-Jan-01 + // Tests found floating point to be 10x faster than 64bit int math. + + timed = ((time.dwHighDateTime * 4294967296e-7) - 11644473600.0) + + (time.dwLowDateTime * 1e-7); + + tv->tv_sec = (long) timed; + tv->tv_usec = (long) ((timed - tv->tv_sec) * 1e6); + + return 0; +} + +#endif //_WIN32 diff --git a/src/utils/wingettimeofday.h b/src/utils/wingettimeofday.h new file mode 100644 index 00000000..66cb100c --- /dev/null +++ b/src/utils/wingettimeofday.h @@ -0,0 +1,38 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _TMWSERV_WINGETTIMEOFDAY_H_ +#define _TMWSERV_WINGETTIMEOFDAY_H_ + +#ifdef _WIN32 + +#include + +/* + * the function gettimeofday() is available on UNIX but not on windows. + * this header defines a windows implementation as a + * GetSystemTimeAsFileTime() wrapper. + */ + +int gettimeofday(struct timeval* tv, void *tz); + +#endif // _WIN32 +#endif -- cgit v1.2.3-70-g09d2