Code:
#include <numeric>
#include "../NetLib/NetKernel.h"
#include "../NetLib/Utility.h"
#include "../NetLib/SystemLog.h"
#include "../NetLib/ProcessLayer.h"
#include "../NetLib/IOLayer.h"
#include "Proc/GKHandler.h"
#include "Proc/ProcSystem.h"
#include "../../../Tool/Src/MachineLicense/LicenseHelper/LicenseHelper.h"
#include "ContextEx.h"
#include <ace/SV_Semaphore_Complex.h>
#include "ProtodefEx.h"
#include "ExceptionDumpEx.h"
static void SetLogText()
{
#define DefText( x ) //GlobalSystemLog::instance()->SetLogText(x, #x);
DefText(LC_GMS_RESPONSE_CONNECT2SVR)
DefText(LC_GMS_RESPONSE_CONNECT2SVR_FAIL)
DefText(LC_GMS_CLOSE_PROCESS)
DefText(LC_GMS_IPC_SYSTEM_MESSAGE)
DefText(LC_GMS_ITC_SYSTEM_MESSAGE)
#undef DefText
}
void ReloadServerAndChannelTypeCfg()
{
g_pServerCtxEx->loadServerAndChannelType =
CONFIG_INT(SERVER_NAME, "LoadServerAndChannelType", 0) == 0 ? false: true;
LOG_TRACE(1, ("LoadServerAndChannelType(%d)", g_pServerCtxEx->loadServerAndChannelType));
if(g_pServerCtxEx->loadServerAndChannelType)
{
for(BYTE sIdx = 1; sIdx <= MAX_SERVERNUM; ++sIdx)
{
static char serverSectionName[256] = {0};
if(g_pServerCtxEx->loadServerAndChannelType)
{
//ServerType Setting
sprintf(serverSectionName, "Server%.2d", sIdx);
if(CONFIG_KEYEXISTS(serverSectionName, "ServerType"))
{
g_pServerCtxEx->serverType[sIdx] =
static_cast<BYTE>(CONFIG_INT(serverSectionName, "ServerType", SERVER_TYPE::NONE));
LOG_TRACE(1, ("serverType[%d]: %d", sIdx, g_pServerCtxEx->serverType[sIdx]));
}
else if(CONFIG_KEYEXISTS("DefaultServerCfg", "ServerType"))
{
g_pServerCtxEx->serverType[sIdx] =
static_cast<BYTE>(CONFIG_INT("DefaultServerCfg", "ServerType", SERVER_TYPE::NONE));
}
}
//ChannelType Setting
for(BYTE gIdx = 1; gIdx <= MAX_GROUPNUM; ++gIdx)
{
SERVERSTATE& serverState = g_pServerCtxEx->serverNodes[sIdx][gIdx];
if(g_pServerCtxEx->loadServerAndChannelType)
{
static char channelTypeName[256] = {0};
sprintf(channelTypeName, "ChannelType%.2d", gIdx);
if(CONFIG_KEYEXISTS(serverSectionName, channelTypeName))
{
serverState.channelData =
static_cast<TChannelData>(CONFIG_INT(serverSectionName,
channelTypeName, CHANNEL_TYPE::NONE));
LOG_TRACE(1, ("channelType[%d][%d]: %d", sIdx, gIdx, serverState.channelData));
}
else if(CONFIG_KEYEXISTS("DefaultServerCfg", channelTypeName))
{
serverState.channelData =
static_cast<TChannelData>(CONFIG_INT("DefaultServerCfg",
channelTypeName, CHANNEL_TYPE::NONE));
}
static char maxUserNumName[256] = {0};
sprintf(maxUserNumName, "MaxUserNum%.2d", gIdx);
if(CONFIG_KEYEXISTS(serverSectionName, maxUserNumName))
{
serverState.maxUserNum =
static_cast<WORD>(CONFIG_INT(serverSectionName,
maxUserNumName, 100));
LOG_TRACE(1, ("maxUserName[%d][%d]: %d", sIdx, gIdx, serverState.maxUserNum));
}
else if(CONFIG_KEYEXISTS("DefaultServerCfg", maxUserNumName))
{
serverState.maxUserNum =
static_cast<WORD>(CONFIG_INT("DefaultServerCfg",
maxUserNumName, 100));
}
static char serverOpenName[256] = {0};
sprintf(serverOpenName, "ServerOpen%.2d", gIdx);
if(CONFIG_KEYEXISTS(serverSectionName, serverOpenName))
{
serverState.isClosed =
(CONFIG_INT(serverSectionName, serverOpenName, 0) == 0) ? true: false;
LOG_TRACE(1, ("ServerOpen[%d][%d]: %d", sIdx, gIdx, (serverState.isClosed == false)));
}
else if(CONFIG_KEYEXISTS("DefaultServerCfg", serverOpenName))
{
serverState.isClosed =
(CONFIG_INT("DefaultServerCfg", serverOpenName, 0) == 0) ? true: false;
}
}
}
}
}
}
static void ReloadCfg(void*)
{
ReloadServerAndChannelTypeCfg();
int serverCount(0);
int allUserCount(0);
for(BYTE sIdx = 0; sIdx <= MAX_SERVERNUM; ++sIdx)
{
for(BYTE gIdx = 0; gIdx <= MAX_GROUPNUM; ++gIdx)
{
SERVERSTATE& serverState = g_pServerCtxEx->serverNodes[sIdx][gIdx];
//PrintServerData
USERCONTEXT* pUserCtx = serverState.pUserCtx;
if(pUserCtx)
{
USERDATACONTEXT* pUserDataCtx((USERDATACONTEXT*)pUserCtx->pData);
ACE_INET_Addr addr(pUserDataCtx->wPort, ACE_NTOHL(pUserDataCtx->dwIP));
WORD userNum = std::accumulate(pUserDataCtx->iUserCount,
pUserDataCtx->iUserCount+MAX_WORLDNUM, (WORD)0);
LOG_TRACE(1, ("%.2d:%.2d (%s:%d) => %d/%d %s",
pUserDataCtx->bServerIdx, pUserDataCtx->bGroupIdx,
addr.get_host_addr(), addr.get_port_number(),
userNum, serverState.maxUserNum,
serverState.isClosed ? "(closed)":""));
serverCount++;
allUserCount += userNum;
}
}
}
LOG_TRACE(1, ("%d servers, %d users", serverCount, allUserCount));
//user
LOG_TRACE(1, ("%d clients", g_pServerCtxEx->userNodes.size()));
//reload data
g_pServerCtxEx->serverNodes[0][0].maxUserNum
= static_cast<WORD>(CONFIG_INT(SERVER_NAME, "WorldMaxUserNum", 250));
g_pServerCtxEx->noNotifyTimeLimit
= static_cast<WORD>(CONFIG_INT(SERVER_NAME, "NoNotifyTimeLimit", 60000));
#ifndef _WIN32
g_pServerCtxEx->licenseVerified =
LH_VerifyLicense(LH_GetCurrentMachineID(), "/etc/cabal/");
LOG_TRACE(1, ("%d%d userNode", g_pServerCtxEx->userNodes.size(),
g_pServerCtxEx->licenseVerified));
#endif
}
void RunServer(CNetKernel& sNetKernel)
{
CProcessLayer sProcessLayer;
CIOLayer sIOLayer(&sProcessLayer);
CProcSystem sProcSystem(&sProcessLayer, &sIOLayer);
if( !sProcessLayer.Active() ||
!sProcSystem.Active() ||
!sIOLayer.Active())
{
LOG_ERROR(("active fail"));
ACE_Reactor::end_event_loop();
}
sNetKernel.Wait();
}
int main(int argc, char** argv)
{
#ifndef _WIN32
extern ACE_SV_Semaphore_Complex g_mtxProcess;
char mutex_name[64];
sprintf(mutex_name, "%s_%d", "ipc_mutex_", NATION_NAME);
g_mtxProcess.open(mutex_name);
if (g_mtxProcess.tryacquire() < 0) return -1;
#endif
INIT_MD5SUM_BINARY()
#if !defined(_WIN32) && !defined(TESTING)
g_pServerCtxEx->licenseVerified =
LH_VerifyLicense(LH_GetCurrentMachineID(), "/etc/cabal/");
#else
g_pServerCtxEx->licenseVerified = true;
#endif /*_WIN32*/
CNetKernel sNetKernel;
if(!sNetKernel.Initialize(argc, argv)) {
return 2;
}
sNetKernel.SetSigHup(ReloadCfg);
// BootTimeInfo
g_pServerCtxEx->bootTick = ACE_OS::gettimeofday().msec();
SetLogText();
bool isClosed = (CONFIG_INT(SERVER_NAME, "DefaultServerOpen", 0) == 0 ? true : false);
for(int i=0; i<=MAX_SERVERNUM; ++i) {
for(int j=0; j<=MAX_GROUPNUM; ++j) {
g_pServerCtxEx->serverNodes[i][j].isClosed = isClosed;
}
}
ReloadServerAndChannelTypeCfg();
RunServer(sNetKernel);
//cleanup
ServerContextSingleton::close();
#ifdef MEDIAWEB_PCBANG
PCBANG::close();
#endif
return 0;
}