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

📄 servertable.cpp

📁 Agent in c++ for DragonRaja
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		MyLog( LOG_NORMAL, "** %s Server(%d) Under Destroy Process(by %s)", GetTypedServerText(this->GetOwnServerData()->dwServerType), this->GetOwnServerData()->wPort, GetFinishTypeText(dwFinishType) );
	}
	else
	{
		MyLog( LOG_NORMAL, "** Server Under Destroy Process(Before Read OwnServerData)" );
	}

	if( !this->SendToProxyServer( szDummy, sizeof(BYTE)+sizeof(DWORD) ) )
	{
		MyLog( LOG_NORMAL, "-- Failed To Notify Server Destroy To Proxy" );
	}

#ifdef __IS_MAP_SERVER
	void SaveAllUserDatas(void);

	SaveAllUserDatas();
#endif

	// Must be placed on end of this member function.
	EnterCriticalSection( &this->m_IsRunningCriticalSection );
	m_bIsServerRunning = false;

	// Added by chan78 at 2001/07/16 :: Rise console event.
	// HANDLE hIn : main() 俊 急攫登绢 乐嚼聪促.
	// " "        : 埃窜窍霸 胶其捞胶甫 涝仿茄吧肺 钦聪促.
	// 1          : 茄官捞飘甫 竟聪促.
	// &dwResult  : Write 茄 搬苞蔼阑 掘绢可聪促.
	DWORD dwResult;
	DWORD dwTries = 0;
	do
	{
		if( dwTries )
		{
			// Input buffer 俊 Write甫 角菩茄 版快 泪矫 措扁沁促 促矫 矫档钦聪促.
			// 窜 霉锅掳 矫档牢 版快浚 措扁窍瘤 臼嚼聪促.
			Sleep( 1000 );
		}
		dwTries++;
		WriteConsoleInput( hIn, (INPUT_RECORD *)" ", 1, &dwResult );

		// 公茄洒 馆汗且荐绰 绝栏骨肺 促几锅 矫档窍绊 器扁钦聪促.
	} while( (dwResult != 1) && (dwTries <= 5) );

	LeaveCriticalSection( &this->m_IsRunningCriticalSection );
	return;
}

void CServerTable::CheckServerConnections()
{
	char szDummyMsg[MM_MAX_PACKET_SIZE];
	LP_SERVER_DATA pDummyServer;
	DWORD NowTime = GetTickCount();

	// 辑滚客 楷搬等 葛电 辑滚狼 立加惑怕甫 八荤茄促.
	for( pDummyServer = this->GetServerListHead(); pDummyServer; pDummyServer = pDummyServer->pNextServerData )
	{
		// 捞 菩哦阑 罐篮 辑滚甸篮 PTCL_SERVER_CONNECTION_OK 肺 览翠窍绊,
		// 捞 辑滚绰 弊 菩哦阑 罐篮 矫痢狼 矫埃阑 弊 辑滚狼 dwLastCheckAliveTime栏肺 盎脚茄促.
		if( pDummyServer->dwConnectionIndex )
		{
			// 老窜 立加 犬牢 菩哦阑 朝赴促.
			szDummyMsg[0] = (BYTE)PTCL_SERVER_TRY_TO_CHECK_CONNECTION;
			if( !this->Send( pDummyServer->dwConnectionIndex, szDummyMsg, sizeof(BYTE) ) )
			{
				MyLog( LOG_IMPORTANT, "Failed To Send Check Connection Packet to %s(%d)", GetTypedServerText(pDummyServer->dwServerType), pDummyServer->wPort );
			}

			if( NowTime < pDummyServer->dwLastCheckAliveTime )
			{
				// GetTickCount()蔼捞 府悸登菌芭唱 辑滚啊 矫累等 饶 贸澜 眉农茄 版快.
				continue;
			}
			else
			{
				if( (NowTime - pDummyServer->dwLastCheckAliveTime) > SERVER_CONNECTION_TIME_OUT )
				{
					// Notify It To Proxy
					LP_SERVER_ERROR_PACKET pErrorPacket = (LP_SERVER_ERROR_PACKET)(szDummyMsg+1);
					szDummyMsg[0] = PTCL_REPORT_SERVER_ERROR;

					pErrorPacket->dwErrorCode = ERROR_SERVER_CONNECTION_TIMED_OUT;
					sprintf( pErrorPacket->szError, "%s(%d) Connection Timed Out. Now Try To Purge This Connection", GetTypedServerText(pDummyServer->dwServerType), pDummyServer->wPort );

					if( !SendToProxyServer( szDummyMsg, sizeof(BYTE)+sizeof(DWORD)+strlen(pErrorPacket->szError)+1 ) )
					{
						MyLog( LOG_IMPORTANT, "Failed To Notify Server Connection(%d) Timed Out to PROXY SERVER", pDummyServer->wPort );
					}

					this->m_pINet->CompulsiveDisconnectServer( pDummyServer->dwConnectionIndex );
				}
			}
		}
	}
}

void CServerTable::CloseServerConnection( LP_SERVER_DATA pServerData )
{
	if( !pServerData )
	{
		MyLog( LOG_IMPORTANT, "CloseServerConnection() :: pServerData is NULL!!!" );
		return;
	}
	if( !pServerData->dwConnectionIndex )
	{
		MyLog( LOG_IMPORTANT, "CloseServerConnection() :: pServerData(%d)->dwConnection is 0!!!", pServerData->wPort );
		return;
	}

	this->m_pINet->CompulsiveDisconnectServer( pServerData->dwConnectionIndex );
	return;
}

void CServerTable::CloseServerConnection( WORD wPort )
{
	LP_SERVER_DATA pServerData;

	pServerData = this->GetServerData( wPort );

	this->CloseServerConnection( pServerData );
	return;
}

void CServerTable::ReportOrderedConnectionResult()
{
	LP_AWAITING_CONNECTION_RESULT_DATA pResult = this->GetConnectionResultData();
	LP_SERVER_PORT_LIST_PACKET pConnectedList = (LP_SERVER_PORT_LIST_PACKET)(pResult->szAnswer+1);

	if( !pResult->pSender->dwConnectionIndex )
	{
		MyLog( LOG_IMPORTANT, "Failed To Report 'PTCL_SERVER_CONNECTING_RESULT' :: PROXY(%d) lost connection", pResult->pSender->wPort );
#ifdef __ON_DEBUG
		_asm int 3;
#else
		this->DestroyServer( FINISH_TYPE_UNKNOWN_ERROR );
#endif
		return;
	}

	// Answer.
	pResult->szAnswer[0] = PTCL_SERVER_CONNECTING_RESULT;
	if( !this->Send( pResult->pSender->dwConnectionIndex, pResult->szAnswer, (sizeof(BYTE)+sizeof(WORD)+(sizeof(WORD)*pConnectedList->wNum)) ) )
	{
		MyLog( LOG_IMPORTANT, "Failed To Report 'PTCL_SERVER_CONNECTING_RESULT' :: PROXY(%d) / send() has failed", pResult->pSender->wPort );
#ifdef __ON_DEBUG
		_asm int 3;
#else
		this->DestroyServer( FINISH_TYPE_UNKNOWN_ERROR );
#endif
		return;
	}


	// DB Demon request
#if defined(__IS_AGENT_SERVER) || defined(__IS_MAP_SERVER)
	if( !this->RequestToSetDBDemon() )
	{
#ifdef __ON_DEBUG
		_asm int 3;
#else
		this->DestroyServer( FINISH_TYPE_UNKNOWN_ERROR );
#endif
		return;
	}

	this->SetServerStatus( STATUS_AWAITING_DB_DEMON_SETTING );
#endif

#if defined(__IS_PROXY_SERVER) || defined(__IS_DB_DEMON)
	this->SetServerStatus( STATUS_ACTIVATED );
#endif

	return;
}

bool CServerTable::OnRecvServerUpMsg( DWORD dwConnectionIndex, WORD wPort )
{
	sockaddr_in* pSockAddr;
	LP_SERVER_DATA pServerData;
	char szIP[32+1];

	pSockAddr = this->m_pINet->GetServerAddress( dwConnectionIndex );

	sprintf( szIP, "%d.%d.%d.%d", pSockAddr->sin_addr.S_un.S_un_b.s_b1
	, pSockAddr->sin_addr.S_un.S_un_b.s_b2
		, pSockAddr->sin_addr.S_un.S_un_b.s_b3
		, pSockAddr->sin_addr.S_un.S_un_b.s_b4 );

	pServerData = this->GetServerData( wPort );

	// Modified by chan78 at 2001/02/21, 府胶飘俊 绝绰 赤籍.
	if( !pServerData )
	{
		this->m_pINet->CompulsiveDisconnectServer( dwConnectionIndex );
		return false;
	}

	// IP啊 促弗 版快, 牢沥窍瘤 臼绰促.
	if( strcmp( pServerData->szIP, szIP ) )
	{
		MyLog( LOG_NORMAL, "OnRecvServerUp() :: SERVER(%d) has wrong IP(%s/%s)", wPort, szIP, pServerData->szIP );
		this->m_pINet->CompulsiveDisconnectServer( dwConnectionIndex );
		return false;
	}

	// 摹疙利牢 坷幅.
	// 捞固 立加登绢乐绰 辑滚啊 SERVER_UP 阑 茄锅 歹 焊陈促. 趣篮 歹 唱慧 惑炔. 部看促.
	if( pServerData->dwConnectionIndex )
	{
		// 捞繁扒 唱坷搁 救蹬~~~~~~ NEVER!!!
#ifdef __ON_DEBUG
//		_asm int 3;
#endif
		MyLog( LOG_FATAL, "OnRecvServerUp() :: SERVER(%d) is Already Connected!!!", wPort );
		this->m_pINet->CompulsiveDisconnectServer( dwConnectionIndex );
		return false;
	}

	// 立加 浚飘府俊 眠啊.
	if( !this->AddConnectedServerDataToHashTable( pServerData, dwConnectionIndex ) )
	{
		MyLog( LOG_FATAL, "OnRecvServerUp() :: SERVER(%d) - AddConnectedServerDataToHashTable() has Failed", wPort );
		this->m_pINet->CompulsiveDisconnectServer( dwConnectionIndex );
		return false;
	}

	// 沥惑利牢 Server UP
	MyLog( LOG_FATAL, "New Server Connection... %s(%d/%d)", GetTypedServerText(pServerData->dwServerType), dwConnectionIndex, wPort );

	// 泅犁 郴 Server Status 舅覆.
	this->NotifyServerStatus( pServerData );

#ifndef __IS_PROXY_SERVER
	// Notify Connection Status Change to Proxy
	this->ReportServerConnectionStatusChange( pServerData, (BYTE)CONNECTION_STATUS_ACCEPTED );
#endif

	return true;
}

bool CServerTable::IsServerRunning()
{
	bool result;
	EnterCriticalSection( &this->m_IsRunningCriticalSection );
	result = m_bIsServerRunning;
	LeaveCriticalSection( &this->m_IsRunningCriticalSection );
	return result;
}

bool CServerTable::OnRecvNegotiationMsgs( LP_SERVER_DATA pSender, BYTE bID, char *pMsg, DWORD dwLength )
{
	WORD i;
	char szDummyMsg[MM_MAX_PACKET_SIZE];

#ifndef __IS_PROXY_SERVER
	// --------------------
	// Sender Must be Proxy
	// --------------------
	if( pSender->dwServerType != SERVER_TYPE_PROXY )
	{
		switch( bID )
		{
		case PTCL_ORDER_DESTROY_SERVER:
		case PTCL_NOTIFY_SERVER_STATUS:
		case PTCL_ORDER_TO_REPORT_SERVER_STATUS:
		case PTCL_SERVER_TRY_TO_CHECK_CONNECTION:
		case PTCL_SERVER_CONNECTION_OK:
			{
				// 抗寇.
			}
			break;
		default:
			{
				// 菩哦 公矫
				return false;
			}
		}
	}
#endif

	// ---------------------
	// Process Packets
	// ---------------------
	switch( bID )
	{
	// --------------
	// 葛电 辑滚 傍烹
	// --------------
	case PTCL_ORDER_DESTROY_SERVER:
		{
			MyLog( LOG_NORMAL, "Server Destroying by PROXY ORDER..." );
			this->SetServerStatus( STATUS_CLOSING );
			this->DestroyServer( FINISH_TYPE_BY_PROXY );
		}
		break;
	case PTCL_NOTIFY_SERVER_STATUS:
		{
			LP_NOTIFY_SERVER_STATUS_PACKET pPacket = (LP_NOTIFY_SERVER_STATUS_PACKET)pMsg;

			//Modified by KBS 020121
			if(pPacket->dwServerStatus == STATUS_RELOAD_GAMESERVER_DATA)
			{
				SetServerStatus( pSender, pPacket->dwServerStatus );
			}
			else if(pPacket->dwServerStatus == STATUS_FINISH_RELOAD_GAMESERVER_DATA)
			{
				SetServerStatus( pSender, STATUS_ACTIVATED );
			}
			else  
				SetServerStatus( pSender, pPacket->dwServerStatus );
			//
		}
		break;
	case PTCL_ORDER_TO_REPORT_SERVER_STATUS:
		{
			if( !this->ReportServerStatus( pSender ) )
			{
				MyLog( LOG_NORMAL, "Failed To Report SERVER STATUS to %s(%d)", GetTypedServerText(pSender->dwServerType), pSender->wPort );
			}
		}
		break;
	case PTCL_SERVER_TRY_TO_CHECK_CONNECTION:
		{
			// ANSWER
			szDummyMsg[0] = (BYTE)PTCL_SERVER_CONNECTION_OK;

			if( !this->Send( pSender->dwConnectionIndex, szDummyMsg, 1 ) )
			{
				MyLog( LOG_IMPORTANT, "Failed To send 'PTCL_SERVER_CONNECTION_OK' to %s(%d)", GetTypedServerText(pSender->dwServerType), pSender->wPort );
			}
		}
		break;
	case PTCL_SERVER_CONNECTION_OK:
		{
			// 矫埃盎脚
			pSender->dwLastCheckAliveTime = GetTickCount();
		}
		break;
	// ---------------------
	// 厚 PROXY SERVER 傈侩 
	// ---------------------
#ifndef __IS_PROXY_SERVER
	// ---------------------
	// SERVER LIST 技泼 菩哦
	// ---------------------
	case PTCL_ORDER_SET_SERVER_LIST:
		{
			if( this->GetServerStatus() > STATUS_AWAITING_SERVER_LIST )
			{
				// 罐栏搁 救等促.
				MyLog( LOG_FATAL, "PTCL_ORDER_SET_SERVER_LIST :: PROXY Sent this Packet AGAIN!" );
				this->DestroyServer( FINISH_TYPE_UNKNOWN_ERROR );
#ifdef __ON_DEBUG
//				_asm int 3;
#endif
			}

			// Server List Accept绰 窜 茄锅父 倾侩茄促.
			LP_SERVER_CINFO_LIST_PACKET pServerList = (LP_SERVER_CINFO_LIST_PACKET)pMsg;
			LP_SERVER_CINFO_LIST_PACKET pFailedServerList = (LP_SERVER_CINFO_LIST_PACKET)(szDummyMsg+1);
			LP_SERVER_DATA pDummyServerData = NULL;

			this->m_dwServerSetNumber = pServerList->dwServerSetNumber;
			pFailedServerList->wNum = 0;

			for( i = 0; i < pServerList->wNum; i++ )
			{
				// alloc new server data and add it to list.
				if ( !(pDummyServerData = this->GetNewServerData( pServerList->pServerData[i].szIP, pServerList->pServerData[i].wPort) ) || !this->AddServerDataToList( pDummyServerData ) )
				{
					// failed
					if( pDummyServerData )
					{
						delete pDummyServerData;
						pDummyServerData = NULL;
					}

					// Build Answer Packet
					memcpy( pFailedServerList->pServerData[pFailedServerList->wNum].szIP, pServerList->pServerData[i].szIP, MM_IP_LENGTH );
					pFailedServerList->pServerData[pFailedServerList->wNum].wPort = pServerList->pServerData[i].wPort;

					pFailedServerList->wNum++;
				}
			}

			if( !pFailedServerList->wNum )
			{
// ---------------------------------------------------------------------------------------------
#ifndef __IS_PROXY_SERVER
				// SERVER SIDE Socket阑 官牢靛 茄促.(瘤陛何磐 促弗 辑滚狼 目池记阑 罐阑 荐 乐促.
				if( !this->StartServer( TYPE_SERVER_SIDE ) )
				{
					MyLog( LOG_FATAL, "SERVER_SIDE Socket Bind Failed!!!" );
					this->DestroyServer( FINISH_TYPE_BIND_FAILED );
				}
				MyLog( LOG_NORMAL, "SERVER_SIDE Socket Binded." );
#ifdef __IS_AGENT_SERVER
				// USER SIDE Socket 官牢靛.
				if( !this->StartServer( TYPE_USER_SIDE ) )
				{
					MyLog( LOG_NORMAL, "USER_SIDE Socket Bind Failed!!!" );
					this->DestroyServer( FINISH_TYPE_BIND_FAILED );
				}
				MyLog( LOG_NORMAL, "USER_SIDE Socket Binded" );
#endif
#endif
// ---------------------------------------------------------------------------------------------
				// 己傍. 立加且 辑滚狼 府胶飘甫 夸备茄促.
				szDummyMsg[0] = (BYTE)PTCL_REQUEST_TO_CONNECT_SERVER_LIST;
				if( !this->Send( pSender->dwConnectionIndex, szDummyMsg, 1 ) )
				{
#ifdef __ON_DEBUG
//					_asm int 3;
#endif
				}

				this->SetServerStatus( STATUS_AWAITING_CONNECTION_ORDER );
			}
			else
			{
				// 角菩.
#ifdef __ON_DEBUG
//				_asm int 3;
#endif
				// 捞 辑滚绰 辆丰等促.
				MyLog( LOG_FATAL, "CServerTable::OnRecvMsgNegotiationMsgs() - Failed To accept Server List" );
				this->DestroyServer( FINISH_TYPE_SERVER_LIST_ACCEPT_FAIL );
			}
		}

⌨️ 快捷键说明

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