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

模拟服务器

开发平台:

C/C++

  1. #define _WIN32_WINNT 0x0400
  2. #include <objbase.h>
  3. #include <initguid.h>
  4. #include "KEngine.h"
  5. #include "KCore.h"
  6. #include "KSafeList.h"
  7. #include "SwordOnLineServer.h"
  8. #include "../Core/src/CoreServerShell.h"
  9. #include <crtdbg.h>
  10. #define GAME_FPS 20
  11. typedef HRESULT ( __stdcall * pfnCreateServerInterface )(
  12. REFIID riid,
  13. void **ppv
  14. );
  15. static const int g_snMaxPlayerCount = 500;
  16. static const int g_snPrecision = 10;
  17. static const int g_snMaxBuffer = 500;
  18. static const int g_snBufferSize = 16384;
  19. KPakList g_PakList;
  20. KSafeList g_PlayerRemoveList;
  21. void __stdcall ServerCallBack(LPVOID lpParam, const unsigned long &ulnID, const unsigned long &uEventType);
  22. enum PLAYER_GAME_STATUS
  23. {
  24. PLAYER_BEGIN = 0,
  25. PLAYER_LISTLOADING,
  26. PLAYER_DBLOADING,
  27. PLAYER_NEWLOADING,
  28. PLAYER_SYNC_REPLY,
  29. PLAYER_PLAYING,
  30. };
  31. KSwordOnLineSever::KSwordOnLineSever()
  32. {
  33. m_bRuning = false;
  34. m_szInformation[0] = 0;
  35. m_szStatusString[0] = 0;
  36. m_pCoreServerShell = NULL;
  37. m_pServer = NULL;
  38. m_nUpdateInterval = DEFUALT_UPDATE_INTERFAL;
  39. m_nLoopElapse = 0;
  40. m_pnPlayerIndex = NULL;
  41. m_pPlayerData = NULL;
  42. m_pPlayerGameStatus = NULL;
  43. m_pNetConnectStatus = NULL;
  44. m_pnPlayerAccountKey = NULL;
  45. m_nMaxPlayer = 0;
  46. m_hModule = 0;
  47. }
  48. KSwordOnLineSever::~KSwordOnLineSever()
  49. {
  50. ShutDown();
  51. }
  52. bool KSwordOnLineSever::Init()
  53. {
  54. m_hModule = ::LoadLibrary( "heaven.dll" );
  55. if ( m_hModule )
  56. {
  57. pfnCreateServerInterface pFactroyFun = ( pfnCreateServerInterface )GetProcAddress( m_hModule, "CreateInterface" );
  58. IServerFactory *pServerFactory = NULL;
  59. if ( SUCCEEDED( pFactroyFun( IID_IServerFactory, reinterpret_cast< void ** >( &pServerFactory ) ) ) )
  60. {
  61. pServerFactory->SetEnvironment( g_snMaxPlayerCount, g_snPrecision, g_snMaxBuffer, g_snBufferSize  );
  62. pServerFactory->CreateServerInterface( IID_IIOCPServer, reinterpret_cast< void ** >( &m_pServer ) );
  63. pServerFactory->Release();
  64. }
  65. }
  66. if (!m_pServer)
  67. return false;
  68. if (FAILED(m_pServer->Startup()))
  69. return false;
  70. m_pServer->RegisterMsgFilter( reinterpret_cast< void * >( m_pServer ), ServerCallBack );
  71. m_nMaxPlayer = g_snMaxPlayerCount + g_snPrecision;
  72. if (m_nMaxPlayer <= 0)
  73. m_nMaxPlayer = 1;
  74. if (!m_pnPlayerIndex)
  75. {
  76. m_pnPlayerIndex = (int *)new int[m_nMaxPlayer];
  77. }
  78. ZeroMemory(m_pnPlayerIndex, sizeof(int) * m_nMaxPlayer);
  79. if (!m_pPlayerData)
  80. {
  81. m_pPlayerData = (LoginData *)new LoginData[m_nMaxPlayer];
  82. }
  83. ZeroMemory(m_pPlayerData, sizeof(LoginData) * m_nMaxPlayer);
  84. if (!m_pPlayerGameStatus)
  85. {
  86. m_pPlayerGameStatus = (GameStatus *)new GameStatus[m_nMaxPlayer];
  87. }
  88. if (!m_pNetConnectStatus)
  89. {
  90. m_pNetConnectStatus = (ConnectStatus *)new ConnectStatus[m_nMaxPlayer];
  91. }
  92. if (!m_pnPlayerAccountKey)
  93. {
  94. m_pnPlayerAccountKey = (DWORD *)new int[m_nMaxPlayer];
  95. }
  96. ZeroMemory(m_pnPlayerAccountKey, sizeof(DWORD) * m_nMaxPlayer);
  97. return true;
  98. }
  99. void KSwordOnLineSever::GetStatus(char* pszStatusString, char* pszInformation)
  100. {
  101. _ASSERT(pszStatusString);
  102. _ASSERT(pszInformation);
  103. strcpy(pszInformation, m_szInformation);
  104. strcpy(pszStatusString, m_szStatusString);
  105. }
  106. int KSwordOnLineSever::GetClientConnectInfo(ClientConnectInfo* pInfo, int& nPos)
  107. {
  108. const char* pChar;
  109. if (pInfo && nPos >= 0 && nPos < m_nMaxPlayer)
  110. {
  111. pInfo->AddrInfo[0] = 0;
  112. pInfo->Character[0] = 0;
  113. while(nPos < m_nMaxPlayer)
  114. {
  115. pChar = (const char *)m_pServer->GetClientInfo(nPos);
  116. if (pChar)
  117. {
  118. strcpy(pInfo->AddrInfo, pChar);
  119. if (m_pCoreServerShell)
  120. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME,
  121. (unsigned int)pInfo->Character, nPos);
  122. nPos++;
  123. return 1;
  124. }
  125. nPos ++;
  126. }
  127. } // need modify later
  128. return 0;
  129. }
  130. bool KSwordOnLineSever::Launch()
  131. {
  132. if (m_bRuning)
  133. {
  134. strcpy(m_szStatusString, "Already launched.");
  135. return true;
  136. }
  137. if (!m_pServer)
  138. {
  139. strcpy(m_szStatusString, "Not init server.");
  140. return false;
  141. }
  142. m_szInformation[0] = 0;
  143. strcpy(m_szStatusString, "Launching...");
  144. #ifdef _DEBUG
  145. g_FindDebugWindow("#32770", "DebugWin");
  146. #endif
  147. g_SetRootPath(NULL);
  148. g_PakList.Open("config.ini");
  149. if (FAILED(m_pServer->OpenService(INADDR_ANY, 6666)))
  150. {
  151. strcpy(m_szInformation, "Failed to Create NetServer.");
  152. strcpy(m_szStatusString, "Launch FAILED!");
  153. return false;
  154. }
  155. if (m_pCoreServerShell == NULL)
  156. m_pCoreServerShell = ::CoreGetServerShell();
  157. if (m_pCoreServerShell == NULL)
  158. {
  159. strcpy(m_szInformation, "Failed to Create CoreShell.");
  160. strcpy(m_szStatusString, "Launch FAILED!");
  161. return false;
  162. }
  163. IServer *pClonServer = NULL;
  164. m_pServer->QueryInterface( IID_IIOCPServer, reinterpret_cast< void ** >( &pClonServer ) );
  165. m_pCoreServerShell->OperationRequest(SSOI_LAUNCH, (unsigned int)pClonServer, 0);
  166. m_Timer.Start();
  167. m_GameLoop = 0;
  168. m_bRuning = true;
  169. m_szInformation[0] = 0;
  170. strcpy(m_szStatusString, "Launch SUCCESSED!");
  171. return true;
  172. }
  173. bool KSwordOnLineSever::ShutDown()
  174. {
  175. strcpy(m_szInformation, "Restart, if U wish to launch again.");
  176. strcpy(m_szStatusString, "Server has been shutdown.");
  177. if (m_pCoreServerShell)
  178. {
  179. m_pCoreServerShell->Release();
  180. m_pCoreServerShell = NULL;
  181. }
  182. if (m_pnPlayerIndex)
  183. {
  184. delete [] m_pnPlayerIndex;
  185. m_pnPlayerIndex = NULL;
  186. }
  187. if (m_pPlayerData)
  188. {
  189. delete [] m_pPlayerData;
  190. m_pPlayerData = NULL;
  191. }
  192. if (m_pPlayerGameStatus)
  193. {
  194. delete [] m_pPlayerGameStatus;
  195. m_pPlayerGameStatus = NULL;
  196. }
  197. if (m_pNetConnectStatus)
  198. {
  199. delete [] m_pNetConnectStatus;
  200. m_pNetConnectStatus = NULL;
  201. }
  202. if (m_pnPlayerAccountKey)
  203. {
  204. delete [] m_pnPlayerAccountKey;
  205. m_pnPlayerAccountKey = NULL;
  206. }
  207. if (m_pServer)
  208. {
  209. m_pServer->CloseService();
  210. m_pServer->Cleanup();
  211. m_pServer->Release();
  212. m_pServer = NULL;
  213. }
  214. if (m_hModule)
  215. {
  216. ::FreeLibrary( m_hModule );
  217. m_hModule = NULL;
  218. }
  219. m_bRuning = false;
  220. return true;
  221. }
  222. //返回值表示状态是否更新
  223. bool KSwordOnLineSever::Breathe()
  224. {
  225. static KTimer s_Timer;
  226. DWORD t1;
  227. if (m_bRuning && m_pCoreServerShell)
  228. {
  229. s_Timer.Start();
  230. MessageLoop(); //处理网络消息
  231. t1 = s_Timer.GetElapse();
  232. g_DebugLog("[FPS]Message:%dms", t1);
  233. if (m_GameLoop * 1000 <= m_Timer.GetElapse() * GAME_FPS)
  234. {
  235. s_Timer.Start();
  236. MainLoop();
  237. t1 = s_Timer.GetElapse();
  238. g_DebugLog("[FPS]MainLoop:%dms", t1);
  239. }
  240. else
  241. {
  242. //SwitchToThread();
  243. Sleep(1);
  244. }
  245. #define STRING_MIN_LEN 9
  246. #define STRING_MAX_LEN 15
  247. char szStatusString[20] = "@ Runing.........";
  248. static int  s_nStatusLen = STRING_MIN_LEN;
  249. if ((++m_nLoopElapse) >= m_nUpdateInterval)
  250. {
  251. /* m_nLoopElapse = 0;
  252. if (szStatusString[0] == '@')
  253. {
  254. memcpy(m_szStatusString, szStatusString, s_nStatusLen);
  255. m_szStatusString[s_nStatusLen] = 0;
  256. if ((++s_nStatusLen) > STRING_MAX_LEN)
  257. s_nStatusLen = STRING_MIN_LEN;
  258. }
  259. */
  260. int nTime = m_pCoreServerShell->GetLoopRate();
  261. int nHour, nMinute, nSecond, nMinSecond;
  262. #define defHOUR_TIME (3600 * 20)
  263. #define defMINUTE_TIME (60 * 20)
  264. #define defSECOND_TIME 20
  265. #define defMINSECOND_TIME 2
  266. nHour = nTime / defHOUR_TIME;
  267. nTime -= nHour * defHOUR_TIME;
  268. nMinute = nTime / defMINUTE_TIME;
  269. nTime -= nMinute * defMINUTE_TIME;
  270. nSecond = nTime / defSECOND_TIME;
  271. nTime -= nSecond * defSECOND_TIME;
  272. nMinSecond = nTime / defMINSECOND_TIME;
  273. sprintf(m_szStatusString, "Time:(%3d:%3d:%3d.%d)", nHour, nMinute, nSecond, nMinSecond);
  274. int nClientCount = -1;
  275. KCoreConnectInfo Info;
  276. nClientCount = m_pServer->GetClientCount();
  277. m_pCoreServerShell->GetConnectInfo(&Info);
  278. sprintf(m_szInformation, "ClientNum:%d PlayerNum:%d", nClientCount, Info.nNumPlayer);
  279. return true;
  280. }
  281. }
  282. return false;
  283. }
  284. void KSwordOnLineSever::SetUpdateInterval(int nInterval)
  285. {
  286. m_nUpdateInterval = nInterval;
  287. }
  288. void KSwordOnLineSever::MessageLoop()
  289. {
  290. DWORD dwNetTime = 0;
  291. DWORD dwProcTime = 0;
  292. const char* pChar = NULL;
  293. unsigned int nSize = 0;
  294. KTimer sTimer;
  295. _ASSERT(m_pCoreServerShell);
  296. g_PlayerRemoveList.Lock();
  297. KIndexNode* pNode = (KIndexNode *)g_PlayerRemoveList.GetHead();
  298. while (pNode)
  299. {
  300. KIndexNode* pTempNode = (KIndexNode *)pNode->GetNext();
  301. m_pCoreServerShell->ClientDisconnect(pNode->m_nIndex);
  302. pNode->Remove();
  303. if (pNode)
  304. {
  305. delete pNode;
  306. pNode = NULL;
  307. }
  308. pNode = pTempNode;
  309. }
  310. g_PlayerRemoveList.Unlock();
  311. for (int i = 0; i < m_nMaxPlayer; i++)
  312. {
  313. if (NET_UNCONNECT == GetNetStatus(i))
  314. continue;
  315. do
  316. {
  317. sTimer.Start();
  318. pChar = (const char*)m_pServer->GetPackFromClient(i, nSize);
  319. dwNetTime += sTimer.GetElapseFrequency();
  320. if (!pChar || 0 == nSize)
  321. break;
  322. sTimer.Start();
  323. if (!m_pCoreServerShell->CheckProtocolSize(pChar, nSize))
  324. break;
  325. MessageProcess(i, pChar, nSize);
  326. dwProcTime += sTimer.GetElapseFrequency();
  327. } while (1);
  328. }
  329. g_DebugLog("[FPS]Net:Proc(%d:%d)", dwNetTime, dwProcTime);
  330. }
  331. BOOL KSwordOnLineSever::ProcessLoginProtocol(int nClient, const char* pChar, int nSize)
  332. {
  333. const char* pBuffer = pChar;
  334. if (*pBuffer != c2s_login)
  335. {
  336. return FALSE;
  337. }
  338. else
  339. {
  340. strcpy(m_pPlayerData[nClient].szAccount, (const char *)pBuffer + 1);
  341. DBI_COMMAND Command = DBI_GETPLAYERLISTFROMACCOUNT;
  342. memset(m_pPlayerData[nClient].PlayerBaseInfo, 0, sizeof(S3DBI_RoleBaseInfo) * MAX_PLAYER_IN_ACCOUNT);
  343. void* pTmpPoint1 = m_pPlayerData[nClient].szAccount;
  344. void* pTmpPoint2 = &m_pPlayerData[nClient];
  345. g_AccessDBMsgList(DBMSG_PUSH, &nClient, (DBI_COMMAND*)&Command, (void **)&pTmpPoint1, (void **)&pTmpPoint2);
  346. return TRUE;
  347. }
  348. return FALSE;
  349. }
  350. void KSwordOnLineSever::SendPlayerListToClient(int nClient)
  351. {
  352. ROLE_LIST_SYNC PlayerListSync;
  353. PlayerListSync.ProtocolType = s2c_syncrolelist;
  354. for (int i = 0; i < MAX_PLAYER_IN_ACCOUNT; i++)
  355. {
  356. strcpy(PlayerListSync.m_RoleList[i].szName, m_pPlayerData[nClient].PlayerBaseInfo[i].szRoleName);
  357. PlayerListSync.m_RoleList[i].Sex = (BYTE)m_pPlayerData[nClient].PlayerBaseInfo[i].nSex;
  358. PlayerListSync.m_RoleList[i].ArmorType = (BYTE)m_pPlayerData[nClient].PlayerBaseInfo[i].nArmorType;
  359. PlayerListSync.m_RoleList[i].HelmType = (BYTE)m_pPlayerData[nClient].PlayerBaseInfo[i].nHelmType;
  360. PlayerListSync.m_RoleList[i].WeaponType = (BYTE)m_pPlayerData[nClient].PlayerBaseInfo[i].nWeaponType;
  361. PlayerListSync.m_RoleList[i].Level = (BYTE)m_pPlayerData[nClient].PlayerBaseInfo[i].nLevel;
  362. }
  363. m_pServer->PackDataToClient(nClient, &PlayerListSync, sizeof(PlayerListSync));
  364. }
  365. // return : -1 失败,协议不对  0 成功 1 协议正确,数据包错误  2 协议正确,校验错误 ……
  366. int KSwordOnLineSever::RecvClientPlayerSelect(int nClient, const char* pChar, int nSize, int *pnSel)
  367. {
  368. const char* pBuffer = pChar;
  369. if (*pBuffer != c2s_dbplayerselect)
  370. {
  371. return -1;
  372. }
  373. else
  374. {
  375. if (sizeof(DB_PLAYERSELECT_COMMAND) != nSize)
  376. return 1;
  377. DB_PLAYERSELECT_COMMAND* pCommand = (DB_PLAYERSELECT_COMMAND*)pBuffer;
  378. if (pCommand->m_nSelect >= 0 && pCommand->m_nSelect < m_pPlayerData[nClient].nRoleCount)
  379. {
  380. *pnSel = pCommand->m_nSelect;
  381. return 0;
  382. }
  383. else
  384. return 2;
  385. }
  386. }
  387. //--------------------------------------------------------------------------------
  388. // return : -1 失败,协议不对  0 成功 1 协议正确,数据包错误
  389. //          2 协议正确,校验错误1 同名  3 协议正确,校验错误2 角色数量已满 ……
  390. //--------------------------------------------------------------------------------
  391. int KSwordOnLineSever::RecvClientPlayerNew(int nClient, const char* pChar, int nSize, int *pnRole, int *pnSeries, char *lpszName)
  392. {
  393. //BYTE* pTemp = m_NetServer.Client_data_array[nClient].RecieveBuffer;
  394. const char* pBuffer = pChar;
  395. // BYTE* pBuffer = pTemp;
  396. if (*pBuffer != c2s_newplayer)
  397. {
  398. return -1;
  399. }
  400. else
  401. {
  402. NEW_PLAYER_COMMAND *pNew = (NEW_PLAYER_COMMAND*)pBuffer;
  403. if (pNew->m_btRoleNo > 1 || pNew->m_btSeries >= series_num)
  404. return 1;
  405. char szName[32];
  406. memset(szName, 0, sizeof(szName));
  407. memcpy(szName, pNew->m_szName, pNew->m_wLength - 2 - 2);
  408. // if CheckCanNew() 判断是否可以添加新角色 not end
  409. *pnRole = pNew->m_btRoleNo;
  410. *pnSeries = pNew->m_btSeries;
  411. strcpy(lpszName, szName);
  412. return 0;
  413. }
  414. }
  415. BOOL KSwordOnLineSever::ProcessSyncReplyProtocol(int nClient, const char* pChar, int nSize)
  416. {
  417. const char* pBuffer = pChar;
  418. if (*pBuffer != c2s_syncend)
  419. {
  420. return FALSE;
  421. }
  422. else
  423. {
  424. return TRUE;
  425. }
  426. return FALSE;
  427. }
  428. int KSwordOnLineSever::OperationRequest(unsigned int uOper, unsigned int uParam, int nParam)
  429. {
  430. if (m_pCoreServerShell)
  431. return m_pCoreServerShell->OperationRequest(uOper, uParam, nParam);
  432. return 0;
  433. }
  434. extern int  CORE_API g_ClearOutOfDateDBMsgInList(int nPlayerIndex);
  435. void KSwordOnLineSever::MainLoop()
  436. {
  437. DWORD dwNowTime;
  438. m_pServer->PreparePackSink();
  439. for (int i = 0; i < m_nMaxPlayer; i++)
  440. {
  441. if (NET_UNCONNECT == GetNetStatus(i))
  442. continue;
  443. m_pPlayerGameStatus[i].sMutex.Lock();
  444. switch(m_pPlayerGameStatus[i].nGameStatus)
  445. {
  446. case PLAYER_LISTLOADING:
  447. if (m_pCoreServerShell->IsCharacterListLoadFinished(i))
  448. {
  449. SendPlayerListToClient(i);
  450. m_pnPlayerIndex[i] = 0;
  451. m_pPlayerGameStatus[i].nGameStatus = PLAYER_DBLOADING;
  452. g_DebugLog("[TRACE]Send List to Client:%d", i);
  453. }
  454. break;
  455. case PLAYER_NEWLOADING:
  456. if (m_pnPlayerIndex[i] > 0)
  457. {
  458. int bSyncEnd = (m_pPlayerData[i].nStep == STEP_SYNC_END);
  459. m_pCoreServerShell->NewPlayerIniLoading(m_pnPlayerIndex[i],
  460. bSyncEnd, m_pPlayerData[i].nStep, m_pPlayerData[i].nParam);
  461. if (bSyncEnd)
  462. {
  463. m_pPlayerGameStatus[i].nGameStatus = PLAYER_SYNC_REPLY;
  464. BYTE SyncEnd;
  465. SyncEnd = s2c_syncend;
  466. m_pServer->PackDataToClient(i, &SyncEnd, sizeof(BYTE));
  467. m_pCoreServerShell->SaveNewRole(m_pnPlayerIndex[i]);
  468. g_DebugLog("[TRACE]SyncEnd:%d", i);
  469. }
  470. }
  471. break;
  472. case PLAYER_DBLOADING:
  473. /* dwNowTime = m_Timer.GetElapse();
  474. if (dwNowTime - m_pPlayerGameStatus[i].dwLastOperationTime > 600000)
  475. {
  476. m_pServer->ShutdownClient(i);
  477. break;
  478. }*/
  479. if (m_pnPlayerIndex[i] > 0)
  480. {
  481. int bSyncEnd = (m_pPlayerData[i].nStep == STEP_SYNC_END);
  482. m_pCoreServerShell->PlayerDbLoading(m_pnPlayerIndex[i],
  483. bSyncEnd, m_pPlayerData[i].nStep, m_pPlayerData[i].nParam);
  484. if (bSyncEnd)
  485. {
  486. m_pPlayerGameStatus[i].dwLastOperationTime = m_Timer.GetElapse();
  487. m_pPlayerGameStatus[i].nGameStatus = PLAYER_SYNC_REPLY;
  488. BYTE SyncEnd;
  489. SyncEnd = s2c_syncend;
  490. m_pServer->PackDataToClient(i, &SyncEnd, sizeof(BYTE));
  491. g_DebugLog("[TRACE]SyncEnd:%d", i);
  492. }
  493. }
  494. break;
  495. /* case PLAYER_SYNC_REPLY:
  496. dwNowTime = m_Timer.GetElapse();
  497. if (dwNowTime - m_pPlayerGameStatus[i].dwLastOperationTime > 600000)
  498. {
  499. m_pServer->ShutdownClient(i);
  500. break;
  501. }
  502. break;*/
  503. default:
  504. break;
  505. }
  506. m_pPlayerGameStatus[i].sMutex.Unlock();
  507. }
  508. m_pCoreServerShell->Breathe();
  509. m_pServer->SendPackToClient();
  510. m_GameLoop++;
  511. }
  512. void KSwordOnLineSever::MessageProcess(const unsigned long i, const char* pChar, size_t nSize)
  513. {
  514. _ASSERT(pChar && nSize);
  515. m_pPlayerGameStatus[i].sMutex.Lock();
  516. switch(m_pPlayerGameStatus[i].nGameStatus)
  517. {
  518. case PLAYER_PLAYING:
  519. m_pCoreServerShell->ProcessClientMessage(i, pChar, nSize);
  520. break;
  521. case PLAYER_BEGIN:
  522. if (ProcessLoginProtocol(i, pChar, nSize))
  523. {
  524. m_pPlayerGameStatus[i].nGameStatus = PLAYER_LISTLOADING;
  525. m_pPlayerGameStatus[i].dwLastOperationTime = m_Timer.GetElapse();
  526. strcpy(m_szStatusString, "A player apply login.");
  527. g_DebugLog("[Login]%s", m_szStatusString);
  528. }
  529. break;
  530. case PLAYER_DBLOADING:
  531. {
  532. int nRet, nSel;
  533. int nRole, nSeries;
  534. char szName[32];
  535. if( (nRet = RecvClientPlayerSelect(i, pChar, nSize, &nSel)) != -1 )
  536. {
  537. if (nRet == 0) // 成功
  538. {
  539. g_DebugLog("[TRACE]Recv role select:%d", nSel);
  540. m_pPlayerGameStatus[i].dwLastOperationTime = m_Timer.GetElapse();
  541. m_pnPlayerIndex[i] = m_pCoreServerShell->AddCharacter(
  542. m_pPlayerData[i].PlayerBaseInfo[nSel].szRoleName, i, m_pPlayerData[i].szAccount);
  543. strcpy(m_szStatusString, "A player Selected character.");
  544. g_DebugLog("[TRACE]%s", m_szStatusString);
  545. if (m_pnPlayerIndex[i] > 0)
  546. {
  547. int bSyncEnd = (m_pPlayerData[i].nStep == STEP_SYNC_END);
  548. m_pCoreServerShell->PlayerDbLoading(m_pnPlayerIndex[i],
  549. bSyncEnd, m_pPlayerData[i].nStep, m_pPlayerData[i].nParam);
  550. if (bSyncEnd)
  551. {
  552. m_pPlayerGameStatus[i].nGameStatus = PLAYER_SYNC_REPLY;
  553. BYTE SyncEnd;
  554. SyncEnd = s2c_syncend;
  555. m_pServer->SendData(i, &SyncEnd, sizeof(BYTE));
  556. g_DebugLog("[TRACE]SyncEnd:%d", i);
  557. }
  558. }
  559. }
  560. else // 失败
  561. {
  562. }
  563. }
  564. else if ( (nRet = RecvClientPlayerNew(i, pChar, nSize, &nRole, &nSeries, szName)) != -1 )
  565. {
  566. if (nRet == 0) // 成功
  567. {
  568. m_pPlayerGameStatus[i].dwLastOperationTime = m_Timer.GetElapse();
  569. m_pnPlayerIndex[i] = m_pCoreServerShell->CreateNewPlayer(i, nRole, nSeries, szName, m_pPlayerData[i].szAccount);
  570. strcpy(m_szStatusString, "Create a new role.");
  571. if (m_pnPlayerIndex[i] > 0)
  572. {
  573. m_pPlayerData[i].nStep = 0;
  574. m_pPlayerData[i].nParam = 0;
  575. m_pPlayerGameStatus[i].nGameStatus = PLAYER_NEWLOADING;
  576. m_pCoreServerShell->NewPlayerIniLoading(m_pnPlayerIndex[i],
  577. FALSE, m_pPlayerData[i].nStep, m_pPlayerData[i].nParam);
  578. }
  579. }
  580. else if (nRet == 1) // 数据包错
  581. {
  582. }
  583. else if (nRet == 2) // 同名 错
  584. {
  585. }
  586. else if (nRet == 3) // 角色数量已满 错
  587. {
  588. }
  589. }
  590. }
  591. break;
  592. case PLAYER_SYNC_REPLY:
  593. if (ProcessSyncReplyProtocol(i, pChar, nSize))
  594. {
  595. m_pCoreServerShell->PlayerSyncReplay(m_pnPlayerIndex[i]);
  596. m_pPlayerGameStatus[i].nGameStatus = PLAYER_PLAYING;
  597. g_DebugLog("[TRACE]Socket %d playing", i);
  598. }
  599. break;
  600. default:
  601. break;
  602. }
  603. m_pPlayerGameStatus[i].sMutex.Unlock();
  604. }
  605. void KSwordOnLineSever::SetNetStatus(const unsigned long lnID, NetStatus nStatus)
  606. {
  607. if (lnID >= m_nMaxPlayer)
  608. return;
  609. // m_pNetConnectStatus[lnID].sMutex.Lock();
  610. if (nStatus == NET_UNCONNECT)
  611. {
  612. g_ClearOutOfDateDBMsgInList(lnID);
  613. // int nIndex = m_pCoreServerShell->GetPlayerIndex(lnID);
  614. KIndexNode* pNode = new KIndexNode;
  615. pNode->m_nIndex = lnID;
  616. g_PlayerRemoveList.Lock();
  617. g_PlayerRemoveList.AddTail(pNode);
  618. g_PlayerRemoveList.Unlock();
  619. }
  620. if (nStatus == NET_CONNECTED)
  621. {
  622. m_pPlayerGameStatus[lnID].sMutex.Lock();
  623. m_pPlayerGameStatus[lnID].nGameStatus = PLAYER_BEGIN;
  624. m_pnPlayerIndex[lnID] = 0;
  625. m_pPlayerGameStatus[lnID].sMutex.Unlock();
  626. }
  627. ::InterlockedExchange( &( m_pNetConnectStatus[lnID].nNetStatus ), nStatus );
  628. // m_pNetConnectStatus[lnID].nNetStatus = nStatus;
  629. // m_pNetConnectStatus[lnID].sMutex.Unlock();
  630. }
  631. int KSwordOnLineSever::GetNetStatus(const unsigned long lnID)
  632. {
  633. if (lnID >= m_nMaxPlayer)
  634. return NET_UNCONNECT;
  635. int nStatus;
  636. // m_pNetConnectStatus[lnID].sMutex.Lock();
  637. nStatus = ::InterlockedExchange( &( m_pNetConnectStatus[lnID].nNetStatus ), 
  638. m_pNetConnectStatus[lnID].nNetStatus );
  639. //nStatus = m_pNetConnectStatus[lnID].nNetStatus;
  640. // m_pNetConnectStatus[lnID].sMutex.Unlock();
  641. return nStatus;
  642. }