root/trunk/src/trinitycore/WorldRunnable.cpp @ 279

Revision 279, 3.2 kB (checked in by yumileroy, 17 years ago)

Merged commit 269 (5f0e38da128a).

Original author: gvcoman
Date: 2008-11-21 14:34:05-05:00

Line 
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/** \file
22    \ingroup Trinityd
23*/
24
25#include "WorldSocketMgr.h"
26#include "Common.h"
27#include "World.h"
28#include "WorldRunnable.h"
29#include "Timer.h"
30#include "ObjectAccessor.h"
31#include "MapManager.h"
32
33#include "Database/DatabaseEnv.h"
34
35#ifdef WIN32
36#define WORLD_SLEEP_CONST 50
37#else
38#define WORLD_SLEEP_CONST 100                               //Is this still needed?? [On linux some time ago not working 50ms]
39#endif
40
41/// Heartbeat for the World
42void WorldRunnable::run()
43{
44    ///- Init new SQL thread for the world database
45    WorldDatabase.ThreadStart();                                // let thread do safe mySQL requests (one connection call enough)
46    sWorld.InitResultQueue();
47
48    uint32 realCurrTime = 0;
49    uint32 realPrevTime = getMSTime();
50
51    uint32 prevSleepTime = 0;                               // used for balanced full tick time length near WORLD_SLEEP_CONST
52
53    ///- While we have not World::m_stopEvent, update the world
54    while (!World::IsStopped())
55    {
56        ++World::m_worldLoopCounter;
57        realCurrTime = getMSTime();
58
59        uint32 diff = getMSTimeDiff(realPrevTime,realCurrTime);
60
61        sWorld.Update( diff );
62        realPrevTime = realCurrTime;
63
64        // diff (D0) include time of previous sleep (d0) + tick time (t0)
65        // we want that next d1 + t1 == WORLD_SLEEP_CONST
66        // we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
67        // d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
68        if (diff <= WORLD_SLEEP_CONST+prevSleepTime)
69        {
70            prevSleepTime = WORLD_SLEEP_CONST+prevSleepTime-diff;
71            ZThread::Thread::sleep(prevSleepTime);
72        }
73        else
74            prevSleepTime = 0;
75    }
76
77    sWorld.KickAllQueued();                                 // kick all queued players (and prevent its login at kick in game players)
78    sWorld.KickAll();                                       // save and kick all players
79    sWorld.UpdateSessions( 1 );                             // real players unload required UpdateSessions call
80
81    sWorldSocketMgr->StopNetwork();
82
83    MapManager::Instance().UnloadAll();                     // unload all grids (including locked in memory)
84
85    ///- End the database thread
86    WorldDatabase.ThreadEnd();                                  // free mySQL thread resources
87}
Note: See TracBrowser for help on using the browser.