Files
TCPlayerbotCore/src/server/authserver/Main.cpp
T

278 lines
8.6 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
#include <ace/Dev_Poll_Reactor.h>
#include <ace/TP_Reactor.h>
#include <ace/ACE.h>
#include <ace/Sig_Handler.h>
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
2009-10-17 15:51:44 -07:00
2009-06-04 21:41:07 +02:00
#include "Common.h"
#include "Database/DatabaseEnv.h"
#include "Configuration/Config.h"
2009-06-04 21:41:07 +02:00
#include "Log.h"
#include "SystemConfig.h"
2008-10-02 16:23:55 -05:00
#include "Util.h"
2010-05-19 21:16:59 +02:00
#include "SignalHandler.h"
2010-03-09 19:01:45 +03:00
#include "RealmList.h"
#include "RealmAcceptor.h"
2008-10-02 16:23:55 -05:00
#ifndef _TRINITY_REALM_CONFIG
2010-06-10 16:39:51 +02:00
# define _TRINITY_REALM_CONFIG "authserver.conf"
2008-10-02 16:23:55 -05:00
#endif
2009-10-17 15:51:44 -07:00
2009-03-20 22:17:39 +01:00
bool StartDB();
2009-10-17 15:51:44 -07:00
bool stopEvent = false; // Setting it to true stops the server
2009-10-17 15:51:44 -07:00
LoginDatabaseWorkerPool LoginDatabase; // Accessor to the realm server database
2009-10-17 15:51:44 -07:00
// Handle realmd's termination signals
2010-05-19 21:16:59 +02:00
class RealmdSignalHandler : public Trinity::SignalHandler
{
public:
virtual void HandleSignal(int SigNum)
{
switch (SigNum)
2010-05-19 21:16:59 +02:00
{
case SIGINT:
case SIGTERM:
stopEvent = true;
break;
2010-05-19 21:16:59 +02:00
}
}
2010-05-19 21:16:59 +02:00
};
2008-10-02 16:23:55 -05:00
/// Print out the usage string for this program on the console.
void usage(const char *prog)
{
sLog->outString("Usage: \n %s [<options>]\n"
" -c config_file use config_file as configuration file\n\r",
prog);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
// Launch the realm server
2008-10-02 16:23:55 -05:00
extern int main(int argc, char **argv)
{
sLog->SetLogDB(false);
// Command line parsing to get the configuration file name
char const *cfg_file = _TRINITY_REALM_CONFIG;
int c = 1;
2009-09-19 00:51:47 -04:00
while(c < argc)
2008-10-02 16:23:55 -05:00
{
if (strcmp(argv[c], "-c") == 0)
2008-10-02 16:23:55 -05:00
{
2009-09-19 00:51:47 -04:00
if (++c >= argc)
2008-10-02 16:23:55 -05:00
{
sLog->outError("Runtime-Error: -c option requires an input argument");
2008-10-02 16:23:55 -05:00
usage(argv[0]);
return 1;
}
else
cfg_file = argv[c];
}
++c;
}
2009-10-17 15:51:44 -07:00
if (!sConfig->SetSource(cfg_file))
2008-10-02 16:23:55 -05:00
{
sLog->outError("Invalid or missing configuration file : %s", cfg_file);
sLog->outError("Verify that the file exists and has \'[authserver]\' written in the top of the file!");
2008-10-02 16:23:55 -05:00
return 1;
}
sLog->Initialize();
2009-10-17 15:51:44 -07:00
sLog->outString("%s (realm-daemon)", _FULLVERSION);
sLog->outString("<Ctrl-C> to stop.\n");
sLog->outString("Using configuration file %s.", cfg_file);
2009-10-17 15:51:44 -07:00
sLog->outDetail("%s (Library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
2009-10-17 15:51:44 -07:00
#if defined (ACE_HAS_EVENT_POLL) || defined (ACE_HAS_DEV_POLL)
ACE_Reactor::instance(new ACE_Reactor(new ACE_Dev_Poll_Reactor(ACE::max_handles(), 1), 1), true);
#else
ACE_Reactor::instance(new ACE_Reactor(new ACE_TP_Reactor(), true), true);
#endif
sLog->outBasic("Max allowed open files is %d", ACE::max_handles());
// realmd PID file creation
std::string pidfile = sConfig->GetStringDefault("PidFile", "");
2009-09-19 00:51:47 -04:00
if (!pidfile.empty())
2008-10-02 16:23:55 -05:00
{
uint32 pid = CreatePIDFile(pidfile);
2009-09-19 00:51:47 -04:00
if (!pid)
2008-10-02 16:23:55 -05:00
{
sLog->outError("Cannot create PID file %s.\n", pidfile.c_str());
2008-10-02 16:23:55 -05:00
return 1;
}
2009-10-17 15:51:44 -07:00
sLog->outString("Daemon PID: %u\n", pid);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
// Initialize the database connection
2009-09-19 00:51:47 -04:00
if (!StartDB())
2008-10-02 16:23:55 -05:00
return 1;
2009-10-17 15:51:44 -07:00
// Initialize the log database
sLog->SetLogDBLater(sConfig->GetBoolDefault("EnableLogDB", false)); // set var to enable DB logging once startup finished.
sLog->SetLogDB(false);
sLog->SetRealmID(0); // ensure we've set realm to 0 (realmd realmid)
// Get the list of realms for the server
sRealmList->Initialize(sConfig->GetIntDefault("RealmsStateUpdateDelay", 20));
2010-05-02 00:06:08 +02:00
if (sRealmList->size() == 0)
2008-10-02 16:23:55 -05:00
{
sLog->outError("No valid realms specified.");
2008-10-02 16:23:55 -05:00
return 1;
}
2009-10-17 15:51:44 -07:00
// Launch the listening network socket
RealmAcceptor acceptor;
2010-05-13 00:15:21 +02:00
uint16 rmport = sConfig->GetIntDefault("RealmServerPort", 3724);
std::string bind_ip = sConfig->GetStringDefault("BindIP", "0.0.0.0");
2010-04-07 18:09:10 +02:00
ACE_INET_Addr bind_addr(rmport, bind_ip.c_str());
2010-04-07 18:09:10 +02:00
2010-05-10 18:06:28 +02:00
if (acceptor.open(bind_addr, ACE_Reactor::instance(), ACE_NONBLOCK) == -1)
2008-10-02 16:23:55 -05:00
{
sLog->outError("Trinity realm can not bind to %s:%d", bind_ip.c_str(), rmport);
2008-10-02 16:23:55 -05:00
return 1;
}
2009-10-17 15:51:44 -07:00
2010-05-19 21:16:59 +02:00
// Initialise the signal handlers
RealmdSignalHandler SignalINT, SignalTERM;
// Register realmd's signal handlers
ACE_Sig_Handler Handler;
Handler.register_handler(SIGINT, &SignalINT);
Handler.register_handler(SIGTERM, &SignalTERM);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
///- Handle affinity for multiple processors and process priority on Windows
#ifdef _WIN32
2008-10-02 16:23:55 -05:00
{
HANDLE hProcess = GetCurrentProcess();
2009-10-17 15:51:44 -07:00
uint32 Aff = sConfig->GetIntDefault("UseProcessors", 0);
2009-09-19 00:51:47 -04:00
if (Aff > 0)
2008-10-02 16:23:55 -05:00
{
ULONG_PTR appAff;
ULONG_PTR sysAff;
2009-10-17 15:51:44 -07:00
2009-09-19 00:51:47 -04:00
if (GetProcessAffinityMask(hProcess,&appAff,&sysAff))
2008-10-02 16:23:55 -05:00
{
ULONG_PTR curAff = Aff & appAff; // remove non accessible processors
2009-10-17 15:51:44 -07:00
2009-09-19 00:51:47 -04:00
if (!curAff)
sLog.outError("Processors marked in UseProcessors bitmask (hex) %x not accessible for realmd. Accessible processors bitmask (hex): %x", Aff, appAff);
else if (SetProcessAffinityMask(hProcess,curAff))
sLog.outString("Using processors (bitmask, hex): %x", curAff);
2008-10-02 16:23:55 -05:00
else
sLog.outError("Can't set used processors (hex): %x", curAff);
2008-10-02 16:23:55 -05:00
}
sLog->outString();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
bool Prio = sConfig->GetBoolDefault("ProcessPriority", false);
2009-10-17 15:51:44 -07:00
2009-09-19 00:51:47 -04:00
if (Prio)
2008-10-02 16:23:55 -05:00
{
if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
sLog->outString("TrinityRealm process priority class set to HIGH");
2008-10-02 16:23:55 -05:00
else
sLog->outError("Can't set realmd process priority class.");
sLog->outString();
2008-10-02 16:23:55 -05:00
}
}
#endif
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// maximum counter for next ping
uint32 numLoops = (sConfig->GetIntDefault("MaxPingTime", 30) * (MINUTE * 1000000 / 100000));
2008-10-02 16:23:55 -05:00
uint32 loopCounter = 0;
2009-10-17 15:51:44 -07:00
2009-03-20 22:17:39 +01:00
// possibly enable db logging; avoid massive startup spam by doing it here.
if (sLog->GetLogDBLater())
2009-03-20 22:17:39 +01:00
{
sLog->outString("Enabling database logging...");
sLog->SetLogDBLater(false);
2009-03-20 22:17:39 +01:00
// login db needs thread for logging
sLog->SetLogDB(true);
2009-03-20 22:17:39 +01:00
}
else
sLog->SetLogDB(false);
2009-10-17 15:51:44 -07:00
// Wait for termination signal
2008-10-02 16:23:55 -05:00
while (!stopEvent)
{
// dont move this outside the loop, the reactor will modify it
ACE_Time_Value interval(0, 100000);
2009-10-17 15:51:44 -07:00
if (ACE_Reactor::instance()->run_reactor_event_loop(interval) == -1)
break;
2009-10-17 15:51:44 -07:00
2009-09-19 00:51:47 -04:00
if ((++loopCounter) == numLoops)
2008-10-02 16:23:55 -05:00
{
loopCounter = 0;
sLog->outDetail("Ping MySQL to keep connection alive");
2010-09-25 01:05:24 +02:00
LoginDatabase.KeepAlive();
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
// Close the Database Pool
2010-08-18 02:25:52 +02:00
LoginDatabase.Close();
2009-10-17 15:51:44 -07:00
sLog->outString("Halting process...");
2008-10-02 16:23:55 -05:00
return 0;
}
2009-10-17 15:51:44 -07:00
// Initialize connection to the database
2009-03-20 22:17:39 +01:00
bool StartDB()
2008-10-02 16:23:55 -05:00
{
std::string dbstring = sConfig->GetStringDefault("LoginDatabaseInfo", "");
2009-09-19 00:51:47 -04:00
if (dbstring.empty())
2008-10-02 16:23:55 -05:00
{
sLog->outError("Database not specified");
2008-10-02 16:23:55 -05:00
return false;
}
2009-10-17 15:51:44 -07:00
uint8 worker_threads = sConfig->GetIntDefault("LoginDatabase.WorkerThreads", 1);
2010-09-27 00:20:56 +02:00
if (worker_threads < 1 || worker_threads > 32)
2010-08-18 02:25:52 +02:00
{
sLog->outError("Improper value specified for LoginDatabase.WorkerThreads, defaulting to 1.");
2010-09-27 00:20:56 +02:00
worker_threads = 1;
2010-08-18 02:25:52 +02:00
}
uint8 synch_threads = sConfig->GetIntDefault("LoginDatabase.SynchThreads", 1);
2010-12-13 09:18:49 +01:00
if (synch_threads < 1 || synch_threads > 32)
2010-09-27 00:20:56 +02:00
{
sLog->outError("Improper value specified for LoginDatabase.SynchThreads, defaulting to 1.");
2010-12-13 09:18:49 +01:00
synch_threads = 1;
2010-09-27 00:20:56 +02:00
}
// NOTE: While authserver is singlethreaded you should keep synch_threads == 1. Increasing it is just silly since only 1 will be used ever.
2010-12-13 09:18:49 +01:00
if (!LoginDatabase.Open(dbstring.c_str(), worker_threads, synch_threads))
2008-10-02 16:23:55 -05:00
{
sLog->outError("Cannot connect to database");
2008-10-02 16:23:55 -05:00
return false;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true;
}