Ejemplo n.º 1
0
//--------------------------------------------------------------------------------------//
//		Log into Character Server
//--------------------------------------------------------------------------------------//
void CClientSession::SendCharServerReq(CNtlPacket * pPacket)
{
	NTL_PRINT(PRINT_APP, "-- - LOGIN CHAR SERVER REQUEST-- - ");
	sUC_LOGIN_REQ * req = (sUC_LOGIN_REQ *)pPacket->GetPacketData();
			
	printf("--- LOGIN CHAR SERVER REQUEST --- \n");
	printf("ACC ID: %i Server: %i auth key: %s \n", req->accountId, req->serverID, req->abyAuthKey);

	this->accountID = req->accountId;

	CNtlPacket packet(sizeof(sCU_LOGIN_RES));
	sCU_LOGIN_RES * res = (sCU_LOGIN_RES *)packet.GetPacketData();

	res->wOpCode = CU_LOGIN_RES;
	res->wResultCode = CHARACTER_SUCCESS;
	//	res->lastServerFarmId = 0;
	//	res->RaceAllowedFlag = 1;

	packet.SetPacketLen(sizeof(sCU_LOGIN_RES));
	int rc = g_pApp->Send(this->GetHandle(), &packet);
		if (NTL_SUCCESS != rc)
		{
			NTL_PRINT(PRINT_APP, "Failed to send packet %d(%s)", rc, NtlGetErrorMessage(rc));
		}
		else{
			CNtlString ipadress;
			WORD pooort;
			this->GetAddressInfo(&ipadress, &pooort, true);
			NTL_PRINT(PRINT_APP, "User %s logedin", ipadress.c_str());
		}
}
Ejemplo n.º 2
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteAccept(DWORD dwTransferedBytes)
{
	FUNCTION_BEGIN();

	UNREFERENCED_PARAMETER( dwTransferedBytes );

	SOCKADDR_IN * pLocalAddr = NULL;
	SOCKADDR_IN * pRemoteAddr = NULL;
	int nLocalAddrLen = 0;
	int nRemoteAddrLen = 0;

	m_socket.GetAcceptExSockaddrs(	m_recvContext.wsabuf.buf,
									0,
									sizeof(SOCKADDR_IN) + 16,
									sizeof(SOCKADDR_IN) + 16,
									(SOCKADDR**) &pLocalAddr,
									&nLocalAddrLen,
									(SOCKADDR**) &pRemoteAddr,
									&nRemoteAddrLen);

	SetAddress( pLocalAddr, pRemoteAddr );
	ZeroMemory( m_recvContext.wsabuf.buf, sizeof(SOCKADDR_IN) + 16 + sizeof(SOCKADDR_IN) + 16 );
	m_bConnected = true;

	int rc = m_pNetworkRef->Associate( this, true );
	if( NTL_SUCCESS != rc )
	{
		NTL_LOGDL( LOG_NETWORK, "Session[%X] Associate Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
		return rc;
	}


	SetStatus( STATUS_ACTIVE );
	m_dwConnectTime = GetTickCount();


	m_pAcceptorRef->OnAccepted();


	m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_ACCEPT, (LPARAM)this );


	rc = PostRecv();
	if( NTL_SUCCESS != rc )
	{
		return rc;
	}


	//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteAccept Called Local[%s:%u] Remote[%s:%u]", pSession, GetLocalIP(), GetLocalPort(), GetRemoteIP(), GetRemotePort());


	return NTL_SUCCESS;
}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( CompleteRecv, CompleteAccept )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostRecv()
{
	FUNCTION_BEGIN();

	if( false == IsStatus( STATUS_ACTIVE ) )
	{
		Disconnect( false );
		return NTL_SUCCESS;
		//return NTL_ERR_NET_CONNECTION_STATUS_WRONG;
	}


	DWORD dwFlags = 0;
	DWORD dwTransferedBytes	= 0;

	m_recvContext.Reset();
	m_recvContext.iomode = IOMODE_RECV;
	m_recvContext.wsabuf.buf = (char*) m_recvBuffer.GetQueuePushPtr();
	m_recvContext.wsabuf.len = m_recvBuffer.GetPushAvailableSize();


	//  [1/20/2007 zeroera] : ¼³¸í : recv buffer overflow ÀÌ°÷¿¡ °É¸®¸é ÀûÀýÇÑ ¹öÇÁ »çÀÌÁ Á¶Á¤ÇÒ °ÍÀ» °í·ÁÇÏÁö¸¸ Fake PacketÀ» º¸³»´Â Ŭ¶óÀ̾ðÆ®¸¦ ´ëºñÇÏ¿© ÀûÀýÇÑ ´ëó¸¦ ÇÒ °Í
	if( m_recvBuffer.GetPushAvailableSize() <= 0 )
	{
		Disconnect( false );

		NTL_LOGDL( LOG_NETWORK, "Session[%X] m_recvBuffer.GetPushAvailableSize() <= 0, m_recvBuffer.GetPushAvailableSize() = %d", this, m_recvBuffer.GetPushAvailableSize() );
		return NTL_ERR_NET_SESSION_RECV_BUFFER_OVERFLOW;
	}



	IncreasePostIoCount();

	int rc = m_socket.RecvEx(	&m_recvContext.wsabuf,
		1,
		&dwTransferedBytes,
		&dwFlags,
		&m_recvContext);

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();

		NTL_LOGDL( LOG_NETWORK, "Session[%X] RecvEx Function Failed (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}

	//NTL_PRINT( PRINT_SYSTEM, "Session[%X]\tPostRecv Complete Success recvBufferSize[%d]", this, m_recvBuffer.GetCurSize() );


	return NTL_SUCCESS;
}
Ejemplo n.º 4
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Acceptor TH ( Run )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostAccept(CNtlAcceptor* pAcceptor)
{
	FUNCTION_BEGIN();

	if( NULL == pAcceptor )
	{
		return NTL_ERR_SYS_INPUT_PARAMETER_WRONG;
	}


	SetStatus( STATUS_ACCEPT );


	m_pAcceptorRef = pAcceptor;


	DWORD dwBytes = 0;

	m_recvContext.Reset();
	m_recvContext.iomode = IOMODE_ACCEPT;
	m_recvContext.wsabuf.buf = (char*) m_recvBuffer.GetQueuePushPtr();
	m_recvContext.wsabuf.len = 0;

	IncreasePostIoCount();
	m_pAcceptorRef->IncreaseCurAcceptingCount();


	int rc = pAcceptor->GetListenSocket().AcceptEx(	m_socket,
		m_recvContext.wsabuf.buf,
		0,
		sizeof(SOCKADDR_IN) + 16,
		sizeof(SOCKADDR_IN) + 16,
		&dwBytes,
		&m_recvContext );

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();
		m_pAcceptorRef->DecreaseCurAcceptingCount();

		NTL_LOGDL( LOG_NETWORK, "Session[%X] AcceptEx Function Failed: (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}


	return NTL_SUCCESS;
}
Ejemplo n.º 5
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteConnect(DWORD dwTransferedBytes)
{
	FUNCTION_BEGIN();

	UNREFERENCED_PARAMETER( dwTransferedBytes );

	m_socket.GetPeerAddr( m_remoteAddr );
	m_socket.GetLocalAddr( m_localAddr );
	m_bConnected = true;

	int rc = m_pNetworkRef->Associate( this, true );
	if( NTL_SUCCESS != rc )
	{
		NTL_LOGDL( LOG_NETWORK, "Session[%X] CompleteConnect Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
		return rc;
	}


	SetStatus( STATUS_ACTIVE );
	m_dwConnectTime = GetTickCount();


	m_pConnectorRef->OnConnected();


	m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_CONNECT, (LPARAM)this );	


	rc = PostRecv();
	if( NTL_SUCCESS != rc )
	{
		return rc;
	}



	//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteConnect Called Local[%s:%u] Remote[%s:%u]", pSession, GetLocalIP(), GetLocalPort(),	GetRemoteIP(), GetRemotePort() );


	return NTL_SUCCESS;
}
Ejemplo n.º 6
0
//--------------------------------------------------------------------------------------//
//		Get the account ID and log in to Char Server									//
//--------------------------------------------------------------------------------------//
void CClientSession::SendCharLogInReq(CNtlPacket * pPacket, CAuthServer * app) 
{
	NTL_PRINT(PRINT_APP, "Client Received Login Request");

	sUA_LOGIN_REQ * req = (sUA_LOGIN_REQ *)pPacket->GetPacketData();

	CNtlPacket packet(sizeof(sAU_LOGIN_RES));
	sAU_LOGIN_RES * res = (sAU_LOGIN_RES *)packet.GetPacketData();


	res->wOpCode = AU_LOGIN_RES;
	memcpy(res->awchUserId, req->awchUserId, NTL_MAX_SIZE_USERID_UNICODE);
	strcpy_s((char*)res->abyAuthKey, NTL_MAX_SIZE_AUTH_KEY, "Dbo");


	app->db->prepare("CALL AuthLogin (? ,?, @acc_id, @result_code)");
	app->db->setString(1, Ntl_WC2MB(req->awchUserId));
	app->db->setString(2, Ntl_WC2MB(req->awchPasswd));
	app->db->execute();
	app->db->execute("SELECT @acc_id, @result_code");
	app->db->fetch(); 

	res->wResultCode = app->db->getInt("@result_code");
	res->accountId = app->db->getInt("@acc_id");

	res->byServerInfoCount = 1;
	strcpy_s(res->aServerInfo[0].szCharacterServerIP, NTL_MAX_LENGTH_OF_IP, IP_SERVER_ALL);
	res->aServerInfo[0].wCharacterServerPortForClient = 20300;
	res->aServerInfo[0].dwLoad = 0;
	res->lastServerFarmId = 0;

	packet.SetPacketLen(sizeof(sAU_LOGIN_RES));
	int rc = g_pApp->Send(this->GetHandle(), &packet);
		if (NTL_SUCCESS != rc)
		{
			NTL_PRINT(PRINT_APP, "Failed to send packet %d(%s)", rc, NtlGetErrorMessage(rc));
		}
		else{
			NTL_PRINT(PRINT_APP, "User %S send to charserver", req->awchUserId);
		 }
}
Ejemplo n.º 7
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteSend(DWORD dwTransferedBytes)
{
	UNREFERENCED_PARAMETER( dwTransferedBytes );

	FUNCTION_BEGIN();


	IncreaseBytesSend( dwTransferedBytes );


	m_mutexSend.Lock();

	m_sendBuffer.IncreasePopPos( dwTransferedBytes );


	int rc = 0;
	if( IsSetControlFlag( CONTROL_FLAG_USE_SEND_QUEUE ) )
	{
		CNtlPacket * pSendPacket = m_sendQueue.PeekPacket();
		while( pSendPacket )
		{
			if( m_sendBuffer.GetPushAvailableSize() <= pSendPacket->GetUsedSize() )
			{
				break;
			}

			BYTE bySequence = (BYTE) ( m_dwPacketSendCount & PACKET_MAX_SEQUENCE );
			SetSequence( pSendPacket->GetPacketHeader(), bySequence );

			#if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__
			#else
			if( m_pPacketEncoder )
			{
				rc = m_pPacketEncoder->TxEncrypt( *pSendPacket );
				if( NTL_SUCCESS != rc )
				{
					m_mutexSend.Unlock();

					NTL_LOGDL( LOG_NETWORK, "Session[%X] TxEncrypt Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
					return NTL_ERR_NET_PACKET_ENCRYPT_FAIL;
				}
			}
			#endif

			memcpy( m_sendBuffer.GetQueuePushPtr(), pSendPacket->GetPacketBuffer(), pSendPacket->GetUsedSize() );
			m_sendBuffer.IncreasePushPos( pSendPacket->GetUsedSize() );
			IncreasePacketSend();

			m_sendQueue.PopPacket();
			SAFE_DELETE( pSendPacket );

			pSendPacket = m_sendQueue.PeekPacket();
		}
	}


	if( m_sendBuffer.GetCurSize() )
	{
		rc = PostSend();
		if( NTL_SUCCESS != rc )
		{
			m_bSending = false;
			m_mutexSend.Unlock();
			//  [1/20/2007 zeroera] : ¼³¸í : Send¿¡ ÀÇÇÑ Close ±â´ÉÀ» ¸·À½. TH1ÀÌ Recv Áß¿¡ TH2 °¡ Send ÇÏ¿© CloseÇÏ°Ô µÇ¸é ¹®Á¦°¡ ¹ß»ý ÇÒ ¼ÒÁö°¡ ÀÖÀ½
			//return rc;
			Disconnect( false );

			NTL_LOGDL( LOG_NETWORK, "Session[%X] PostSend Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
			return NTL_SUCCESS;
		}
	}
	else
	{
		m_bSending = false;
	}


	m_mutexSend.Unlock();

	//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteSend Complete SendBufferSize[%d] TotalSendBytes[%u]", this, m_sendBuffer.GetCurSize(), m_dwTotalSendSize );

	return NTL_SUCCESS;
}
Ejemplo n.º 8
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by Worker TH ( IOCP )
//-----------------------------------------------------------------------------------
int CNtlConnection::CompleteRecv(DWORD dwTransferedBytes)
{
	FUNCTION_BEGIN();


	// Remote close
	if( 0 == dwTransferedBytes )
	{
		return NTL_ERR_NET_SESSION_CLOSED;
	}


	IncreaseBytesRecv( dwTransferedBytes );


	if( false == m_recvBuffer.IncreasePushPos( dwTransferedBytes ) ) //-> µ¿±âÈ­ÇÊ¿ä
	{
		NTL_LOGDL( LOG_NETWORK, "Session[%X] Recv Buffer OverFlow : BufferCur[%d] BufferMax[%d] TransferedBytes[%u]", this, m_recvBuffer.GetCurSize(), m_recvBuffer.GetQueueSize(), dwTransferedBytes );
		return NTL_ERR_NET_SESSION_RECV_BUFFER_OVERFLOW;
	}

	int rc = NTL_SUCCESS;
	WORD wPacketLength = 0;

#if 0
	while( MakeSureCompletedPacket( &m_recvBuffer, &wPacketLength, m_pPacketEncoder ) )
	{
		if( true != IsValidPacket( m_recvBuffer.GetQueueWorkPtr(), wPacketLength ) )
		{
			NTL_LOG_ASSERT("true != IsValidPacket()");
		}

		CNtlPacket packet;

		packet.Attach( m_recvBuffer.GetQueueWorkPtr(), wPacketLength );

		m_recvBuffer.IncreaseWorkPos( packet.GetUsedSize() );

		if( m_pPacketEncoder )
		{
			rc = m_pPacketEncoder->RxDecrypt( packet );
			if( NTL_SUCCESS != rc )
			{
				return rc;
			}
		}

		//  [9/10/2007 zeroera] : ¼³¸í : sequence°ªÀº 0 ~ 255
		BYTE byValidSequence = (BYTE) ( m_dwPacketRecvCount & PACKET_MAX_SEQUENCE );
		if( false == HasValidSequence( packet.GetPacketHeader(), byValidSequence ) )
		{
			return NTL_ERR_NET_PACKET_SEQUENCE_FAIL;
		}


		IncreasePacketRecv();

		//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteRecv Complete recvBufferSize[%d], TotalRecvBytes[%u]", this, m_recvBuffer.GetCurSize(), m_dwTotalRecvSize );

		m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_RECV, (LPARAM)this );	
	}
	
#else

	CNtlPacket packet;
	do 
	{
		rc = MakeSureCompletedPacket( &m_recvBuffer, &wPacketLength, m_pPacketEncoder );
		if( NTL_SUCCESS != rc )
		{
			if( NTL_ERR_NET_PACKET_PENDING_HEADER == rc || NTL_ERR_NET_PACKET_PENDING_DATA == rc )
			{
				break;
			}
			
			NTL_LOGDL( LOG_NETWORK, "Session[%X] MakeSureCompletedPacket Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
			return rc;
		}

		if( true != IsValidPacket( m_recvBuffer.GetQueueWorkPtr(), wPacketLength ) )
		{
			NTL_LOG_ASSERT("true != IsValidPacket()");
		}

		packet.AttachData( m_recvBuffer.GetQueueWorkPtr(), wPacketLength );

		m_recvBuffer.IncreaseWorkPos( packet.GetUsedSize() );

		#if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__
		#else
			if( m_pPacketEncoder )
			{
				rc = m_pPacketEncoder->RxDecrypt( packet );
				if( NTL_SUCCESS != rc )
				{
					NTL_LOGDL( LOG_NETWORK, "Session[%X] RxDecrypt Error[%d:%s]", this, rc, NtlGetErrorMessage(rc) );
					return rc;
				}
			}
		#endif

		//  [9/10/2007 zeroera] : ¼³¸í : sequence°ªÀº 0 ~ 255
		BYTE byValidSequence = (BYTE) ( m_dwPacketRecvCount & PACKET_MAX_SEQUENCE );
		if( false == HasValidSequence( packet.GetPacketHeader(), byValidSequence ) )
		{
			NTL_LOGDL( LOG_NETWORK, "Session[%X] Sequence Error[%d:%s] PacketSEQ[%u] CurrentSEQ[%u]", this, rc, NtlGetErrorMessage(rc), GetSequence( m_recvBuffer.GetQueueWorkPtr() ), byValidSequence );
			return NTL_ERR_NET_PACKET_SEQUENCE_FAIL;
		}


		IncreasePacketRecv();

		//NTL_PRINT(PRINT_SYSTEM, "Session[%X]\tCompleteRecv Complete recvBufferSize[%d], TotalRecvBytes[%u]", this, m_recvBuffer.GetCurSize(), m_dwTotalRecvSize );

		m_pNetworkRef->PostNetEventMessage( (WPARAM)NETEVENT_RECV, (LPARAM)this );	


	} while( NTL_SUCCESS == rc );

#endif


	rc = PostRecv();
	if( NTL_SUCCESS != rc )
	{
		return rc;
	}

	return NTL_SUCCESS;
}
Ejemplo n.º 9
0
//-----------------------------------------------------------------------------------
//		Purpose	:
//		Return	:
//-----------------------------------------------------------------------------------
// Called by App TH, Worker TH ( CompleteSend, PushPacket )
//-----------------------------------------------------------------------------------
int CNtlConnection::PostSend()
{
	FUNCTION_BEGIN();

	if( false == IsStatus( STATUS_ACTIVE ) )
	{
		Disconnect( false );
		return NTL_SUCCESS;
		//return NTL_ERR_NET_CONNECTION_STATUS_WRONG;
	}


	DWORD dwFlags		= 0;
	DWORD dwSendBytes	= 0;

	m_sendContext.Reset();
	m_sendContext.iomode = IOMODE_SEND;
	m_sendContext.wsabuf.buf = (char*) m_sendBuffer.GetQueuePopPtr();

#if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__
	bool bIsIf = false;
#endif

	if( m_sendBuffer.GetPopPos() + m_sendBuffer.GetCurSize() >= m_sendBuffer.GetQueueSize() )
	{

#if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__
		bIsIf = true;
#endif

		//  [9/13/2007 zeroera] : ¼öÁ¤ : ¼º´É °³¼±À» À§ÇØ ¼öÁ¤ÇÒ ºÎºÐ
		m_sendContext.wsabuf.len = m_sendBuffer.GetQueueSize() - m_sendBuffer.GetPopPos();

		//if( m_sendBuffer.GetQueueSize() <= m_sendBuffer.GetPopPos() )
		//{
		//	NTL_LOG_ASSERT("m_sendBuffer.GetQueueSize() <= m_sendBuffer.GetPopPos(), m_sendBuffer.GetQueueSize() = %d, m_sendBuffer.GetPopPos() = %d", m_sendBuffer.GetQueueSize(), m_sendBuffer.GetPopPos());
		//	Disconnect( false );
		//	return NTL_SUCCESS;
		//}
	}
	else
	{
		m_sendContext.wsabuf.len = m_sendBuffer.GetCurSize();

		//if( m_sendBuffer.GetCurSize() <= 0 )
		//{
		//	NTL_LOG_ASSERT("m_sendBuffer.GetCurSize() <= 0, m_sendBuffer.GetCurSize() = %d", m_sendBuffer.GetCurSize());
		//	Disconnect( false );
		//	return NTL_SUCCESS;
		//}
	}


	IncreasePostIoCount();

	int rc = m_socket.SendEx(	&m_sendContext.wsabuf,
							 1,
							 &dwSendBytes,
							 dwFlags,
							 &m_sendContext);

	if( NTL_SUCCESS != rc )
	{
		DecreasePostIoCount();

		NTL_LOGDL( LOG_NETWORK, "Session[%X] SendEx Function Failed: (%d)%s", this, rc, NtlGetErrorMessage( rc ) );
		return rc;
	}

#if __SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__
	m_cConsolLog.WriteBotSystemLog( reinterpret_cast< void * >( m_sendContext.wsabuf.buf )
		, m_sendContext.wsabuf.len
		, bIsIf 
		, &m_sendBuffer
		, m_nCharacterPCID);
#endif//__SGPRO_BOTSYSTEM_SENDANDRECVTESTCODE__

	//NTL_PRINT( PRINT_SYSTEM, "Session[%X]\tPostSend Complete SendBufferSize[%d]", this, m_sendBuffer.GetCurSize() );

	return NTL_SUCCESS;
}