2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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.
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.
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
23 #include "WorldSocketMgr.h"
26 #include "WorldRunnable.h"
28 #include "ObjectAccessor.h"
29 #include "MapManager.h"
30 #include "BattleGroundMgr.h"
32 #include "Database/DatabaseEnv.h"
35 #define WORLD_SLEEP_CONST 50
37 #define WORLD_SLEEP_CONST 100 //Is this still needed?? [On linux some time ago not working 50ms]
41 #include "ServiceWin32.h"
42 extern int m_ServiceStatus
;
45 /// Heartbeat for the World
46 void WorldRunnable::run()
48 ///- Init new SQL thread for the world database
49 WorldDatabase
.ThreadStart(); // let thread do safe mySQL requests (one connection call enough)
50 sWorld
.InitResultQueue();
52 uint32 realCurrTime
= 0;
53 uint32 realPrevTime
= getMSTime();
55 uint32 prevSleepTime
= 0; // used for balanced full tick time length near WORLD_SLEEP_CONST
57 ///- While we have not World::m_stopEvent, update the world
58 while (!World::IsStopped())
60 ++World::m_worldLoopCounter
;
61 realCurrTime
= getMSTime();
63 uint32 diff
= getMSTimeDiff(realPrevTime
,realCurrTime
);
65 sWorld
.Update( diff
);
66 realPrevTime
= realCurrTime
;
68 // diff (D0) include time of previous sleep (d0) + tick time (t0)
69 // we want that next d1 + t1 == WORLD_SLEEP_CONST
70 // we can't know next t1 and then can use (t0 + d1) == WORLD_SLEEP_CONST requirement
71 // d1 = WORLD_SLEEP_CONST - t0 = WORLD_SLEEP_CONST - (D0 - d0) = WORLD_SLEEP_CONST + d0 - D0
72 if (diff
<= WORLD_SLEEP_CONST
+prevSleepTime
)
74 prevSleepTime
= WORLD_SLEEP_CONST
+prevSleepTime
-diff
;
75 ZThread::Thread::sleep(prevSleepTime
);
81 if (m_ServiceStatus
== 0) World::StopNow(SHUTDOWN_EXIT_CODE
);
82 while (m_ServiceStatus
== 2) Sleep(1000);
86 sWorld
.KickAll(); // save and kick all players
87 sWorld
.UpdateSessions( 1 ); // real players unload required UpdateSessions call
89 // unload battleground templates before different singletons destroyed
90 sBattleGroundMgr
.DeleteAlllBattleGrounds();
92 sWorldSocketMgr
->StopNetwork();
94 MapManager::Instance().UnloadAll(); // unload all grids (including locked in memory)
96 ///- End the database thread
97 WorldDatabase
.ThreadEnd(); // free mySQL thread resources