summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2006-05-14 16:47:52 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2006-05-14 16:47:52 +0000
commit99c97f75e2b93ea2cb36f126cd75b298bda5f5a9 (patch)
tree3ad904f3cfb78513f81241d2985b89dd1c034423 /src
parent0125cb14a9a4f4ffced61ba50412f13fe00adbb4 (diff)
downloadmanaserv-99c97f75e2b93ea2cb36f126cd75b298bda5f5a9.tar.gz
manaserv-99c97f75e2b93ea2cb36f126cd75b298bda5f5a9.tar.bz2
manaserv-99c97f75e2b93ea2cb36f126cd75b298bda5f5a9.tar.xz
manaserv-99c97f75e2b93ea2cb36f126cd75b298bda5f5a9.zip
Applied a patch by Guillaume that makes the server sleep between ticks instead
of polling for the next one. Somebody will need to verify that this works for on Dev-C++ as well.
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp12
-rw-r--r--src/utils/timer.cpp12
-rw-r--r--src/utils/timer.h42
3 files changed, 39 insertions, 27 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 22ac6a2a..db594c76 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -319,7 +319,7 @@ 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
@@ -402,11 +402,7 @@ int main(int argc, char *argv[])
// - Update all active objects/beings
state.update(*connectionHandler);
}
- else {
- // when there is nothing to do give away some processor time to
- // other tasks
- sched_yield();
- };
+ worldTimer.sleep();
/*ENetEvent netEvent;
@@ -442,13 +438,13 @@ int main(int argc, char *argv[])
enet_packet_destroy(netEvent.packet);
}
break;
-
+
case ENET_EVENT_TYPE_DISCONNECT:
printf("%s disconected.\n", netEvent.peer->data);
netEvent.peer->data = NULL;
break;
-
+
default:
printf("Unhandled enet event\n");
break;
diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp
index 97965c75..0e9a6a7e 100644
--- a/src/utils/timer.cpp
+++ b/src/utils/timer.cpp
@@ -19,6 +19,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <time.h>
#include "timer.h"
namespace tmwserv
@@ -33,6 +34,17 @@ Timer::Timer(unsigned int ms, bool createActive)
lastpulse = getTimeInMillisec();
};
+void Timer::sleep()
+{
+ if (!active) return;
+ uint64_t now = getTimeInMillisec();
+ if (now - lastpulse >= interval) return;
+ struct timespec req;
+ req.tv_sec = 0;
+ req.tv_nsec = (interval - (now - lastpulse)) * (1000 * 1000);
+ nanosleep(&req, 0);
+}
+
int Timer::poll()
{
int elapsed = 0;
diff --git a/src/utils/timer.h b/src/utils/timer.h
index a921a828..603d24cd 100644
--- a/src/utils/timer.h
+++ b/src/utils/timer.h
@@ -43,54 +43,58 @@ namespace utils
/**
* This class is for timing purpose as a replacement for SDL_TIMER
*/
-
class Timer
{
public:
/**
- * Constructor.
- */
+ * Constructor.
+ */
Timer(unsigned int ms, bool createActive = true);
/**
- * returns the number of elapsed tics since last call
- */
+ * Returns the number of elapsed ticks since last call.
+ */
int poll();
/**
- * activates the timer
- */
+ * Sleeps till the next tick occurs.
+ */
+ void sleep();
+
+ /**
+ * Activates the timer.
+ */
void start();
/**
- * deactivates the timer
- */
+ * Deactivates the timer.
+ */
void stop();
/**
- * changes the interval between two pulses
- */
+ * Changes the interval between two pulses.
+ */
void changeInterval (unsigned int newinterval);
private:
/**
- * calls gettimeofday() and converts it into milliseconds
- */
+ * Calls gettimeofday() and converts it into milliseconds.
+ */
uint64_t getTimeInMillisec();
/**
- * interval between two pulses
- */
+ * Interval between two pulses.
+ */
unsigned int interval;
/**
- * the time the last pulse occured
- */
+ * The time the last pulse occured.
+ */
uint64_t lastpulse;
/**
- * activity status of the timer
- */
+ * Activity status of the timer.
+ */
bool active;
};