aboutsummaryrefslogtreecommitdiff
path: root/src/trinityrealm/Main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/trinityrealm/Main.cpp')
-rw-r--r--src/trinityrealm/Main.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/trinityrealm/Main.cpp b/src/trinityrealm/Main.cpp
index 061d8a9e26f..7e53ad15094 100644
--- a/src/trinityrealm/Main.cpp
+++ b/src/trinityrealm/Main.cpp
@@ -17,12 +17,15 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
/// \addtogroup realmd Realm Daemon
/// @{
/// \file
+
#include "Common.h"
#include "Database/DatabaseEnv.h"
#include "RealmList.h"
+
#include "Config/ConfigEnv.h"
#include "Log.h"
#include "sockets/ListenSocket.h"
@@ -31,14 +34,17 @@
#include "Util.h"
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
+
// Format is YYYYMMDDRR where RR is the change in the conf file
// for that day.
#ifndef _REALMDCONFVERSION
# define _REALMDCONFVERSION 2009081701
#endif
+
#ifndef _TRINITY_REALM_CONFIG
# define _TRINITY_REALM_CONFIG "TrinityRealm.conf"
#endif //_TRINITY_REALM_CONFIG
+
#ifdef WIN32
#include "ServiceWin32.h"
char serviceName[] = "TrinityRealm";
@@ -52,12 +58,16 @@ char serviceDescription[] = "Massive Network Game Object Server";
*/
int m_ServiceStatus = -1;
#endif
+
bool StartDB();
void UnhookSignals();
void HookSignals();
+
bool stopEvent = false; ///< Setting it to true stops the server
RealmList m_realmList; ///< Holds the list of realms for this server
+
DatabaseType loginDatabase; ///< Accessor to the realm server database
+
/// Print out the usage string for this program on the console.
void usage(const char *prog)
{
@@ -71,6 +81,7 @@ void usage(const char *prog)
#endif
,prog);
}
+
/// Launch the realm server
extern int main(int argc, char **argv)
{
@@ -91,6 +102,7 @@ extern int main(int argc, char **argv)
else
cfg_file = argv[c];
}
+
#ifdef WIN32
////////////
//Services//
@@ -130,15 +142,18 @@ extern int main(int argc, char **argv)
#endif
++c;
}
+
if (!sConfig.SetSource(cfg_file))
{
sLog.outError("Could not find configuration file %s.", cfg_file);
return 1;
}
sLog.Initialize();
+
sLog.outString("%s (realm-daemon)", _FULLVERSION);
sLog.outString("<Ctrl-C> to stop.\n");
sLog.outString("Using configuration file %s.", cfg_file);
+
///- Check the version of the configuration file
uint32 confVersion = sConfig.GetIntDefault("ConfVersion", 0);
if (confVersion < _REALMDCONFVERSION)
@@ -149,8 +164,10 @@ extern int main(int argc, char **argv)
sLog.outError(" strange behavior.");
sLog.outError("*****************************************************************************");
clock_t pause = 3000 + clock();
+
while (pause > clock()) {}
}
+
sLog.outDetail("%s (Library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
if (SSLeay() < 0x009080bfL)
{
@@ -160,6 +177,7 @@ extern int main(int argc, char **argv)
while (pause > clock()) {}
return 1;
}
+
/// realmd PID file creation
std::string pidfile = sConfig.GetStringDefault("PidFile", "");
if (!pidfile.empty())
@@ -170,11 +188,14 @@ extern int main(int argc, char **argv)
sLog.outError("Cannot create PID file %s.\n", pidfile.c_str());
return 1;
}
+
sLog.outString("Daemon PID: %u\n", pid);
}
+
///- Initialize the database connection
if (!StartDB())
return 1;
+
///- Initialize the log database
if (sConfig.GetBoolDefault("EnableLogDB", false))
{
@@ -190,6 +211,7 @@ extern int main(int argc, char **argv)
sLog.SetLogDB(false);
sLog.SetRealmID(0);
}
+
///- Get the list of realms for the server
m_realmList.Initialize(sConfig.GetIntDefault("RealmsStateUpdateDelay", 20));
if (m_realmList.size() == 0)
@@ -197,9 +219,11 @@ extern int main(int argc, char **argv)
sLog.outError("No valid realms specified.");
return 1;
}
+
///- Launch the listening network socket
port_t rmport = sConfig.GetIntDefault("RealmServerPort", DEFAULT_REALMSERVER_PORT);
std::string bind_ip = sConfig.GetStringDefault("BindIP", "0.0.0.0");
+
SocketHandler h;
ListenSocket<AuthSocket> authListenSocket(h);
if (authListenSocket.Bind(bind_ip.c_str(),rmport))
@@ -207,21 +231,27 @@ extern int main(int argc, char **argv)
sLog.outError("Trinity realm can not bind to %s:%d",bind_ip.c_str(), rmport);
return 1;
}
+
h.Add(&authListenSocket);
+
///- Catch termination signals
HookSignals();
+
///- Handle affinity for multiple processors and process priority on Windows
#ifdef WIN32
{
HANDLE hProcess = GetCurrentProcess();
+
uint32 Aff = sConfig.GetIntDefault("UseProcessors", 0);
if (Aff > 0)
{
ULONG_PTR appAff;
ULONG_PTR sysAff;
+
if (GetProcessAffinityMask(hProcess,&appAff,&sysAff))
{
ULONG_PTR curAff = Aff & appAff; // remove non accessible processors
+
if (!curAff)
{
sLog.outError("Processors marked in UseProcessors bitmask (hex) %x not accessible for realmd. Accessible processors bitmask (hex): %x",Aff,appAff);
@@ -236,7 +266,9 @@ extern int main(int argc, char **argv)
}
sLog.outString();
}
+
bool Prio = sConfig.GetBoolDefault("ProcessPriority", false);
+
if (Prio)
{
if (SetPriorityClass(hProcess,HIGH_PRIORITY_CLASS))
@@ -247,9 +279,11 @@ extern int main(int argc, char **argv)
}
}
#endif
+
// maximum counter for next ping
uint32 numLoops = (sConfig.GetIntDefault("MaxPingTime", 30) * (MINUTE * 1000000 / 100000));
uint32 loopCounter = 0;
+
// possibly enable db logging; avoid massive startup spam by doing it here.
if (sLog.GetLogDBLater())
{
@@ -263,10 +297,13 @@ extern int main(int argc, char **argv)
sLog.SetLogDB(false);
sLog.SetLogDBLater(false);
}
+
///- Wait for termination signal
while (!stopEvent)
{
+
h.Select(0, 100000);
+
if ((++loopCounter) == numLoops)
{
loopCounter = 0;
@@ -278,14 +315,18 @@ extern int main(int argc, char **argv)
while (m_ServiceStatus == 2) Sleep(1000);
#endif
}
+
///- Wait for the delay thread to exit
loginDatabase.ThreadEnd();
loginDatabase.HaltDelayThread();
+
///- Remove signal handling before leaving
UnhookSignals();
+
sLog.outString("Halting process...");
return 0;
}
+
/// Handle termination signals
/** Put the global variable stopEvent to 'true' if a termination signal is caught **/
void OnSignal(int s)
@@ -303,8 +344,10 @@ void OnSignal(int s)
break;
#endif
}
+
signal(s, OnSignal);
}
+
/// Initialize connection to the database
bool StartDB()
{
@@ -314,14 +357,17 @@ bool StartDB()
sLog.outError("Database not specified");
return false;
}
+
if (!loginDatabase.Initialize(dbstring.c_str()))
{
sLog.outError("Cannot connect to database");
return false;
}
loginDatabase.ThreadStart();
+
return true;
}
+
/// Define hook 'OnSignal' for all termination signals
void HookSignals()
{
@@ -332,6 +378,7 @@ void HookSignals()
signal(SIGBREAK, OnSignal);
#endif
}
+
/// Unhook the signals before leaving
void UnhookSignals()
{
@@ -342,4 +389,5 @@ void UnhookSignals()
signal(SIGBREAK, 0);
#endif
}
+
/// @}