

#include "StdAfx.h"

#include "cCliGate.h"
#include "cNetwork.h"

#include "common_vs2022/pMaster.h"
#include "common_vs2022/pGate.h"
#include "common_vs2022/pNetwork.h"
#include "common_vs2022/pServer.h"
#include "common_vs2022/pSession.h"
#include "common_vs2022/cVersion.h"

#include "common_vs2022/pPass2.h"


#include "../Flow/Flow.h"
#include "../Flow/FlowMgr.h"


cCliGate::cCliGate(void) : cClient(nSession::User)
{
	m_nIsChangeServer = false;


}


cCliGate::~cCliGate(void)
{
	cClient::Stop();

	DBG("destroyed...");
}


bool cCliGate::start(void)
{
	if(IsNetOff())
	{
		DBG("NETWORK IS DISABLED...");
		g_CriticalLog.Log(_T("[Gate] start skipped because network is disabled."));
//		MessageBoxA(null, "\n서버와 연결하지 않고 시작합니다.   ", "digimon masters", MB_OK);
		return true;
	}

	int nConnectTryCount = 0;
	g_CriticalLog.Log(_T("[Gate] start begin. port=%d"), port);

nextwork:
	if(Bind() == false)
	{
		DBG("gate bind failure");
		g_CriticalLog.Log(_T("[Gate] bind failed."));
		PostQuitMessage(nErr::Bind);
		return false;
	}

	EnableEncryption( cVersion::GetVersion() );

	if(!Connect(ip, port))
	{
		if(++nConnectTryCount < 5)
		{
			DBG("connect(%s:%d) try:%d failure", ip, port, nConnectTryCount);
			g_CriticalLog.Log(_T("[Gate] connect retry failed. port=%d try=%d"), port, nConnectTryCount);
			Sleep( 999 );
			goto nextwork;
		}

		PostQuitMessage(nErr::Connect);
		DBG("connect(%s:%d) failure...", ip, port);
		g_CriticalLog.Log(_T("[Gate] connect failed after retries. port=%d"), port);
		return false;
	}

//	if(Run(2) == false) // 2 work thread
	if(Run(0) == false)
	{
		DBG("run failure");
		g_CriticalLog.Log(_T("[Gate] run failed after connect."));
		PostQuitMessage(nErr::Run);
		return false;
	}

	g_CriticalLog.Log(_T("[Gate] start success. connected tick=%u"), GetTickCount());
	return true;
}



void cCliGate::OnConnected(void)
{
#ifndef _GIVE
	LOG("게이트 서버 접속");
#endif //_GIVE

	SendAccessCode();
	g_CriticalLog.Log(_T("[Gate] connected. Sending access code."));
	//SetWindowTitle(true);

//#ifndef _GIVE
	//Sleep(333);
//#endif //_GIVE
}


void cCliGate::OnDisconnected(char *msg)
{
	const int nReasonCode = (msg != NULL) ? atoi(msg) : -1;
	const bool bExpectedChangeServerDisconnect = (m_nIsChangeServer && nReasonCode == cPrintMsg::SERVER_DISCONNET);
#ifdef _DEBUG
	if( !bExpectedChangeServerDisconnect )
		CsMessageBoxA( MB_OK, "서버 끊김 : %s", msg );
#endif

#ifndef _GIVE
	LOG("게임 서버 접속 종료");
#endif //_GIVE
	g_CriticalLog.Log(_T("[Gate] disconnected callback received."));
	const int nReasonLen = (msg != NULL) ? (int)strlen(msg) : 0;
	g_CriticalLog.Log(_T("[Gate] disconnected. changeServer=%d reasonCode=%d reasonLen=%d"), m_nIsChangeServer ? 1 : 0, nReasonCode, nReasonLen);
	if( bExpectedChangeServerDisconnect )
	{
		g_CriticalLog.Log(_T("[Gate] expected change-server disconnect (10003); debug popup suppressed."));
	}

	if(stricmp(msg, "999") == 0)
	{
		cPrintMsg::PrintMsg( 10013 );
		return;
	}

	if(m_nIsChangeServer)
	{
		return;
	}

	SetWindowTitle(false);
	cPrintMsg::PrintMsg( cPrintMsg::SERVER_DISCONNET );
}


void cCliGate::OnExecute(void)
{
	WORD nProtocol = GetProtocol();
	switch(nProtocol)
	{
#ifdef HANDSHAKE_DISABLE
	case 65535: RecvHandshake487(); return;
#endif
	case pSvr::Change			: RecvChangeServer();			return;
	case pMaster::BanUser		: RecvBanUser();				return;
	case pMaster::DiscUser		: RecvDiscUser();				return;
	case pTamer::Remove			: RecvRemoveTamer();			return;

	case pTamer::List			: RecvTamerList();				return;
	case pTamer::CreateSuccess	: RecvTamerCreateSuccess();		return;
	case pTamer::CreateFailure	: RecvTamerCreateFailure();		return;	

	case pTamer::SelectSuccess	: RecvTamerSelectSuccess();		return;
	case pTamer::SelectFailure	: RecvTamerSelectFailure();		return;

	case pTamer::CheckDoubleName: RecvCheckDoubleName();		return;
	case pTamer::CreateWaitTime	: RecvTamerCreateWaitTime();	return;
#ifdef SERVER_WAITING_LINE
	case pGate::WaitTime		: RecvWaitTime();		return;
#endif
	}


	g_CriticalLog.Log(_T("[Gate] unknown protocol=%u"), nProtocol);
	DBG("unknown protocol(%d)", nProtocol);
	xassert1(false, "unknown protocol(%d)", nProtocol);
}








