Login.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:30k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /************************************************ *****************************************
  2. // 界面--login窗口
  3. // Copyright : Kingsoft 2002
  4. // Author :   Wooy(Wu yue)
  5. // CreateTime: 2002-8-13
  6. ------------------------------------------------------------------------------------------
  7. *****************************************************************************************/
  8. #include "KWin32.h"
  9. #include "KEngine.h"
  10. #include "LoginDef.h"
  11. #include "Login.h"
  12. #include "../NetConnect/NetConnectAgent.h"
  13. #include "KProtocol.h"
  14. #include "crtdbg.h"
  15. #include "../Ui/UiBase.h"
  16. #include "../Ui/UiShell.h"
  17. #include "../../Engine/Src/Cryptography/EDOneTimePad.h"
  18. #include "time.h"
  19. #define SERVER_LIST_FILE "\Settings\ServerList.ini"
  20. KLogin g_LoginLogic;
  21. bool GetIpAddress(const char* szAddress, unsigned char* pcAddress)
  22. {
  23. _ASSERT(pcAddress);
  24. int nValue[4];
  25. int nRet = sscanf(szAddress, "%d.%d.%d.%d", &nValue[0], &nValue[1], &nValue[2], &nValue[3]);
  26. if (nRet == 4 &&
  27. nValue[0] >= 0 && nValue[0] < 256 &&
  28. nValue[1] >= 0 && nValue[1] < 256 &&
  29. nValue[2] >= 0 && nValue[2] < 256 &&
  30. nValue[3] >= 0 && nValue[3] < 256)
  31. {
  32. pcAddress[0] = nValue[0];
  33. pcAddress[1] = nValue[1];
  34. pcAddress[2] = nValue[2];
  35. pcAddress[3] = nValue[3];
  36. return true;
  37. }
  38. return false;
  39. }
  40. static unsigned gs_holdrand = time(NULL);
  41. static inline unsigned _Rand()
  42. {
  43.     gs_holdrand = gs_holdrand * 244213L + 1541021L;
  44.      
  45.     return gs_holdrand;
  46. }
  47. void RandMemSet(int nSize, unsigned char *pbyBuffer)
  48. {
  49.     _ASSERT(nSize);
  50.     _ASSERT(pbyBuffer);
  51.     while (nSize--)
  52.     {
  53.         *pbyBuffer++ = (unsigned char)_Rand();
  54.     }
  55. }
  56. //--------------------------------------------------------------------------
  57. // 功能:构造函数
  58. //--------------------------------------------------------------------------
  59. KLogin::KLogin()
  60. {
  61. m_Status = LL_S_IDLE;
  62. m_Result = LL_R_NOTHING;
  63. m_bInAutoProgress = false;
  64. m_nNumRole = 0;
  65. memset(&m_Choices, 0, sizeof(LOGIN_CHOICE));
  66. ClearAccountPassword(true, true);
  67. m_LeftTime = 0;
  68. }
  69. void KLogin::ClearAccountPassword(bool bAccount, bool bPassword)
  70. {
  71. if (bAccount)
  72. memset(m_Choices.Account, 0xff, sizeof(m_Choices.Account));
  73. if (bPassword)
  74. memset(&m_Choices.Password, 0xff, sizeof(m_Choices.Password));
  75. }
  76. //--------------------------------------------------------------------------
  77. // 功能:析构函数
  78. //--------------------------------------------------------------------------
  79. KLogin::~KLogin()
  80. {
  81. _ASSERT(m_Status == LL_S_IDLE);
  82. }
  83. //--------------------------------------------------------------------------
  84. // 功能:与(账号)服务器建立连接
  85. // 状态切换:成功 LL_S_IDLE -> LL_S_WAIT_INPUT_ACCOUNT
  86. //   失败 状态保持
  87. //--------------------------------------------------------------------------
  88. int KLogin::CreateConnection(const unsigned char* pAddress)
  89. {
  90. int nRet;
  91. if (m_Status == LL_S_IDLE && pAddress &&
  92. ConnectAccountServer(pAddress))
  93. {
  94. RegistNetAgent();
  95. m_Status = LL_S_WAIT_INPUT_ACCOUNT;
  96. m_Result = LL_R_NOTHING;
  97. if (m_bInAutoProgress)
  98. {
  99. char szAccount[32];
  100.             KSG_PASSWORD Password;
  101. GetAccountPassword(szAccount, &Password);
  102. AccountLogin(szAccount, Password, false);
  103. memset(szAccount, 0, sizeof(szAccount));
  104. memset(&Password, 0, sizeof(Password));
  105. }
  106. nRet = true;
  107. }
  108. else
  109. {
  110. if (m_bInAutoProgress)
  111. m_bInAutoProgress = false;
  112. m_Result = LL_R_CONNECT_FAILED;
  113. nRet = false;
  114. }
  115. return nRet;
  116. }
  117. //--------------------------------------------------------------------------
  118. // 功能:传入帐号密码,账号登陆
  119. // 状态切换:成功 LL_S_WAIT_INPUT_ACCOUNT -> LL_S_ACCOUNT_CONFIRMING
  120. //   失败 状态保持
  121. //--------------------------------------------------------------------------
  122. int KLogin::AccountLogin(const char* pszAccount, const KSG_PASSWORD& crPassword, bool bOrignPassword)
  123. {
  124. int nRet;
  125. if (m_Status == LL_S_WAIT_INPUT_ACCOUNT &&
  126. pszAccount && 
  127. Request(pszAccount, &crPassword, LOGIN_A_LOGIN))
  128. {
  129.         if (bOrignPassword)
  130.         {
  131.      SetAccountPassword(pszAccount, &crPassword);
  132.         }
  133. m_Status = LL_S_ACCOUNT_CONFIRMING;
  134. m_Result = LL_R_NOTHING;
  135. nRet = true;
  136. }
  137. else
  138. {
  139. if (m_bInAutoProgress)
  140. m_bInAutoProgress = false;
  141. m_Result = LL_R_CONNECT_FAILED;
  142. nRet = false;
  143. }
  144. return nRet;
  145. }
  146. //--------------------------------------------------------------------------
  147. // 功能:选中游戏角色
  148. // 状态切换:成功 LL_S_ROLE_LIST_READY -> LL_S_WAIT_TO_LOGIN_GAMESERVER
  149. //   失败 状态保持
  150. //--------------------------------------------------------------------------
  151. int KLogin::SelectRole(int nIndex)
  152. {
  153. int nRet;
  154. if (m_Status == LL_S_ROLE_LIST_READY && nIndex >= 0 && nIndex < m_nNumRole)
  155. {
  156. tagDBSelPlayer NetCommand;
  157. NetCommand.cProtocol = c2s_dbplayerselect;
  158. strcpy(NetCommand.szRoleName, m_RoleList[nIndex].Name);
  159. g_NetConnectAgent.SendMsg(&NetCommand, sizeof(tagDBSelPlayer));
  160. g_NetConnectAgent.UpdateClientRequestTime(false);
  161. strcpy(m_Choices.szProcessingRoleName, NetCommand.szRoleName);
  162. m_Status = LL_S_WAIT_TO_LOGIN_GAMESERVER;
  163. m_Result = LL_R_NOTHING;
  164. nRet = true;
  165. }
  166. else
  167. {
  168. if (m_bInAutoProgress)
  169. m_bInAutoProgress = false;
  170. m_Result = LL_R_CONNECT_FAILED;
  171. nRet = false;
  172. }
  173. return nRet;
  174. }
  175. //--------------------------------------------------------------------------
  176. // 功能:请求新建游戏角色
  177. // 状态切换:成功 LL_S_ROLE_LIST_READY -> LL_S_CREATING_ROLE
  178. //   失败 状态保持
  179. //--------------------------------------------------------------------------
  180. int KLogin::CreateRole(KRoleChiefInfo* pCreateInfo)
  181. {
  182. int nRet = false;
  183. m_Result = LL_R_CONNECT_FAILED;
  184. if (m_Status == LL_S_ROLE_LIST_READY && pCreateInfo && m_nNumRole < MAX_PLAYER_PER_ACCOUNT &&
  185. pCreateInfo->Attribute >= 0 && pCreateInfo->Attribute < series_num)
  186. {
  187. int nNameLen = strlen(pCreateInfo->Name);
  188. if (nNameLen >= 1 && nNameLen < sizeof(pCreateInfo->Name))
  189. {
  190. char Data[sizeof(TProcessData) + sizeof(NEW_PLAYER_COMMAND)];
  191. TProcessData* pNetCommand = (TProcessData*)&Data;
  192. NEW_PLAYER_COMMAND* pInfo = (NEW_PLAYER_COMMAND*)pNetCommand->pDataBuffer;
  193. pInfo->m_btRoleNo = pCreateInfo->Gender;
  194. pInfo->m_btSeries = pCreateInfo->Attribute;
  195. pInfo->m_NativePlaceId = pCreateInfo->NativePlaceId;
  196. memcpy(pInfo->m_szName, pCreateInfo->Name, nNameLen);
  197. pInfo->m_szName[nNameLen] = '';
  198. pNetCommand->nProtoId = c2s_newplayer;
  199. pNetCommand->nDataLen = sizeof(NEW_PLAYER_COMMAND) - sizeof(pInfo->m_szName) + nNameLen + 1/* sizeof( '' ) */;
  200. pNetCommand->ulIdentity = 0;
  201. g_NetConnectAgent.SendMsg(&Data, sizeof(TProcessData) - sizeof(pNetCommand->pDataBuffer) + pNetCommand->nDataLen);
  202. g_NetConnectAgent.UpdateClientRequestTime(false);
  203. memcpy(m_Choices.szProcessingRoleName, pCreateInfo->Name, nNameLen);
  204. m_Choices.szProcessingRoleName[nNameLen] = 0;
  205. m_Status = LL_S_CREATING_ROLE;
  206. m_Result = LL_R_NOTHING;
  207. nRet = true;
  208. }
  209. }
  210. return nRet;
  211. }
  212. //--------------------------------------------------------------------------
  213. // 功能:请求删除游戏角色
  214. // 状态切换:成功 LL_S_ROLE_LIST_READY -> LL_S_DELETING_ROLE
  215. //   失败 状态保持
  216. //--------------------------------------------------------------------------
  217. int KLogin::DeleteRole(int nIndex, const KSG_PASSWORD &crSupperPassword)
  218. {
  219. int nRet;
  220. if (m_Status == LL_S_ROLE_LIST_READY && nIndex >= 0 && nIndex < m_nNumRole)
  221. {
  222. tagDBDelPlayer NetCommand;
  223. RandMemSet(sizeof(tagDBDelPlayer), (BYTE*)&NetCommand); // random memory for make a cipher
  224. NetCommand.cProtocol = c2s_roleserver_deleteplayer;
  225. GetAccountPassword(NetCommand.szAccountName, NULL);
  226.         NetCommand.Password = crSupperPassword;
  227. strncpy(NetCommand.szRoleName, m_RoleList[nIndex].Name, sizeof(NetCommand.szRoleName));
  228.         NetCommand.szRoleName[sizeof(NetCommand.szRoleName) - 1] = '';
  229. g_NetConnectAgent.SendMsg(&NetCommand, sizeof(tagDBDelPlayer));
  230. memset(&NetCommand.Password, 0, sizeof(NetCommand.Password));
  231. g_NetConnectAgent.UpdateClientRequestTime(false);
  232. strcpy(m_Choices.szProcessingRoleName, m_RoleList[nIndex].Name);
  233. m_Status = LL_S_DELETING_ROLE;
  234. m_Result = LL_R_NOTHING;
  235. nRet = true;
  236. }
  237. else
  238. {
  239. nRet = false;
  240. m_Result = LL_R_CONNECT_FAILED;
  241. }
  242. return nRet;
  243. }
  244. //--------------------------------------------------------------------------
  245. // 功能:通知等待返回结果超时了
  246. // 状态切换:成功 LL_S_??? -> LL_S_IDLE
  247. //--------------------------------------------------------------------------
  248. void KLogin::NotifyTimeout()
  249. {
  250. if (m_Status != LL_S_IDLE)
  251. {
  252. ReturnToIdle();
  253. m_Result = LL_R_CONNECT_TIMEOUT;
  254. }
  255. }
  256. //通知网络连接(意外)断开了
  257. void KLogin::NotifyDisconnect()
  258. {
  259. if (m_Status != LL_S_IDLE)
  260. {
  261. ReturnToIdle();
  262. m_Result = LL_R_CONNECT_FAILED;
  263. }
  264. }
  265. //--------------------------------------------------------------------------
  266. // 功能:通知要开始游戏了
  267. // 状态切换:成功 LL_S_ENTERING_GAME -> LL_S_IN_GAME
  268. //--------------------------------------------------------------------------
  269. void KLogin::NotifyToStartGame()
  270. {
  271. if (m_Status == LL_S_ENTERING_GAME)
  272. {
  273. g_NetConnectAgent.UpdateClientRequestTime(true);
  274. char szAccount[32];
  275. GetAccountPassword(szAccount, NULL);
  276. g_UiBase.SetUserAccount(szAccount, m_Choices.szProcessingRoleName);
  277. m_Status = LL_S_IN_GAME;
  278. m_Result = LL_R_NOTHING;
  279. if (m_bInAutoProgress)
  280. m_bInAutoProgress = false;
  281. UiOnGameServerStartSyncEnd();
  282. }
  283. }
  284. //--------------------------------------------------------------------------
  285. // 功能:回到空闲状态
  286. // 状态切换:LL_S_??? -> LL_S_IN_GAME
  287. //--------------------------------------------------------------------------
  288. void KLogin::ReturnToIdle()
  289. {
  290. if (m_Status != LL_S_IDLE)
  291. {
  292. UnRegistNetAgent();
  293. g_NetConnectAgent.DisconnectGameSvr();
  294. g_NetConnectAgent.DisconnectClient();
  295. m_Status = LL_S_IDLE;
  296. }
  297. m_Choices.bIsRoleNewCreated = false;
  298. m_Result = LL_R_NOTHING;
  299. m_bInAutoProgress = false;
  300. }
  301. //--------------------------------------------------------------------------
  302. // 功能:全程自动连接
  303. //--------------------------------------------------------------------------
  304. void KLogin::AutoLogin()
  305. {
  306. ReturnToIdle();
  307. if (IsAutoLoginEnable())
  308. {
  309. m_bInAutoProgress = true;
  310. if (m_Choices.AccountServer.Address[0] == 0 &&
  311. m_Choices.AccountServer.Address[1] == 0 &&
  312. m_Choices.AccountServer.Address[2] == 0 &&
  313. m_Choices.AccountServer.Address[3] == 0)
  314. {
  315. int nCount, nSel;
  316. KLoginServer* pList = GetServerList(-1, nCount, nSel);
  317. if (pList)
  318. {
  319. free(pList);
  320. pList = NULL;
  321. }
  322. }
  323. CreateConnection(m_Choices.AccountServer.Address);
  324. }
  325. }
  326. //--------------------------------------------------------------------------
  327. // 功能:判断是否可以执行全程自动连接
  328. //--------------------------------------------------------------------------
  329. int KLogin::IsAutoLoginEnable()
  330. {
  331. return ((~m_Choices.Account[0]) &&
  332. (~m_Choices.Password.szPassword[0]) &&
  333. m_Choices.szProcessingRoleName[0] &&
  334. m_Choices.AccountServer.Title[0]);
  335. }
  336. //设置纪录标记
  337. void KLogin::SetRememberAccountFlag(bool bEnable)
  338. {
  339. m_Choices.bRememberAccount = bEnable;
  340. if (bEnable == false)
  341. m_Choices.bRememberAll = false;
  342. }
  343. //设置纪录标记
  344. void KLogin::SetRememberAllFlag(bool bEnable)
  345. {
  346. m_Choices.bRememberAll = bEnable;
  347. if (bEnable)
  348. m_Choices.bRememberAccount = true;
  349. }
  350. //--------------------------------------------------------------------------
  351. // 功能:获取某个角色的信息
  352. //--------------------------------------------------------------------------
  353. int KLogin::GetRoleInfo(int nIndex, KRoleChiefInfo* pInfo)
  354. {
  355. if (nIndex >= 0 && nIndex < m_nNumRole)
  356. {
  357. if (pInfo)
  358. *pInfo = m_RoleList[nIndex];
  359. return true;
  360. }
  361. return false;
  362. }
  363. //--------------------------------------------------------------------------
  364. // 功能:处理账号登陆的响应
  365. // 状态切换:成功 LL_S_ACCOUNT_CONFIRMING -> LL_S_WAIT_ROLE_LIST
  366. //   失败 LL_S_ACCOUNT_CONFIRMING -> LL_S_IDLE
  367. //--------------------------------------------------------------------------
  368. void KLogin::ProcessAccountLoginResponse(KLoginStructHead* pResponse)
  369. {
  370. //_ASSERT(m_Status == LL_S_ACCOUNT_CONFIRMING && pResponse != NULL);
  371. if (((pResponse->Param & LOGIN_ACTION_FILTER) == LOGIN_A_LOGIN) && //操作性为要匹配
  372. pResponse->Size >= sizeof(KLoginAccountInfo)) //数据内容的大小也要匹配
  373. {
  374. KLoginAccountInfo* pInfo = (KLoginAccountInfo*)pResponse;
  375. char szAccount[32];
  376.         KSG_PASSWORD Password;
  377. GetAccountPassword(szAccount, &Password);
  378. if (strcmp(pInfo->Account,  szAccount)  == 0 &&
  379. strcmp(pInfo->Password.szPassword, Password.szPassword) == 0)
  380. {
  381. int nResult = ((pResponse->Param) & ~LOGIN_ACTION_FILTER);
  382. if (nResult == LOGIN_R_SUCCESS)
  383. {
  384. g_NetConnectAgent.UpdateClientRequestTime(false);
  385. m_Status = LL_S_WAIT_ROLE_LIST;
  386. m_Result = LL_R_ACCOUNT_CONFIRM_SUCCESS;
  387. m_LeftTime = pInfo->nLeftTime;
  388. }
  389. else
  390. {
  391. LOGIN_LOGIC_RESULT_INFO eResult = LL_R_NOTHING;
  392. switch(nResult)
  393. {
  394. case LOGIN_R_ACCOUNT_OR_PASSWORD_ERROR:
  395. eResult = LL_R_ACCOUNT_PWD_ERROR;
  396. m_Status = LL_S_WAIT_INPUT_ACCOUNT;
  397. break;
  398. case LOGIN_R_ACCOUNT_EXIST:
  399. eResult = LL_R_ACCOUNT_LOCKED;
  400. m_Status = LL_S_WAIT_INPUT_ACCOUNT;
  401. break;
  402. case LOGIN_R_FREEZE:
  403. eResult = LL_R_ACCOUNT_FREEZE;
  404. m_Status = LL_S_WAIT_INPUT_ACCOUNT;
  405. break;
  406. case LOGIN_R_INVALID_PROTOCOLVERSION:
  407. eResult = LL_R_INVALID_PROTOCOLVERSION;
  408. break;
  409. case LOGIN_R_FAILED:
  410. eResult = LL_R_CONNECT_SERV_BUSY;
  411. break;
  412. case LOGIN_R_TIMEOUT:
  413. eResult = LL_R_ACCOUNT_NOT_ENOUGH_POINT;
  414. m_Status = LL_S_WAIT_INPUT_ACCOUNT;
  415. break;
  416. default:
  417. eResult = LL_R_CONNECT_FAILED;
  418. break;
  419. }
  420. if (m_Status != LL_S_WAIT_INPUT_ACCOUNT ||
  421. m_bInAutoProgress)
  422. {
  423. ReturnToIdle();
  424. }
  425. m_Result = eResult;
  426. }
  427. }
  428. memset(szAccount, 0, sizeof(szAccount));
  429. memset(&Password, 0, sizeof(Password));
  430. }
  431. }
  432. //--------------------------------------------------------------------------
  433. // 功能:角色列表返回
  434. // 状态切换:成功 LL_S_WAIT_ROLE_LIST -> LL_S_ROLE_LIST_READY
  435. //   失败 状态保持
  436. //--------------------------------------------------------------------------
  437. void KLogin::ProcessRoleListResponse(TProcessData* pResponse)
  438. {
  439. //_ASSERT(m_Status == LL_S_WAIT_ROLE_LIST && pResponse != NULL);
  440. if (pResponse->nProtoId == s2c_roleserver_getrolelist_result)
  441. {
  442. m_nNumRole = pResponse->pDataBuffer[0];
  443. if (m_nNumRole > MAX_PLAYER_PER_ACCOUNT)
  444. m_nNumRole = MAX_PLAYER_PER_ACCOUNT;
  445. else if (m_nNumRole < 0)
  446. m_nNumRole = 0;
  447. RoleBaseInfo* pList = (RoleBaseInfo*)&pResponse->pDataBuffer[1];
  448. for (int i = 0; i < m_nNumRole; i++)
  449. {
  450. if (pList->szName[0])
  451. {
  452. strcpy(m_RoleList[i].Name, pList->szName);
  453. m_RoleList[i].Attribute = pList->Series;
  454. m_RoleList[i].Gender = pList->Sex;
  455. m_RoleList[i].nLevel = pList->Level;
  456. pList ++;
  457. }
  458. else
  459. {
  460. m_nNumRole = i;
  461. break;
  462. }
  463. }
  464. g_NetConnectAgent.UpdateClientRequestTime(true);
  465. m_Status = LL_S_ROLE_LIST_READY;
  466. m_Result = LL_R_NOTHING;
  467. if (m_bInAutoProgress)
  468. {
  469. int nAdviceChoice;
  470. GetRoleCount(nAdviceChoice);
  471. SelectRole(nAdviceChoice);
  472. }
  473. }
  474. }
  475. //--------------------------------------------------------------------------
  476. // 功能:新建角色列表的响应
  477. // 状态切换:LL_S_CREATING_ROLE -> LL_S_ROLE_LIST_READY
  478. //--------------------------------------------------------------------------
  479. void KLogin::ProcessCreateRoleResponse(tagNewDelRoleResponse* pResponse)
  480. {
  481. //_ASSERT(m_Status == LL_S_DELETING_ROLE && pResponse != NULL);
  482. if (pResponse->cProtocol == s2c_rolenewdelresponse)
  483. {
  484. // if (strcmp(pResponse->szRoleName, m_Choices.szProcessingRoleName) == 0) //to be check/* 刘鹏调试版 */
  485. {
  486. if (pResponse->bSucceeded)
  487. {
  488. g_NetConnectAgent.UpdateClientRequestTime(false);
  489. m_Choices.bIsRoleNewCreated = true;
  490. m_Status = LL_S_WAIT_TO_LOGIN_GAMESERVER;
  491. m_Result = LL_R_CREATE_ROLE_SUCCESS;
  492. }
  493. else
  494. {
  495. g_NetConnectAgent.UpdateClientRequestTime(true);
  496. m_Status = LL_S_ROLE_LIST_READY;
  497. m_Result = LL_R_INVALID_ROLENAME;
  498. }
  499. }
  500. }
  501. }
  502. //--------------------------------------------------------------------------
  503. // 功能:删除角色列表的响应
  504. // 状态切换:LL_S_DELETING_ROLE -> LL_S_ROLE_LIST_READY
  505. //--------------------------------------------------------------------------
  506. void KLogin::ProcessDeleteRoleResponse(tagNewDelRoleResponse* pResponse)
  507. {
  508. //_ASSERT(m_Status == LL_S_DELETING_ROLE && pResponse != NULL);
  509. if (pResponse->cProtocol == s2c_rolenewdelresponse)
  510. {
  511. // if (strcmp(pResponse->szRoleName, m_Choices.szProcessingRoleName) == 0) //to be check/* 刘鹏调试版 */
  512. {
  513. g_NetConnectAgent.UpdateClientRequestTime(true);
  514. m_Status = LL_S_ROLE_LIST_READY;
  515. if (pResponse->bSucceeded)
  516. {
  517. char szAccount[32];
  518. GetAccountPassword(szAccount, NULL);
  519. g_UiBase.SetUserAccount(szAccount, m_Choices.szProcessingRoleName);
  520. g_UiBase.CleanPrivateDataFolder();
  521. for (int i = 0; i < m_nNumRole; i++)
  522. {
  523. if (strcmp(m_RoleList[i].Name, m_Choices.szProcessingRoleName) == 0)
  524. {
  525. m_nNumRole--;
  526. for (; i < m_nNumRole; i++)
  527. m_RoleList[i] = m_RoleList[i + 1];
  528. break;
  529. }
  530. }
  531. m_Result = LL_R_NOTHING;
  532. }
  533. else
  534. {
  535. m_Result = LL_R_INVALID_PASSWORD;
  536. }
  537. }
  538. }
  539. }
  540. //--------------------------------------------------------------------------
  541. // 功能:准备登陆游戏服务器的响应
  542. // 状态切换:LL_S_WAIT_TO_LOGIN_GAMESERVER -> LL_S_ENTERING_GAME
  543. //--------------------------------------------------------------------------
  544. void KLogin::ProcessToLoginGameServResponse(tagNotifyPlayerLogin* pResponse)
  545. {
  546. //_ASSERT(m_Status == LL_S_WAIT_TO_LOGIN_GAMESERVER && pResponse != NULL);
  547. if (pResponse->cProtocol == s2c_notifyplayerlogin)
  548. {
  549. if (strcmp((const char*)pResponse->szRoleName, m_Choices.szProcessingRoleName) == 0)
  550. {
  551. g_NetConnectAgent.UpdateClientRequestTime(true);
  552. // 开始与GameSvr进行连接
  553. if (g_NetConnectAgent.ConnectToGameSvr(
  554. (const unsigned char*)&pResponse->nIPAddr,
  555. pResponse->nPort, &pResponse->guid))
  556. {
  557. m_Status = LL_S_ENTERING_GAME;
  558. m_Result = LL_R_NOTHING;
  559. }
  560. else
  561. {
  562. ReturnToIdle();
  563. m_Result = LL_R_CONNECT_FAILED;
  564. }
  565. // 断开与网关的连接
  566. g_NetConnectAgent.DisconnectClient();
  567. }
  568. else
  569. {
  570. ReturnToIdle();
  571. m_Result = LL_R_SERVER_SHUTDOWN;
  572. }
  573. }
  574. }
  575. //--------------------------------------------------------------------------
  576. // 功能:接受网络消息
  577. //--------------------------------------------------------------------------
  578. void KLogin::AcceptNetMsg(void* pMsgData)
  579. {
  580. if (pMsgData == NULL)
  581. return;
  582. switch(m_Status)
  583. {
  584. case LL_S_ACCOUNT_CONFIRMING:
  585. ProcessAccountLoginResponse((KLoginStructHead*) (((char*)pMsgData) + PROTOCOL_MSG_SIZE));
  586. break;
  587. case LL_S_WAIT_ROLE_LIST:
  588. ProcessRoleListResponse((TProcessData*)pMsgData);
  589. break;
  590. case LL_S_CREATING_ROLE:
  591. ProcessCreateRoleResponse((tagNewDelRoleResponse*)pMsgData);
  592. break;
  593. case LL_S_DELETING_ROLE:
  594. ProcessDeleteRoleResponse((tagNewDelRoleResponse*)pMsgData);
  595. break;
  596. case LL_S_WAIT_TO_LOGIN_GAMESERVER:
  597. ProcessToLoginGameServResponse((tagNotifyPlayerLogin*)pMsgData);
  598. break;
  599. }
  600. }
  601. //获取操作的结果信息
  602. LOGIN_LOGIC_RESULT_INFO KLogin::GetResult()
  603. {
  604. LOGIN_LOGIC_RESULT_INFO eReturn = m_Result;
  605. m_Result = LL_R_NOTHING;
  606. return eReturn;
  607. }
  608. //获取角色的数目
  609. int KLogin::GetRoleCount(int& nAdviceChoice)
  610. {
  611. nAdviceChoice = 0;
  612. if (m_Choices.szProcessingRoleName[0])
  613. {
  614. for (int i = 0; i < m_nNumRole; i++)
  615. {
  616. if (strcmp(m_Choices.szProcessingRoleName, m_RoleList[i].Name) == 0)
  617. {
  618. nAdviceChoice = i;
  619. break;
  620. }
  621. }
  622. }
  623. return m_nNumRole;
  624. }
  625. //--------------------------------------------------------------------------
  626. // 功能:获取建议(旧的)登陆账号
  627. //--------------------------------------------------------------------------
  628. bool KLogin::GetLoginAccount(char* pszAccount)
  629. {
  630. if (pszAccount)
  631. GetAccountPassword(pszAccount, NULL);
  632. return m_Choices.bRememberAccount;
  633. }
  634. #define $LOGIN "Login"
  635. #define $LAST_ACCOUNT "LastAccount"
  636. #define $LAST_PASSWORD "LastPassword"
  637. //--------------------------------------------------------------------------
  638. // 功能:读取以前的的登陆选择
  639. //--------------------------------------------------------------------------
  640. void KLogin::LoadLoginChoice()
  641. {
  642. if (m_Choices.bLoaded)
  643. return;
  644. memset(&m_Choices, 0, sizeof(m_Choices));
  645. ClearAccountPassword(true, true);
  646. m_Choices.bLoaded = true;
  647. KIniFile* pSetting = g_UiBase.GetCommSettingFile();
  648. char szAccount[32];
  649.     KSG_PASSWORD Password;
  650. if (pSetting)
  651. {
  652. pSetting->GetInteger($LOGIN, "SelServerRegion", 0, &m_Choices.nServerRegionIndex);
  653. pSetting->GetString($LOGIN, "LastGameServer", "", m_Choices.AccountServer.Title, sizeof(m_Choices.AccountServer.Title));
  654. szAccount[0] = 0;
  655. pSetting->GetStruct($LOGIN, $LAST_ACCOUNT, szAccount, sizeof(szAccount));
  656. if (szAccount[0])
  657. {
  658. EDOneTimePad_Decipher(szAccount, strlen(szAccount));
  659. m_Choices.bRememberAccount = true;
  660. SetAccountPassword(szAccount, NULL);
  661. Password.szPassword[0] = '';
  662. pSetting->GetStruct($LOGIN, $LAST_PASSWORD, Password.szPassword, sizeof(Password.szPassword));
  663. if (Password.szPassword[0])
  664. {
  665. EDOneTimePad_Decipher(Password.szPassword, strlen(Password.szPassword));
  666. m_Choices.bRememberAll = true;
  667. SetAccountPassword(NULL, &Password);
  668. memset(&Password, 0, sizeof(Password));
  669. }
  670. }
  671. if (szAccount[0])
  672. {
  673. KIniFile* pPrivate = g_UiBase.GetPrivateSettingFile();
  674. if (pPrivate)
  675. {
  676. if (pPrivate->GetString("Main", "LastSelCharacter", "",
  677. m_Choices.szProcessingRoleName, sizeof(m_Choices.szProcessingRoleName)))
  678. {
  679. EDOneTimePad_Decipher(m_Choices.szProcessingRoleName, strlen(m_Choices.szProcessingRoleName));
  680. }
  681. }
  682. g_UiBase.ClosePrivateSettingFile(false);
  683. }
  684. g_UiBase.CloseCommSettingFile(false);
  685. }
  686. }
  687. //--------------------------------------------------------------------------
  688. // 功能:保存登陆选择设置
  689. //--------------------------------------------------------------------------
  690. void KLogin::SaveLoginChoice()
  691. {
  692. KIniFile* pSetting = g_UiBase.GetCommSettingFile();
  693. int i;
  694. if (pSetting)
  695. {
  696. //----纪录选择的服务器----
  697. pSetting->WriteInteger($LOGIN, "SelServerRegion", m_Choices.nServerRegionIndex);
  698. if (m_Choices.AccountServer.Title[0])
  699. {
  700. pSetting->WriteString($LOGIN, "LastGameServer", m_Choices.AccountServer.Title);
  701. }
  702. char szBuffer[32];
  703. //----纪录最后一次登陆账号----
  704. pSetting->EraseKey($LOGIN, $LAST_ACCOUNT);
  705. if (m_Choices.bRememberAccount)
  706. {
  707. GetAccountPassword(szBuffer, NULL);
  708. i = strlen(szBuffer);
  709. EDOneTimePad_Encipher(szBuffer, i);
  710. pSetting->WriteStruct($LOGIN, $LAST_ACCOUNT, szBuffer, sizeof(szBuffer));
  711. if (m_Choices.bRememberAll)
  712. {
  713.                 KSG_PASSWORD Password;
  714. GetAccountPassword(NULL, &Password);
  715. i = strlen(Password.szPassword);
  716. EDOneTimePad_Encipher(Password.szPassword, i);
  717. pSetting->WriteStruct($LOGIN, $LAST_PASSWORD, Password.szPassword, sizeof(Password.szPassword));
  718. }
  719. KIniFile* pPrivate = g_UiBase.GetPrivateSettingFile();
  720. if (pPrivate)
  721. {
  722. if (m_Choices.szProcessingRoleName[0])
  723. {
  724. i = strlen(m_Choices.szProcessingRoleName);
  725. memcpy(szBuffer, m_Choices.szProcessingRoleName, i);
  726. szBuffer[i] = 0;
  727. EDOneTimePad_Encipher(szBuffer, i);
  728. pPrivate->WriteString("Main", "LastSelCharacter", szBuffer);
  729. }
  730. g_UiBase.ClosePrivateSettingFile(true);
  731. }
  732. }
  733. g_UiBase.CloseCommSettingFile(true);
  734. }
  735. }
  736. //--------------------------------------------------------------------------
  737. // 功能:获取服务器区域的列表
  738. //--------------------------------------------------------------------------
  739. KLoginServer* KLogin::GetServerRegionList(int& nCount, int& nAdviceChoice)
  740. {
  741. KLoginServer* pServers = NULL;
  742. nCount = 0;
  743. nAdviceChoice = 0;
  744. KIniFile File;
  745. int i;
  746. if (File.Load(SERVER_LIST_FILE))
  747. {
  748. int nReadCount = 0;
  749. char szKey[32];
  750. File.GetInteger("List", "RegionCount", 0, &nReadCount);
  751. if (nReadCount > 0)
  752. {
  753. pServers = (KLoginServer*)malloc(sizeof(KLoginServer) * nReadCount);
  754. if (pServers)
  755. {
  756. for (i = 0; i < nReadCount; i++)
  757. {
  758. sprintf(szKey, "Region_%d", i);
  759. if (File.GetString("List", szKey, "", pServers[nCount].Title,
  760. sizeof(pServers[nCount].Title)) &&
  761. pServers[nCount].Title[0])
  762. {
  763. nCount ++;
  764. }
  765. }
  766. if (nCount == 0)
  767. {
  768. free(pServers);
  769. pServers = NULL;
  770. }
  771. }
  772. }
  773. }
  774. if (m_Choices.nServerRegionIndex < 0 || m_Choices.nServerRegionIndex >= nCount)
  775. m_Choices.nServerRegionIndex = 0;
  776. nAdviceChoice = m_Choices.nServerRegionIndex;
  777. return pServers;
  778. }
  779. //--------------------------------------------------------------------------
  780. // 功能:登陆服务器列表获取
  781. //--------------------------------------------------------------------------
  782. KLoginServer* KLogin::GetServerList(int nRegion, int& nCount, int& nAdviceChoice)
  783. {
  784. KLoginServer* pServers = NULL;
  785. nCount = 0;
  786. nAdviceChoice = 0;
  787. if (nRegion < 0)
  788. {
  789. KIniFile* pSetting = g_UiBase.GetCommSettingFile();
  790. if (pSetting)
  791. {
  792. pSetting->GetInteger($LOGIN, "SelServerRegion", 0, &nRegion);
  793. g_UiBase.CloseCommSettingFile(false);
  794. }
  795. }
  796. KIniFile File;
  797. int i;
  798. if (File.Load(SERVER_LIST_FILE))
  799. {
  800. int nReadCount = 0;
  801. char szSection[32], szKey[32], szBuffer[32];
  802. File.GetInteger("List", "RegionCount", 0, &nReadCount); //区域的数目
  803. if (nReadCount > 0 || nRegion >= 0 && nRegion < nReadCount)
  804. {
  805. m_Choices.nServerRegionIndex = nRegion;
  806. sprintf(szSection, "Region_%d", nRegion);
  807. File.GetInteger(szSection, "Count", 0, &nReadCount); //该区域服务器的数目
  808. if (nReadCount > 0)
  809. {
  810. pServers = (KLoginServer*)malloc(sizeof(KLoginServer) * nReadCount);
  811. if (pServers)
  812. {
  813. for (i = 0; i < nReadCount; i++)
  814. {
  815. sprintf(szKey, "%d_Address", i);
  816. if (!File.GetString(szSection, szKey, "", szBuffer, sizeof(szBuffer)) ||
  817. GetIpAddress(szBuffer, pServers[nCount].Address) == false)
  818. {
  819. continue;
  820. }
  821. sprintf(szKey, "%d_Title", i);
  822. if (File.GetString(szSection, szKey, "", pServers[nCount].Title,
  823. sizeof(pServers[nCount].Title)) &&
  824. pServers[nCount].Title[0])
  825. {
  826. nCount ++;
  827. }
  828. }
  829. if (nCount == 0)
  830. {
  831. free(pServers);
  832. pServers = NULL;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. if (nCount)
  839. {
  840. for (i = 0; i < nCount; i++)
  841. {
  842. if (strcmp(pServers[i].Title, m_Choices.AccountServer.Title) == 0)
  843. {
  844. nAdviceChoice = i;
  845. break;
  846. }
  847. }
  848. if (i >= nCount)
  849. strcpy(m_Choices.AccountServer.Title, pServers[nAdviceChoice].Title);
  850. m_Choices.AccountServer.Address[0] = pServers[nAdviceChoice].Address[0];
  851. m_Choices.AccountServer.Address[1] = pServers[nAdviceChoice].Address[1];
  852. m_Choices.AccountServer.Address[2] = pServers[nAdviceChoice].Address[2];
  853. m_Choices.AccountServer.Address[3] = pServers[nAdviceChoice].Address[3];
  854. }
  855. return pServers;
  856. }
  857. int KLogin::SetAccountServer(const KLoginServer &rcSelectServer)
  858. {
  859.     m_Choices.AccountServer = rcSelectServer;
  860.     
  861.     return true;
  862. }
  863. extern void RandMemSet(int nSize, unsigned char *pbyBuffer);
  864. //--------------------------------------------------------------------------
  865. // 功能:申请账号
  866. //--------------------------------------------------------------------------
  867. int KLogin::Request(const char* pszAccount, const KSG_PASSWORD* pcPassword, int nAction)
  868. {
  869. BYTE Buff[sizeof(KLoginAccountInfo) + PROTOCOL_MSG_SIZE];
  870. RandMemSet(sizeof(Buff), (BYTE*)Buff); // random memory for make a cipher
  871. if (pszAccount && pcPassword)
  872. {
  873. (*(PROTOCOL_MSG_TYPE*)Buff) = c2s_login;
  874. KLoginAccountInfo* pInfo = (KLoginAccountInfo*)&Buff[PROTOCOL_MSG_SIZE];
  875. pInfo->Size  = sizeof(KLoginAccountInfo);
  876. pInfo->Param = nAction | LOGIN_R_REQUEST;
  877. strncpy(pInfo->Account,  pszAccount, sizeof(pInfo->Account));
  878.         pInfo->Account[sizeof(pInfo->Account) - 1] = '';
  879. pInfo->Password = *pcPassword;
  880.         #ifdef USE_KPROTOCOL_VERSION
  881.         // Add by Freeway Chen in 2003.7.1
  882.         pInfo->ProtocolVersion = KPROTOCOL_VERSION;    //  传输协议版本,以便校验是否兼容
  883.         #endif
  884. if (g_NetConnectAgent.SendMsg(Buff, sizeof(KLoginAccountInfo) + PROTOCOL_MSG_SIZE))
  885. {
  886. g_NetConnectAgent.UpdateClientRequestTime(false);
  887. return true;
  888. }
  889. }
  890. return false;
  891. }
  892. //--------------------------------------------------------------------------
  893. // 功能:连接游戏服务
  894. //--------------------------------------------------------------------------
  895. int KLogin::ConnectAccountServer(const unsigned char* pIpAddress)
  896. {
  897. KIniFile IniFile;
  898. if (pIpAddress && IniFile.Load("\Config.ini"))
  899. {
  900. int nPort;
  901. IniFile.GetInteger("Server", "GameServPort", 8888, &nPort);
  902. return g_NetConnectAgent.ClientConnectByNumericIp(pIpAddress, nPort);
  903. }
  904. return false;
  905. }
  906. void KLogin::RegistNetAgent()
  907. {
  908. g_NetConnectAgent.RegisterMsgTargetObject(s2c_login, this);
  909. g_NetConnectAgent.RegisterMsgTargetObject(s2c_roleserver_getrolelist_result, this);
  910. g_NetConnectAgent.RegisterMsgTargetObject(s2c_notifyplayerlogin, this);
  911. g_NetConnectAgent.RegisterMsgTargetObject(s2c_rolenewdelresponse, this);
  912. }
  913. void KLogin::UnRegistNetAgent()
  914. {
  915. g_NetConnectAgent.RegisterMsgTargetObject(s2c_login, NULL);
  916. g_NetConnectAgent.RegisterMsgTargetObject(s2c_roleserver_getrolelist_result, NULL);
  917. g_NetConnectAgent.RegisterMsgTargetObject(s2c_notifyplayerlogin, NULL);
  918. g_NetConnectAgent.RegisterMsgTargetObject(s2c_rolenewdelresponse, NULL);
  919. }
  920. void KLogin::SetAccountPassword(const char* pszAccount, const KSG_PASSWORD* pcPassword)
  921. {
  922. int i;
  923. if (pszAccount)
  924. {
  925. strncpy(m_Choices.Account, pszAccount, sizeof(m_Choices.Account));
  926. for (i = 0; i < 32; i++)
  927. m_Choices.Account[i] = ~m_Choices.Account[i];
  928. }
  929. if (pcPassword)
  930. {
  931. m_Choices.Password = *pcPassword;
  932. for (i = 0; i < KSG_PASSWORD_MAX_SIZE; i++)
  933. m_Choices.Password.szPassword[i] = ~m_Choices.Password.szPassword[i];
  934. }
  935. }
  936. void KLogin::GetAccountPassword(char* pszAccount, KSG_PASSWORD* pPassword)
  937. {
  938. int i;
  939. if (pszAccount)
  940. {
  941. memcpy(pszAccount, m_Choices.Account, sizeof(m_Choices.Account));
  942. for (i = 0; i < 32; i++)
  943. pszAccount[i] = ~pszAccount[i];
  944. }
  945. if (pPassword)
  946. {
  947.         *pPassword = m_Choices.Password;
  948. for (i = 0; i < KSG_PASSWORD_MAX_SIZE; i++)
  949. pPassword->szPassword[i] = ~pPassword->szPassword[i];
  950. }
  951. }