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

Revision 6, 16.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Added ACE for Linux and Windows (Thanks Derex for Linux part and partial Windows part)
* Updated to 6721 and 676
* Fixed TrinityScript? logo
* Version updated to 0.2.6721.676

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