Exemplo n.º 1
0
void TempServerSession::OnRecv(BYTE *pMsg, WORD wSize)
{
	printf(">> TempServerSession::OnRecv\n");
	
	assert( m_bFirst == TRUE);
	if ( !m_bFirst)
		return;
	
	m_bFirst = FALSE;
	
	if ( wSize < sizeof(MSG_CONNECTION_SYN) ) {
		return;
	}
	
	MSG_CONNECTION_SYN * recvMsg = (MSG_CONNECTION_SYN *)pMsg;
		
	switch( recvMsg->ServerType )
	{
	case KEYV_SERVER:
		{
			printf("Recv KeyVServer.\n");
			//ServerSession * Obj = g_pLoginServer->GetKeyvSession();
			//assert( Obj );
			//
			//Session * pSession = this->m_pSession;
			//if ( pSession != NULL ) 
			//{
			//	m_pSession->UnbindNetworkObject();
			//	pSession->BindNetworkObject(Obj);
			//	
			//	Obj->Init();
			//}
			break;
		}
	case JSON_SERVER:
		{
			printf("Recv JsonServer.\n");
			ServerSession * Obj = g_pLoginServer->GetJsonSession();
			assert( Obj );
			
			Session * pSession = this->m_pSession;
			if ( pSession != NULL ) 
			{
				m_pSession->UnbindNetworkObject();
				pSession->BindNetworkObject(Obj);
				
				//Obj->Init();
			}
			break;
		}
	default:
		{
			// Connected warning.
			this->Release();
			return;
		}
	}
	
}
Exemplo n.º 2
0
void SyncHandler::ProcessAcceptedSessionList()
{
	SESSION_LIST_ITER 	it;
	Session 		*pSession;
	
	m_pAcceptedSessionList->Lock();
	m_pTemplateList->splice( *m_pAcceptedSessionList );
	m_pAcceptedSessionList->Unlock();

	SESSION_LIST		activeList;
	while( !m_pTemplateList->empty() ) 
	{
		pSession = m_pTemplateList->pop_front();
		if (m_numActiveSessions >= m_dwMaxAcceptSession) {
			printf("Connection full! no available accept socket! \n");
			FreeSession(pSession);
			continue;
		}
		
		if (!AddEpollEvent(pSession)) {
			FreeSession(pSession);
			continue;
		}
		
		NetworkObject * pNet = m_fnCreateAcceptedObject();
		assert(pNet);
		
		pSession->BindNetworkObject(pNet);
		pSession->OnAccept();
		
		++m_numActiveSessions;
		activeList.push_back(pSession);
	}
	
	if ( !activeList.empty() ) {
		m_pActiveSessionList->Lock();
		m_pActiveSessionList->splice( activeList );
		m_pActiveSessionList->Unlock();
	}
}
Exemplo n.º 3
0
DWORD SyncHandler::Connect(NetworkObject * pNet, char *szIP, WORD wPort)
{
	if (pNet->m_pSession != NULL) 
		return 0;
		
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	
	SOCKADDR_IN addr;
	addr.sin_family			= AF_INET;
	addr.sin_addr.s_addr 	= inet_addr( szIP );
	addr.sin_port 			= htons( wPort );
	
	Session * pSession = AllocConnectSession();
	assert( pSession != NULL && "Connect dwMaxConnectSession");
	
	pSession->SetSocket( sock );
	pSession->SetSockAddr( addr );
	
	pSession->BindNetworkObject(pNet);
	m_pConnector->Connect(pSession);
	
	return pSession->GetIndex();
}
Exemplo n.º 4
0
void TempUserSession::OnRecv(BYTE *pMsg, WORD wSize)
{
	printf(">>>>[TempUserSession::OnRecv]<<<<\n");
	printf("Step2: <1> CA_Login_REQ\n");
	
	assert( m_bFirst == TRUE );
	if ( !m_bFirst ) {
		printf("Debug m_bFirst != TRUE. So return.\n");
		return;
	}
	
	MSG_CA_LOGIN_REQ * pRecvMsg = (MSG_CA_LOGIN_REQ *) pMsg;
	printf("RootID = %d, UserKey = %s\n", pRecvMsg->m_uiRootID, pRecvMsg->m_byUserKey);
	
	RootID * pRoot = RootidManager::Instance()->FindRootID( pRecvMsg->m_uiRootID );
	if ( pRoot == NULL ) {
		printf("\n[RootidManager::Instance()->FindRootID] Fail.\n");
				
		// 发送出错信息 ( 还未测试 )
		MSG_CA_LOGIN_NAK msgError;
		msgError.m_dwParameter = pRecvMsg->m_dwParameter; // User ID
		msgError.m_dwErrorCode = 1234; // ???

		Session * pSession = this->m_pSession;
		if ( pSession != NULL ) {
			//m_pSession->UnbindNetworkObject();
			pSession->BindNetworkObject(this);
		}
		pSession->Send( (BYTE *)&msgError, sizeof(MSG_CA_LOGIN_NAK) );
		
		printf("\n>>>> Free TempUserSesion <<<<\n");
		AgentFactory::Instance()->FreeTempUserSession(this);	
		return;
	}
	
	// 验证 UserKey ( MD5 )
	BOOL bRet = pRoot->IsSameUserKey(pRecvMsg->m_byUserKey);
	if ( !bRet ) {
		printf("\n[RootidManager::Instance()->IsSameRootID] Fail.\n");
		// 发送出错信息
		//MSG_CA_LOGIN_NAK msgError;
		return;
	}
	

	// TempUser 转变为 User
	User * obj = AgentFactory::Instance()->AllocUser(); // 
	if ( obj == NULL) {
		printf("\nAgentFactory::Instance()->AllocUser() Fail.\n");
		return;
	}
	// 添加进 UserManager 进行管理
	DWORD dwID = g_UserManager.AddUser(obj); // 返回的是 ObjKey
	printf("g_UserManager AddUser, Return ObjKey = %d\n", dwID);
	obj->SetUserID(dwID);
	obj->SetRootID(pRecvMsg->m_uiRootID); // 保存 RootID
	
	Session * pSession = this->m_pSession;
	if ( pSession != NULL ) {
		m_pSession->UnbindNetworkObject();
		pSession->BindNetworkObject( obj );
	}
	printf("[TempUserSession] ==> [User] Success.\n");
	
	printf("Send MSG_AG_LOGIN_REQ to [GameSrv].\n");
	
	// 发送 AG_Login_REQ 消息包给 GameServer
	MSG_AG_LOGIN_REQ msg2;
	msg2.m_dwParameter = dwID; // User ID
	msg2.m_uiRootID = pRecvMsg->m_uiRootID; // RootID
	g_AgentServer->SendToGameServer( (BYTE *)&msg2, sizeof(msg2) );
	
	//pSession->Send( (BYTE *)&ancMsg, sizeof( MSG_AG_LOGIN_REQ) );
	
	printf("\n>>>> Free TempUserSesion <<<<\n");
	AgentFactory::Instance()->FreeTempUserSession(this);
}