⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 com.cpp

📁 决战帝王1.5武神降临对喜爱决战的玩家共享研究用
💻 CPP
📖 第 1 页 / 共 5 页
字号:
///////////////////////////////////////////////////////////////////
//	COM.cpp : implementation file for the COM class
//  Copyright (C) 2000,  Jin-Soo Jeon
//
//	If this code works, it was written by JinSoo Jeon.
//	If not, I don't know who wrote it.
//
#include "stdafx.h"
#include "Server.h"
#include "USER.h"
#include "COM.h"
#include "BufferEx.h"
#include "Extern.h"
#include <process.h>
#include "ServerDlg.h"
#include "Search.h"
#include "RoyalRumble.h"
#include "EventZone.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define GUILD_BEGIN_WAR_TIME		10

//////////////////////////////////////////////////////////////////////
//	Global Variable
// IKING 2001.1.
extern CServerDlg			*g_pMainDlg;
extern int					MAXPOSSIBLE_USER;
extern CSearch				*g_pUserList;
extern CRITICAL_SECTION		m_CS_SqlData;
extern long					nSqlDataCount;		
extern CPtrList				RecvSqlData;
extern CRoyalRumble			g_RR;

HANDLE				g_hIOCP				= NULL;
SOCKET				g_sdListen			= INVALID_SOCKET;
WSAEVENT			g_hListenEvent		= WSA_INVALID_EVENT;

_int64				g_OnlineEnd			= 0;
_int64				g_OnlineMinStart	= 0;
_int64				g_Online_Update_Min_ticks = 0;

long				g_GameTime			= 0;
long				g_GameDay			= 0;
long				g_GameMinute		= 0;

volatile long g_bShutDown = 0;

// Quest Event Class
CEventZone	g_QuestEventZone;
///////////////////////////////////////////////////////////////////////////
// Global functions

//DWORD WINAPI SendThreadMain(LPVOID pVoid)
//unsigned __stdcall SendThreadMain( void *pVoid )
UINT SendThreadMain( void *pVoid )
{
	COM* pCom = (COM*)pVoid;

	int					nRet				= 0;
	int					iRemainCount		= 0;
	DWORD				dwBytesTransferred	= 0;
	DWORD				dwKey				= 0;
	LPOVERLAPPED		pOverlapped			= NULL;

	SEND_DATA*			pSendData			= NULL;
	SEND_DATA*			pNewData			= NULL;

	int					i					= 0;

	int					modsid				= 0;
	CRITICAL_SECTION*	pSendCS				= NULL;

	CPtrList			LocalDataList;
	int					nLocalDataCount = 0;

	while( TRUE )
	{
		nRet = ::GetQueuedCompletionStatus(pCom->m_hSendIOCP, &dwBytesTransferred, &dwKey, &pOverlapped, INFINITE);

		// assume we are always using an INFINITE timeout
		if( nRet == FALSE && pOverlapped == NULL )
		{
			TRACE("####Error In SendThreadMain()\n");
			break;
		}

		if( nRet == FALSE ) continue;


		modsid = (int)dwKey;

		if( modsid < 0 || modsid > AUTOMATA_THREAD ) continue;


		pSendCS = &(pCom->m_critSendData[modsid]);

		if( !pSendCS ) continue;


		pCom->nSendDataCount[modsid] = pCom->SendDataList[modsid].GetCount();

		if( pCom->nSendDataCount[modsid] < 1 ) continue;


		EnterCriticalSection( pSendCS );

		while( pCom->nSendDataCount[modsid] >= 1 )
		{
			pSendData = (SEND_DATA*)pCom->SendDataList[modsid].RemoveHead();
			pCom->nSendDataCount[modsid] = pCom->SendDataList[modsid].GetCount();

			if( pSendData )
			{
				LocalDataList.AddTail( pSendData );
			}
		}

		LeaveCriticalSection( pSendCS );

		nLocalDataCount = LocalDataList.GetCount();


		while( nLocalDataCount >= 1 )
		{
			pSendData = (SEND_DATA*)LocalDataList.RemoveHead();
			nLocalDataCount = LocalDataList.GetCount();

			if( !pSendData ) continue;

			pCom->Send( pSendData );

			delete pSendData;
			pSendData = NULL;
		}
	}

	return 0;
}

//DWORD WINAPI TimerThread(LPVOID lParam)
//unsigned __stdcall  TimerThread(void *lParam)
UINT TimerThread(void *lParam)
{
	COM* pCom = (COM*) lParam;

	BOOL bRain = FALSE;
	int	 i, iTime = 0;
	int  iUserCountUpdate = 0;

	int iRainTime[30] = {1, 1, 14, 11, 14, 16, 11, 13, 16, 12,
						 11, 12, 13, 11, 14, 10, 11, 13, 15, 12,
						 14, 12, 9, 11, 14, 16, 16, 13, 16, 12};

	g_GameTime = 3;		// 抛胶飘甫 困秦...


	while(TRUE)
	{
		pCom->CheckGuildWarTime();										// 辨靛傈牢瘤 眉农茄促.
		pCom->CheckFortressWarTime();
		g_RR.CheckRRStatus();

		QueryPerformanceCounter((LARGE_INTEGER*)&g_OnlineEnd);

		if((g_OnlineEnd - g_OnlineMinStart) >= g_Online_Update_Min_ticks)
		{
			g_OnlineMinStart += g_Online_Update_Min_ticks;

			InterlockedExchangeAdd(&g_GameMinute, 10);				// 公炼扒 10盒究 刘啊
																	// 60盒 捞搁 0栏肺 檬扁拳	
			if(g_GameMinute == 60)
			{
				::InterlockedExchange(&g_GameMinute, 0);

				InterlockedExchangeAdd(&g_GameTime, 1);				// 1矫埃究 刘啊茄促. 
				if(g_GameTime == 24) 
				{ 
					InterlockedExchangeAdd(&g_GameDay, 1);			// 24矫捞搁 窍风 刘啊茄促.
					::InterlockedExchange(&g_GameTime, 1);			// 矫埃篮 1矫肺 悸泼
				}
				if(g_GameDay == 31) ::InterlockedExchange(&g_GameDay, 1);// 朝楼档 窍风

				pCom->SendTimeToAll();
				
				if(iRainTime[g_GameDay] == g_GameTime)
				{
//					TRACE("厚棵矫埃牢单...Day : %d, Time : %d\n", g_GameDay, iRainTime[g_GameDay]);

					for(i = 0; i < g_WeatherZone.GetSize(); i++)
					{
	//					if(rand()%2 == 0)	//犬伏 50%
	//					{
//							TRACE("厚啊柯促...Day : %d, Time : %d\n", g_GameDay, iRainTime[g_GameDay]);
							bRain = TRUE;
							iTime = g_GameTime;
							g_WeatherZone[i]->bRaining = TRUE;
							pCom->SetWeatherZone(g_WeatherZone[i]->iZone, 2, 1);
	//					}
					}
				}

				if(bRain && (iTime+2 == g_GameTime))
				{
//					TRACE("厚~场...Day : %d, Time : %d\n", g_GameDay, iRainTime[g_GameDay]);
					for(i = 0; i < g_WeatherZone.GetSize(); i++)
					{
						if(g_WeatherZone[i]->bRaining) 
						{
							bRain = FALSE;
							g_WeatherZone[i]->bRaining = FALSE;
							pCom->SetWeatherZone(g_WeatherZone[i]->iZone, 2, 0);
						}
					}
				}
			}

//			TRACE("瘤陛 盒俊辑绰 矫埃 %d : %d盒\n", g_GameTime, g_GameMinute);
		}
		Sleep(1000);
/*
		iUserCountUpdate++;

		if( iUserCountUpdate >= 600 )
		{
			iUserCountUpdate = 0;

			pCom->UpdateUserCount();
*/
/*
			int Datalength;
			SQLDATAPACKET *pSDP;
			pSDP = new SQLDATAPACKET;
			pSDP->code = DB_USER_COUNT;
			Datalength = 0;
			pSDP->dcount = Datalength;
			pSDP->UID = 0;
			pSDP->pData = NULL;

			EnterCriticalSection( &m_CS_SqlData );
			RecvSqlData.AddTail(pSDP);
			nSqlDataCount = RecvSqlData.GetCount();
			LeaveCriticalSection( &m_CS_SqlData );
*/
//		}
	}
	return 1;
}

////////////////////////////////////////////////////////////////////////
//	Event 扁埃阑 悸泼窍扁 困茄 Thread
//
UINT EventTimerThread(void *lParam)
{
	COM* pCom = (COM*) lParam;

	while(TRUE)
	{
		SYSTEMTIME st;
		::GetLocalTime(&st);
		COleDateTime oleEventStart(2002, 12, 27, 9, 0, 0), oleEventEnd(2002, 12, 28, 9, 0, 0);
		COleDateTime oleToday;

		if(st.wYear == 2002 && st.wMonth == 12)
		{
			if(st.wDay >= 27 && st.wDay <= 28) 
			{
				oleToday.SetDateTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
				if(oleToday >= oleEventStart && oleToday <= oleEventEnd) g_iMoonEvent = 1;
				else g_iMoonEvent = 1;
			}
			else 
			{
				g_iMoonEvent = 1;
			}
		}
		else 
		{
			g_iMoonEvent = 1;
		}

		Sleep(1000 * 60);
	}

	return 1;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction

COM::COM()
{
	m_bInit = FALSE;
	m_dwConcurrency = 1;
}

COM::~COM()
{
	if(m_bInit)
	{
		for(int i = 0; i < AUTOMATA_THREAD+1; i++) DeleteCriticalSection( &m_critSendData[i] );

		DeleteCriticalSection( &m_critGuild );
		DeleteCriticalSection( &m_critEvent );

		// IKING 2001.1.
		DeleteCriticalSection( &m_critThrowItem );
		//

		DeleteAll();
	}
}

///////////////////////////////////////////////////////////////////////
//	劝寸沁带 皋葛府甫 秦力茄促.
//
void COM::DeleteAll()
{
	DeleteThrowItemArray();
}

/////////////////////////////////////////////////////////////////////////
//	Item Array 昏力
//
void COM::DeleteThrowItemArray()
{
	int i;

	for(i = 0; i < MAX_THROW_ITEM; i++ )
	{
		if( m_ThrowItemArray[i] )
		{
			delete m_ThrowItemArray[i];
			m_ThrowItemArray[i] = NULL;
		}
	}
}

/////////////////////////////////////////////////////////////////////////
//	甘俊 阶捞绰 酒捞袍 Array 檬扁拳
//
void COM::InitThrowItemArray()
{
	int i;

	m_ThrowAddIndex = 0;

	for(i = 0; i < MAX_THROW_ITEM; i++)
	{
		m_ThrowItemArray[i] = new CThrowItem;
	}
}

////////////////////////////////////////////////////////////////////////
//	Send Thread 檬扁拳
//
/*
void COM::InitSendThread()
{
	m_hSendIOCP = INVALID_HANDLE_VALUE;
}
*/

////////////////////////////////////////////////////////////////////////
//	家南 檬扁拳
//
BOOL COM::Init(int port)
{
	GetCPUCount();

	char name[255];
//	PHOSTENT hostinfo;

	if( gethostname ( name, sizeof(name)) == 0)
	{
//		  if((hostinfo = gethostbyname(name)) != NULL)
//		  {
//				m_strIP = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
//		  }
		m_strIP.Format( "%s", name );
	}

	for( int i = 0; i < AUTOMATA_THREAD+1; i++ )
		InitializeCriticalSection( &m_critSendData[i] );

	InitializeCriticalSection( &m_critGuild );
	InitializeCriticalSection( &m_critEvent );

	// IKING 2001.1.
	InitializeCriticalSection( &m_critThrowItem );
	//
	
	InitThrowItemArray();
	
	CreateTimerThread();
	CreateEventTimerThread();
	
	// New Send Thread - alisia
//	InitSendThread();
//	CreateSendThread();

	m_bInit = TRUE;
	m_TotalUserCount = 0;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////
//	Send 甫 淬寸且 Thread甫 父电促. (泅犁绰 CPU 肮荐 * 2)
//
/*
void COM::CreateSendThread()
{
	// ensure we aren't wiping out a valid completion port
	ASSERT( m_hSendIOCP == INVALID_HANDLE_VALUE );

	m_hSendIOCP = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
	if(SErr(m_hSendIOCP == NULL, _T("CreateWorkerThread"))) return;

	HANDLE			hThread = NULL;
	unsigned int	dwWorkerId;

	for(DWORD i = 0; i < AUTOMATA_THREAD * 2; i++)
	{
		//if(SErr((hThread = ::CreateThread( NULL, 0, SendThreadMain, (LPVOID)this, 0, &dwWorkerId)) == NULL, _T("CreateSendThread"))) return;
		if(SErr((hThread = (HANDLE)_beginthreadex( NULL, 0, SendThreadMain, (LPVOID)this, 0, &dwWorkerId)) == NULL, _T("CreateSendThread"))) return;
		//if(SErr((hThread = (HANDLE)_beginthreadex( NULL, 0, &SendThreadMain, (LPVOID)this, 0, &dwWorkerId)) == NULL, _T("CreateSendThread"))) return;
		
		//m_pSendThread = AfxBeginThread(SendThreadMain, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
		//ASSERT_POINTER( m_pSendThread, CWinThread );
	}
}
*/

///////////////////////////////////////////////////////////////////////////////////
//	global Time甫 蜡瘤且 Timer Thread 甫 父电促.
//
BOOL COM::CreateTimerThread()
{
	//DWORD id;
	//unsigned int id;
	//DWORD dwThreadAffinityMask = 0;
	//HANDLE hTimerThread;

	_int64 frequence = 0;

	if(!QueryPerformanceFrequency((LARGE_INTEGER*)&frequence)) return FALSE;

//	g_Online_Update_ticks = (UPDATE_TICKS * frequence) / 1000;		// 500000
	g_Online_Update_Min_ticks = (UPDATE_TICKS * frequence) / 5000;	// 100000

//	QueryPerformanceCounter((LARGE_INTEGER*)&g_OnlineStart);
	QueryPerformanceCounter((LARGE_INTEGER*)&g_OnlineMinStart);

	//hTimerThread = ::CreateThread( NULL, 0, TimerThread, (LPVOID)this, 0, &id);
	//hTimerThread = (HANDLE)_beginthreadex( NULL, 0, &TimerThread, (LPVOID)this, 0, &id);
	m_hTimerThread = AfxBeginThread( TimerThread, (LPVOID)this );

	//if(SErr(NULL == hTimerThread, _T("CreateTimerThread"))) return FALSE;
	//DWORD result = SetThreadAffinityMask (hTimerThread, dwThreadAffinityMask);
	//if(result == 0) return FALSE;

	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////////
//	Create Event Timer Thread 
//
BOOL COM::CreateEventTimerThread()
{
//	AfxBeginThread(EventTimerThread, (LPVOID)this );

	return TRUE;
}

////////////////////////////////////////////////////////////////////////////////////
//	秦寸 粮甫 茫酒 朝揪, 矫埃甫 舅妨霖促.
//
void COM::SetWeatherZone(int iZone, BYTE tType, BYTE tOnOff)
{
	// alisia
	BOOL bFound = FALSE;
	int iZoneIndex = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -