📄 serversystem.cpp
字号:
// ServerSystem.cpp: implementation of the CServerSystem class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ServerSystem.h"
#include "Console.h"
#include "Network.h"
#include "DataBase.h"
#include "CommonNetworkMsgParser.h"
#include "AgentNetworkMsgParser.h"
#include "Usertable.h"
#include "Servertable.h"
#include <MemoryPoolTempl.h>
#include "AgentDBMsgParser.h"
#include "Crypt.h"
#include "FilteringTable.h"
#include "MHTimeManager.h"
#include "BootManager.h"
#include "MHFile.h"
#include "AgentDBMsgParser.h"
#include "TrafficLog.h"
#ifdef _NETWORKTEST
DWORD RecvServerMsgAmount[MP_MAX] = {0,};
DWORD RecvUserMsgAmount[MP_MAX] = {0,};
extern DWORD SendServerMsgAmount[MP_MAX];
extern DWORD SendUserMsgAmount[MP_MAX];
#endif
void fConnectionCheck();
DWORD g_dwAcceptUser = 0;
BOOL g_bCloseWindow = FALSE;
extern DWORD g_dwAcceptUser2;
extern int g_nServerSetNum;
HWND g_hWnd;
HHOOK hHook;
LRESULT WINAPI CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
CWPSTRUCT* lpWp = (CWPSTRUCT*)lParam;
if(lpWp->message == WM_CLOSE)
{
DefWindowProc(g_hWnd, lpWp->message, lpWp->wParam, lpWp->lParam);
PostQuitMessage(0);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// taiyo
CServerSystem * g_pServerSystem = NULL;
// MAPSERVER俊辑绰 USERINFO啊 酒聪扼 CPlayer啊 等促!
CMemoryPoolTempl<USERINFO> g_UserInfoPool;
CServerSystem::CServerSystem()
{
CoInitialize(NULL);
#ifdef _CRYPTCHECK_
m_bUseCrypt = TRUE;
#else
m_bUseCrypt = FALSE;
#endif
}
CServerSystem::~CServerSystem()
{
CoUninitialize();
}
void CServerSystem::Start(WORD ServerNum)
{
m_wServerNum = ServerNum;
srand(GetTickCount());
m_IdxCreater = ICCreate();
ICInitialize(m_IdxCreater,5000);
//////////////////////////////////////////////////////////////////////////
// 能贾 檬扁拳
// 滚瓢 汲沥.寇何涝仿侩 滚瓢捞 28俺 霖厚登绢乐促.茄俺档 荤侩窍瘤 臼阑 版快 pCustomButton 鞘靛甫 NULL肺 盲款促.
MENU_CUSTOM_INFO pMenu[3];
strcpy(pMenu[0].szMenuName, "Button1");
pMenu[0].cbMenuCommand = ButtonProc1;
strcpy(pMenu[1].szMenuName, "Button2");
pMenu[1].cbMenuCommand = ButtonProc2;
strcpy(pMenu[2].szMenuName, "Button3");
pMenu[2].cbMenuCommand = ButtonProc3;
if(!g_Console.Init(3,pMenu,OnCommand))
MessageBox(NULL,"Console Initializing Failed",0,0);
char TitleText[255];
sprintf(TitleText, "Agent Server: %d", ServerNum);
SetWindowText(GetActiveWindow(), TitleText);
g_hWnd = GetActiveWindow();
DWORD dwProcessID = NULL;
DWORD dwTreadID = ::GetWindowThreadProcessId(g_hWnd, &dwProcessID );
HOOKPROC hProc;
hProc = CallWndProc;
hHook = ::SetWindowsHookEx(WH_CALLWNDPROC, hProc, (HINSTANCE)NULL, dwTreadID);
SetMapChangeInfo();
//////////////////////////////////////////////////////////////////////////
// 匙飘况农 汲沥
/// 匙飘况农 颇辑 汲沥-------------------------------
g_pServerMsgParser[0] = NULL; // YH 0锅篮 厚奎促. MP_SERVER == 1
for(int i=1 ; i<MP_MAX ; ++i)
g_pServerMsgParser[i] = TransToClientMsgParser;
g_pServerMsgParser[MP_SERVER] = MP_AGENTSERVERMsgParser;
g_pServerMsgParser[MP_POWERUP] = MP_POWERUPMsgParser;
//g_pServerMsgParser[MP_CHAR] = MP_CHARMsgParser;
g_pServerMsgParser[MP_MAP] = TransToClientMsgParser;
g_pServerMsgParser[MP_ITEM] = TransToClientMsgParser;
g_pServerMsgParser[MP_CHAT] = MP_CHATServerMsgParser;
g_pServerMsgParser[MP_USERCONN] = MP_USERCONNMsgParser;
g_pServerMsgParser[MP_MOVE] = TransToClientMsgParser;
g_pServerMsgParser[MP_MUGONG] = TransToClientMsgParser;
g_pServerMsgParser[MP_AUCTIONBOARD] = TransToClientMsgParser;
g_pServerMsgParser[MP_CHEAT] = TransToClientMsgParser;
g_pServerMsgParser[MP_QUICK] = TransToClientMsgParser;
g_pServerMsgParser[MP_PACKEDDATA] = MP_PACKEDMsgParser;
g_pServerMsgParser[MP_PARTY] = MP_PARTYServerMsgParser;
g_pServerMsgParser[MP_MUNPA] = MP_MUNPAServerMsgParser;
g_pServerMsgParser[MP_FRIEND] = MP_FRIENDMsgParser;
g_pServerMsgParser[MP_WANTED] = MP_WANTEDServerMsgParser;
g_pServerMsgParser[MP_AUCTION] = TransToClientMsgParser;
g_pServerMsgParser[MP_MORNITORMAPSERVER] = MP_MonitorMsgParser;
g_pServerMsgParser[MP_MURIMNET] = MP_MURIMNETServerMsgParser;
g_pServerMsgParser[MP_CHEAT] = MP_CHEATServerMsgParser;
for(i=1 ; i<MP_MAX ; ++i)
g_pUserMsgParser[i] = TransToMapServerMsgParser;
g_pUserMsgParser[MP_USERCONN] = MP_USERCONNMsgParser;
g_pUserMsgParser[MP_CHAT] = MP_CHATMsgParser;
g_pUserMsgParser[MP_FRIEND] = MP_FRIENDMsgParser;
g_pUserMsgParser[MP_MUNPA] = MP_MUNPAUserMsgParser;
g_pUserMsgParser[MP_MURIMNET] = MP_MURIMNETUserMsgParser;
g_pUserMsgParser[MP_OPTION] = MP_OPTIONUserMsgParser;
g_pUserMsgParser[MP_DEBUG] = MP_DebugMsgParser;
g_pUserMsgParser[MP_CHEAT] = MP_CHEATUserMsgParser;
g_pUserMsgParser[MP_HACKCHECK] = MP_HACKCHECKMsgParser;
// servertable usertable 檬扁拳 ---------------------
g_UserInfoPool.Init(3000, 500,"UserInfoPool");
g_pUserTable = new CUserTable; // KEY : dwConnectionIndex
g_pUserTable->Init(4000);
g_pUserTableForUserID = new CUserTable; // KEY : dwUserIdx
g_pUserTableForUserID->Init(4000);
g_pUserTableForObjectID = new CUserTable; // KEY : dwObjectID
g_pUserTableForObjectID->Init(4000);
g_pServerTable = new CServerTable; // KEY : ServerPort
g_pServerTable->Init(50);
CUSTOM_EVENT ev[3];
ev[0].dwPeriodicTime = 1000;
ev[0].pEventFunc = GameProcess;
ev[1].dwPeriodicTime = 0;
ev[1].pEventFunc = ProcessDBMessage;
ev[2].dwPeriodicTime = 1000*30; // 30sec
ev[2].pEventFunc = fConnectionCheck;
DESC_NETWORK desc;
desc.OnAcceptServer = OnAcceptServer;
desc.OnDisconnectServer = OnDisconnectServer;
desc.OnAcceptUser = OnAcceptUser;
desc.OnDisconnectUser = OnDisconnectUser;
desc.OnRecvFromServerTCP = ReceivedMsgFromServer;
desc.OnRecvFromUserTCP = ReceivedMsgFromUser;
desc.dwCustomDefineEventNum = 3;
desc.pEvent = ev;
desc.dwMainMsgQueMaxBufferSize = 20480000;
desc.dwMaxServerNum = 50;
desc.dwMaxUserNum = 4000;
desc.dwServerBufferSizePerConnection = 512000;
desc.dwServerMaxTransferSize = 65000;
desc.dwUserBufferSizePerConnection = 256000;
desc.dwUserMaxTransferSize = 65000;
desc.dwConnectNumAtSameTime = 4000;
desc.dwFlag = NETDDSC_DEBUG_LOG;
g_Network.Init(&desc);
BOOTMNGR->AddSelfBootList(AGENT_SERVER, ServerNum, g_pServerTable);
if(!BOOTMNGR->StartServer(&g_Network, g_pServerTable->GetSelfServer()))
{
ASSERT(0);
}
if(!BOOTMNGR->ConnectToMS(&g_Network, g_pServerTable))
{
SERVERINFO info;
info.wServerKind = MONITOR_SERVER;
OnConnectServerFail(&info);
}
//////////////////////////////////////////////////////////////////////////
// DB 汲沥
DWORD maxthread = 2;
DWORD maxqueryinsametime = 1024;
FILE* fp = fopen("AgentDBInfo.txt","r");
if(fp)
{
fscanf(fp,"%d %d",&maxthread,&maxqueryinsametime);
fclose(fp);
}
if(g_DB.Init(maxthread,maxqueryinsametime) == FALSE)
MessageBox(NULL,"DataBase Initializing Failed",0,0);
// g_Network.Start();
/* char temp2[10036],temp[10036] = {0,};
for(int n=0;n<500;++n)
strcat(temp,"hi~ ^0^");
sprintf(temp2,"EXEC %s %d, 'HI', \'%s\', %d, %d, %d", "MP_MUNPA_BoardRegContent",
61, temp, 6468, 100, 20);
g_DB.FreeLargeQuery(NULL,0,temp2);
*/
//KES
//鞘磐傅 檬扁拳
FILTERTABLE->Init();
TRAFFIC->StartRecord();
g_Console.LOG(4, "----------- AGENT SERVER START -----------------");
// 捞 何盒篮 扩档快狼 皋矫瘤 风橇.CONSOLE阑 荤侩窍电 扩档快 皋矫瘤 扁馆栏肺 楼电 绢恫侥栏肺电 捞亥飘甫 措扁秦具窍
// 窍骨肺 蓖满篮 皋矫瘤 风橇 累己阑 窍绊 酵瘤 臼促搁 捞 皋家靛甫 荤侩茄促.弊 粹绊 粹篮 皋矫瘤 风橇客 悼老茄 扁瓷阑
// 荐青茄促.捞 皋家靛绰 I4DyuchiCONSOLE 促捞倔肺弊狼 x滚瓢阑 穿甫锭鳖瘤 府畔窍瘤 臼绰促.捞 皋家靛啊 府畔茄促搁 橇
// 肺弊伐捞 辆丰窍绰 巴捞促.
g_bReady = TRUE;
//Get GMPowerList
// GM_GetGMPowerList( 0, 0 );
//俊捞傈飘甫 懦 锭 捞 俊捞傈飘俊 乐绰 蜡历甫 肺弊牢 抛捞喉俊辑 瘤款促.
char txt[128];
sprintf(txt, "EXEC Up_Agent_Down %d, %d", g_nServerSetNum , g_pServerTable->GetSelfServer()->wServerNum);
g_DB.LoginQuery(eQueryType_FreeQuery, eAgentDown, 0, txt);
g_Console.WaitMessage();
}
void CServerSystem::Process()
{ // YH 泅犁 1檬付促 茄锅究 甸绢咳
if(g_bCloseWindow)
{
SendMessage(g_hWnd, WM_CLOSE, 0,0);
g_bCloseWindow = FALSE;
g_bReady = FALSE;
}
MHTIMEMGR_OBJ->Process();
TRAFFIC->Process();
}
void CServerSystem::ConnectionCheck()
{ // YH 泅犁 30檬付促 茄锅究 甸绢咳
DWORD _60sec = 60*1000;
USERINFO* pInfo;
DWORD elapsedtime;
if(g_bReady == FALSE)
return;
g_pUserTableForUserID->SetPositionHead();
while(pInfo = g_pUserTableForUserID->GetData())
{
if(pInfo->dwConnectionIndex == 0)
{
// 酒流 立加捞 力措肺 捞凤瘤瘤 臼篮 版快
elapsedtime = gCurTime - pInfo->dwLastConnectionCheckTime;
if(elapsedtime > _60sec*2)
{
ASSERT(pInfo->dwCharacterID == 0);
LoginCheckDelete(pInfo->dwUserID); // 肺弊牢眉农 抛捞喉俊辑 昏力
g_pServerSystem->ReleaseAuthKey(pInfo->dwUniqueConnectIdx);
g_pUserTableForUserID->RemoveUser(pInfo->dwUserID);
memset(pInfo,0,sizeof(USERINFO));
g_UserInfoPool.Free(pInfo);
// g_Console.LOG(4, "UserCount %d %d %d",
// g_pUserTableForUserID->GetUserCount(),
// g_pUserTable->GetUserCount(),
// g_pUserTableForObjectID->GetUserCount());
}
}
else
{
SendSpeedHackCheckMsg(pInfo);
if(pInfo->UserLevel == eUSERLEVEL_PROGRAMMER)
continue;
elapsedtime = gCurTime - pInfo->dwLastConnectionCheckTime;
if( elapsedtime > _60sec*2 )
{
// 立加捞 捞凤柳 版快
if(pInfo->m_ConnectionCheckFailed) // 父距 牧池记 眉农甫 焊陈绰单 1盒悼救 览翠捞 绝菌促搁
{
// DisconnectUser(pInfo->dwConnectionIndex);
DisconnectUser(pInfo->dwConnectionIndex, 1);
continue;
}
else
{
pInfo->m_ConnectionCheckFailed = TRUE;
SendConnectionCheckMsg(pInfo);
pInfo->dwLastConnectionCheckTime = gCurTime;
}
}
}
}
}
void CServerSystem::SendConnectionCheckMsg(USERINFO* pInfo)
{
MSGBASE msg;
msg.Category = MP_USERCONN;
msg.Protocol = MP_USERCONN_CONNECTION_CHECK;
g_Network.Send2User(pInfo->dwConnectionIndex,(char*)&msg,sizeof(msg));
/*
MSG_DWORD msg;
msg.Category = MP_USERCONN;
msg.Protocol = MP_USERCONN_CONNECTION_CHECK;
msg.dwData = gCurTime;
g_Network.Send2User(pInfo->dwConnectionIndex,(char*)&msg,sizeof(msg));
*/
}
void CServerSystem::SendSpeedHackCheckMsg(USERINFO* pInfo)
{
if( pInfo->wUserMapNum )
{
MSG_DWORD msg;
msg.Category = MP_HACKCHECK;
msg.Protocol = MP_HACKCHECK_SPEEDHACK;
msg.dwData = gCurTime; //累篮 矫埃阑 林绰 巴篮 宝满促.
g_Network.Send2User(pInfo->dwConnectionIndex,(char*)&msg,sizeof(msg));
}
}
void CServerSystem::End()
{
g_bReady = FALSE;
TRAFFIC->EndRecord();
UnhookWindowsHookEx(hHook);
g_pUserTableForUserID->SetPositionHead();
USERINFO* pInfo;
while(pInfo = g_pUserTableForUserID->GetData())
{
LoginCheckDelete(pInfo->dwUserID);
g_pServerSystem->ReleaseAuthKey(pInfo->dwUniqueConnectIdx);
g_pUserTableForUserID->RemoveUser(pInfo->dwUserID);
g_UserInfoPool.Free(pInfo);
}
ICRelease(m_IdxCreater);
g_DB.Release();
g_Network.Release();
g_Console.Release();
if(g_pUserTable)
{
g_pUserTable->RemoveAllUser();
delete g_pUserTable;
g_pUserTable = NULL;
}
if(g_pUserTableForObjectID)
{
g_pUserTableForObjectID->RemoveAllUser();
delete g_pUserTableForObjectID;
g_pUserTableForObjectID = NULL;
}
if(g_pServerTable)
{
delete g_pServerTable;
g_pServerTable = NULL;
}
CoFreeUnusedLibraries();
}
void CServerSystem::SetMapChangeInfo()
{
char filename[64];
CMHFile file;
#ifdef _FILE_BIN_
sprintf(filename,"./Resource/MapChange.bin");
if(!file.Init(filename, "rb"))
return;
#else
sprintf(filename,"./Resource/MapChange.txt");
if(!file.Init(filename, "rt"))
return;
#endif
int n =0;
while(1)
{
if(file.IsEOF())
break;
m_MapChange[n].Kind = file.GetWord();
file.GetString(m_MapChange[n].CurMapName);
file.GetString(m_MapChange[n].ObjectName);
m_MapChange[n].CurMapNum = file.GetWord();
m_MapChange[n].MoveMapNum = file.GetWord();
m_MapChange[n].CurPoint.x = file.GetFloat();
m_MapChange[n].CurPoint.y = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -