www.pudn.com > [Server]Distribute.rar > 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 "DistributeNetworkMsgParser.h" 
#include "DistributeDBMsgParser.h" 
 
#include "Usertable.h" 
#include "Servertable.h" 
#include  
#include "BootManager.h" 
#include "UserManager.h" 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
//#define _DBTHREAD 
 
DWORD _stdcall ProcessDBMessageThread(void* p) 
{ 
	while(1) 
	{ 
		Sleep(5); 
		 
		g_DB.ProcessingDBMessage(); 
 
		if(g_pServerSystem->m_DBThreadTerminate) 
			break; 
	} 
	return 0; 
} 
 
void ProcessDBMessage() 
{ 
	g_DB.ProcessingDBMessage(); 
} 
 
void ReceivedMsgFromServer(DWORD dwConnectionIndex,char* pMsg,DWORD dwLength); 
void ReceivedMsgFromUser(DWORD dwConnectionIndex,char* pMsg,DWORD dwLength); 
void OnAcceptServer(DWORD dwConnectionIndex); 
void OnDisconnectServer(DWORD dwConnectionIndex); 
void OnAcceptUser(DWORD dwConnectionIndex); 
void OnDisconnectUser(DWORD dwConnectionIndex); 
 
void GameProcess(); 
 
void ButtonProc1(); 
void ButtonProc2(); 
void ButtonProc3(); 
void OnCommand(char* szCommand); 
 
 
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);  
}  
 
 
 
// taiyo  
// Á¶±ÝÀ̳ª¸¶ ¼ÓµµÇâ»óÀ» º¸±â À§ÇØ Àü¿ª º¯¼ö·Î~! 
CServerSystem * g_pServerSystem = NULL; 
CMemoryPoolTempl g_UserInfoPool; 
 
CServerSystem::CServerSystem() 
{ 
	// servertable usertable ÃʱâÈ­ --------------------- 
	g_UserInfoPool.Init(3000, 500,"USERINFO"); 
	g_pUserTable = new CUserTable; 
	g_pUserTable->Init(4000); 
	g_pServerTable = new CServerTable; 
	g_pServerTable->Init(50); 
 
	CoInitialize(NULL); 
} 
 
CServerSystem::~CServerSystem() 
{ 
	CoUninitialize(); 
} 
 
void CServerSystem::Start(WORD ServerNum) 
{	 
	BOOTMNGR->AddSelfBootList(DISTRIBUTE_SERVER, ServerNum, g_pServerTable); 
	gUserMGR.LoadDistributeDesc(); 
 
	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,"Fail initialize Console",0,0); 
 
	char windowTitleMsg[64]; 
	sprintf(windowTitleMsg,"Distribute Server_%d", ServerNum); 
	SetWindowText(GetActiveWindow(), windowTitleMsg); 
 
	g_hWnd = GetActiveWindow(); 
	DWORD    dwProcessID = NULL; 
    DWORD    dwTreadID = ::GetWindowThreadProcessId(g_hWnd, &dwProcessID ); 
 
	HOOKPROC hProc; 
    hProc = CallWndProc; 
    hHook = ::SetWindowsHookEx(WH_CALLWNDPROC, hProc, (HINSTANCE)NULL, dwTreadID); 
 
 
	////////////////////////////////////////////////////////////////////////// 
	// ³×Æ®¿öÅ© ¼³Á¤ 
 
	/// ³×Æ®¿öÅ© ÆÄ¼­ ¼³Á¤------------------------------- 
	g_pServerMsgParser[0] = NULL; 
	for(int i=1 ; iLoadServerInfo(); 
	//g_pListCollect->LoadDistributeNum(); 
 
	CUSTOM_EVENT ev[2]; 
	ev[0].dwPeriodicTime = 1000; 
	ev[0].pEventFunc = GameProcess; 
	ev[1].dwPeriodicTime = 0; 
	ev[1].pEventFunc = ProcessDBMessage; 
	 
	DESC_NETWORK desc; 
	desc.OnAcceptServer = OnAcceptServer; 
	desc.OnDisconnectServer = OnDisconnectServer; 
	desc.OnAcceptUser = OnAcceptUser; 
	desc.OnDisconnectUser = OnDisconnectUser; 
	desc.OnRecvFromServerTCP = ReceivedMsgFromServer; 
	desc.OnRecvFromUserTCP = ReceivedMsgFromUser; 
 
#ifdef _DBTHREAD 
	desc.dwCustomDefineEventNum = 1; 
#else 
	desc.dwCustomDefineEventNum = 2; 
#endif 
 
	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 = NULL; 
 
	g_Network.Init(&desc); 
	//g_Network.Start(); 
	 
	// Booting 
	BOOTMNGR->AddSelfBootList(DISTRIBUTE_SERVER, ServerNum, g_pServerTable); 
	if(!BOOTMNGR->StartServer(&g_Network, g_pServerTable->GetSelfServer())) 
	{ 
		MessageBox(NULL,"Server Initialize Failed (StartServer)",NULL,NULL); 
		return; 
	} 
	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("DistributeDBInfo.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); 
 
	m_DBThreadTerminate = FALSE; 
#ifdef _DBTHREAD 
	CreateThread(0,0,ProcessDBMessageThread,0,0,&m_DBThreadID); 
#endif 
	 
	 
	// ·Î±×ÀΠüũ Å×À̺í ÃʱâÈ­ 
	LoginCheckInit(); 
	 
 
	g_Console.LOG(4, "-----------   DISTRIBUTE SERVER START  -----------------"); 
	// ÀÌ ºÎºÐÀº À©µµ¿ìÀÇ ¸Þ½ÃÁö ·çÇÁ.CONSOLEÀ» »ç¿ëÇϵç À©µµ¿ì ¸Þ½ÃÁö ±â¹ÝÀ¸·Î Â¥µç ¾î¶²½ÄÀ¸·Îµç À̺¥Æ®¸¦ ´ë±âÇØ¾ßÇÏ 
	// ÇϹǷΠ±ÍÂúÀº ¸Þ½ÃÁö ·çÇÁ ÀÛ¼ºÀ» ÇÏ°í ½ÍÁö ¾Ê´Ù¸é ÀÌ ¸Þ¼Òµå¸¦ »ç¿ëÇÑ´Ù.±× ´â°í ´âÀº ¸Þ½ÃÁö ·çÇÁ¿Í µ¿ÀÏÇÑ ±â´ÉÀ»  
	// ¼öÇàÇÑ´Ù.ÀÌ ¸Þ¼Òµå´Â I4DyuchiCONSOLE ´ÙÀ̾ó·Î±×ÀÇ x¹öưÀ» ´©¸¦¶§±îÁö ¸®ÅÏÇÏÁö ¾Ê´Â´Ù.ÀÌ ¸Þ¼Òµå°¡ ¸®ÅÏÇÑ´Ù¸é ÇÁ 
	// ·Î±×·¥ÀÌ Á¾·áÇÏ´Â °ÍÀÌ´Ù. 
	 
	g_bReady = TRUE; 
 
	g_Console.WaitMessage(); 
} 
void CServerSystem::Process() 
{ 
} 
 
 
void CServerSystem::End() 
{ 
	UnhookWindowsHookEx(hHook); 
	m_DBThreadTerminate = TRUE; 
	ICRelease(m_IdxCreater); 
	 
	g_DB.Release(); 
	 
	g_Network.Release(); 
	g_Console.Release(); 
 
//	if(g_pListCollect) 
//	{ 
//		delete g_pListCollect; 
//		g_pListCollect = NULL; 
//	} 
	if(g_pUserTable) 
	{ 
		delete g_pUserTable; 
		g_pUserTable = NULL; 
	} 
	if(g_pServerTable) 
	{ 
		delete g_pServerTable; 
		g_pServerTable = NULL; 
	} 
	CoFreeUnusedLibraries(); 
} 
 
DWORD CServerSystem::MakeAuthKey() 
{ 
	//return ICAllocIndex(m_IdxCreater) + 1; 
	static DWORD ID = 1;//GetTickCount()*GetTickCount(); 
	if(ID == 0) 
		++ID; 
	return ID++; 
} 
void CServerSystem::ReleaseAuthKey(DWORD key) 
{ 
	//ICFreeIndex(m_IdxCreater,key-1); 
} 
 
 
 
 
// global function 
 
void OnConnectServerSuccess(DWORD dwConnectionIndex, void* pVoid) 
{ 
	SERVERINFO* info = (SERVERINFO*)pVoid; 
	info->dwConnectionIndex = dwConnectionIndex; 
	if(info->wServerKind == MONITOR_SERVER) 
	{ 
		BOOTMNGR->NotifyBootUpToMS(&g_Network); 
		g_Console.LOG(4, "Connected to the MS : %s, %d, (%d)", info->szIPForServer, info->wPortForServer, dwConnectionIndex); 
	} 
	else 
	{ 
		BOOTMNGR->SendConnectSynMsg(&g_Network, dwConnectionIndex, g_pServerTable); 
		g_Console.LOG(4, "Connected to the Server : %s, %d, (%d)", info->szIPForServer, info->wPortForServer, dwConnectionIndex); 
	} 
 
/* 
	SERVERINFO* info = (SERVERINFO*)pVoid; 
	if(!dwConnectionIndex) return; 
 
	info->dwConnectionIndex = dwConnectionIndex;					// ÀÌ°Ô Á¦´ë·Î µ¿ÀÛÇϳª? üũ 
	MSG_WORD msg; 
	msg.Category = MP_SERVER; 
	msg.Protocol = MP_SERVER_PWRUP; 
	msg.wData = g_pServerTable->GetSelfServer()->wPortForServer; 
	g_Network.Send2Server(dwConnectionIndex, (char*)&msg, sizeof(msg));*/ 
 
	//if(g_pServerTable->m_MaxServerConnectionIndex < dwIndex)			//?????? 
			//g_pServerTable->m_MaxServerConnectionIndex = dwIndex;	 
} 
 
void OnDisconnectServer(DWORD dwConnectionIndex) 
{ 
	g_Console.LOG(4, "Server Disconnected : ConnectionIndex %d", dwConnectionIndex);//pjslocal 
 
	// ¼­¹öÁ¤º¸ Áö¿ò 
	SERVERINFO * delInfo = g_pServerTable->RemoveServer(dwConnectionIndex); 
	if(!delInfo) 
	{ 
		//BootManagerÀÛµ¿ÀÌ ¿Ï·áÇÏÁö ¾Ê¾ÒÀ» ¶§ ¼­¹ö°¡ ²¨Áú °æ¿ì ¹ß»ý,(¿Ã¹Ù¸¥ ¿¡·¯) 
		ASSERT(0); 
	} 
	delete delInfo; 
} 
 
void OnConnectServerFail(void* pVoid) 
{	 
	SERVERINFO* info = (SERVERINFO*)pVoid; 
	if(info->wServerKind == MONITOR_SERVER) 
	{ 
		BOOTMNGR->AddBootListINI(DISTRIBUTE_SERVER, 0, g_pServerTable); 
		BOOTMNGR->BactchConnectToMap(&g_Network, g_pServerTable); 
		g_Console.LOG(4, "Failed to Connect to the MS : %s, %d", info->szIPForServer, info->wPortForServer); 
	} 
	else 
	{ 
		//not process 
		g_Console.LOG(4, "Failed to Connect to the Server : %s, %d", info->szIPForServer, info->wPortForServer); 
		BOOTMNGR->RemoveBootList(g_pServerTable, info->wPortForServer); 
	}	 
} 
void OnAcceptServer(DWORD dwConnectionIndex) 
{// not used 
	char strr[255]; 
	wsprintf(strr, "%d ¹ø connectionIndex ¼­¹ö Á¢¼Ó ", dwConnectionIndex);//pjslocal 
 
	g_Console.LOG(4, strr); 
} 
 
void OnAcceptUser(DWORD dwConnectionIndex) 
{	 
	if(g_bReady == FALSE) 
	{ 
		// ÃʱâÈ­°¡ ¿ÏÀüÈ÷ ¾ÈµÆ´Âµ¥ µé¾î¿Â°æ¿ì. 
		MSGBASE send; 
		send.Category = MP_USERCONN; 
		send.Protocol = MP_USERCONN_SERVER_NOTREADY; 
		send.dwObjectID = 0; 
		g_Network.Send2User(dwConnectionIndex, (char *)&send, sizeof(send)); 
 
		DisconnectUser(dwConnectionIndex); 
		return; 
	} 
 
	DWORD authkey = g_pServerSystem->MakeAuthKey(); 
	USERINFO * info = g_UserInfoPool.Alloc(); 
	memset(info, 0, sizeof(USERINFO)); 
	info->dwConnectionIndex = dwConnectionIndex; 
	info->dwUniqueConnectIdx = authkey; 
	g_pUserTable->AddUser(info,dwConnectionIndex); 
 
	MSGBASE send; 
	send.Category = MP_USERCONN; 
	send.Protocol = MP_USERCONN_DIST_CONNECTSUCCESS; 
	send.dwObjectID = authkey; 
	g_Network.Send2User(dwConnectionIndex, (char *)&send, sizeof(send)); 
 
//	g_Console.Log(eLogDisplay,4, "OnAcceptUser : Client Connected - Idx:%d, AuthKey:%d, Total(%d)",dwConnectionIndex,authkey, g_pUserTable->GetUserCount()); 
//	g_Console.Log(eLogFile,4, "OnAcceptUser : Client Connected - Idx:%d, AuthKey:%d, Total(%d)",dwConnectionIndex,authkey, g_pUserTable->GetUserCount()); 
} 
 
void OnDisconnectUser(DWORD dwConnectionIndex) 
{ 
	g_pUserTable->OnDisconnectUser(dwConnectionIndex); 
//	g_Console.Log(eLogDisplay,4, "OnAcceptUser : Client Disconnected - Idx:%d, Total(%d)",dwConnectionIndex, g_pUserTable->GetUserCount()); 
//	g_Console.Log(eLogFile,4, "OnAcceptUser : Client Disconnected - Idx:%d, Total(%d)",dwConnectionIndex, g_pUserTable->GetUserCount()); 
} 
 
void ReceivedMsgFromServer(DWORD dwConnectionIndex,char* pMsg,DWORD dwLength) 
{ 
	MSGROOT* pTempMsg = reinterpret_cast(pMsg); 
	 
	if( g_pServerMsgParser[pTempMsg->Category] == NULL ) 
	{ 
		int a = 0; 
		return; 
	} 
	g_pServerMsgParser[pTempMsg->Category](dwConnectionIndex, pMsg, dwLength); 
} 
 
void ReceivedMsgFromUser(DWORD dwConnectionIndex,char* pMsg,DWORD dwLength) 
{ 
	MSGROOT* pTempMsg = reinterpret_cast(pMsg); 
//	ServerTraffic * Msg = reinterpret_cast(pMsg); 
	ASSERT(g_pUserMsgParser[pTempMsg->Category]); 
	if(g_pUserMsgParser[pTempMsg->Category] == NULL) 
		return; 
	g_pUserMsgParser[pTempMsg->Category](dwConnectionIndex, pMsg, dwLength); 
} 
 
void GameProcess() 
{ 
	g_pServerSystem->Process(); 
} 
 
// console interface function 
void ButtonProc1() 
{ 
	//SendMessage(g_hWnd, WM_CLOSE, 0, 0); 
} 
 
void ButtonProc2() 
{ 
	g_Console.Log(eLogDisplay,4, "UserCount : %d", g_pUserTable->GetUserCount()); 
} 
 
void ButtonProc3() 
{ 
 
} 
 
void OnCommand(char* szCommand) 
{ 
 
}