root/trunk/src/trinitycore/Master.cpp @ 2

Revision 2, 15.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19/** \file
20    \ingroup mangosd
21*/
22
23#include "Master.h"
24#include "sockets/SocketHandler.h"
25#include "sockets/ListenSocket.h"
26#include "WorldSocket.h"
27#include "WorldSocketMgr.h"
28#include "WorldRunnable.h"
29#include "World.h"
30#include "Log.h"
31#include "Timer.h"
32#include <signal.h>
33#include "Policies/SingletonImp.h"
34#include "SystemConfig.h"
35#include "Config/ConfigEnv.h"
36#include "Database/DatabaseEnv.h"
37#include "CliRunnable.h"
38#include "RASocket.h"
39#include "ScriptCalls.h"
40#include "Util.h"
41
42#include "sockets/TcpSocket.h"
43#include "sockets/Utility.h"
44#include "sockets/Parse.h"
45#include "sockets/Socket.h"
46
47#ifdef WIN32
48#include "ServiceWin32.h"
49extern int m_ServiceStatus;
50#endif
51
52/// \todo Warning disabling not useful under VC++2005. Can somebody say on which compiler it is useful?
53#pragma warning(disable:4305)
54
55INSTANTIATE_SINGLETON_1( Master );
56
57volatile uint32 Master::m_masterLoopCounter = 0;
58
59class FreezeDetectorRunnable : public ZThread::Runnable
60{
61public:
62    FreezeDetectorRunnable() { _delaytime = 0; }
63    uint32 m_loops, m_lastchange;
64    uint32 w_loops, w_lastchange;
65    uint32 _delaytime;
66    void SetDelayTime(uint32 t) { _delaytime = t; }
67    void run(void)
68    {
69        if(!_delaytime)
70            return;
71        sLog.outString("Starting up anti-freeze thread (%u seconds max stuck time)...",_delaytime/1000);
72        m_loops = 0;
73        w_loops = 0;
74        m_lastchange = 0;
75        w_lastchange = 0;
76        while(!World::m_stopEvent)
77        {
78            ZThread::Thread::sleep(1000);
79            uint32 curtime = getMSTime();
80            //DEBUG_LOG("anti-freeze: time=%u, counters=[%u; %u]",curtime,Master::m_masterLoopCounter,World::m_worldLoopCounter);
81
82            // normal work
83            if(m_loops != Master::m_masterLoopCounter)
84            {
85                m_lastchange = curtime;
86                m_loops = Master::m_masterLoopCounter;
87            }
88            // possible freeze
89            else if(getMSTimeDiff(m_lastchange,curtime) > _delaytime)
90            {
91                sLog.outError("Main/Sockets Thread hangs, kicking out server!");
92                *((uint32 volatile*)NULL) = 0;                       // bang crash
93            }
94
95            // normal work
96            if(w_loops != World::m_worldLoopCounter)
97            {
98                w_lastchange = curtime;
99                w_loops = World::m_worldLoopCounter;
100            }
101            // possible freeze
102            else if(getMSTimeDiff(w_lastchange,curtime) > _delaytime)
103            {
104                sLog.outError("World Thread hangs, kicking out server!");
105                *((uint32 volatile*)NULL) = 0;                       // bang crash
106            }
107        }
108        sLog.outString("Anti-freeze thread exiting without problems.");
109    }
110};
111
112Master::Master()
113{
114}
115
116Master::~Master()
117{
118}
119
120/// Main function
121int Master::Run()
122{
123    sLog.outString( "%s (core-daemon)", _FULLVERSION );
124    sLog.outString( "<Ctrl-C> to stop.\n" );
125
126    sLog.outTitle( " ______                       __");
127    sLog.outTitle( "/\\__  _\\       __          __/\\ \\__");
128    sLog.outTitle( "\\/_/\\ \\/ _ __ /\\_\\    ___ /\\_\\ \\ ,_\\  __  __");
129    sLog.outTitle( "   \\ \\ \\/\\`'__\\/\\ \\ /' _ `\\/\\ \\ \\ \\/ /\\ \\/\\ \\");
130    sLog.outTitle( "    \\ \\ \\ \\ \\/ \\ \\ \\/\\ \\/\\ \\ \\ \\ \\ \\_\\ \\ \\_\\ \\");
131    sLog.outTitle( "     \\ \\_\\ \\_\\  \\ \\_\\ \\_\\ \\_\\ \\_\\ \\__\\\\/`____ \\");
132    sLog.outTitle( "      \\/_/\\/_/   \\/_/\\/_/\\/_/\\/_/\\/__/ `/___/> \\");
133    sLog.outTitle( "                                 C O R E  /\\___/");
134    sLog.outTitle( "http://TrinityCore.org                    \\/__/\n");
135
136    /// worldd PID file creation
137    std::string pidfile = sConfig.GetStringDefault("PidFile", "");
138    if(!pidfile.empty())
139    {
140        uint32 pid = CreatePIDFile(pidfile);
141        if( !pid )
142        {
143            sLog.outError( "Cannot create PID file %s.\n", pidfile.c_str() );
144            return 1;
145        }
146
147        sLog.outString( "Daemon PID: %u\n", pid );
148    }
149
150    ///- Start the databases
151    if (!_StartDB())
152        return 1;
153
154    ///- Initialize the World
155    sWorld.SetInitialWorldSettings();
156
157    ///- Launch the world listener socket
158    port_t wsport = sWorld.getConfig(CONFIG_PORT_WORLD);
159    std::string bind_ip = sConfig.GetStringDefault("BindIP", "0.0.0.0");
160
161    SocketHandler h;
162    ListenSocket<WorldSocket> worldListenSocket(h);
163    if (worldListenSocket.Bind(bind_ip.c_str(),wsport))
164    {
165        clearOnlineAccounts();
166        sLog.outError("MaNGOS cannot bind to %s:%d",bind_ip.c_str(), wsport);
167        return 1;
168    }
169
170    h.Add(&worldListenSocket);
171
172    ///- Catch termination signals
173    _HookSignals();
174
175    ///- Launch WorldRunnable thread
176    ZThread::Thread t(new WorldRunnable);
177    t.setPriority ((ZThread::Priority )2);
178
179    // set server online
180    loginDatabase.PExecute("UPDATE realmlist SET color = 0, population = 0 WHERE id = '%d'",realmID);
181
182#ifdef WIN32
183    if (sConfig.GetBoolDefault("Console.Enable", true) && (m_ServiceStatus == -1)/* need disable console in service mode*/)
184#else
185    if (sConfig.GetBoolDefault("Console.Enable", true))
186#endif
187    {
188        ///- Launch CliRunnable thread
189        ZThread::Thread td1(new CliRunnable);
190    }
191
192    ///- Launch the RA listener socket
193    ListenSocket<RASocket> RAListenSocket(h);
194    if (sConfig.GetBoolDefault("Ra.Enable", false))
195    {
196        port_t raport = sConfig.GetIntDefault( "Ra.Port", 3443 );
197        std::string stringip = sConfig.GetStringDefault( "Ra.IP", "0.0.0.0" );
198        ipaddr_t raip;
199        if(!Utility::u2ip(stringip, raip))
200            sLog.outError( "MaNGOS RA can not bind to ip %s", stringip.c_str());
201        else if (RAListenSocket.Bind(raip, raport))
202            sLog.outError( "MaNGOS RA can not bind to port %d on %s", raport, stringip.c_str());
203        else
204        {
205            h.Add(&RAListenSocket);
206
207            sLog.outString("Starting Remote access listner on port %d on %s", raport, stringip.c_str());
208        }
209    }
210
211    ///- Handle affinity for multiple processors and process priority on Windows
212    #ifdef WIN32
213    {
214        HANDLE hProcess = GetCurrentProcess();
215
216        uint32 Aff = sConfig.GetIntDefault("UseProcessors", 0);
217        if(Aff > 0)
218        {
219            ULONG_PTR appAff;
220            ULONG_PTR sysAff;
221
222            if(GetProcessAffinityMask(hProcess,&appAff,&sysAff))
223            {
224                ULONG_PTR curAff = Aff & appAff;            // remove non accessible processors
225
226                if(!curAff )
227                {
228                    sLog.outError("Processors marked in UseProcessors bitmask (hex) %x not accessible for mangosd. Accessible processors bitmask (hex): %x",Aff,appAff);
229                }
230                else
231                {
232                    if(SetProcessAffinityMask(hProcess,curAff))
233                        sLog.outString("Using processors (bitmask, hex): %x", curAff);
234                    else
235                        sLog.outError("Can't set used processors (hex): %x",curAff);
236                }
237            }
238            sLog.outString();
239        }
240
241        bool Prio = sConfig.GetBoolDefault("ProcessPriority", false);
242
243//        if(Prio && (m_ServiceStatus == -1)/* need set to default process priority class in service mode*/)
244        if(Prio)
245        {
246            if(SetPriorityClass(hProcess,HIGH_PRIORITY_CLASS))
247                sLog.outString("TrinityCore process priority class set to HIGH");
248            else
249                sLog.outError("ERROR: Can't set mangosd process priority class.");
250            sLog.outString();
251        }
252    }
253    #endif
254
255    uint32 realCurrTime, realPrevTime;
256    realCurrTime = realPrevTime = getMSTime();
257
258    uint32 socketSelecttime = sWorld.getConfig(CONFIG_SOCKET_SELECTTIME);
259
260    // maximum counter for next ping
261    uint32 numLoops = (sConfig.GetIntDefault( "MaxPingTime", 30 ) * (MINUTE * 1000000 / socketSelecttime));
262    uint32 loopCounter = 0;
263
264    ///- Start up freeze catcher thread
265    uint32 freeze_delay = sConfig.GetIntDefault("MaxCoreStuckTime", 0);
266    if(freeze_delay)
267    {
268        FreezeDetectorRunnable *fdr = new FreezeDetectorRunnable();
269        fdr->SetDelayTime(freeze_delay*1000);
270        ZThread::Thread t(fdr);
271        t.setPriority(ZThread::High);
272    }
273
274    ///- Wait for termination signal
275    while (!World::m_stopEvent)
276    {
277        ++Master::m_masterLoopCounter;
278#ifdef WIN32
279        if (m_ServiceStatus == 0) World::m_stopEvent = true;
280        while (m_ServiceStatus == 2) Sleep(1000);
281#endif
282        if (realPrevTime > realCurrTime)
283            realPrevTime = 0;
284
285        realCurrTime = getMSTime();
286        sWorldSocketMgr.Update( getMSTimeDiff(realPrevTime,realCurrTime) );
287        realPrevTime = realCurrTime;
288
289        h.Select(0, socketSelecttime);
290
291        // ping if need
292        if( (++loopCounter) == numLoops )
293        {
294            loopCounter = 0;
295            sLog.outDetail("Ping MySQL to keep connection alive");
296            delete WorldDatabase.Query("SELECT 1 FROM command LIMIT 1");
297            delete loginDatabase.Query("SELECT 1 FROM realmlist LIMIT 1");
298            delete CharacterDatabase.Query("SELECT 1 FROM bugreport LIMIT 1");
299        }
300    }
301
302    // set server offline
303    loginDatabase.PExecute("UPDATE realmlist SET color = 2 WHERE id = '%d'",realmID);
304
305    ///- Remove signal handling before leaving
306    _UnhookSignals();
307
308    // when the main thread closes the singletons get unloaded
309    // since worldrunnable uses them, it will crash if unloaded after master
310    t.wait();
311
312    ///- Clean database before leaving
313    clearOnlineAccounts();
314
315    ///- Wait for delay threads to end
316    CharacterDatabase.HaltDelayThread();
317    WorldDatabase.HaltDelayThread();
318    loginDatabase.HaltDelayThread();
319
320    sLog.outString( "Halting process..." );
321
322    #ifdef WIN32
323    if (sConfig.GetBoolDefault("Console.Enable", true))
324    {
325        // this only way to terminate CLI thread exist at Win32 (alt. way exist only in Windows Vista API)
326        //_exit(1);
327        // send keyboard input to safely unblock the CLI thread
328        INPUT_RECORD b[5];
329        HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
330        b[0].EventType = KEY_EVENT;
331        b[0].Event.KeyEvent.bKeyDown = TRUE;
332        b[0].Event.KeyEvent.uChar.AsciiChar = 'X';
333        b[0].Event.KeyEvent.wVirtualKeyCode = 'X';
334        b[0].Event.KeyEvent.wRepeatCount = 1;
335
336        b[1].EventType = KEY_EVENT;
337        b[1].Event.KeyEvent.bKeyDown = FALSE;
338        b[1].Event.KeyEvent.uChar.AsciiChar = 'X';
339        b[1].Event.KeyEvent.wVirtualKeyCode = 'X';
340        b[1].Event.KeyEvent.wRepeatCount = 1;
341
342        b[2].EventType = KEY_EVENT;
343        b[2].Event.KeyEvent.bKeyDown = TRUE;
344        b[2].Event.KeyEvent.dwControlKeyState = 0;
345        b[2].Event.KeyEvent.uChar.AsciiChar = '\r';
346        b[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
347        b[2].Event.KeyEvent.wRepeatCount = 1;
348        b[2].Event.KeyEvent.wVirtualScanCode = 0x1c;
349
350        b[3].EventType = KEY_EVENT;
351        b[3].Event.KeyEvent.bKeyDown = FALSE;
352        b[3].Event.KeyEvent.dwControlKeyState = 0;
353        b[3].Event.KeyEvent.uChar.AsciiChar = '\r';
354        b[3].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
355        b[3].Event.KeyEvent.wVirtualScanCode = 0x1c;
356        b[3].Event.KeyEvent.wRepeatCount = 1;
357        DWORD numb;
358        BOOL ret = WriteConsoleInput(hStdIn, b, 4, &numb);
359    }
360    #endif
361
362    // for some unknown reason, unloading scripts here and not in worldrunnable
363    // fixes a memory leak related to detaching threads from the module
364    UnloadScriptingModule();
365
366    return sWorld.GetShutdownMask() & SHUTDOWN_MASK_RESTART ? 2 : 0;
367}
368
369/// Initialize connection to the databases
370bool Master::_StartDB()
371{
372    ///- Get world database info from configuration file
373    std::string dbstring;
374    if(!sConfig.GetString("WorldDatabaseInfo", &dbstring))
375    {
376        sLog.outError("Database not specified in configuration file");
377        return false;
378    }
379    sLog.outString("World Database: %s", dbstring.c_str());
380
381    ///- Initialise the world database
382    if(!WorldDatabase.Initialize(dbstring.c_str()))
383    {
384        sLog.outError("Cannot connect to world database %s",dbstring.c_str());
385        return false;
386    }
387
388    if(!sConfig.GetString("CharacterDatabaseInfo", &dbstring))
389    {
390        sLog.outError("Character Database not specified in configuration file");
391        return false;
392    }
393    sLog.outString("Character Database: %s", dbstring.c_str());
394
395    ///- Initialise the Character database
396    if(!CharacterDatabase.Initialize(dbstring.c_str()))
397    {
398        sLog.outError("Cannot connect to Character database %s",dbstring.c_str());
399        return false;
400    }
401
402    ///- Get login database info from configuration file
403    if(!sConfig.GetString("LoginDatabaseInfo", &dbstring))
404    {
405        sLog.outError("Login database not specified in configuration file");
406        return false;
407    }
408
409    ///- Initialise the login database
410    sLog.outString("Login Database: %s", dbstring.c_str() );
411    if(!loginDatabase.Initialize(dbstring.c_str()))
412    {
413        sLog.outError("Cannot connect to login database %s",dbstring.c_str());
414        return false;
415    }
416
417    ///- Get the realm Id from the configuration file
418    realmID = sConfig.GetIntDefault("RealmID", 0);
419    if(!realmID)
420    {
421        sLog.outError("Realm ID not defined in configuration file");
422        return false;
423    }
424    sLog.outString("Realm running as realm ID %d", realmID);
425
426    ///- Clean the database before starting
427    clearOnlineAccounts();
428
429    QueryResult* result = WorldDatabase.Query("SELECT version FROM db_version LIMIT 1");
430    if(result)
431    {
432        Field* fields = result->Fetch();
433
434        sLog.outString("Using %s", fields[0].GetString());
435        delete result;
436    }
437    else
438        sLog.outString("Using unknown world database.");
439
440    return true;
441}
442
443/// Clear 'online' status for all accounts with characters in this realm
444void Master::clearOnlineAccounts()
445{
446    // Cleanup online status for characters hosted at current realm
447    /// \todo Only accounts with characters logged on *this* realm should have online status reset. Move the online column from 'account' to 'realmcharacters'?
448    loginDatabase.PExecute(
449        "UPDATE account SET online = 0 WHERE online > 0 "
450        "AND id IN (SELECT acctid FROM realmcharacters WHERE realmid = '%d')",realmID);
451   
452
453    CharacterDatabase.Execute("UPDATE characters SET online = 0");
454}
455
456/// Handle termination signals
457/** Put the World::m_stopEvent to 'true' if a termination signal is caught **/
458void Master::_OnSignal(int s)
459{
460    switch (s)
461    {
462        case SIGINT:
463        case SIGTERM:
464        #ifdef _WIN32
465        case SIGBREAK:
466        #endif
467            World::m_stopEvent = true;
468            break;
469    }
470
471    signal(s, _OnSignal);
472}
473
474/// Define hook '_OnSignal' for all termination signals
475void Master::_HookSignals()
476{
477    signal(SIGINT, _OnSignal);
478    signal(SIGTERM, _OnSignal);
479    #ifdef _WIN32
480    signal(SIGBREAK, _OnSignal);
481    #endif
482}
483
484/// Unhook the signals before leaving
485void Master::_UnhookSignals()
486{
487    signal(SIGINT, 0);
488    signal(SIGTERM, 0);
489    #ifdef _WIN32
490    signal(SIGBREAK, 0);
491    #endif
492}
Note: See TracBrowser for help on using the browser.