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

📄 mirkernel.cpp

📁 这是传奇代码私服之二
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MirKernel.cpp: implementation of the CMirKernel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MirX.h"
#include "MirKernel.h"

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


#include "MirSocket.h"
#define	BUFFERLENGTH 4096

CStringList*pLogList;


///////////////////////////////////////////////////////////////////////
//
// 函数名       : LogSystem
// 功能描述     : 将字符串格式化后输出到用户界面
// 参数         : const char*line
// 参数         : ...
// 返回值       : void 
//
///////////////////////////////////////////////////////////////////////
void LogSystem(const char*line,...)
{
	CString str;
	va_list	vl;
	va_start(vl,line);
	str.Format(line,vl);
	va_end(vl);
	pLogList->AddTail((LPCTSTR)str);
}


///////////////////////////////////////////////////////////////////////
//
// 函数名       : _NetworkThread
// 功能描述     : 网络传输线程,所有有关网络通讯的工作将再这个线程内完成
// 参数         : LPVOID pParam
// 返回值       : UINT 
//
///////////////////////////////////////////////////////////////////////
UINT _NetworkThread(LPVOID pParam)
{
	MMSG msg;
	LoginInfo * pLoginInfo;
	CMirSocket s_Gate,s_SelChar,s_Game;
	CString SelCharIP,SID,CharName,s;
	int retval=0,SelCharPort=0,CharCount=0,CharIndex=0,i=0,leftlen=0;
	char tmpBuf[BUFFERLENGTH],tmpBuf2[BUFFERLENGTH];
	DWORD mode = 1;

	//下面的代码初始化局部变量
	{
		ZeroMemory(&msg,sizeof(msg));

		SelCharIP.Empty();
		SID.Empty();
		CharName.Empty();
		s.Empty();

		retval=SelCharPort=CharCount=CharIndex=i=leftlen=tmpBuf[0]=tmpBuf2[0]=0;

		mode=1;

		pLoginInfo=(LoginInfo*)pParam;

		s_Gate.Create();
		s_SelChar.Create();
		s_Game.Create();
	}

	pLoginInfo->m_Character &= 1;
	pLogList=pLoginInfo->m_LogList;

relogin:
	LogSystem("开始连接游戏服务器...");
	if(s_Gate.Connect(pLoginInfo->m_ServerIP,7000)==0)
	{
		LogSystem("连接失败");
		return FALSE;
	}
	retval=s_Gate.Send(MIRVERSION,strlen(MIRVERSION));
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("发送版本号失败");
		return FALSE;
	}
	retval=s_Gate.Receive(tmpBuf,BUFFERLENGTH);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("接收服务器验证信息失败");
		return FALSE;
	}
	tmpBuf[retval-1]='\0';
	CMirSocket::UnGateCode(&tmpBuf[1],tmpBuf2);// 解码验证信息
	sprintf(tmpBuf,"%s/",tmpBuf2);
	CMirSocket::CodeGateCode(tmpBuf,tmpBuf2);
	sprintf(tmpBuf,"<%s>",tmpBuf2);// 转换验证信息

	retval=s_Gate.Send(tmpBuf,strlen(tmpBuf));
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("发送验证信息失败");
		return FALSE;
	}
	retval=s_Gate.Receive(tmpBuf,BUFFERLENGTH);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("接收服务器处理结果失败");
		return FALSE;
	}
	tmpBuf[retval]='\0';
	if(stricmp(tmpBuf,"<pass>")==0)
	{
		// 客户端验证通过!
		LogSystem("客户端验证通过!");
	}
	else
	{
		s_Gate.Close();
		LogSystem("客户端没有通过验证");
		return FALSE;
	}
getpasswordret:
	ZeroMemory(&msg,sizeof(msg));
	msg.wCmd=0x7d1;
	sprintf(msg.data,"%s/%s",pLoginInfo->m_Account,pLoginInfo->m_Password);
	retval=s_Gate.SendGateMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("发送帐号密码失败");
		return FALSE;
	}
	ZeroMemory(&msg,sizeof(msg));
	retval=s_Gate.RecvGateMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("接收验证结果出错");
		return FALSE;
	}
	if(msg.wCmd==0x7d1)
	{
		LogSystem("重新发送账号/密码");
		goto getpasswordret;
	}
	if(msg.wCmd==0x211 )
	{
		LogSystem("账号/密码验证通过!");
		if(msg.wb>0)
		{
			LogSystem("你的账户还有%d小时.",msg.wb);
		}
		//return TRUE;
	}
	else
	{
		if(msg.wCmd==0x1f7)
		{
			if(msg.dwFlag==0xffffffff)
			{
				LogSystem("密码错误!");
			}
			if(msg.dwFlag==0)
			{
				LogSystem("此用户不存在!");
			}
			if(msg.dwFlag==0xfffffffe)
			{
				LogSystem("三次密码错误,服务器拒绝!");
			}
			if(msg.dwFlag==0xfffffffd)
			{
				LogSystem("该用户正在使用中,可能被踢出服务器。");
			}
		}
		else
		{
			LogSystem("未知错误,错误代号 0x%04x, 0x%04x",msg.dwFlag,msg.wCmd);
		}
		s_Gate.Close();
		//LogSystem("2秒后重连....");
		//Sleep(2000);
		goto relogin;
		return FALSE;
	}
	ZeroMemory(&msg,sizeof(msg));
	msg.wCmd=0x68;
	strcpy(msg.data,pLoginInfo->m_ServerName);
	retval=s_Gate.SendGateMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("发送服务器名字失败");
		return FALSE;
	}
	retval=s_Gate.RecvGateMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_Gate.Close();
		LogSystem("接收结果时出错");
		return FALSE;
	}
	if(msg.wCmd!=0x212)
	{
		s_Gate.Close();
		LogSystem("未得到服务器地址!");
		return FALSE;
	}
	s_Gate.GetString(msg.data);
	SelCharIP=s_Gate.m_StringList[0];
	SelCharPort=atoi(s_Gate.m_StringList[1]);
	SID=s_Gate.m_StringList[2];
	s_Gate.Close();
	LogSystem("开始连接角色服务器...");
	if(s_SelChar.Connect(SelCharIP,SelCharPort)==0)
	{
		LogSystem("连接失败");
		return FALSE;
	}
	ZeroMemory(&msg,sizeof(msg));
	msg.wCmd=0x64;
	sprintf(msg.data,"%s/%s",pLoginInfo->m_Account,SID);
	retval=s_SelChar.SendMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_SelChar.Close();
		LogSystem("发送验证信息时出错");
		return FALSE;
	}
	ZeroMemory(&msg,sizeof(msg));
	retval=s_SelChar.RecvMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_SelChar.Close();
		LogSystem("接收验证结果时出错");
		return FALSE;
	}
	if(msg.wCmd!=0x208)
	{
		s_SelChar.Close();
		LogSystem("未得到角色列表!");
		return FALSE;
	}
	retval=s_SelChar.GetString(msg.data);
	CharCount=msg.wa&0xff;
	CharIndex=pLoginInfo->m_Character;
	if(CharCount==0)
	{
		s_SelChar.Close();
		LogSystem("没有角色,无法进入游戏!");
		return FALSE;
	}
	if(CharIndex<0 || CharIndex>=CharCount)
	{
		s_SelChar.Close();
		LogSystem("所选择地角色不存在!");
		return FALSE;
	}
	CharName=s_SelChar.m_StringList[5*CharIndex];
	if(CharName[0]=='*')CharName=CharName.Mid(1);
	s.Format("使用[%s]登陆,级别:%s",CharName,s_SelChar.m_StringList[5*CharIndex+3]);
	LogSystem(s);
	ZeroMemory(&msg,sizeof(msg));
	msg.wCmd=0x67;
	sprintf(msg.data,"%s/%s",pLoginInfo->m_Account,CharName);
	retval=s_SelChar.SendMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_SelChar.Close();
		LogSystem("发送选人请求时出错");
		return FALSE;
	}
	retval=s_SelChar.RecvMsg(&msg);
	if(retval==SOCKET_ERROR)
	{
		s_SelChar.Close();
		LogSystem("接收选人结果时出错");
		return FALSE;
	}
	if(msg.wCmd!=0x20d)
	{
		s_SelChar.Close();
		LogSystem("选人未被服务器接受!");
		LogSystem("得到消息是: 0x%x  [%s]",msg.wCmd,msg.data);
		return FALSE;
	}
	s_SelChar.GetString(msg.data);
	s_SelChar.Close();
	LogSystem("正在进入游戏服务器,请稍候...");
	if(s_Game.Connect(s_SelChar.m_StringList[0],atoi(s_SelChar.m_StringList[1]))==0)
	{
		LogSystem("连接游戏服务器出错!");
		return FALSE;
	}
	pLoginInfo->m_GameSocket=&s_Game;
	sprintf(tmpBuf,"**%s/%s/%s/20020522/0",pLoginInfo->m_Account,CharName,SID);
	CMirSocket::CodeGameCode((BYTE*)tmpBuf,strlen(tmpBuf),(BYTE*)tmpBuf2);
	sprintf(tmpBuf,"#%c%s!",s_Game.GetMsgFlag(),tmpBuf2);
	s_Game.SendString(tmpBuf);
	if(retval==SOCKET_ERROR)
	{
		s_Game.Close();
		LogSystem("发送用户验证信息出错");
		return FALSE;
	}
	LogSystem("开始游戏消息循环");
	mode = 1;

	if(s_Game.IOCtl(FIONBIO,&mode)==0)
	{
		pLoginInfo->m_InGame=FALSE;
		LogSystem("设置模式错误!!");
	}

	while(pLoginInfo->m_InGame)
	{
		//LogSystem("循环");
		Sleep(1);
		retval=s_Game.Receive(tmpBuf,BUFFERLENGTH);
		if(retval==SOCKET_ERROR/* || retval==0*/)
		{
			retval=GetLastError();
			if(retval!=WSAEWOULDBLOCK)
			{
				LogSystem("接收中发生错误");
				pLoginInfo->m_InGame=FALSE;// 设置标志,循环退出
			}
		}
		else
		{
			for(i=0;i<retval;i++)
			{
				if(tmpBuf[i]=='*')

⌨️ 快捷键说明

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