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

模拟服务器

开发平台:

C/C++

  1. #include "StdAfx.h"
  2. #ifdef _STANDALONE
  3. #ifndef __linux
  4. #include <winsock2.h>
  5. #include <malloc.h>
  6. #else
  7. #include <unistd.h>
  8. #include <netdb.h>
  9. #include <sys/time.h>
  10. #endif
  11. #include "KWin32.h"
  12. #include "package.h"
  13. #endif
  14. #include "CRC32.h"
  15. #ifdef _STANDALONE
  16. #include "KCore.h"
  17. #include "KPlayerSet.h"
  18. #include "CRC32.c"
  19. #endif
  20. #ifndef WIN32
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <sys/ioctl.h>
  24. #include <netinet/in.h>
  25. #include <net/if.h>
  26. #include <arpa/inet.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #endif
  30. #define _SERVER
  31. #ifdef _STANDALONE
  32. // #ifdef WIN32
  33. // #define FAILED(x) ((x) != S_OK)
  34. // #else
  35. #define FAILED(x) ((x) <= 0)
  36. // #endif
  37. #endif
  38. #ifndef _STANDALONE
  39. #include <crtdbg.h>
  40. #include <objbase.h>
  41. #include <initguid.h>
  42. #include "Library.h"
  43. #include "inoutmac.h"
  44. #endif
  45. #include <iostream>
  46. //#include <strstream>
  47. #include "S3DBInterface.h"
  48. #include "KProtocolDef.h"
  49. #include "KProtocol.h"
  50. #include "KRelayProtocol.h"
  51. #include "KTongProtocol.h"
  52. #include "KSOServer.h"
  53. #ifndef _STANDALONE
  54. using OnlineGameLib::Win32::CBuffer;
  55. using OnlineGameLib::Win32::CLibrary;
  56. using OnlineGameLib::Win32::CCriticalSection;
  57. CLibrary g_theHeavenLibrary( "heaven.dll" );
  58. CLibrary g_theRainbowLibrary( "rainbow.dll" );
  59. OnlineGameLib::Win32::CUsesWinsock  g_SocketInit;
  60. CCriticalSection g_csFlow;
  61. #else
  62. ZMutex g_mutexFlow;
  63. #endif
  64. CPackager m_theRecv;
  65. CPackager m_theSend;
  66. #define GAME_FPS 18
  67. using namespace std;
  68. //const int KSwordOnLineSever::m_snMaxPlayerCount = 500;
  69. //const int KSwordOnLineSever::m_snPrecision = 10;
  70. const int KSwordOnLineSever::m_snMaxBuffer = 10;
  71. const int KSwordOnLineSever::m_snBufferSize = 1024 * 16;
  72. KSwordOnLineSever g_SOServer;
  73. enum PLAYER_GAME_STATUS
  74. {
  75. enumPlayerBegin = 0,
  76. enumPlayerSyncEnd,
  77. enumPlayerPlaying,
  78. enumPlayerExchangingServer,
  79. };
  80. enum PLAYER_EXCHANGE_STATUS
  81. {
  82. enumExchangeBegin = 0,
  83. enumExchangeSearchingWay,
  84. enumExchangeWaitForGameSvrRespone,
  85. enumExchangeCleaning,
  86. };
  87. int g_nTongPCSize[defTONG_PROTOCOL_CLIENT_NUM] = 
  88. {
  89. -1, // enumS2C_TONG_CREATE_SUCCESS
  90. sizeof(STONG_CREATE_FAIL_SYNC), // enumS2C_TONG_CREATE_FAIL
  91. sizeof(STONG_ADD_MEMBER_SUCCESS_SYNC), // enumS2C_TONG_ADD_MEMBER_SUCCESS
  92. sizeof(STONG_ADD_MEMBER_FAIL_SYNC), // enumS2C_TONG_ADD_MEMBER_FAIL
  93. -1, // enumS2C_TONG_HEAD_INFO
  94. -1, // enumS2C_TONG_MANAGER_INFO
  95. -1, // enumS2C_TONG_MEMBER_INFO
  96. sizeof(STONG_BE_INSTATED_SYNC), // enumS2C_TONG_BE_INSTATED
  97. sizeof(STONG_INSTATE_SYNC), // enumS2C_TONG_INSTATE
  98. sizeof(STONG_KICK_SYNC), // enumS2C_TONG_KICK
  99. sizeof(STONG_BE_KICKED_SYNC), // enumS2C_TONG_BE_KICKED
  100. sizeof(STONG_LEAVE_SYNC), // enumS2C_TONG_LEAVE
  101. sizeof(STONG_CHECK_GET_MASTER_POWER_SYNC), // enumS2C_TONG_CHECK_CHANGE_MASTER_POWER
  102. sizeof(STONG_CHANGE_MASTER_FAIL_SYNC), // enumS2C_TONG_CHANGE_MASTER_FAIL
  103. sizeof(STONG_CHANGE_AS_SYNC), // enumS2C_TONG_CHANGE_AS
  104. sizeof(STONG_CHANGE_MASTER_SYNC), // enumS2C_TONG_CHANGE_MASTER
  105. sizeof(STONG_LOGIN_DATA_SYNC), // enumS2C_TONG_LOGIN_DATA
  106. };
  107. #ifndef _STANDALONE
  108. typedef HRESULT ( __stdcall * pfnCreateClientInterface )( 
  109. REFIID riid, 
  110. void **ppv 
  111. );
  112. typedef HRESULT ( __stdcall * pfnCreateServerInterface )(
  113. REFIID riid,
  114. void **ppv
  115. );
  116. #endif
  117. #ifndef _STANDALONE
  118. void __stdcall ServerEventNotify(
  119. #else
  120. void ServerEventNotify(  
  121. #endif
  122. LPVOID lpParam,
  123. const unsigned long &ulnID,
  124. const unsigned long &ulnEventType )
  125. {
  126. #ifndef _STANDALONE
  127. CCriticalSection::Owner locker( g_csFlow );
  128. #else
  129. g_mutexFlow.lock();
  130. #endif
  131. switch( ulnEventType )
  132. {
  133. case enumClientConnectCreate:
  134. g_SOServer.SetNetStatus(ulnID, enumNetConnected);
  135. break;
  136. case enumClientConnectClose:
  137. g_SOServer.SetNetStatus(ulnID, enumNetUnconnect);
  138. break;
  139. }
  140. #ifdef _STANDALONE
  141. g_mutexFlow.unlock();
  142. #endif
  143. }
  144. #ifndef _STANDALONE
  145. void __stdcall GatewayClientEventNotify(
  146. #else
  147. void GatewayClientEventNotify(
  148. #endif
  149. LPVOID lpParam,
  150. const unsigned long &ulnEventType )
  151. {
  152. switch( ulnEventType )
  153. {
  154. case enumServerConnectCreate:
  155. break;
  156. case enumServerConnectClose:
  157. printf("GateWay lostn");
  158. g_SOServer.SetRunningStatus(FALSE);
  159. break;
  160. }
  161. }
  162. #ifndef _STANDALONE
  163. void __stdcall ChatClientEventNotify(
  164. #else
  165. void ChatClientEventNotify(
  166. #endif
  167. LPVOID lpParam,
  168. const unsigned long &ulnEventType )
  169. {
  170. switch( ulnEventType )
  171. {
  172. case enumServerConnectCreate:
  173. break;
  174. case enumServerConnectClose:
  175. printf("Chat disconnectn");
  176. break;
  177. }
  178. }
  179. #ifndef _STANDALONE
  180. void __stdcall TongClientEventNotify(
  181. #else
  182. void TongClientEventNotify(
  183. #endif
  184. LPVOID lpParam,
  185. const unsigned long &ulnEventType )
  186. {
  187. switch( ulnEventType )
  188. {
  189. case enumServerConnectCreate: // 连接建立时候的处理
  190. break;
  191. case enumServerConnectClose: // 连接段开时候的处理
  192. printf("Tong disconnectn");
  193. break;
  194. }
  195. }
  196. #ifndef _STANDALONE
  197. void __stdcall DatabaseClientEventNotify(
  198. #else
  199. void DatabaseClientEventNotify(
  200. #endif
  201. LPVOID lpParam,
  202. const unsigned long &ulnEventType )
  203. {
  204. switch( ulnEventType )
  205. {
  206. case enumServerConnectCreate:
  207. break;
  208. case enumServerConnectClose:
  209. printf("DataBase lostn");
  210. g_SOServer.SetRunningStatus(FALSE);
  211. break;
  212. }
  213. }
  214. #ifndef _STANDALONE
  215. void __stdcall TransferClientEventNotify(
  216. #else
  217. void TransferClientEventNotify(
  218. #endif
  219. LPVOID lpParam,
  220. const unsigned long &ulnEventType )
  221. {
  222. switch( ulnEventType )
  223. {
  224. case enumServerConnectCreate:
  225. break;
  226. case enumServerConnectClose:
  227. /// g_SOServer.SetRunningStatus(FALSE);
  228. break;
  229. }
  230. }
  231. KSwordOnLineSever::KSwordOnLineSever()
  232. {
  233. m_nGameLoop = 0;
  234. m_bIsRunning = TRUE;
  235. m_nServerPort = 6666;
  236. m_nGatewayPort = 5632;
  237. m_nDatabasePort = 5001;
  238. m_nTransferPort = 5003;
  239. m_nChatPort = 5004;
  240. m_nTongPort = 5005;
  241. ZeroMemory(m_szGatewayIP, sizeof(m_szGatewayIP));
  242. ZeroMemory(m_szDatabaseIP, sizeof(m_szDatabaseIP));
  243. ZeroMemory(m_szTransferIP, sizeof(m_szTransferIP));
  244. ZeroMemory(m_szChatIP, sizeof(m_szChatIP));
  245. ZeroMemory(m_szTongIP, sizeof(m_szTongIP));
  246. m_pServer = NULL;
  247. m_pGatewayClient = NULL;
  248. m_pDatabaseClient = NULL;
  249. m_pTransferClient = NULL;
  250. m_pChatClient = NULL;
  251. m_pTongClient = NULL;
  252. m_pCoreServerShell = NULL;
  253. m_pGameStatus = NULL;
  254. }
  255. KSwordOnLineSever::~KSwordOnLineSever()
  256. {
  257. }
  258. BOOL KSwordOnLineSever::Init()
  259. {
  260. /*
  261.  * Initialize class member
  262.  */
  263. m_bIsRunning = TRUE;
  264. g_SetRootPath(NULL);
  265. g_SetFilePath("\");
  266. KIniFile iniFile;
  267. iniFile.Load("ServerCfg.ini");
  268. iniFile.GetInteger("GameServer", "Port", 6666, &m_nServerPort);
  269. extern int g_nPort;
  270. if (g_nPort)
  271. m_nServerPort = g_nPort;
  272. iniFile.GetString("Gateway", "Ip", "192.168.26.1", m_szGatewayIP, sizeof(m_szGatewayIP));
  273. iniFile.GetInteger("Gateway", "Port", 5632, &m_nGatewayPort);
  274. iniFile.GetString("Database", "Ip", "192.168.22.104", m_szDatabaseIP, sizeof(m_szDatabaseIP));
  275. iniFile.GetInteger("Database", "Port", 5001, &m_nDatabasePort);
  276. iniFile.GetString("Transfer", "Ip", "192.168.22.105", m_szTransferIP, sizeof(m_szTransferIP));
  277. iniFile.GetInteger("Transfer", "Port", 5003, &m_nTransferPort);
  278. iniFile.GetString("Chat", "Ip", "192.168.22.105", m_szChatIP, sizeof(m_szChatIP));
  279. iniFile.GetInteger("Chat", "Port", 5004, &m_nChatPort);
  280. iniFile.GetString("Tong", "Ip", "192.168.22.105", m_szTongIP, sizeof(m_szTongIP));
  281. iniFile.GetInteger("Tong", "Port", 5005, &m_nTongPort);
  282. #ifdef WIN32
  283. iniFile.GetInteger("Overload", "MaxPlayer", 450, &m_nMaxPlayerCount);
  284. iniFile.GetInteger("Overload", "Precision", 20, &m_nPrecision);
  285. #else
  286. iniFile.GetInteger("Overload", "MaxPlayer", 1000, &m_nMaxPlayerCount);
  287. iniFile.GetInteger("Overload", "Precision", 200, &m_nPrecision);
  288. #endif
  289. m_nMaxPlayer = m_nMaxPlayerCount + m_nPrecision;
  290. if (m_nMaxPlayer <= 0)
  291. {
  292. cout << "Maximal player number <= 0!" << endl;
  293. return FALSE;
  294. }
  295. if (!m_pGameStatus)
  296. {
  297. m_pGameStatus = new GameStatus[m_nMaxPlayer];
  298. }
  299. // strcpy(m_szGameSvrIP, OnlineGameLib::Win32::net_ntoa(dwIp));
  300. /*
  301.  * Open this server to player
  302.  */
  303. #ifndef _STANDALONE
  304. pfnCreateServerInterface pFactroyFun = ( pfnCreateServerInterface )( g_theHeavenLibrary.GetProcAddress( "CreateInterface" ) );
  305. IServerFactory *pServerFactory = NULL;
  306. if ( pFactroyFun && SUCCEEDED( pFactroyFun( IID_IServerFactory, reinterpret_cast< void ** >( &pServerFactory ) ) ) )
  307. {
  308. pServerFactory->SetEnvironment( m_nMaxPlayer, m_nPrecision, m_snMaxBuffer, m_snBufferSize  );
  309. pServerFactory->CreateServerInterface( IID_IIOCPServer, reinterpret_cast< void ** >( &m_pServer ) );
  310. pServerFactory->Release();
  311. }
  312. else
  313. {
  314. return FALSE;
  315. }
  316. #else
  317. m_pServer = new IServer(m_nMaxPlayer, 4, 200 * 1024);
  318. #endif
  319. if (!m_pServer)
  320. {
  321. cout << "Initialization failed! Don't find a correct heaven.dll" << endl;
  322. return FALSE;
  323. }
  324. m_pServer->Startup();
  325. m_pServer->RegisterMsgFilter( reinterpret_cast< void * >( m_pServer ), ServerEventNotify );
  326. if ( FAILED( m_pServer->OpenService( INADDR_ANY, m_nServerPort ) ) )
  327. {
  328. return FALSE;
  329. }
  330. if (!GetLocalIpAddress(&m_dwIntranetIp, &m_dwInternetIp))
  331. {
  332. cout << "Can't get server ip" << endl;
  333. return FALSE;
  334. }
  335. cout << m_dwIntranetIp << " : " << m_dwInternetIp << endl;
  336. #ifndef _STANDALONE
  337. IServer *pCloneServer = NULL;
  338. m_pServer->QueryInterface( IID_IIOCPServer, ( void ** )&pCloneServer );
  339. #else 
  340. IServer *pCloneServer = m_pServer;
  341. #endif
  342. /*
  343.  * Init GameWorld
  344.  */
  345. if (m_pCoreServerShell == NULL)
  346. m_pCoreServerShell = ::CoreGetServerShell();
  347. if (m_pCoreServerShell == NULL)
  348. {
  349. cout << "Failed to Create CoreShell." << endl;
  350. return FALSE;
  351. }
  352. m_pCoreServerShell->OperationRequest(SSOI_LAUNCH, (unsigned int)pCloneServer, 0);
  353. /*
  354.  * Connect to database
  355.  */
  356. #ifndef _STANDALONE
  357. pfnCreateClientInterface pClientFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  358. IClientFactory *pClientFactory = NULL;
  359. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  360. {
  361. pClientFactory->SetEnvironment( 1024 * 128 );
  362. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pDatabaseClient ) );
  363. pClientFactory->Release();
  364. }
  365. else
  366. {
  367. return FALSE;
  368. }
  369. #else
  370. // net_buffer = new ZBuffer(20240000, 400);
  371. m_pDatabaseClient = new IClient(6000*1024, 6000*1024);
  372. #endif
  373. if ( !m_pDatabaseClient )
  374. {
  375. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  376. return FALSE;
  377. }
  378. if (FAILED(m_pDatabaseClient->Startup()))
  379. {
  380. cout << "Can't not startup database client service!" << endl;
  381. return FALSE;
  382. }
  383. cout << "Database IP : " << m_szDatabaseIP << " - Port : " << m_nDatabasePort << endl;
  384. m_pDatabaseClient->RegisterMsgFilter( reinterpret_cast< void * >( m_pDatabaseClient ), DatabaseClientEventNotify );
  385. if ( FAILED( m_pDatabaseClient->ConnectTo( m_szDatabaseIP, m_nDatabasePort ) ) )
  386. {
  387. cout << "Connect to database is failed!" << endl;
  388. return FALSE;
  389. }
  390. cout << "Connect database successful!" << endl;
  391. /*
  392.  * Connect to transfer
  393.  */
  394. #ifndef _STANDALONE
  395. pClientFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  396. pClientFactory = NULL;
  397. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  398. {
  399. pClientFactory->SetEnvironment( 1024 * 512 );
  400. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pTransferClient ) );
  401. pClientFactory->Release();
  402. }
  403. else
  404. {
  405. return FALSE;
  406. }
  407. #else
  408. m_pTransferClient = new IClient(6000*1024, 6000*1024);
  409. #endif
  410. if ( !m_pTransferClient )
  411. {
  412. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  413. return FALSE;
  414. }
  415. if (FAILED(m_pTransferClient->Startup()))
  416. {
  417. cout << "Can't not startup transfer client service!" << endl;
  418. return FALSE;
  419. }
  420. cout << "Transfer IP : " << m_szTransferIP << " - Port : " << m_nTransferPort << endl;
  421. m_pTransferClient->RegisterMsgFilter( reinterpret_cast< void * >( m_pTransferClient ), TransferClientEventNotify );
  422. if ( FAILED( m_pTransferClient->ConnectTo( m_szTransferIP, m_nTransferPort ) ) )
  423. {
  424. cout << "Connect to transfer is failed!" << endl;
  425. if (m_pTransferClient)
  426. {
  427. m_pTransferClient->Shutdown();
  428. m_pTransferClient->Cleanup();
  429. m_pTransferClient->Release();
  430. m_pTransferClient = NULL;
  431. }
  432. }
  433. else
  434. {
  435. cout << "Connect transfer successful!" << endl;
  436. }
  437. /*
  438.  * Connect to chat
  439.  */
  440. #ifndef _STANDALONE
  441. pClientFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  442. pClientFactory = NULL;
  443. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  444. {
  445. pClientFactory->SetEnvironment( 1024 * 1024 * 4 );
  446. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pChatClient ) );
  447. pClientFactory->Release();
  448. }
  449. else
  450. {
  451. return FALSE;
  452. }
  453. #else
  454. m_pChatClient = new IClient(6000*1024, 6000*1024);
  455. #endif
  456. if ( !m_pChatClient )
  457. {
  458. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  459. return FALSE;
  460. }
  461. if (FAILED(m_pChatClient->Startup()))
  462. {
  463. cout << "Can't not startup chat client service!" << endl;
  464. return FALSE;
  465. }
  466. cout << "Chat IP : " << m_szChatIP << " - Port : " << m_nChatPort << endl;
  467. m_pChatClient->RegisterMsgFilter( reinterpret_cast< void * >( m_pChatClient ), ChatClientEventNotify );
  468. if ( FAILED( m_pChatClient->ConnectTo( m_szChatIP, m_nChatPort ) ) )
  469. {
  470. cout << "Connect to chat is failed!" << endl;
  471. if (m_pChatClient)
  472. {
  473. m_pChatClient->Shutdown();
  474. m_pChatClient->Cleanup();
  475. m_pChatClient->Release();
  476. m_pChatClient = NULL;
  477. }
  478. }
  479. else
  480. {
  481. cout << "Connect chat successful!" << endl;
  482. }
  483. //------------------------ Connect to tong ----------------------------
  484. #ifndef _STANDALONE
  485. pClientFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  486. pClientFactory = NULL;
  487. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  488. {
  489. pClientFactory->SetEnvironment( 1024 * 1024 * 4 );
  490. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pTongClient ) );
  491. pClientFactory->Release();
  492. }
  493. else
  494. {
  495. return FALSE;
  496. }
  497. #else
  498. m_pTongClient = new IClient(6000*1024, 6000*1024);
  499. #endif
  500. if ( !m_pTongClient )
  501. {
  502. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  503. return FALSE;
  504. }
  505. if (FAILED(m_pTongClient->Startup()))
  506. {
  507. cout << "Can't not startup tong client service!" << endl;
  508. return FALSE;
  509. }
  510. cout << "Tong IP : " << m_szTongIP << " - Port : " << m_nTongPort << endl;
  511. m_pTongClient->RegisterMsgFilter( reinterpret_cast< void * >( m_pTongClient ), TongClientEventNotify );
  512. if ( FAILED( m_pTongClient->ConnectTo( m_szTongIP, m_nTongPort ) ) )
  513. {
  514. cout << "Connect to tong is failed!" << endl;
  515. if (m_pTongClient)
  516. {
  517. m_pTongClient->Shutdown();
  518. m_pTongClient->Cleanup();
  519. m_pTongClient->Release();
  520. m_pTongClient = NULL;
  521. }
  522. }
  523. else
  524. {
  525. cout << "Connect tong successful!" << endl;
  526. }
  527. //------------------------ Connect to tong end ----------------------------
  528. /*
  529.  * Connect to gateway
  530.  */
  531. #ifndef _STANDALONE
  532. pClientFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  533. pClientFactory = NULL;
  534. if ( pClientFactroyFun && SUCCEEDED( pClientFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  535. {
  536. pClientFactory->SetEnvironment( 1024 * 1024 );
  537. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &m_pGatewayClient ) );
  538. pClientFactory->Release();
  539. }
  540. else
  541. {
  542. return FALSE;
  543. }
  544. #else
  545. m_pGatewayClient = new IClient(6000*1024, 6000*1024);
  546. #endif
  547. if ( !m_pGatewayClient )
  548. {
  549. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  550. return FALSE;
  551. }
  552. if ( FAILED(m_pGatewayClient->Startup()))
  553. {
  554. cout << "Can't not startup gateway client service!" << endl;
  555. return FALSE;
  556. }
  557. cout << "Gateway IP : " << m_szGatewayIP << " - Port : " << m_nGatewayPort << endl;
  558. m_pGatewayClient->RegisterMsgFilter( reinterpret_cast< void * >( m_pGatewayClient ), GatewayClientEventNotify );
  559. if ( FAILED( m_pGatewayClient->ConnectTo( m_szGatewayIP, m_nGatewayPort ) ) )
  560. {
  561. cout << "Connect to gateway is failed!" << endl;
  562. return FALSE;
  563. }
  564. cout << "Connect gateway successful!" << endl;
  565. /*
  566.  * Game timer start
  567.  */
  568. m_Timer.Start();
  569. m_nGameLoop = 0;
  570. return TRUE;
  571. }
  572. void KSwordOnLineSever::Release()
  573. {
  574. #ifndef _STANDALONE
  575. CCriticalSection::Owner locker( g_csFlow );
  576. #else
  577. g_mutexFlow.lock();
  578. #endif
  579. if (m_pServer)
  580. {
  581. // Close service for no one can login again
  582. m_pServer->CloseService();
  583. // Regist callback function to null
  584. m_pServer->RegisterMsgFilter( reinterpret_cast< void * >( m_pServer ), NULL );
  585. // Shut down all client and save their data.
  586. const char *pChar = NULL;
  587. ExitAllPlayer();
  588. }
  589. if (m_pCoreServerShell)
  590. {
  591. m_pCoreServerShell->Release();
  592. m_pCoreServerShell = NULL;
  593. }
  594. if (m_pGameStatus)
  595. {
  596. delete [] m_pGameStatus;
  597. m_pGameStatus = NULL;
  598. }
  599. if (m_pServer)
  600. {
  601. m_pServer->Cleanup();
  602. m_pServer->Release();
  603. #ifndef _STANDALONE
  604. m_pServer = NULL;
  605. #else
  606. delete m_pServer; m_pServer = NULL;
  607. #endif
  608. }
  609. if (m_pGatewayClient)
  610. {
  611. m_pGatewayClient->Shutdown();
  612. m_pGatewayClient->Cleanup();
  613. m_pGatewayClient->Release();
  614. m_pGatewayClient = NULL;
  615. }
  616. if (m_pDatabaseClient)
  617. {
  618. m_pDatabaseClient->Shutdown();
  619. m_pDatabaseClient->Cleanup();
  620. m_pDatabaseClient->Release();
  621. m_pDatabaseClient = NULL;
  622. }
  623. if (m_pTransferClient)
  624. {
  625. m_pTransferClient->Shutdown();
  626. m_pTransferClient->Cleanup();
  627. m_pTransferClient->Release();
  628. m_pTransferClient = NULL;
  629. }
  630. if (m_pChatClient)
  631. {
  632. m_pChatClient->Shutdown();
  633. m_pChatClient->Cleanup();
  634. m_pChatClient->Release();
  635. m_pChatClient = NULL;
  636. }
  637. if (m_pTongClient)
  638. {
  639. m_pTongClient->Shutdown();
  640. m_pTongClient->Cleanup();
  641. m_pTongClient->Release();
  642. m_pTongClient = NULL;
  643. }
  644. IP2CONNECTUNIT::iterator it;
  645. for (it = m_mapIp2TransferUnit.begin(); it != m_mapIp2TransferUnit.end(); it++)
  646. {
  647. KTransferUnit* pUnit = (*it).second;
  648. if (pUnit)
  649. {
  650. delete pUnit;
  651. pUnit = NULL;
  652. }
  653. }
  654. m_mapIp2TransferUnit.clear();
  655. #ifdef _STANDALONE
  656. // delete net_buffer;
  657. delete m_pTransferClient; m_pTransferClient = NULL;
  658. delete m_pDatabaseClient; m_pDatabaseClient = NULL;
  659. delete m_pGatewayClient; m_pGatewayClient = NULL;
  660. #endif
  661. #ifdef _STANDALONE
  662. g_mutexFlow.unlock();
  663. #endif
  664. }
  665. BOOL KSwordOnLineSever::Breathe()
  666. {
  667. if (m_pCoreServerShell && m_bIsRunning)
  668. {
  669. MessageLoop();
  670. //printf("breathe..%un", m_Timer.GetElapse());
  671. if (m_nGameLoop * 1000 <= m_Timer.GetElapse() * GAME_FPS)
  672. {
  673. MainLoop();
  674. #ifdef WIN32
  675. Sleep(0);
  676. #else
  677. usleep(0);
  678. #endif
  679. #ifdef _STANDALONE
  680. if (m_nGameLoop % (GAME_FPS * 60) == 0) //一分钟一次
  681. {
  682. extern ZPerf g_sendPerf;
  683. extern ZPerf g_recvPerf;
  684. DWORD dwElapse = m_Timer.GetElapse();
  685. printf("GameLoop= %06d, Time= %02d:%02d:%02d, Svr(S: %dK R: %dK), Clt(S: %dK R: %dK), Online= %dn",
  686. m_nGameLoop,
  687. dwElapse / (1000 * 3600),
  688. (dwElapse / (1000 * 60)) % 60,
  689. (dwElapse / 1000) % 60,
  690. m_pServer->sendPerf.total_size / 1024,
  691. m_pServer->recvPerf.total_size / 1024,
  692. g_sendPerf.total_size / 1024,
  693. g_recvPerf.total_size / 1024,
  694. PlayerSet.GetOnlinePlayerCount());
  695. }
  696. #endif
  697. }
  698. else
  699. {
  700. #ifndef __linux //SwitchToThread();
  701. Sleep(1);
  702. #else
  703. usleep(1000);
  704. #endif
  705. #ifdef _STANDALONE
  706. void DoEveryThing(IServer* pIServer);
  707. DoEveryThing(m_pServer);
  708. #endif
  709. }
  710. return TRUE;
  711. }
  712. printf("main thread exitn");
  713. return FALSE;
  714. }
  715. void KSwordOnLineSever::MessageLoop()
  716. {
  717. int i;
  718. const char* pChar = NULL;
  719. unsigned int uSize = 0;
  720. #ifndef _STANDALONE
  721. CCriticalSection::Owner locker( g_csFlow );
  722. #else
  723. g_mutexFlow.lock();
  724. #endif
  725. while(m_pGatewayClient)
  726. {
  727. pChar = (const char*)m_pGatewayClient->GetPackFromServer(uSize);
  728. if (!pChar || 0 == uSize)
  729. break;
  730. GatewayMessageProcess(pChar, uSize);
  731. }
  732. while(m_pDatabaseClient)
  733. {
  734. pChar = (const char*)m_pDatabaseClient->GetPackFromServer(uSize);
  735. if (!pChar || 0 == uSize)
  736. break;
  737. DatabaseMessageProcess(pChar, uSize);
  738. }
  739. m_pCoreServerShell->SendNetMsgToTransfer(m_pTransferClient);
  740. while(m_pTransferClient)
  741. {
  742. pChar = (const char*)m_pTransferClient->GetPackFromServer(uSize);
  743. if (!pChar || 0 == uSize)
  744. break;
  745. TransferMessageProcess(pChar, uSize);
  746. }
  747. m_pCoreServerShell->SendNetMsgToChat(m_pChatClient);
  748. while(m_pChatClient)
  749. {
  750. pChar = (const char*)m_pChatClient->GetPackFromServer(uSize);
  751. if (!pChar || 0 == uSize)
  752. break;
  753. ChatMessageProcess(pChar, uSize);
  754. }
  755. m_pCoreServerShell->SendNetMsgToTong(m_pTongClient);
  756. while(m_pTongClient)
  757. {
  758. pChar = (const char*)m_pTongClient->GetPackFromServer(uSize);
  759. if (!pChar || 0 == uSize)
  760. break;
  761. TongMessageProcess(pChar, uSize);
  762. }
  763. pChar = NULL;
  764. uSize = 0;
  765. for (i = 0; i < m_nMaxPlayer; i++)
  766. {
  767. if (enumNetUnconnect == GetNetStatus(i))
  768. continue;
  769. while(m_pServer)
  770. {
  771. pChar = (const char*)m_pServer->GetPackFromClient(i, uSize);
  772. if (!pChar || 0 == uSize)
  773. break;
  774. //printf("player msg..n");
  775. PlayerMessageProcess(i, pChar, uSize);
  776. }
  777. }
  778. #ifdef _STANDALONE
  779. g_mutexFlow.unlock();
  780. #endif
  781. }
  782. void KSwordOnLineSever::ChatMessageProcess(const char *pChar, size_t nSize)
  783. {
  784. _ASSERT( pChar && nSize);
  785. #ifndef _STANDALONE
  786. BYTE cProtocol = CPackager::Peek( pChar );
  787. #else
  788. BYTE cProtocol = *(BYTE *)pChar;
  789. #endif
  790. switch (cProtocol)
  791. {
  792. case chat_relegate:
  793. {
  794. CHAT_RELEGATE* pCR = (CHAT_RELEGATE*)pChar;
  795. DWORD theID = 0;
  796. switch (pCR->TargetCls)
  797. {
  798. case tgtcls_team:
  799. case tgtcls_fac:
  800. theID = pCR->TargetID;
  801. break;
  802. case tgtcls_scrn:
  803. theID = m_pGameStatus[pCR->TargetID].nPlayerIndex;
  804. break;
  805. default: 
  806. return;
  807. }
  808. m_pCoreServerShell->GroupChat(
  809. m_pChatClient,
  810. pCR->nFromIP,
  811. pCR->nFromRelayID,
  812. pCR->channelid,
  813. pCR->TargetCls,
  814. theID,
  815. pCR + 1,
  816. pCR->routeDateLength);
  817. }
  818. break;
  819. case chat_groupman:
  820. ChatGroupMan(pChar, nSize);
  821. break;
  822. case chat_specman:
  823. ChatSpecMan(pChar, nSize);
  824. break;
  825. case chat_everyone:
  826. {{
  827. CHAT_EVERYONE* pCeo = (CHAT_EVERYONE*)pChar;
  828. m_pCoreServerShell->OperationRequest(SSOI_BROADCASTING, (unsigned int)(pCeo + 1), pCeo->wChatLength);
  829. }}
  830. break;
  831. default:
  832. break;
  833. }
  834. }
  835. void KSwordOnLineSever::TongMessageProcess(const char *pChar, size_t nSize)
  836. {
  837. if (!pChar)
  838. return;
  839. EXTEND_HEADER* pHeader = (EXTEND_HEADER*)pChar;
  840. if (pHeader->ProtocolFamily == pf_extend)
  841. {
  842. if (pHeader->ProtocolID == extend_s2c_passtosomeone)
  843. {
  844. EXTEND_PASSTOSOMEONE* pEps = (EXTEND_PASSTOSOMEONE*)pChar;
  845. if (CheckPlayerID(pEps->lnID ,pEps->nameid))
  846. {
  847. _ASSERT(sizeof(tagExtendProtoHeader) <= sizeof(EXTEND_PASSTOSOMEONE));
  848. unsigned long lnID = pEps->lnID;
  849. size_t pckgsize = sizeof(tagExtendProtoHeader) + pEps->datasize;
  850. tagExtendProtoHeader* pExHdr = (tagExtendProtoHeader*)(pEps + 1) - 1;
  851. pExHdr->ProtocolType = s2c_extendfriend;
  852. pExHdr->wLength = pckgsize - 1;
  853. m_pServer->PackDataToClient(lnID, pExHdr, pckgsize);
  854. }
  855. }
  856. else if (pHeader->ProtocolID == extend_s2c_passtobevy)
  857. {
  858. EXTEND_PASSTOBEVY* pEpb = (EXTEND_PASSTOBEVY*)pChar;
  859. if (pEpb->playercount > 0)
  860. {
  861. _ASSERT(sizeof(tagExtendProtoHeader) <= sizeof(EXTEND_PASSTOBEVY));
  862. void* pExPckg = pEpb + 1;
  863. WORD playercount = pEpb->playercount;
  864. tagPlusSrcInfo* pPlayers = (tagPlusSrcInfo*)((BYTE*)pExPckg + pEpb->datasize);
  865. size_t pckgsize = sizeof(tagExtendProtoHeader) + pEpb->datasize;
  866. tagExtendProtoHeader* pExHdr = (tagExtendProtoHeader*)pExPckg - 1;
  867. pExHdr->ProtocolType = s2c_extendfriend;
  868. pExHdr->wLength = pckgsize - 1;
  869. for (WORD i = 0; i < playercount; i++)
  870. {
  871. if (CheckPlayerID(pPlayers[i].lnID, pPlayers[i].nameid))
  872. m_pServer->PackDataToClient(pPlayers[i].lnID, pExHdr, pckgsize);
  873. }
  874. }
  875. }
  876. }
  877. else if (pHeader->ProtocolFamily == pf_tong)
  878. {
  879. // 协议长度检测
  880. if (nSize < sizeof(EXTEND_HEADER))
  881. return;
  882. if (pHeader->ProtocolID >= enumS2C_TONG_NUM)
  883. return;
  884. if (g_nTongPCSize[pHeader->ProtocolID] < 0)
  885. {
  886. if (nSize <= sizeof(EXTEND_HEADER) + 2)
  887. return;
  888. WORD wLength = *((WORD*)((BYTE*)pChar + sizeof(EXTEND_HEADER)));
  889. if (wLength != nSize)
  890. return;
  891. }
  892. else if (g_nTongPCSize[pHeader->ProtocolID] != nSize)
  893. {
  894. return;
  895. }
  896. switch (pHeader->ProtocolID)
  897. {
  898. case enumS2C_TONG_CREATE_SUCCESS:
  899. {
  900. STONG_CREATE_SUCCESS_SYNC *pSync = (STONG_CREATE_SUCCESS_SYNC*)pChar;
  901. STONG_SERVER_TO_CORE_CREATE_SUCCESS sSuccess;
  902. sSuccess.m_nCamp = pSync->m_btCamp;
  903. sSuccess.m_dwPlayerNameID = pSync->m_dwPlayerNameID;
  904. sSuccess.m_nPlayerIdx = pSync->m_dwParam;
  905. memcpy(sSuccess.m_szTongName, pSync->m_szTongName, pSync->m_btTongNameLength);
  906. sSuccess.m_szTongName[pSync->m_btTongNameLength] = 0;
  907. if (m_pCoreServerShell->OperationRequest(SSOI_TONG_CREATE, (unsigned int)&sSuccess, 0))
  908. {
  909. }
  910. else
  911. {
  912. }
  913. }
  914. break;
  915. case enumS2C_TONG_CREATE_FAIL:
  916. {
  917. STONG_CREATE_FAIL_SYNC *pFail = (STONG_CREATE_FAIL_SYNC*)pChar;
  918. int nNetID;
  919. TONG_CREATE_FAIL_SYNC sFail;
  920. sFail.ProtocolType = s2c_extendtong;
  921. sFail.m_btMsgId = enumTONG_SYNC_ID_CREATE_FAIL;
  922. sFail.m_btFailId = pFail->m_btFailID;
  923. sFail.m_wLength = sizeof(sFail) - 1;
  924. nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pFail->m_dwParam, 0);
  925. if (m_pServer)
  926. m_pServer->PackDataToClient(nNetID, &sFail, sFail.m_wLength + 1);
  927. }
  928. break;
  929. case enumS2C_TONG_ADD_MEMBER_SUCCESS:
  930. {
  931. STONG_ADD_MEMBER_SUCCESS_SYNC *pSync = (STONG_ADD_MEMBER_SUCCESS_SYNC*)pChar;
  932. STONG_SERVER_TO_CORE_ADD_SUCCESS sAdd;
  933. sAdd.m_nCamp = pSync->m_btCamp;
  934. sAdd.m_dwPlayerNameID = pSync->m_dwPlayerNameID;
  935. sAdd.m_nPlayerIdx = pSync->m_dwParam;
  936. memcpy(sAdd.m_szMasterName, pSync->m_szMasterName, sizeof(sAdd.m_szMasterName));
  937. memcpy(sAdd.m_szTitleName, pSync->m_szTitleName, sizeof(sAdd.m_szTitleName));
  938. memcpy(sAdd.m_szTongName, pSync->m_szTongName, sizeof(sAdd.m_szTongName));
  939. if (m_pCoreServerShell->OperationRequest(SSOI_TONG_ADD, (unsigned int)&sAdd, 0))
  940. {
  941. }
  942. else
  943. {
  944. }
  945. }
  946. break;
  947. case enumS2C_TONG_ADD_MEMBER_FAIL:
  948. {
  949. STONG_ADD_MEMBER_FAIL_SYNC *pSync = (STONG_ADD_MEMBER_FAIL_SYNC*)pChar;
  950. }
  951. break;
  952. case enumS2C_TONG_HEAD_INFO:
  953. {
  954. STONG_HEAD_INFO_SYNC *pInfo = (STONG_HEAD_INFO_SYNC*)pChar;
  955. if ((int)pInfo->m_dwParam <= 0 || (int)pInfo->m_dwParam >= MAX_PLAYER)
  956. break;
  957. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pInfo->m_dwParam, 0);
  958. if (nNetID < 0)
  959. break;
  960. TONG_HEAD_INFO_SYNC sInfo;
  961. sInfo.ProtocolType = s2c_extendtong;
  962. sInfo.m_btMsgId = enumTONG_SYNC_ID_HEAD_INFO;
  963. sInfo.m_dwNpcID = pInfo->m_dwNpcID;
  964. sInfo.m_dwMoney = pInfo->m_dwMoney;
  965. sInfo.m_nCredit = pInfo->m_nCredit;
  966. sInfo.m_btCamp = pInfo->m_btCamp;
  967. sInfo.m_btLevel = pInfo->m_btLevel;
  968. sInfo.m_btDirectorNum = pInfo->m_btDirectorNum;
  969. sInfo.m_btManagerNum = pInfo->m_btManagerNum;
  970. sInfo.m_dwMemberNum = pInfo->m_dwMemberNum;
  971. memcpy(sInfo.m_szTongName, pInfo->m_szTongName, sizeof(pInfo->m_szTongName));
  972. memset(sInfo.m_sMember, 0, sizeof(sInfo.m_sMember));
  973. memcpy(sInfo.m_sMember, pInfo->m_sMember, sizeof(TONG_ONE_LEADER_INFO) * (1 + sInfo.m_btDirectorNum));
  974. sInfo.m_wLength = sizeof(sInfo) - 1 - sizeof(sInfo.m_sMember) + sizeof(TONG_ONE_LEADER_INFO) * (1 + sInfo.m_btDirectorNum);
  975. if (m_pServer)
  976. m_pServer->PackDataToClient(nNetID, &sInfo, sInfo.m_wLength + 1);
  977. }
  978. break;
  979. case enumS2C_TONG_MANAGER_INFO:
  980. {
  981. STONG_MANAGER_INFO_SYNC *pInfo = (STONG_MANAGER_INFO_SYNC*)pChar;
  982. if ((int)pInfo->m_dwParam <= 0 || (int)pInfo->m_dwParam >= MAX_PLAYER)
  983. break;
  984. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pInfo->m_dwParam, 0);
  985. if (nNetID < 0)
  986. break;
  987. TONG_MANAGER_INFO_SYNC sInfo;
  988. sInfo.ProtocolType = s2c_extendtong;
  989. sInfo.m_btMsgId = enumTONG_SYNC_ID_MANAGER_INFO;
  990. sInfo.m_dwMoney = pInfo->m_dwMoney;
  991. sInfo.m_nCredit = pInfo->m_nCredit;
  992. sInfo.m_btCamp = pInfo->m_btCamp;
  993. sInfo.m_btLevel = pInfo->m_btLevel;
  994. sInfo.m_btDirectorNum = pInfo->m_btDirectorNum;
  995. sInfo.m_btManagerNum = pInfo->m_btManagerNum;
  996. sInfo.m_dwMemberNum = pInfo->m_dwMemberNum;
  997. sInfo.m_btStateNo = pInfo->m_btStartNo;
  998. sInfo.m_btCurNum = pInfo->m_btCurNum;
  999. memcpy(sInfo.m_szTongName, pInfo->m_szTongName, sizeof(pInfo->m_szTongName));
  1000. memset(sInfo.m_sMember, 0, sizeof(sInfo.m_sMember));
  1001. memcpy(sInfo.m_sMember, pInfo->m_sMember, sizeof(TONG_ONE_LEADER_INFO) * sInfo.m_btCurNum);
  1002. sInfo.m_wLength = sizeof(sInfo) - 1 - sizeof(TONG_ONE_LEADER_INFO) * (defTONG_ONE_PAGE_MAX_NUM - sInfo.m_btCurNum);
  1003. if (m_pServer)
  1004. m_pServer->PackDataToClient(nNetID, &sInfo, sInfo.m_wLength + 1);
  1005. }
  1006. break;
  1007. case enumS2C_TONG_MEMBER_INFO:
  1008. {
  1009. STONG_MEMBER_INFO_SYNC *pInfo = (STONG_MEMBER_INFO_SYNC*)pChar;
  1010. if ((int)pInfo->m_dwParam <= 0 || (int)pInfo->m_dwParam >= MAX_PLAYER)
  1011. break;
  1012. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pInfo->m_dwParam, 0);
  1013. if (nNetID < 0)
  1014. break;
  1015. TONG_MEMBER_INFO_SYNC sInfo;
  1016. sInfo.ProtocolType = s2c_extendtong;
  1017. sInfo.m_btMsgId = enumTONG_SYNC_ID_MEMBER_INFO;
  1018. sInfo.m_dwMoney = pInfo->m_dwMoney;
  1019. sInfo.m_nCredit = pInfo->m_nCredit;
  1020. sInfo.m_btCamp = pInfo->m_btCamp;
  1021. sInfo.m_btLevel = pInfo->m_btLevel;
  1022. sInfo.m_btDirectorNum = pInfo->m_btDirectorNum;
  1023. sInfo.m_btManagerNum = pInfo->m_btManagerNum;
  1024. sInfo.m_dwMemberNum = pInfo->m_dwMemberNum;
  1025. sInfo.m_btStateNo = pInfo->m_btStartNo;
  1026. sInfo.m_btCurNum = pInfo->m_btCurNum;
  1027. memcpy(sInfo.m_szTitle, pInfo->m_szTitle, sizeof(pInfo->m_szTitle));
  1028. memcpy(sInfo.m_szTongName, pInfo->m_szTongName, sizeof(pInfo->m_szTongName));
  1029. memset(sInfo.m_sMember, 0, sizeof(sInfo.m_sMember));
  1030. memcpy(sInfo.m_sMember, pInfo->m_sMember, sizeof(TONG_ONE_MEMBER_INFO) * sInfo.m_btCurNum);
  1031. sInfo.m_wLength = sizeof(sInfo) - 1 - sizeof(TONG_ONE_MEMBER_INFO) * (defTONG_ONE_PAGE_MAX_NUM - sInfo.m_btCurNum);
  1032. if (m_pServer)
  1033. m_pServer->PackDataToClient(nNetID, &sInfo, sInfo.m_wLength + 1);
  1034. }
  1035. break;
  1036. case enumS2C_TONG_BE_INSTATED:
  1037. {
  1038. STONG_BE_INSTATED_SYNC *pSync = (STONG_BE_INSTATED_SYNC*)pChar;
  1039. if (m_pGameStatus[pSync->m_dwParam].nPlayerIndex <= 0)
  1040. break;
  1041. STONG_SERVER_TO_CORE_BE_INSTATED sInstated;
  1042. sInstated.m_nPlayerIdx = m_pGameStatus[pSync->m_dwParam].nPlayerIndex;
  1043. sInstated.m_btFigure = pSync->m_btFigure;
  1044. sInstated.m_btPos = pSync->m_btPos;
  1045. memcpy(sInstated.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1046. memcpy(sInstated.m_szTitle, pSync->m_szTitle, sizeof(pSync->m_szTitle));
  1047. m_pCoreServerShell->GetGameData(SGDI_TONG_BE_INSTATED, (unsigned int)&sInstated, 0);
  1048. }
  1049. break;
  1050. case enumS2C_TONG_INSTATE:
  1051. {
  1052. STONG_INSTATE_SYNC *pSync = (STONG_INSTATE_SYNC*)pChar;
  1053. TONG_INSTATE_SYNC sInstate;
  1054. if ((int)pSync->m_dwParam <= 0 || (int)pSync->m_dwParam >= MAX_PLAYER)
  1055. break;
  1056. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pSync->m_dwParam, 0);
  1057. if (nNetID < 0)
  1058. break;
  1059. sInstate.ProtocolType = s2c_extendtong;
  1060. sInstate.m_btMsgId = enumTONG_SYNC_ID_INSTATE;
  1061. sInstate.m_wLength = sizeof(sInstate) - 1;
  1062. sInstate.m_dwTongNameID = pSync->m_dwTongNameID;
  1063. sInstate.m_btSuccessFlag = pSync->m_btSuccessFlag;
  1064. sInstate.m_btOldFigure = pSync->m_btOldFigure;
  1065. sInstate.m_btOldPos = pSync->m_btOldPos;
  1066. sInstate.m_btNewFigure = pSync->m_btNewFigure;
  1067. sInstate.m_btNewPos = pSync->m_btNewPos;
  1068. memcpy(sInstate.m_szTitle, pSync->m_szTitle, sizeof(pSync->m_szTitle));
  1069. memcpy(sInstate.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1070. if (m_pServer)
  1071. m_pServer->PackDataToClient(nNetID, &sInstate, sInstate.m_wLength + 1);
  1072. }
  1073. break;
  1074. case enumS2C_TONG_KICK:
  1075. {
  1076. STONG_KICK_SYNC *pSync = (STONG_KICK_SYNC*)pChar;
  1077. TONG_KICK_SYNC sKick;
  1078. if ((int)pSync->m_dwParam <= 0 || (int)pSync->m_dwParam >= MAX_PLAYER)
  1079. break;
  1080. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, pSync->m_dwParam, 0);
  1081. if (nNetID < 0)
  1082. break;
  1083. sKick.ProtocolType = s2c_extendtong;
  1084. sKick.m_btMsgId = enumTONG_SYNC_ID_KICK;
  1085. sKick.m_wLength = sizeof(sKick) - 1;
  1086. sKick.m_dwTongNameID = pSync->m_dwTongNameID;
  1087. sKick.m_btSuccessFlag = pSync->m_btSuccessFlag;
  1088. sKick.m_btFigure = pSync->m_btFigure;
  1089. sKick.m_btPos = pSync->m_btPos;
  1090. memcpy(sKick.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1091. if (m_pServer)
  1092. m_pServer->PackDataToClient(nNetID, &sKick, sKick.m_wLength + 1);
  1093. }
  1094. break;
  1095. case enumS2C_TONG_BE_KICKED:
  1096. {
  1097. STONG_BE_KICKED_SYNC *pSync = (STONG_BE_KICKED_SYNC*)pChar;
  1098. if (m_pGameStatus[pSync->m_dwParam].nPlayerIndex <= 0)
  1099. break;
  1100. STONG_SERVER_TO_CORE_BE_KICKED sKicked;
  1101. sKicked.m_nPlayerIdx = m_pGameStatus[pSync->m_dwParam].nPlayerIndex;
  1102. sKicked.m_btFigure = pSync->m_btFigure;
  1103. sKicked.m_btPos = pSync->m_btPos;
  1104. memcpy(sKicked.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1105. m_pCoreServerShell->GetGameData(SGDI_TONG_BE_KICKED, (unsigned int)&sKicked, 0);
  1106. }
  1107. break;
  1108. case enumS2C_TONG_LEAVE:
  1109. {
  1110. STONG_LEAVE_SYNC *pSync = (STONG_LEAVE_SYNC*)pChar;
  1111. STONG_SERVER_TO_CORE_LEAVE sLeave;
  1112. sLeave.m_nPlayerIdx = pSync->m_dwParam;
  1113. sLeave.m_bSuccessFlag = pSync->m_btSuccessFlag;
  1114. memcpy(sLeave.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1115. m_pCoreServerShell->GetGameData(SGDI_TONG_LEAVE, (unsigned int)&sLeave, 0);
  1116. }
  1117. break;
  1118. case enumS2C_TONG_CHECK_CHANGE_MASTER_POWER:
  1119. {
  1120. STONG_CHECK_GET_MASTER_POWER_SYNC *pSync = (STONG_CHECK_GET_MASTER_POWER_SYNC*)pChar;
  1121. if (m_pGameStatus[pSync->m_dwParam].nPlayerIndex <= 0)
  1122. break;
  1123. STONG_SERVER_TO_CORE_CHECK_GET_MASTER_POWER sCheck;
  1124. sCheck.m_dwTongNameID = pSync->m_dwTongNameID;
  1125. sCheck.m_btFigure = pSync->m_btFigure;
  1126. sCheck.m_btPos = pSync->m_btPos;
  1127. sCheck.m_nPlayerIdx = m_pGameStatus[pSync->m_dwParam].nPlayerIndex;
  1128. memcpy(sCheck.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1129. int nRet = 0;
  1130. if (m_pCoreServerShell)
  1131. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_GET_MASTER_POWER, (unsigned int)&sCheck, 0);
  1132. STONG_ACCEPT_MASTER_COMMAND sAccept;
  1133. sAccept.ProtocolFamily = pf_tong;
  1134. sAccept.ProtocolID = enumC2S_TONG_ACCEPT_MASTER;
  1135. sAccept.m_dwParam = m_pGameStatus[pSync->m_dwParam].nPlayerIndex;
  1136. sAccept.m_dwTongNameID = pSync->m_dwTongNameID;
  1137. sAccept.m_btFigure = pSync->m_btFigure;
  1138. sAccept.m_btPos = pSync->m_btPos;
  1139. if (nRet)
  1140. sAccept.m_btAcceptFalg = 1;
  1141. else
  1142. sAccept.m_btAcceptFalg = 0;
  1143. memcpy(sAccept.m_szName, pSync->m_szName, sizeof(pSync->m_szName));
  1144. if (m_pTongClient)
  1145. m_pTongClient->SendPackToServer((const void*)&sAccept, sizeof(sAccept));
  1146. }
  1147. break;
  1148. case enumS2C_TONG_CHANGE_MASTER_FAIL:
  1149. {
  1150. STONG_CHANGE_MASTER_FAIL_SYNC *pFail = (STONG_CHANGE_MASTER_FAIL_SYNC*)pChar;
  1151. int nPlayer = pFail->m_dwParam;
  1152. switch (pFail->m_btFailID)
  1153. {
  1154. case 0:
  1155. break;
  1156. case 1:
  1157. nPlayer = m_pGameStatus[pFail->m_dwParam].nPlayerIndex;
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. if (nPlayer <= 0 || nPlayer >= MAX_PLAYER)
  1163. break;
  1164. int nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, nPlayer, 0);
  1165. if (nNetID < 0)
  1166. break;
  1167. TONG_CHANGE_MASTER_FAIL_SYNC sFail;
  1168. sFail.ProtocolType = s2c_extendtong;
  1169. sFail.m_btMsgId = enumTONG_SYNC_ID_CHANGE_MASTER_FAIL;
  1170. sFail.m_wLength = sizeof(sFail) - 1;
  1171. sFail.m_dwTongNameID = pFail->m_dwTongNameID;
  1172. sFail.m_btFailID = pFail->m_btFailID;
  1173. memcpy(sFail.m_szName, pFail->m_szName, sizeof(pFail->m_szName));
  1174. if (m_pServer)
  1175. m_pServer->PackDataToClient(nNetID, &sFail, sFail.m_wLength + 1);
  1176. }
  1177. break;
  1178. case enumS2C_TONG_CHANGE_AS:
  1179. {
  1180. STONG_CHANGE_AS_SYNC *pAs = (STONG_CHANGE_AS_SYNC*)pChar;
  1181. if (m_pGameStatus[pAs->m_dwParam].nPlayerIndex <= 0)
  1182. break;
  1183. STONG_SERVER_TO_CORE_CHANGE_AS sChange;
  1184. sChange.m_nPlayerIdx = m_pGameStatus[pAs->m_dwParam].nPlayerIndex;
  1185. sChange.m_dwTongNameID = pAs->m_dwTongNameID;
  1186. sChange.m_btFigure = pAs->m_btFigure;
  1187. sChange.m_btPos = pAs->m_btPos;
  1188. // 自己的新头衔
  1189. memcpy(sChange.m_szTitle, pAs->m_szTitle, sizeof(pAs->m_szTitle));
  1190. // 新帮主的名字
  1191. memcpy(sChange.m_szName, pAs->m_szName, sizeof(pAs->m_szName));
  1192. m_pCoreServerShell->GetGameData(SGDI_TONG_CHANGE_AS, (unsigned int)&sChange, 0);
  1193. }
  1194. break;
  1195. case enumS2C_TONG_CHANGE_MASTER:
  1196. {
  1197. STONG_CHANGE_MASTER_SYNC *pMaster = (STONG_CHANGE_MASTER_SYNC*)pChar;
  1198. STONG_SERVER_TO_CORE_CHANGE_MASTER sChange;
  1199. sChange.m_dwTongNameID = pMaster->m_dwTongNameID;
  1200. memcpy(sChange.m_szName, pMaster->m_szName, sizeof(pMaster->m_szName));
  1201. m_pCoreServerShell->GetGameData(SGDI_TONG_CHANGE_MASTER, (unsigned int)&sChange, 0);
  1202. }
  1203. break;
  1204. case enumS2C_TONG_LOGIN_DATA:
  1205. {
  1206. STONG_LOGIN_DATA_SYNC *pLogin = (STONG_LOGIN_DATA_SYNC*)pChar;
  1207. STONG_SERVER_TO_CORE_LOGIN sLogin;
  1208. sLogin.m_dwParam = pLogin->m_dwParam;
  1209. sLogin.m_nFlag = pLogin->m_btFlag;
  1210. sLogin.m_nCamp = pLogin->m_btCamp;
  1211. sLogin.m_nFigure = pLogin->m_btFigure;
  1212. sLogin.m_nPos = pLogin->m_btPos;
  1213. memcpy(sLogin.m_szTongName, pLogin->m_szTongName, sizeof(pLogin->m_szTongName));
  1214. memcpy(sLogin.m_szTitle, pLogin->m_szTitle, sizeof(pLogin->m_szTitle));
  1215. memcpy(sLogin.m_szMaster, pLogin->m_szMaster, sizeof(pLogin->m_szMaster));
  1216. memcpy(sLogin.m_szName, pLogin->m_szName, sizeof(pLogin->m_szName));
  1217. m_pCoreServerShell->GetGameData(SGDI_TONG_LOGIN, (unsigned int)&sLogin, 0);
  1218. }
  1219. break;
  1220. default:
  1221. break;
  1222. }
  1223. }
  1224. else if (pHeader->ProtocolFamily == pf_friend)
  1225. {
  1226. }
  1227. }
  1228. void KSwordOnLineSever::ChatGroupMan(const void *pData, size_t dataLength)
  1229. {
  1230. _ASSERT(pData && dataLength);
  1231. CHAT_GROUPMAN* pCgc = (CHAT_GROUPMAN *)pData;
  1232. _ASSERT(pCgc->wSize + 1 == dataLength);
  1233. _ASSERT(sizeof(tagExtendProtoHeader) <= sizeof(CHAT_GROUPMAN));
  1234. void* pExPckg = pCgc + 1;
  1235. BYTE hasIdentify = pCgc->byHasIdentify;
  1236. WORD playercount = pCgc->wPlayerCount;
  1237. void* pPlayersData = (tagPlusSrcInfo*)((BYTE*)pExPckg + pCgc->wChatLength);
  1238. size_t pckgsize = sizeof(tagExtendProtoHeader) + pCgc->wChatLength;
  1239. tagExtendProtoHeader* pExHdr = (tagExtendProtoHeader*)pExPckg - 1;
  1240. pExHdr->ProtocolType = s2c_extendchat;
  1241. pExHdr->wLength = pckgsize - 1;
  1242. if (hasIdentify)
  1243. {
  1244. tagPlusSrcInfo* pPlayers = (tagPlusSrcInfo*)pPlayersData;
  1245. for (int i = 0; i < playercount; i++)
  1246. {
  1247. //TODO: 要加检验NameID和lnID的一致性
  1248. if (CheckPlayerID(pPlayers[i].lnID, pPlayers[i].nameid))
  1249. m_pServer->PackDataToClient(pPlayers[i].lnID, pExHdr, pckgsize);
  1250. }
  1251. }
  1252. else
  1253. {
  1254. WORD* pPlayers = (WORD*)pPlayersData;
  1255. for (int i = 0; i < playercount; i++)
  1256. {
  1257. //TODO: 不需要加检验NameID和lnID的一致性
  1258. //if (pPlayers[i] >= 0)
  1259. m_pServer->PackDataToClient((unsigned long)pPlayers[i], pExHdr, pckgsize);
  1260. }
  1261. }
  1262. }
  1263. void KSwordOnLineSever::ChatSpecMan(const void *pData, size_t dataLength)
  1264. {
  1265. _ASSERT(pData && dataLength);
  1266. CHAT_SPECMAN* pCsm = (CHAT_SPECMAN *)pData;
  1267. _ASSERT(pCsm->wSize + 1 == dataLength);
  1268. if (!CheckPlayerID(pCsm->lnID, pCsm->nameid))
  1269. return;
  1270. _ASSERT(sizeof(tagExtendProtoHeader) <= sizeof(CHAT_SPECMAN));
  1271. void* pExPckg = pCsm + 1;
  1272. unsigned long lnID = pCsm->lnID;
  1273. size_t pckgsize = sizeof(tagExtendProtoHeader) + pCsm->wChatLength;
  1274. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)(pCsm + 1) - 1;
  1275. pExHeader->ProtocolType = s2c_extendchat;
  1276. pExHeader->wLength = pckgsize - 1;
  1277. m_pServer->PackDataToClient(lnID, pExHeader, pckgsize);
  1278. }
  1279. void KSwordOnLineSever::DatabaseMessageProcess(const char* pData, size_t dataLength)
  1280. {
  1281. _ASSERT( pData && dataLength );
  1282. #ifndef _STANDALONE
  1283. BYTE cProtocol = CPackager::Peek( pData );
  1284. #else
  1285. BYTE cProtocol = *(BYTE *)pData;
  1286. #endif
  1287. // 大包处理(用于排名的数据)
  1288. if ( cProtocol < s2c_micropackbegin )
  1289. {
  1290. DatabaseLargePackProcess(pData, dataLength);
  1291. return;
  1292. }
  1293. switch ( cProtocol )
  1294. {
  1295. case s2c_roleserver_saverole_result:
  1296. //m_bSaveFlag = TRUE;
  1297. {
  1298. TProcessData* pPD = (TProcessData *)pData;
  1299. int nIndex = pPD->ulIdentity;
  1300. m_pCoreServerShell->SetSaveStatus(nIndex, SAVE_IDLE);
  1301. printf("  Save Player Data finished(%d)!n", nIndex);
  1302. }
  1303. break;
  1304. default:
  1305. printf("Protocol:(%d) -- database error", cProtocol);
  1306. break;
  1307. }
  1308. }
  1309. void KSwordOnLineSever::DatabaseLargePackProcess(const char* pData, size_t dataLength)
  1310. {
  1311. _ASSERT( pData && dataLength );
  1312. #ifndef _STANDALONE
  1313. CBuffer *pBuffer = m_theRecv.PackUp( pData, dataLength );
  1314. #else
  1315. char *pBuffer = m_theRecv.PackUp( pData, dataLength );
  1316. #endif
  1317. if ( pBuffer )
  1318. {
  1319. #ifndef _STANDALONE
  1320. BYTE cProtocol = CPackager::Peek( pBuffer->GetBuffer() );
  1321. #else
  1322. BYTE cProtocol = *(BYTE *)pBuffer;
  1323. #endif
  1324. // BYTE cProtocol = CPackager::Peek( pBuffer->GetBuffer() );
  1325. switch ( cProtocol )
  1326. {
  1327. case s2c_gamestatistic:
  1328. {
  1329. #ifndef _STANDALONE
  1330. TProcessData* pRD = (TProcessData *)pBuffer->GetBuffer();
  1331. #else
  1332. TProcessData* pRD = (TProcessData *)pBuffer;
  1333. #endif
  1334. _ASSERT( pRD->nDataLen == sizeof(TGAME_STAT_DATA) );
  1335. if (m_pCoreServerShell)
  1336. {
  1337. m_pCoreServerShell->SetLadder((void *)pRD->pDataBuffer, pRD->nDataLen);
  1338. }
  1339. }
  1340. break;
  1341. default:
  1342. break;
  1343. }
  1344. #ifndef _STANDALONE
  1345. try
  1346. {
  1347. if( pBuffer != NULL )
  1348. {
  1349. pBuffer->Release();
  1350. pBuffer = NULL; 
  1351. }
  1352. }
  1353. catch(...) 
  1354. {
  1355. //TRACE("SAFE_RELEASE errorn");
  1356. }
  1357. #endif
  1358. }
  1359. }
  1360. void KSwordOnLineSever::TransferMessageProcess(const char* pChar, size_t nSize)
  1361. {
  1362. //#ifndef _STANDALONE
  1363. _ASSERT(pChar && nSize && nSize < 1024);
  1364. EXTEND_HEADER *pEH = (EXTEND_HEADER *)pChar;
  1365. if (nSize < sizeof(EXTEND_HEADER))
  1366. return;
  1367. // 是否寻路包?
  1368. if (pEH->ProtocolID == relay_c2c_askwaydata && pEH->ProtocolFamily == pf_relay)
  1369. {
  1370. TransferAskWayMessageProcess(pChar, nSize);
  1371. return;
  1372. }
  1373. RELAY_DATA *pRD = (RELAY_DATA *)pEH;
  1374. if (nSize <= sizeof(RELAY_DATA) || nSize != pRD->routeDateLength + sizeof(RELAY_DATA))
  1375. return;
  1376. // 是否寻路失败包
  1377. if (pEH->ProtocolID == relay_s2c_loseway && pEH->ProtocolFamily == pf_relay)
  1378. {
  1379. TransferLoseWayMessageProcess(pChar + sizeof(RELAY_DATA), nSize - sizeof(RELAY_DATA));
  1380. return;
  1381. }
  1382. if (pEH->ProtocolID != relay_c2c_data && pEH->ProtocolFamily != pf_relay)
  1383. return;
  1384. KTransferUnit* pUnit = NULL;
  1385. IP2CONNECTUNIT::iterator it = m_mapIp2TransferUnit.find(pRD->nFromRelayID);
  1386. if (it != m_mapIp2TransferUnit.end())
  1387. {
  1388. pUnit = (*it).second;
  1389. _ASSERT(pUnit);
  1390. }
  1391. else
  1392. {
  1393. pUnit = new KTransferUnit(pRD->nFromIP, pRD->nFromRelayID);
  1394. m_mapIp2TransferUnit.insert(IP2CONNECTUNIT::value_type(pRD->nFromRelayID, pUnit));
  1395. }
  1396. #ifndef _STANDALONE
  1397. BYTE cProtocol = CPackager::Peek(pChar, sizeof(RELAY_DATA));
  1398. #else
  1399. BYTE cProtocol = *(BYTE *)(pChar + sizeof(RELAY_DATA));
  1400. #endif
  1401. if ( cProtocol < c2c_micropackbegin )
  1402. {
  1403. TransferLargePackProcess(pChar + sizeof(RELAY_DATA), pRD->routeDateLength, pUnit);
  1404. }
  1405. else if ( cProtocol > c2c_micropackbegin )
  1406. {
  1407. TransferSmallPackProcess(pChar + sizeof(RELAY_DATA), pRD->routeDateLength, pUnit);
  1408. }
  1409. else
  1410. {
  1411. _ASSERT( FALSE && "Error!" );
  1412. }
  1413. //#endif
  1414. }
  1415. void KSwordOnLineSever::TransferLargePackProcess(const void *pData, size_t dataLength, KTransferUnit *pUnit)
  1416. {
  1417. _ASSERT(pData && dataLength);
  1418. if (!pData || !dataLength)
  1419. return;
  1420. #ifndef _STANDALONE
  1421. CBuffer *pBuffer = pUnit->m_RecvData.PackUp( pData, dataLength );
  1422. #else
  1423. char *pBuffer = pUnit->m_RecvData.PackUp( pData, dataLength );
  1424. #endif
  1425. char szChar[1024];
  1426. if ( pBuffer )
  1427. {
  1428. #ifndef _STANDALONE
  1429. BYTE cProtocol = CPackager::Peek( pBuffer->GetBuffer() );
  1430. #else
  1431. BYTE cProtocol = *(BYTE *)pBuffer;
  1432. #endif
  1433. switch ( cProtocol )
  1434. {
  1435. case c2c_transferroleinfo:
  1436. {
  1437. #ifndef _STANDALONE
  1438. tagGuidableInfo *pGI = (tagGuidableInfo *)pBuffer->GetBuffer();
  1439. #else
  1440. tagGuidableInfo *pGI = (tagGuidableInfo *)pBuffer;
  1441. #endif
  1442. GUID guid;
  1443. memcpy( &guid, &( pGI->guid ), sizeof( GUID ) );
  1444. if ( pGI->datalength > 0 )
  1445. {
  1446. const TRoleData *pRoleData = ( const TRoleData * )( pGI->szData );
  1447. DWORD dwCRC = 0;
  1448. dwCRC = CRC32(dwCRC, pRoleData, pRoleData->dwDataLen - 4);
  1449. DWORD dwCheck = *(DWORD *)(pGI->szData + pRoleData->dwDataLen - 4);
  1450. BOOL bCRC = (dwCheck == dwCRC);
  1451. int nIdx = 0;
  1452. if (bCRC)
  1453. {
  1454. nIdx = m_pCoreServerShell->AddCharacter(pGI->nExtPoint, pGI->nChangePoint, (void *)pRoleData, &guid);
  1455. }
  1456. else
  1457. {
  1458. FILE *fLog = fopen("crc_transfer_error", "a+b");
  1459. if(fLog) 
  1460. {
  1461. char buffer[255];
  1462. sprintf(buffer, "----rn%srb%srn", pRoleData->BaseInfo.szName, pRoleData->BaseInfo.caccname);
  1463. fwrite(buffer, 1, strlen(buffer), fLog);
  1464. fwrite(pRoleData, 1, pRoleData->dwDataLen, fLog);
  1465. fclose(fLog);
  1466. }
  1467. }
  1468. RELAY_DATA* pRD = (RELAY_DATA *)szChar;
  1469. pRD->ProtocolFamily = pf_relay;
  1470. pRD->ProtocolID = relay_c2c_data;
  1471. pRD->nFromIP = 0;
  1472. pRD->nFromRelayID = 0;
  1473. pRD->nToIP = pUnit->GetIp();
  1474. pRD->nToRelayID = pUnit->GetRelayID();
  1475. pRD->routeDateLength = sizeof(tagPermitPlayerExchange);
  1476. tagPermitPlayerExchange *pPPEO = (tagPermitPlayerExchange *)(szChar + sizeof(RELAY_DATA));
  1477. pPPEO->cProtocol = c2c_permitplayerexchangein;
  1478. memcpy(&pPPEO->guid, &guid, sizeof(GUID));
  1479. pPPEO->dwIp = m_dwInternetIp;//OnlineGameLib::Win32::net_aton(m_szGameSvrIP);
  1480. pPPEO->wPort = m_nServerPort;
  1481. if (nIdx)
  1482. {
  1483. pPPEO->bPermit = true;
  1484. tagRegisterAccount ra;
  1485. ra.cProtocol = c2s_registeraccount;
  1486. strcpy((char *)ra.szAccountName, pRoleData->BaseInfo.caccname);
  1487. m_pGatewayClient->SendPackToServer(&ra, sizeof(tagRegisterAccount));
  1488. }
  1489. else
  1490. {
  1491. pPPEO->bPermit = false;
  1492. }
  1493. m_pTransferClient->SendPackToServer(pRD, sizeof(RELAY_DATA) + sizeof(tagPermitPlayerExchange));
  1494. }
  1495. else
  1496. {
  1497. // cout << "Don't find any valid data!" << endl;
  1498. }
  1499. }
  1500. break;
  1501. default:
  1502. break;
  1503. }
  1504. #ifndef _STANDALONE
  1505. try
  1506. {
  1507. if( pBuffer != NULL )
  1508. {
  1509. pBuffer->Release();
  1510. pBuffer = NULL; 
  1511. }
  1512. }
  1513. catch(...) 
  1514. {
  1515. //TRACE("SAFE_RELEASE errorn");
  1516. }
  1517. #endif
  1518. }
  1519. }
  1520. void KSwordOnLineSever::TransferSmallPackProcess(const void *pData, size_t dataLength, KTransferUnit* pUnit)
  1521. {
  1522. //#ifndef _STANDALONE
  1523. _ASSERT(pUnit && pData && dataLength);
  1524. if (!pUnit || !pData || !dataLength)
  1525. return;
  1526. #ifndef _STANDALONE
  1527. BYTE cProtocol = CPackager::Peek( pData );
  1528. #else
  1529. BYTE cProtocol = *(BYTE *)pData;
  1530. #endif
  1531. switch ( cProtocol )
  1532. {
  1533. case s2s_broadcast:
  1534. {
  1535. BYTE* pS = (BYTE *)pData;
  1536. m_pCoreServerShell->ProcessBroadcastMessage((const char *)(pS + 1), dataLength - 1);
  1537. }
  1538. break;
  1539. case s2s_execute:
  1540. {
  1541. BYTE* pS = (BYTE *)pData;
  1542. m_pCoreServerShell->ProcessExecuteMessage((const char *)(pS + 1), dataLength - 1);
  1543. }
  1544. break;
  1545. case c2c_permitplayerexchangein:
  1546. {
  1547. tagPermitPlayerExchange* pPermit = (tagPermitPlayerExchange *)pData;
  1548. int nIndex, lnID;
  1549. m_pCoreServerShell->GetPlayerIndexByGuid(&pPermit->guid, &nIndex, &lnID);
  1550. if (nIndex == 0 || lnID == -1)
  1551. break;
  1552. if (m_pGameStatus[lnID].nExchangeStatus != enumExchangeWaitForGameSvrRespone)
  1553. break;
  1554. tagNotifyPlayerExchange npe;
  1555. if (pPermit->bPermit)
  1556. {
  1557. // SavePlayerData(nIndex);
  1558. m_pCoreServerShell->RemovePlayerForExchange(nIndex);
  1559. npe.cProtocol = s2c_notifyplayerexchange;
  1560. memcpy(&npe.guid, &pPermit->guid, sizeof(GUID));
  1561. npe.nIPAddr = pPermit->dwIp;
  1562. npe.nPort = pPermit->wPort;
  1563. m_pServer->SendData(lnID, &npe, sizeof(tagNotifyPlayerExchange));
  1564. tagLeaveGame sLeaveGame;
  1565. sLeaveGame.cProtocol = c2s_leavegame;
  1566. sLeaveGame.cCmdType = HOLDACC_LEAVEGAME;
  1567. char szName[32];
  1568. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ACCOUNT, (unsigned int)szName, nIndex);
  1569. sLeaveGame.nExtPoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINTCHANGED, nIndex, 0);
  1570. strcpy((char *)sLeaveGame.szAccountName, (char *)szName);
  1571. m_pGatewayClient->SendPackToServer(&sLeaveGame, sizeof(tagLeaveGame));
  1572. tagLeaveGame2 lg2;
  1573. lg2.ProtocolFamily = pf_normal;
  1574. lg2.ProtocolID = c2s_leavegame;
  1575. lg2.cCmdType = HOLDACC_LEAVEGAME;
  1576. strcpy((char *)lg2.szAccountName, (char *)szName);
  1577. if (m_pTransferClient)
  1578. m_pTransferClient->SendPackToServer(&lg2, sizeof(tagLeaveGame2));
  1579. // cout << sLeaveGame.szAccountName << " leave game!" << endl;
  1580. m_pGameStatus[lnID].nExchangeStatus = enumExchangeCleaning;
  1581. m_pGameStatus[lnID].nPlayerIndex = 0;
  1582. }
  1583. else
  1584. {
  1585. npe.cProtocol = s2c_notifyplayerexchange;
  1586. memset(&npe.guid, 0, sizeof(GUID));
  1587. // if false, ip to 0
  1588. npe.nIPAddr = 0;
  1589. npe.nPort = 0;
  1590. m_pServer->SendData(lnID, &npe, sizeof(tagNotifyPlayerExchange));
  1591. // TODO: Recover player to old game
  1592. m_pCoreServerShell->RecoverPlayerExchange(nIndex);
  1593. tagRoleEnterGame reg;
  1594. reg.ProtocolType = c2s_roleserver_lock;
  1595. reg.bLock = true;
  1596. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)reg.Name, nIndex);
  1597. if (m_pDatabaseClient)
  1598. m_pDatabaseClient->SendPackToServer((const void *)&reg, sizeof(tagRoleEnterGame));
  1599. m_pGameStatus[lnID].nGameStatus = enumPlayerPlaying;
  1600. m_pGameStatus[lnID].nExchangeStatus = enumExchangeBegin;
  1601. }
  1602. }
  1603. break;
  1604. case c2c_notifyexchange:
  1605. {
  1606. tagSearchWay *pSW = (tagSearchWay *)pData;
  1607. if (pSW->lnID < 0 || pSW->lnID >= m_nMaxPlayer)
  1608. {
  1609. printf("break 1n");
  1610. break;
  1611. }
  1612. if (m_pGameStatus[pSW->lnID].nExchangeStatus != enumExchangeSearchingWay)
  1613. {
  1614. printf("break 2n");
  1615. break;
  1616. }
  1617. int nIndex = m_pGameStatus[pSW->lnID].nPlayerIndex;
  1618. if (nIndex <= 0 || nIndex > m_nMaxPlayer || nIndex != pSW->nIndex)
  1619. {
  1620. printf("break 3n");
  1621. break;
  1622. }
  1623. if (pSW->dwPlayerID != m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, nIndex, 0))
  1624. {
  1625. printf("break 4n");
  1626. break;
  1627. }
  1628. char szData[1024];
  1629. RELAY_DATA *pRD = (RELAY_DATA *)szData;
  1630. pRD->ProtocolFamily = pf_relay;
  1631. pRD->ProtocolID = relay_c2c_data;
  1632. pRD->nToIP = pUnit->GetIp();
  1633. pRD->nToRelayID = pUnit->GetRelayID();
  1634. pRD->nFromIP = 0;
  1635. pRD->nFromRelayID = 0;
  1636. tagGuidableInfo gi;
  1637. TRoleData* pData = (TRoleData *)m_pCoreServerShell->PreparePlayerForExchange(nIndex);
  1638. if (!pData)
  1639. {
  1640. printf("break 5n");
  1641. break;
  1642. }
  1643. gi.cProtocol = c2c_transferroleinfo;
  1644. m_pCoreServerShell->GetGuid(nIndex, &gi.guid);
  1645. gi.nExtPoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINT, nIndex, 0);
  1646. gi.nChangePoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINTCHANGED, nIndex, 0);
  1647. pData->dwDataLen += 4;
  1648. gi.datalength = pData->dwDataLen;
  1649. DWORD dwCRC = 0;
  1650. dwCRC = CRC32(dwCRC, pData, pData->dwDataLen - 4);
  1651. m_theSend.AddData(c2c_transferroleinfo, (const char*)&gi, sizeof(tagGuidableInfo));
  1652. m_theSend.AddData(c2c_transferroleinfo, (const char*)pData, gi.datalength - 4);
  1653. m_theSend.AddData(c2c_transferroleinfo, (const char*)&dwCRC, 4);
  1654. #ifndef _STANDALONE
  1655. CBuffer* pBuffer = m_theSend.GetHeadPack(c2c_transferroleinfo, 512);
  1656. #else
  1657. size_t actLen = 0;
  1658. char* pBuffer = m_theSend.GetHeadPack(c2c_transferroleinfo, actLen, 512);
  1659. #endif
  1660. while(pBuffer)
  1661. {
  1662. #ifndef _STANDALONE
  1663. pRD->routeDateLength = pBuffer->GetUsed();
  1664. #else
  1665. pRD->routeDateLength = actLen;
  1666. #endif
  1667. _ASSERT(pRD->routeDateLength + sizeof(RELAY_DATA) < 1024);
  1668. #ifndef _STANDALONE
  1669. memcpy(szData + sizeof(RELAY_DATA), pBuffer->GetBuffer(), pBuffer->GetUsed());
  1670. #else
  1671. memcpy(szData + sizeof(RELAY_DATA), pBuffer, actLen);
  1672. #endif
  1673. m_pTransferClient->SendPackToServer(szData, pRD->routeDateLength + sizeof(RELAY_DATA));
  1674. #ifndef _STANDALONE
  1675. try
  1676. {
  1677. if (pBuffer)
  1678. {
  1679. pBuffer->Release();
  1680. pBuffer = NULL;
  1681. }
  1682. }
  1683. catch(...) 
  1684. {
  1685. //TRACE("SAFE_RELEASE errorn");
  1686. }
  1687. #endif
  1688. #ifndef _STANDALONE
  1689. pBuffer = m_theSend.GetNextPack(c2c_transferroleinfo);
  1690. #else
  1691. pBuffer = m_theSend.GetNextPack(c2c_transferroleinfo, actLen);
  1692. #endif
  1693. }
  1694. m_theSend.DelData(c2c_transferroleinfo);
  1695. #ifndef _STANDALONE
  1696. try
  1697. {
  1698. if (pBuffer)
  1699. {
  1700. pBuffer->Release();
  1701. pBuffer = NULL;
  1702. }
  1703. }
  1704. catch(...) 
  1705. {
  1706. //TRACE("SAFE_RELEASE errorn");
  1707. }
  1708. #endif
  1709. m_pGameStatus[pSW->lnID].nExchangeStatus = enumExchangeWaitForGameSvrRespone;
  1710. }
  1711. break;
  1712. }
  1713. //#endif
  1714. }
  1715. void KSwordOnLineSever::PlayerMessageProcess(const unsigned long lnID, const char* pData, size_t dataLength)
  1716. {
  1717. //#ifndef _STANDALONE
  1718. _ASSERT(pData && dataLength);
  1719. #ifndef _STANDALONE
  1720. BYTE cProtocol = CPackager::Peek( pData );
  1721. #else
  1722. BYTE cProtocol = *(BYTE *)pData;
  1723. #endif
  1724. int nGameStatus = m_pGameStatus[lnID].nGameStatus;
  1725. int nIndex = m_pGameStatus[lnID].nPlayerIndex;
  1726. BYTE protocoltype = *(BYTE*)pData;
  1727. if (protocoltype >= _c2s_begin_relay && protocoltype <= _c2s_end_relay)
  1728. {
  1729. IClient* pClient = NULL;
  1730. switch (protocoltype)
  1731. {
  1732. case c2s_extend: pClient = m_pTransferClient; break;
  1733. case c2s_extendchat: pClient = m_pChatClient; break;
  1734. case c2s_extendfriend: pClient = m_pTongClient; break;
  1735. default:
  1736. return;
  1737. };
  1738. if (pClient && nIndex && dataLength > sizeof(tagExtendProtoHeader))
  1739. {
  1740. size_t exsize = dataLength - sizeof(tagExtendProtoHeader);
  1741. const void* pExPckg = pData + sizeof(tagExtendProtoHeader);
  1742. if (protocoltype == c2s_extendchat)
  1743. {
  1744. CHAT_CHANNELCHAT_CMD* pEh = (CHAT_CHANNELCHAT_CMD*)pExPckg;
  1745. if (pEh->ProtocolType == chat_channelchat && pEh->channelid != 0) //非GM频道要过滤和付钱
  1746. {
  1747. if (!m_pCoreServerShell->PayForSpeech(nIndex, pEh->cost))
  1748. return;
  1749. }
  1750. }
  1751. size_t pckgsize = exsize + sizeof(tagPlusSrcInfo);
  1752. #ifdef WIN32
  1753. void* pPckg2 = _alloca(pckgsize);
  1754. #else
  1755. void* pPckg2 = (new char[pckgsize]);
  1756. #endif
  1757. memcpy(pPckg2, pExPckg, exsize);
  1758. tagPlusSrcInfo* pSrcInfo = (tagPlusSrcInfo*)((BYTE*)pPckg2 + exsize);
  1759. pSrcInfo->nameid = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, nIndex, 0);
  1760. pSrcInfo->lnID = lnID;
  1761. pClient->SendPackToServer(pPckg2, pckgsize);
  1762. #ifndef WIN32
  1763. delete (char*)pPckg2;
  1764. #endif
  1765. }
  1766. return;
  1767. }
  1768. if (!m_pCoreServerShell->CheckProtocolSize(pData, dataLength))
  1769. return;
  1770. //printf("player msg arrived...%dn", nGameStatus); //[wxb 2003-7-28]
  1771. switch(nGameStatus)
  1772. {
  1773. case enumPlayerPlaying:
  1774. if (nIndex)
  1775. {
  1776. if (*(BYTE*)pData == c2s_ping)
  1777. {
  1778. ProcessPingReply(lnID, pData, dataLength);
  1779. }
  1780. else if (*(BYTE*)pData == c2s_extendtong)
  1781. {
  1782. ProcessPlayerTongMsg(nIndex, pData, dataLength);
  1783. }
  1784. else
  1785. {
  1786. m_pCoreServerShell->ProcessClientMessage(nIndex, pData, dataLength);
  1787. }
  1788. }
  1789. break;
  1790. case enumPlayerExchangingServer:
  1791. break;
  1792. case enumPlayerSyncEnd:
  1793. if (ProcessSyncReplyProtocol(lnID, pData, dataLength))
  1794. {
  1795. m_pCoreServerShell->AddPlayerToWorld(nIndex);
  1796. m_pGameStatus[lnID].nGameStatus = enumPlayerPlaying;
  1797. PingClient(lnID);
  1798. tagEnterGame eg;
  1799. eg.cProtocol = c2s_entergame;
  1800. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ACCOUNT, (unsigned int)eg.szAccountName, nIndex);
  1801. if (m_pGatewayClient)
  1802. m_pGatewayClient->SendPackToServer( ( const void * )&eg, sizeof( tagEnterGame ) );
  1803. tagEnterGame2 eg2;
  1804. eg2.ProtocolFamily = pf_normal;
  1805. eg2.ProtocolID = c2s_entergame;
  1806. strcpy(( char * )eg2.szAccountName, (char *)eg.szAccountName);
  1807. eg2.dwNameID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, nIndex, 0);
  1808. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)eg2.szCharacterName, nIndex);
  1809. eg2.lnID = lnID;
  1810. if (m_pTransferClient)
  1811. m_pTransferClient->SendPackToServer( (const void *)&eg2, sizeof(tagEnterGame2) );
  1812. //锁定角色数据库
  1813. tagRoleEnterGame reg;
  1814. reg.ProtocolType = c2s_roleserver_lock;
  1815. reg.bLock = true;
  1816. strcpy((char *)reg.Name, (char *)eg2.szCharacterName);
  1817. if (m_pDatabaseClient)
  1818. m_pDatabaseClient->SendPackToServer((const void *)&reg, sizeof(tagRoleEnterGame));
  1819. // 获取帮会信息
  1820. int nPlayerIdx = m_pGameStatus[lnID].nPlayerIndex;
  1821. if (nPlayerIdx > 0 && nPlayerIdx < MAX_PLAYER)
  1822. {
  1823. DWORD dwTongNameID = m_pCoreServerShell->GetGameData(SGDI_TONG_GET_TONG_NAMEID, 0, nPlayerIdx);
  1824. char szName[32];
  1825. szName[0] = 0;
  1826. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)szName, nPlayerIdx);
  1827. if (dwTongNameID > 0 && szName[0])
  1828. {
  1829. STONG_GET_LOGIN_DATA_COMMAND sLogin;
  1830. sLogin.ProtocolFamily = pf_tong;
  1831. sLogin.ProtocolID = enumC2S_TONG_GET_LOGIN_DATA;
  1832. sLogin.m_dwParam = nPlayerIdx;
  1833. sLogin.m_dwTongNameID = dwTongNameID;
  1834. strcpy(sLogin.m_szName, szName);
  1835. if (m_pTongClient)
  1836. m_pTongClient->SendPackToServer((const void*)&sLogin, sizeof(sLogin));
  1837. }
  1838. m_pCoreServerShell->GetGameData(SGDI_TONG_SEND_SELF_INFO, 0, nPlayerIdx);
  1839. }
  1840. }
  1841. break;
  1842. case enumPlayerBegin:
  1843. {
  1844. int nIndex = ProcessLoginProtocol(lnID, pData, dataLength);
  1845. //printf("process login %d...n", nIndex);
  1846. if (nIndex)
  1847. {
  1848. if (SendGameDataToClient(lnID, nIndex))
  1849. {
  1850. //printf("process login %d ok...n", nIndex);
  1851. m_pGameStatus[lnID].nGameStatus = enumPlayerSyncEnd;
  1852. m_pGameStatus[lnID].nPlayerIndex = nIndex;
  1853. }
  1854. else
  1855. {
  1856. //printf("process login %d fail...n", nIndex);
  1857. m_pGameStatus[lnID].nGameStatus = enumPlayerBegin;
  1858. m_pGameStatus[lnID].nPlayerIndex = nIndex;
  1859. #ifndef _STANDALONE
  1860. m_pServer->ShutdownClient(lnID);
  1861. #endif
  1862. }
  1863. }
  1864. }
  1865. break;
  1866. }
  1867. //printf("player msg over..n");
  1868. //#endif
  1869. }
  1870. void KSwordOnLineSever::GatewayMessageProcess(const char* pData, size_t dataLength)
  1871. {
  1872. _ASSERT(pData && dataLength);
  1873. #ifndef _STANDALONE
  1874. BYTE cProtocol = CPackager::Peek( pData );
  1875. #else
  1876. BYTE cProtocol = *(unsigned char *)pData;
  1877. #endif
  1878. if ( cProtocol < s2c_micropackbegin )
  1879. {
  1880. GatewayLargePackProcess(pData, dataLength);
  1881. }
  1882. else if ( cProtocol > s2c_micropackbegin )
  1883. {
  1884. GatewaySmallPackProcess(pData, dataLength);
  1885. }
  1886. else
  1887. {
  1888. _ASSERT( FALSE && "Error!" );
  1889. }
  1890. }
  1891. void KSwordOnLineSever::GatewayLargePackProcess(const void *pData, size_t dataLength)
  1892. {
  1893. #ifndef _STANDALONE
  1894. _ASSERT( pData && dataLength );
  1895. CBuffer *pBuffer = m_theRecv.PackUp( pData, dataLength );
  1896. #else
  1897. char *pBuffer = m_theRecv.PackUp( pData, dataLength );
  1898. #endif
  1899. if ( pBuffer )
  1900. {
  1901. #ifndef _STANDALONE
  1902. BYTE cProtocol = CPackager::Peek( pBuffer->GetBuffer() );
  1903. #else
  1904. BYTE cProtocol = *(BYTE *)pBuffer;
  1905. #endif
  1906. switch ( cProtocol )
  1907. {
  1908. case s2c_syncgamesvr_roleinfo_cipher:
  1909. {
  1910. #ifndef _STANDALONE
  1911. tagGuidableInfo *pGI = ( tagGuidableInfo * )pBuffer->GetBuffer();
  1912. #else
  1913. tagGuidableInfo *pGI = ( tagGuidableInfo * )pBuffer;
  1914. #endif
  1915. GUID guid;
  1916. memcpy( &guid, &( pGI->guid ), sizeof( GUID ) );
  1917. if ( pGI->datalength > 0 )
  1918. {
  1919. const TRoleData *pRoleData = ( const TRoleData * )( pGI->szData );
  1920. BOOL bCRCCheck = TRUE;
  1921. if (pRoleData->dwFriendOffset == pRoleData->dwDataLen - 4 && pRoleData->nFriendCount == 0)
  1922. {
  1923. // 说明是加过校验的,所以好友的偏移比长度少了4个字节
  1924. DWORD dwCRC = 0;
  1925. dwCRC = CRC32(dwCRC, pRoleData, pRoleData->dwDataLen - 4);
  1926. DWORD dwCheck = *(DWORD *)(pGI->szData + pRoleData->dwDataLen - 4);
  1927. if (dwCheck != dwCRC)
  1928. {
  1929. FILE *fLog = fopen("crc_db_error", "a+b");
  1930. if(fLog) 
  1931. {
  1932. char buffer[255];
  1933. sprintf(buffer, "----rn%srb%srn", pRoleData->BaseInfo.szName, pRoleData->BaseInfo.caccname);
  1934. fwrite(buffer, 1, strlen(buffer), fLog);
  1935. fwrite(pRoleData, 1, pRoleData->dwDataLen, fLog);
  1936. fclose(fLog);
  1937. }
  1938. bCRCCheck = FALSE;
  1939. }
  1940. }
  1941. // cout << "AccName : " << pRoleData->BaseInfo.caccname;
  1942. // cout << " - Name : " << pRoleData->BaseInfo.szName << endl;
  1943. tagPermitPlayerLogin ppl;
  1944. ppl.cProtocol = c2s_permitplayerlogin;
  1945. memcpy( &( ppl.guid ), &guid, sizeof( GUID ) );
  1946. strcpy( ( char * )( ppl.szRoleName ), ( const char * )( pRoleData->BaseInfo.szName ) );
  1947. ppl.bPermit = false;
  1948. KCoreConnectInfo Info;
  1949. int nCurrentPlayerCount = m_pCoreServerShell->GetConnectInfo(&Info);
  1950. if (Info.nNumPlayer < m_nMaxPlayer && bCRCCheck ) // 去掉注释CRC错的用户就无法进入游戏了
  1951. {
  1952. int nIdx = m_pCoreServerShell->AddCharacter(pGI->nExtPoint, pGI->nChangePoint, (void *)pRoleData, &guid);
  1953. if (nIdx > 0)
  1954. {
  1955. ppl.bPermit = true;
  1956. }
  1957. }
  1958. m_pGatewayClient->SendPackToServer( ( const void * )&ppl, sizeof( tagPermitPlayerLogin ) );
  1959. }
  1960. }
  1961. break;
  1962. default:
  1963. break;
  1964. }
  1965. #ifndef _STANDALONE
  1966. try
  1967. {
  1968. if( pBuffer != NULL )
  1969. {
  1970. pBuffer->Release();
  1971. pBuffer = NULL; 
  1972. }
  1973. }
  1974. catch(...) 
  1975. {
  1976. //TRACE("SAFE_RELEASE errorn");
  1977. }
  1978. #endif
  1979. }
  1980. }
  1981. void KSwordOnLineSever::GatewaySmallPackProcess(const void *pData, size_t dataLength)
  1982. {
  1983. _ASSERT( pData && dataLength );
  1984. #ifndef _STANDALONE
  1985. BYTE cProtocol = CPackager::Peek( pData );
  1986. #else 
  1987. BYTE cProtocol = *(BYTE *)pData;
  1988. #endif
  1989. /* printf("-----REALndatalen = %u, data = ", dataLength);
  1990. for (int i = 0; i < 32; i++)
  1991. printf("[%02X] ", ((BYTE *)pData)[i]);
  1992. printf("n");*/
  1993. switch ( cProtocol )
  1994. {
  1995. case s2c_querymapinfo:
  1996. {
  1997. tagUpdateMapID *pUMI;
  1998. char sMapID[80];
  1999. int nMapCount = m_pCoreServerShell->GetGameData(SGDI_LOADEDMAP_ID, (unsigned int)&sMapID, sizeof(sMapID));
  2000. pUMI = ( tagUpdateMapID * )new BYTE[ nMapCount * sizeof(char) + sizeof( tagUpdateMapID ) ];
  2001. memset( pUMI, 0, nMapCount * sizeof(char) + sizeof( tagUpdateMapID ) );
  2002. pUMI->cProtocol = c2s_updatemapinfo;
  2003. pUMI->cMapCount = nMapCount;
  2004. memcpy( pUMI->szMapID, sMapID, nMapCount * sizeof(char));
  2005. /* printf("-----Send ndatalen = %u, data = ", nMapCount * sizeof(char) + sizeof( tagUpdateMapID ));
  2006. for (int i = 0; i < 32; i++)
  2007. printf("[%02X] ", ((BYTE *)pUMI)[i]);
  2008. printf("n");*/
  2009. m_pGatewayClient->SendPackToServer( ( const void * )pUMI, nMapCount * sizeof(char) + sizeof( tagUpdateMapID ) );
  2010. if(pUMI)
  2011. {
  2012. delete pUMI;
  2013. pUMI = NULL;
  2014. }
  2015. }
  2016. break;
  2017. case s2c_querygameserverinfo:
  2018. {
  2019. tagGameSvrInfo ni;
  2020. ni.cProtocol = c2s_updategameserverinfo;
  2021. ni.nIPAddr_Internet = m_dwInternetIp;
  2022. ni.nIPAddr_Intraner = m_dwIntranetIp;
  2023. ni.nPort = m_nServerPort;
  2024. ni.wCapability = m_nMaxPlayerCount;
  2025. m_pGatewayClient->SendPackToServer( ( const void * )&ni, sizeof( tagGameSvrInfo ) );
  2026. }
  2027. break;
  2028. case s2c_gateway_broadcast:
  2029. GatewayBoardCastProcess((const char*)pData, dataLength);
  2030. break;
  2031. }
  2032. }
  2033. void KSwordOnLineSever::GatewayBoardCastProcess(const char* pData, size_t dataLength)
  2034. {
  2035. //#ifndef _STANDALONE
  2036. if (dataLength != sizeof(tagGatewayBroadCast))
  2037. return;
  2038. tagGatewayBroadCast *pGatewayBroadCast;
  2039. pGatewayBroadCast = (tagGatewayBroadCast *)pData;
  2040. //printf("BroadCast type = %d, %08x, %08x, %dn",pGatewayBroadCast->uCmdType, (unsigned int)pGatewayBroadCast->szData, pGatewayBroadCast->szData, strlen(pGatewayBroadCast->szData));
  2041. switch(pGatewayBroadCast->uCmdType)
  2042. {
  2043. case AP_WARNING_ALL_PLAYER_QUIT:
  2044. {
  2045. m_pCoreServerShell->OperationRequest(SSOI_BROADCASTING, (unsigned int)pGatewayBroadCast->szData, strlen(pGatewayBroadCast->szData));
  2046. // TODO: Save Player and ExitGame when time out
  2047. printf("ALL_PLAYER_QUITn");
  2048. SetRunningStatus(FALSE);
  2049. // ExitAllPlayer();
  2050. }
  2051. break;
  2052. case AP_NOTIFY_GAMESERVER_SAFECLOSE:
  2053. {
  2054. m_pCoreServerShell->OperationRequest(SSOI_BROADCASTING, (unsigned int)pGatewayBroadCast->szData, strlen(pGatewayBroadCast->szData));
  2055. printf("Safe close servern");
  2056. SetRunningStatus(FALSE);
  2057. }
  2058. break;
  2059. case AP_NOTIFY_ALL_PLAYER:
  2060. {
  2061. m_pCoreServerShell->OperationRequest(SSOI_BROADCASTING, (unsigned int)pGatewayBroadCast->szData, strlen(pGatewayBroadCast->szData));
  2062. }
  2063. break;
  2064. default:
  2065. break;
  2066. }
  2067. //#endif
  2068. }
  2069. void KSwordOnLineSever::MainLoop()
  2070. {
  2071. #ifndef _STANDALONE
  2072. CCriticalSection::Owner locker( g_csFlow );
  2073. #else
  2074. g_mutexFlow.lock();
  2075. #endif
  2076. SavePlayerData();
  2077. PlayerLogoutGateway();
  2078. PlayerExchangeServer();
  2079. // m_pServer->PreparePackSink(); 空函数,没有调用的必要
  2080. m_pCoreServerShell->Breathe();
  2081. // 定期向数据库查询排名情况
  2082. #define defMAX_STAT_QUERY_TIME 18000 * 20 // 5小时查询
  2083. if ( 0 == ( m_nGameLoop % defMAX_STAT_QUERY_TIME ) )
  2084. {
  2085. if (m_pDatabaseClient)
  2086. {
  2087. TProcessData ProcessData;
  2088. ProcessData.nProtoId = c2s_gamestatistic;
  2089. ProcessData.nDataLen = 1;
  2090. ProcessData.ulIdentity = -1;
  2091. ProcessData.pDataBuffer[0] = 0;
  2092. m_pDatabaseClient->SendPackToServer(&ProcessData, sizeof(TProcessData));
  2093. }
  2094. }
  2095. // 检查玩家是否很长时间没有发PING值了(1min)
  2096. int lnID = m_nGameLoop % m_nMaxPlayer;
  2097. int nIndex = m_pGameStatus[lnID].nPlayerIndex;
  2098. if (nIndex > 0 && nIndex <= m_nMaxPlayer && m_pGameStatus[lnID].nGameStatus == enumPlayerPlaying)
  2099. {
  2100. #define defMAX_PING_TIMEOUT 60 * 20 // 60sec
  2101. #define defMAX_PING_INTERVAL 5 * 20
  2102. if (m_pGameStatus[lnID].nReplyPingTime != 0)
  2103. {
  2104. if (m_nGameLoop - m_pGameStatus[lnID].nSendPingTime > defMAX_PING_INTERVAL)
  2105. {
  2106. //printf("Send ping to client....n");
  2107. PingClient(lnID);
  2108. }
  2109. }
  2110. else if (m_nGameLoop - m_pGameStatus[lnID].nSendPingTime > defMAX_PING_TIMEOUT)
  2111. {
  2112. printf("no response from client(%d, %d), kill it...n", m_nGameLoop, m_pGameStatus[lnID].nSendPingTime);
  2113. m_pServer->ShutdownClient(lnID);
  2114. }
  2115. }
  2116. if (m_nGameLoop & 0x01)
  2117. {
  2118. m_pServer->SendPackToClient(-1);
  2119. }
  2120. m_nGameLoop++;
  2121. #ifdef _STANDALONE
  2122. g_mutexFlow.unlock();
  2123. #endif
  2124. }
  2125. void KSwordOnLineSever::PingClient(const unsigned long lnID)
  2126. {
  2127. _ASSERT(lnID < m_nMaxPlayer);
  2128. _ASSERT(m_pGameStatus[lnID].nPlayerIndex > 0 && m_pGameStatus[lnID].nPlayerIndex <= m_nMaxPlayer);
  2129. //printf("PingClient(%d) calledn", lnID);
  2130. PING_COMMAND pc;
  2131. pc.m_dwTime = m_nGameLoop;
  2132. pc.ProtocolType = s2c_ping;
  2133. m_pServer->PackDataToClient(lnID, &pc, sizeof(PING_COMMAND));
  2134. m_pGameStatus[lnID].nReplyPingTime = 0;
  2135. m_pGameStatus[lnID].nSendPingTime = m_nGameLoop;
  2136. }
  2137. void KSwordOnLineSever::ProcessPingReply(const unsigned long lnID, const char* pData, size_t dataLength)
  2138. {
  2139. _ASSERT(lnID < m_nMaxPlayer);
  2140. _ASSERT(m_pGameStatus[lnID].nPlayerIndex > 0 && m_pGameStatus[lnID].nPlayerIndex <= m_nMaxPlayer);
  2141. //printf("receive ping from client...n");
  2142. if (dataLength != sizeof(PING_CLIENTREPLY_COMMAND))
  2143. {
  2144. printf("ping cmd size not correct, may be Non-offical Client...n");
  2145. m_pServer->ShutdownClient(lnID);
  2146. return;
  2147. }
  2148. PING_CLIENTREPLY_COMMAND* pPC = (PING_CLIENTREPLY_COMMAND *)pData;
  2149. if (pPC->m_dwReplyServerTime != m_pGameStatus[lnID].nSendPingTime)
  2150. {
  2151. printf("wrong time in ping cmd content, kill it...n");
  2152. m_pServer->ShutdownClient(lnID);
  2153. return;
  2154. }
  2155. m_pGameStatus[lnID].nReplyPingTime = m_nGameLoop;
  2156. // reply client ping to client to announce ping interval;
  2157. PING_COMMAND pc;
  2158. pc.ProtocolType = s2c_replyclientping;
  2159. pc.m_dwTime = pPC->m_dwClientTime;
  2160. m_pServer->PackDataToClient(lnID, &pc, sizeof(PING_COMMAND));
  2161. }
  2162. // 保持连接的玩家自动存盘
  2163. void KSwordOnLineSever::SavePlayerData()
  2164. {
  2165. if (!m_pDatabaseClient)
  2166. return;
  2167. int i = 0;
  2168. TProcessData sProcessData;
  2169. sProcessData.nProtoId = c2s_roleserver_saveroleinfo;
  2170. sProcessData.ulIdentity = -1;
  2171. // 遍历网络连接
  2172. for (i = 0; i < m_nMaxPlayer; i++)
  2173. {
  2174. if (GetNetStatus(i) == enumNetUnconnect)
  2175. continue;
  2176. int nIndex = m_pGameStatus[i].nPlayerIndex;
  2177. if (nIndex <= 0 ||nIndex > m_nMaxPlayer)
  2178. continue;
  2179. //TRoleData* pData = (TRoleData *)m_pCoreServerShell->SavePlayerData(nIndex); 
  2180. if (m_pCoreServerShell->GetSaveStatus(nIndex) == SAVE_REQUEST)
  2181. {
  2182. SavePlayerData(nIndex, false);
  2183. }
  2184. }
  2185. }
  2186. BOOL KSwordOnLineSever::SavePlayerData(int nIndex, bool bUnLock)
  2187. {
  2188. if (!m_pDatabaseClient)
  2189. return FALSE;
  2190. if (nIndex <= 0 ||nIndex > m_nMaxPlayer)
  2191. return FALSE;
  2192. TProcessData sProcessData;
  2193. sProcessData.nProtoId = c2s_roleserver_saveroleinfo;
  2194. sProcessData.ulIdentity = nIndex;
  2195. sProcessData.bLeave = bUnLock;
  2196. TRoleData* pData = (TRoleData *)m_pCoreServerShell->SavePlayerDataAtOnce(nIndex);
  2197. if (pData && pData->dwDataLen)
  2198. {
  2199. sProcessData.nDataLen = pData->dwDataLen + sizeof(TProcessData) - 1;
  2200. pData->dwDataLen += 4;
  2201. // CRC
  2202. DWORD dwCRC = 0;
  2203. dwCRC = CRC32(dwCRC, pData, pData->dwDataLen - 4);
  2204. // CRC END
  2205. m_theSend.AddData(c2s_roleserver_saveroleinfo, (const char*)&sProcessData, sizeof(TProcessData) - 1);
  2206. m_theSend.AddData(c2s_roleserver_saveroleinfo, (const char*)pData, pData->dwDataLen - 4);
  2207. m_theSend.AddData(c2s_roleserver_saveroleinfo, (const char*)&dwCRC, 4);
  2208. #ifndef _STANDALONE
  2209. CBuffer* pBuffer = m_theSend.GetHeadPack(c2s_roleserver_saveroleinfo);
  2210. #else
  2211. size_t actLen = 0;
  2212. char* pBuffer = m_theSend.GetHeadPack(c2s_roleserver_saveroleinfo, actLen);
  2213. #endif
  2214. while(pBuffer)
  2215. {
  2216. #ifndef _STANDALONE
  2217. m_pDatabaseClient->SendPackToServer(pBuffer->GetBuffer(), pBuffer->GetUsed());
  2218. #else
  2219. m_pDatabaseClient->SendPackToServer(pBuffer, actLen);
  2220. #endif
  2221. // cout << "Save small package:" << pBuffer->GetUsed() << endl;
  2222. #ifndef _STANDALONE
  2223. try
  2224. {
  2225. if (pBuffer)
  2226. {
  2227. pBuffer->Release();
  2228. pBuffer = NULL;
  2229. }
  2230. }
  2231. catch(...) 
  2232. {
  2233. //TRACE("SAFE_RELEASE errorn");
  2234. }
  2235. #endif
  2236. #ifndef _STANDALONE
  2237. pBuffer = m_theSend.GetNextPack(c2s_roleserver_saveroleinfo);
  2238. #else
  2239. pBuffer = m_theSend.GetNextPack(c2s_roleserver_saveroleinfo, actLen);
  2240. #endif
  2241. }
  2242. m_theSend.DelData(c2s_roleserver_saveroleinfo);
  2243. #ifndef _STANDALONE
  2244. try
  2245. {
  2246. if (pBuffer)
  2247. {
  2248. pBuffer->Release();
  2249. pBuffer = NULL;
  2250. }
  2251. }
  2252. catch(...) 
  2253. {
  2254. //TRACE("SAFE_RELEASE errorn");
  2255. }
  2256. #endif
  2257. //printf("Saving %s(%d)'s data, size:(%d)...n", pData->BaseInfo.szName, nIndex, sProcessData.nDataLen);
  2258. m_pCoreServerShell->SetSaveStatus(nIndex, SAVE_DOING);
  2259. //printf("Save Player %d data ok.....n", nIndex);
  2260. return TRUE;
  2261. }
  2262. //printf("Save Player %d data failed.....n", nIndex);
  2263. return FALSE;
  2264. }
  2265. void KSwordOnLineSever::PlayerExchangeServer()
  2266. {
  2267. if (!m_pGatewayClient || !m_pCoreServerShell)
  2268. return;
  2269. int i;
  2270. char szChar[1024];
  2271. // 遍历网络连接
  2272. for (i = 0; i < m_nMaxPlayer; i++)
  2273. {
  2274. int nIndex = m_pGameStatus[i].nPlayerIndex;
  2275. if (nIndex <= 0 || nIndex > m_nMaxPlayer)
  2276. continue;
  2277. if (m_pCoreServerShell->IsPlayerExchangingServer(nIndex))
  2278. {
  2279. if (!m_pTransferClient)
  2280. {
  2281. tagNotifyPlayerExchange npe;
  2282. npe.cProtocol = s2c_notifyplayerexchange;
  2283. memset(&npe.guid, 0, sizeof(GUID));
  2284. // if false, ip to 0
  2285. npe.nIPAddr = 0;
  2286. npe.nPort = 0;
  2287. m_pServer->SendData(i, &npe, sizeof(tagNotifyPlayerExchange));
  2288. m_pCoreServerShell->RecoverPlayerExchange(nIndex);
  2289. tagRoleEnterGame reg;
  2290. reg.ProtocolType = c2s_roleserver_lock;
  2291. reg.bLock = true;
  2292. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)reg.Name, nIndex);
  2293. if (m_pDatabaseClient)
  2294. m_pDatabaseClient->SendPackToServer((const void *)&reg, sizeof(tagRoleEnterGame));
  2295. m_pGameStatus[i].nGameStatus = enumPlayerPlaying;
  2296. m_pGameStatus[i].nExchangeStatus = enumExchangeBegin;
  2297. continue;
  2298. }
  2299. m_pGameStatus[i].nGameStatus = enumPlayerExchangingServer;
  2300. switch (m_pGameStatus[i].nExchangeStatus)
  2301. {
  2302. case enumExchangeBegin:
  2303. {
  2304. if (!SavePlayerData(nIndex, true))
  2305. continue;
  2306. RELAY_ASKWAY_DATA *pRAD = (RELAY_ASKWAY_DATA *)szChar;
  2307. pRAD->ProtocolFamily = pf_relay;
  2308. pRAD->ProtocolID = relay_c2c_askwaydata;
  2309. pRAD->nFromIP = 0; // 0.0.0.0
  2310. pRAD->nFromRelayID = 0;
  2311. pRAD->seekRelayCount = 0;
  2312. // 利用地图ID找SERVER
  2313. pRAD->seekMethod = rm_map_id;
  2314. pRAD->wMethodDataLength = 4;
  2315. pRAD->routeDateLength = sizeof(tagSearchWay);
  2316. *(DWORD *)(szChar + sizeof(RELAY_ASKWAY_DATA)) = m_pCoreServerShell->GetExchangeMap(nIndex);
  2317. tagSearchWay *pSW = (tagSearchWay *)(szChar + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength);
  2318. // pSW->ProtocolFamily = pf_gameworld;
  2319. // pSW->ProtocolID = c2c_notifyexchange;
  2320. pSW->cProtocol = c2c_notifyexchange;
  2321. pSW->lnID = i;
  2322. pSW->nIndex = m_pGameStatus[i].nPlayerIndex;
  2323. pSW->dwPlayerID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, pSW->nIndex, 0);
  2324. // ask another game svr ip
  2325. m_pTransferClient->SendPackToServer(pRAD, sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength + pRAD->routeDateLength);
  2326. m_pGameStatus[i].nExchangeStatus = enumExchangeSearchingWay;
  2327. }
  2328. break;
  2329. case enumExchangeSearchingWay:
  2330. break;
  2331. case enumExchangeWaitForGameSvrRespone:
  2332. {
  2333. }
  2334. break;
  2335. case enumExchangeCleaning:
  2336. {
  2337. }
  2338. break;
  2339. }
  2340. }
  2341. }
  2342. }
  2343. void KSwordOnLineSever::PlayerLogoutGateway()
  2344. {
  2345. if (!m_pGatewayClient || !m_pCoreServerShell)
  2346. return;
  2347. tagLeaveGame sLeaveGame;
  2348. sLeaveGame.cProtocol = c2s_leavegame;
  2349. sLeaveGame.cCmdType = NORMAL_LEAVEGAME;
  2350. tagLeaveGame2 lg2;
  2351. lg2.ProtocolFamily = pf_normal;
  2352. lg2.ProtocolID = c2s_leavegame;
  2353. lg2.cCmdType = NORMAL_LEAVEGAME;
  2354. // 遍历玩家(不能遍历连接,因为Timeout部分的玩家没有连接)
  2355. for (int nIndex = 1; nIndex < MAX_PLAYER; nIndex++)
  2356. {
  2357. // Clear player which connect to gateway but not connect to gamesvr
  2358. if (m_pCoreServerShell->IsPlayerLoginTimeOut(nIndex))
  2359. {
  2360. char szName[32];
  2361. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ACCOUNT, (unsigned int)szName, nIndex);
  2362. sLeaveGame.nExtPoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINTCHANGED, nIndex, 0);
  2363. strcpy((char *)sLeaveGame.szAccountName, (char *)szName);
  2364. m_pGatewayClient->SendPackToServer(&sLeaveGame, sizeof(tagLeaveGame));
  2365. strcpy((char *)lg2.szAccountName, (char *)szName);
  2366. if (m_pTransferClient)
  2367. m_pTransferClient->SendPackToServer(&lg2, sizeof(tagLeaveGame2));
  2368. // cout << sLeaveGame.szAccountName << " leave game!" << endl;
  2369. m_pCoreServerShell->RemovePlayerLoginTimeOut(nIndex);
  2370. }
  2371. else if (m_pCoreServerShell->IsCharacterQuiting(nIndex))
  2372. {
  2373. SavePlayerData(nIndex, true);
  2374. // if (!SavePlayerData(nIndex))
  2375. // continue;
  2376. char szName[32];
  2377. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ACCOUNT, (unsigned int)szName, nIndex);
  2378. sLeaveGame.nExtPoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINTCHANGED, nIndex, 0);
  2379. strcpy((char *)sLeaveGame.szAccountName, (char *)szName);
  2380. m_pGatewayClient->SendPackToServer(&sLeaveGame, sizeof(tagLeaveGame));
  2381. strcpy((char *)lg2.szAccountName, (char *)szName);
  2382. if (m_pTransferClient)
  2383. m_pTransferClient->SendPackToServer(&lg2, sizeof(tagLeaveGame2));
  2384. // cout << sLeaveGame.szAccountName << " leave game!" << endl;
  2385. /*
  2386. m_pGameStatus[lnID].nExchangeStatus = enumExchangeBegin;
  2387. m_pGameStatus[lnID].nGameStatus = enumPlayerBegin;
  2388. m_pGameStatus[lnID].nPlayerIndex = 0;
  2389. */
  2390. m_pCoreServerShell->RemoveQuitingPlayer(nIndex);
  2391. }
  2392. }
  2393. }
  2394. int KSwordOnLineSever::GetNetStatus(const unsigned long lnID)
  2395. {
  2396. if (lnID >= m_nMaxPlayer)
  2397. return enumNetUnconnect;
  2398. return m_pGameStatus[lnID].nNetStatus;
  2399. }
  2400. void KSwordOnLineSever::SetNetStatus(const unsigned long lnID, NetStatus nStatus)
  2401. {
  2402. if (lnID >= m_nMaxPlayer)
  2403. return;
  2404. if (nStatus == enumNetUnconnect)
  2405. {
  2406. if (m_pGameStatus[lnID].nGameStatus == enumPlayerPlaying
  2407. || (m_pGameStatus[lnID].nGameStatus == enumPlayerExchangingServer 
  2408. && m_pGameStatus[lnID].nExchangeStatus != enumExchangeCleaning)) // 跨服务器时自己处理
  2409. {
  2410. int nIndex = m_pGameStatus[lnID].nPlayerIndex;
  2411. m_pCoreServerShell->ClientDisconnect(nIndex);
  2412. }
  2413. else
  2414. {
  2415. int nIndex = m_pGameStatus[lnID].nPlayerIndex;
  2416. m_pCoreServerShell->PreparePlayerForLoginFailed(nIndex);
  2417. }
  2418. m_pGameStatus[lnID].nGameStatus = enumPlayerBegin;
  2419. m_pGameStatus[lnID].nPlayerIndex = 0;
  2420. m_pGameStatus[lnID].nExchangeStatus = enumExchangeBegin;
  2421. m_pGameStatus[lnID].nReplyPingTime = 0;
  2422. m_pGameStatus[lnID].nSendPingTime = 0;
  2423. }
  2424. if (nStatus == enumNetConnected)
  2425. {
  2426. m_pGameStatus[lnID].nGameStatus = enumPlayerBegin;
  2427. m_pGameStatus[lnID].nPlayerIndex = 0;
  2428. m_pGameStatus[lnID].nExchangeStatus = enumExchangeBegin;
  2429. m_pGameStatus[lnID].nReplyPingTime = 0;
  2430. m_pGameStatus[lnID].nSendPingTime = 0;
  2431. }
  2432. m_pGameStatus[lnID].nNetStatus = nStatus;
  2433. }
  2434. int KSwordOnLineSever::ProcessLoginProtocol(const unsigned long lnID, const char* pData, size_t dataLength)
  2435. {
  2436. const char* pBuffer = pData;
  2437. if (*pBuffer != c2s_logiclogin)
  2438. {
  2439. return 0;
  2440. }
  2441. else
  2442. {
  2443. tagLogicLogin *pLL = ( tagLogicLogin * )pData;
  2444. // cout << "A client try to login..." << endl;
  2445. int nIdx = m_pCoreServerShell->AttachPlayer(lnID, &pLL->guid);
  2446. if (nIdx)
  2447. {
  2448. // cout << "Found player " << nIdx << " is logging in system!" << endl;
  2449. return nIdx;
  2450. }
  2451. else
  2452. {
  2453. // 非法的玩家,该怎么处理怎么处理
  2454. return 0;
  2455. }
  2456. }
  2457. return 0;
  2458. }
  2459. BOOL KSwordOnLineSever::SendGameDataToClient(const unsigned long lnID, const int nPlayerIndex)
  2460. {
  2461. BOOL bRet = FALSE;
  2462. //#ifndef _STANDALONE
  2463. _ASSERT(m_pServer);
  2464. int nStep = 0;
  2465. unsigned int nParam = 0;
  2466. int bSyncEnd = 0;
  2467. m_pServer->PreparePackSink();
  2468. while(true)
  2469. {
  2470. bSyncEnd = (nStep == STEP_SYNC_END);
  2471. bRet = m_pCoreServerShell->PlayerDbLoading(nPlayerIndex, bSyncEnd, nStep, nParam);
  2472. if (!bRet)
  2473. {
  2474. printf("PlayerDbLoading failed.n"); //[wxb 2003-7-28]
  2475. break;
  2476. }
  2477. if (bSyncEnd)
  2478. {
  2479. m_pGameStatus[lnID].nGameStatus = enumPlayerSyncEnd;
  2480. BYTE SyncEnd;
  2481. SyncEnd = s2c_syncend;
  2482. #ifndef _STANDALONE
  2483. if (FAILED(m_pServer->PackDataToClient(lnID, &SyncEnd, sizeof(BYTE))))
  2484. #else
  2485. if (!SUCCEEDED(m_pServer->PackDataToClient(lnID, &SyncEnd, sizeof(BYTE))))
  2486. #endif
  2487. {
  2488. printf("Packing failed. %d, %d, %dn", lnID); //[wxb 2003-7-28]
  2489. bRet = FALSE;
  2490. break;
  2491. }
  2492. g_DebugLog("[TRACE]SyncEnd:%d", lnID);
  2493. break;
  2494. }
  2495. }
  2496. #ifdef WIN32
  2497. if (FAILED(m_pServer->SendPackToClient(lnID)))
  2498. {
  2499. bRet = FALSE;
  2500. }
  2501. #else
  2502. if (m_pServer->SendPackToClient(lnID) <= 0)
  2503. bRet = FALSE;
  2504. #endif
  2505. return bRet;
  2506. }
  2507. BOOL KSwordOnLineSever::ProcessSyncReplyProtocol(const unsigned long lnID, const char* pData, size_t dataLength)
  2508. {
  2509. const char* pBuffer = pData;
  2510. if (*pBuffer != c2s_syncend)
  2511. {
  2512. return FALSE;
  2513. }
  2514. else
  2515. {
  2516. return TRUE;
  2517. }
  2518. return FALSE;
  2519. }
  2520. void KSwordOnLineSever::ExitAllPlayer()
  2521. {
  2522. #ifndef __linux //SwitchToThread();
  2523. Sleep(4000);
  2524. #else
  2525. usleep(4000000);
  2526. #endif
  2527. int lnID;
  2528. const char* pData;
  2529. unsigned int uSize;
  2530. tagLeaveGame sLeaveGame;
  2531. sLeaveGame.cProtocol = c2s_leavegame;
  2532. sLeaveGame.cCmdType = NORMAL_LEAVEGAME;
  2533. for (lnID = 0; lnID < m_nMaxPlayer; lnID++)
  2534. {
  2535. int nIndex = m_pGameStatus[lnID].nPlayerIndex;
  2536. if (nIndex == 0)
  2537. continue;
  2538. BOOL bSave = SavePlayerData(nIndex, true);
  2539. int nCount = 0;
  2540. while(bSave)
  2541. {
  2542. nCount++;
  2543. pData = (const char *)m_pDatabaseClient->GetPackFromServer(uSize);
  2544. if (pData && uSize)
  2545. {
  2546. DatabaseMessageProcess(pData, uSize);
  2547. if (m_pCoreServerShell->GetSaveStatus(nIndex) == SAVE_IDLE)
  2548. break;
  2549. }
  2550. if (nCount > 1000)
  2551. break;
  2552. #ifdef WIN32
  2553. Sleep(1);
  2554. #else
  2555. usleep(1000);
  2556. #endif
  2557. }
  2558. m_pCoreServerShell->ClientDisconnect(nIndex);
  2559. if (m_pCoreServerShell->IsCharacterQuiting(nIndex))
  2560. {
  2561. char szName[32];
  2562. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ACCOUNT, (unsigned int)szName, nIndex);
  2563. sLeaveGame.nExtPoint = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_EXTPOINTCHANGED, nIndex, 0);
  2564. strcpy((char *)sLeaveGame.szAccountName, (char *)szName);
  2565. m_pGatewayClient->SendPackToServer(&sLeaveGame, sizeof(tagLeaveGame));
  2566. if (m_pTransferClient)
  2567. m_pTransferClient->SendPackToServer(&sLeaveGame, sizeof(tagLeaveGame));
  2568. m_pCoreServerShell->RemoveQuitingPlayer(nIndex);
  2569. }
  2570. #ifdef WIN32
  2571. Sleep(1);
  2572. #else
  2573. usleep(1000);
  2574. #endif
  2575. }
  2576. }
  2577. void KSwordOnLineSever::SetRunningStatus(BOOL bStatus)
  2578. {
  2579. m_bIsRunning = bStatus;
  2580. }
  2581. BOOL KSwordOnLineSever::ConformAskWay(const void* pData, int nSize, DWORD *lpnNetID)
  2582. {
  2583. BOOL bRet = FALSE;
  2584. RELAY_ASKWAY_DATA* pRAD = (RELAY_ASKWAY_DATA *)pData;
  2585. // TODO: 检查是否寻路正确
  2586. int nMethod = pRAD->seekMethod;
  2587. switch(nMethod)
  2588. {
  2589. case rm_role_id:
  2590. case rm_account_id:
  2591. {
  2592. DWORD dwNameID;
  2593. DWORD lnID;
  2594. LPDWORD lpdwPTR = (DWORD *)(((char *)pData) + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength - sizeof(DWORD)*2);
  2595. dwNameID = *lpdwPTR;
  2596. lnID = *(lpdwPTR + 1);
  2597. if (lnID >= m_nMaxPlayer || m_pGameStatus[lnID].nPlayerIndex <= 0 || m_pGameStatus[lnID].nPlayerIndex > m_nMaxPlayer)
  2598. {
  2599. bRet = FALSE;
  2600. break;
  2601. }
  2602. DWORD dwNameIDbyIndex = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, m_pGameStatus[lnID].nPlayerIndex, 0);
  2603. if (dwNameIDbyIndex != dwNameID)
  2604. {
  2605. bRet = FALSE;
  2606. break;
  2607. }
  2608. *lpnNetID = lnID;
  2609. bRet = TRUE;
  2610. }
  2611. break;
  2612. case rm_map_id:
  2613. {
  2614. DWORD dwMapID;
  2615. dwMapID = *(DWORD *)(((char *)pData) + sizeof(RELAY_ASKWAY_DATA));
  2616. if (0) // TODO:check mapid here
  2617. {
  2618. bRet = FALSE;
  2619. break;
  2620. }
  2621. bRet = TRUE;
  2622. }
  2623. break;
  2624. }
  2625. return bRet;
  2626. }
  2627. BOOL KSwordOnLineSever::GetLocalIpAddress(DWORD *pIntranetAddr, DWORD *pInternetAddr)
  2628. {
  2629. #ifndef _STANDALONE
  2630. return gGetMacAndIPAddress(NULL, pIntranetAddr, NULL, pInternetAddr);
  2631. #else
  2632. char szIp[16];
  2633. DWORD dwAddr1, dwAddr2;
  2634. dwAddr1 = dwAddr2 = 0;
  2635. char strHost[MAX_PATH];
  2636. strHost[0] = 0;
  2637. if(SOCKET_ERROR != gethostname(strHost, sizeof(strHost))) {
  2638. struct hostent* hp;
  2639. hp = gethostbyname( strHost );
  2640. #ifndef WIN32
  2641. if (!hp ||
  2642. hp && hp->h_addr_list[0] && *(unsigned long *)hp->h_addr_list[0] == 0x0100007f)
  2643. {
  2644. int sock;
  2645. struct ifreq ifr;
  2646. char* ip = NULL;
  2647. int err = -1;
  2648. sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  2649. if (sock >= 0) {
  2650. strcpy(ifr.ifr_name, "eth0");
  2651. ifr.ifr_addr.sa_family = AF_INET;
  2652. err = ioctl(sock, SIOCGIFADDR, &ifr);
  2653. if (err == 0) {
  2654. dwAddr1 = (((struct sockaddr_in*) &(ifr.ifr_addr))->sin_addr).s_addr;
  2655. ip =  inet_ntoa(((struct sockaddr_in*) &(ifr.ifr_addr))->sin_addr);
  2656. printf("IP-addr1: %sn", ip);
  2657. }
  2658. strcpy(ifr.ifr_name, "eth1");
  2659. ifr.ifr_addr.sa_family = AF_INET;
  2660. err = ioctl(sock, SIOCGIFADDR, &ifr);
  2661. if (err == 0) {
  2662. dwAddr2 = (((struct sockaddr_in*) &(ifr.ifr_addr))->sin_addr).s_addr;
  2663. ip =  inet_ntoa(((struct sockaddr_in*) &(ifr.ifr_addr))->sin_addr);
  2664. printf("IP-addr2: %sn", ip);
  2665. }
  2666. else
  2667. dwAddr2 = dwAddr1;
  2668. closesocket(sock);
  2669. }
  2670. }
  2671. else
  2672. #endif
  2673. {
  2674. if(hp && hp->h_addr_list[0]) {
  2675. dwAddr1 = *(unsigned long *)hp->h_addr_list[0];
  2676. if (!dwAddr1)
  2677. dwAddr1 = *(unsigned long *)hp->h_addr_list[1];
  2678. /* *((char *)&dwAddr1) = hp->h_addr_list[0][3];
  2679. *((char *)&dwAddr1 + 1) = hp->h_addr_list[0][2];
  2680. *((char *)&dwAddr1 + 2) = hp->h_addr_list[0][1];
  2681. *((char *)&dwAddr1 + 3) = hp->h_addr_list[0][0];*/
  2682. // dwAddr1 = hp->h_addr_list[0];
  2683. }
  2684. if(hp && hp->h_addr_list[1]) {
  2685. dwAddr2 = *(unsigned long *)hp->h_addr_list[1];
  2686. if (!dwAddr2)
  2687. dwAddr2 = *(unsigned long *)hp->h_addr_list[0];
  2688. // dwAddr2 = hp->h_addr_list[1];
  2689. /* *((char *)&dwAddr2) = hp->h_addr_list[1][3];
  2690. *((char *)&dwAddr2 + 1) = hp->h_addr_list[1][2];
  2691. *((char *)&dwAddr2 + 2) = hp->h_addr_list[1][1];
  2692. *((char *)&dwAddr2 + 3) = hp->h_addr_list[1][0];*/
  2693. }
  2694. else
  2695. dwAddr2 = dwAddr1;
  2696. }
  2697. }
  2698. if ((dwAddr1 & 0x0000FFFF) == 0x0000a8c0) // intranet
  2699. {
  2700. *pIntranetAddr = dwAddr1;
  2701. *pInternetAddr = dwAddr2;
  2702. }
  2703. else
  2704. {
  2705. *pIntranetAddr = dwAddr2;
  2706. *pInternetAddr = dwAddr1;
  2707. }
  2708. return TRUE;
  2709. #endif
  2710. }
  2711. void KSwordOnLineSever::TransferAskWayMessageProcess(const char *pData, size_t dataLength)
  2712. {
  2713. char szChar[1024];
  2714. EXTEND_HEADER *pEH = (EXTEND_HEADER *)pData;
  2715. RELAY_ASKWAY_DATA *pRAD = (RELAY_ASKWAY_DATA *)pData;
  2716. RELAY_DATA *pRD = (RELAY_DATA *)szChar;
  2717. pRD->ProtocolFamily = pEH->ProtocolFamily;
  2718. pRD->nToIP = pRAD->nFromIP;
  2719. pRD->nToRelayID = pRAD->nFromRelayID;
  2720. pRD->nFromIP = 0;
  2721. pRD->nFromRelayID = 0;
  2722. pRD->routeDateLength = pRAD->routeDateLength;
  2723. DWORD lnID;
  2724. // TODO: check routeDataLength
  2725. if (!ConformAskWay(pRAD, dataLength, &lnID))
  2726. {
  2727. pRD->ProtocolID = relay_s2c_loseway;
  2728. memcpy(szChar + sizeof(RELAY_DATA), pData, dataLength);
  2729. m_pTransferClient->SendPackToServer(pRD, sizeof(RELAY_DATA) + dataLength);
  2730. }
  2731. else
  2732. {
  2733. pRD->ProtocolID = relay_c2c_data;
  2734. switch(pRAD->seekMethod)
  2735. {
  2736. case rm_map_id:
  2737. memcpy(szChar + sizeof(RELAY_DATA), pData + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength, pRD->routeDateLength);
  2738. m_pTransferClient->SendPackToServer(pRD, sizeof(RELAY_DATA) + pRD->routeDateLength);
  2739. break;
  2740. case rm_role_id:
  2741. case rm_account_id:
  2742. {
  2743. if (pf_normal != *(pData + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength))
  2744. {
  2745. m_pCoreServerShell->ProcessNewClientMessage(
  2746. m_pTransferClient,
  2747. pRAD->nFromIP,
  2748. pRAD->nFromRelayID,
  2749. m_pGameStatus[lnID].nPlayerIndex, 
  2750. (pData + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength), 
  2751. pRAD->routeDateLength);
  2752. }
  2753. else
  2754. {
  2755. m_pCoreServerShell->ProcessClientMessage(
  2756. m_pGameStatus[lnID].nPlayerIndex, 
  2757. (pData + sizeof(RELAY_ASKWAY_DATA) + pRAD->wMethodDataLength + 1), 
  2758. pRAD->routeDateLength - 1);
  2759. }
  2760. }
  2761. break;
  2762. default:
  2763. break;
  2764. }
  2765. }
  2766. }
  2767. void KSwordOnLineSever::TransferLoseWayMessageProcess(const char *pData, size_t dataLength)
  2768. {
  2769. if (!m_pCoreServerShell || !m_pServer)
  2770. return;
  2771. EXTEND_HEADER* pInHeader = (EXTEND_HEADER*)pData;
  2772. if (pInHeader->ProtocolFamily == pf_relay)
  2773. {
  2774. tagSearchWay *pSW = NULL;
  2775. switch (pInHeader->ProtocolID)
  2776. {
  2777. case relay_c2c_data:
  2778. pSW = (tagSearchWay *)(pData + sizeof(RELAY_DATA));
  2779. break;
  2780. case relay_c2c_askwaydata:
  2781. pSW = (tagSearchWay *)(pData + sizeof(RELAY_ASKWAY_DATA) + ((RELAY_ASKWAY_DATA*)pData)->wMethodDataLength);
  2782. break;
  2783. default:
  2784. return;
  2785. }
  2786. int lnID, nIndex;
  2787. lnID = pSW->lnID;
  2788. if (lnID < 0 || lnID >= m_nMaxPlayer)
  2789. return;
  2790. if (m_pGameStatus[lnID].nExchangeStatus != enumExchangeSearchingWay)
  2791. return;
  2792. if (m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, pSW->nIndex, 0) != pSW->dwPlayerID)
  2793. return;
  2794. nIndex = m_pGameStatus[lnID].nPlayerIndex;
  2795. tagNotifyPlayerExchange npe;
  2796. npe.cProtocol = s2c_notifyplayerexchange;
  2797. memset(&npe.guid, 0, sizeof(GUID));
  2798. // if false, ip to 0
  2799. npe.nIPAddr = 0;
  2800. npe.nPort = 0;
  2801. m_pServer->SendData(lnID, &npe, sizeof(tagNotifyPlayerExchange));
  2802. m_pCoreServerShell->RecoverPlayerExchange(nIndex);
  2803. tagRoleEnterGame reg;
  2804. reg.ProtocolType = c2s_roleserver_lock;
  2805. reg.bLock = true;
  2806. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)reg.Name, nIndex);
  2807. if (m_pDatabaseClient)
  2808. m_pDatabaseClient->SendPackToServer((const void *)&reg, sizeof(tagRoleEnterGame));
  2809. m_pGameStatus[lnID].nExchangeStatus = enumExchangeBegin;
  2810. m_pGameStatus[lnID].nGameStatus = enumPlayerPlaying;
  2811. }
  2812. }
  2813. void KSwordOnLineSever::ProcessPlayerTongMsg(const unsigned long nPlayerIdx, const char* pData, size_t dataLength)
  2814. {
  2815. if (nPlayerIdx <= 0 || nPlayerIdx >= MAX_PLAYER)
  2816. return;
  2817. if (!pData)
  2818. return;
  2819. if (dataLength < sizeof(STONG_PROTOCOL_HEAD))
  2820. return;
  2821. int nPLength = ((STONG_PROTOCOL_HEAD*)pData)->m_wLength;
  2822. if (nPLength + 1 > dataLength)
  2823. return;
  2824. switch (((STONG_PROTOCOL_HEAD*)pData)->m_btMsgId)
  2825. {
  2826. // 申请创建帮会
  2827. case enumTONG_COMMAND_ID_APPLY_CREATE:
  2828. {
  2829. TONG_APPLY_CREATE_COMMAND *pApply = (TONG_APPLY_CREATE_COMMAND*)pData;
  2830. STONG_SERVER_TO_CORE_APPLY_CREATE sApply;
  2831. sApply.m_nCamp = pApply->m_btCamp;
  2832. sApply.m_nPlayerIdx = nPlayerIdx;
  2833. memcpy(sApply.m_szTongName, pApply->m_szName, sizeof(pApply->m_szName));
  2834. // 创建帮会条件检测
  2835. int nRet = 0xff;
  2836. if (m_pCoreServerShell)
  2837. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_APPLY_CREATE, (unsigned int)&sApply, 0);
  2838. // 创建帮会条件成立
  2839. if (nRet == 0)
  2840. {
  2841. char szPlayerName[32];
  2842. STONG_CREATE_COMMAND sCreate;
  2843. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)szPlayerName, nPlayerIdx);
  2844. sCreate.ProtocolFamily = pf_tong;
  2845. sCreate.ProtocolID = enumC2S_TONG_CREATE;
  2846. sCreate.m_btCamp = pApply->m_btCamp;
  2847. sCreate.m_dwParam = nPlayerIdx;
  2848. sCreate.m_dwPlayerNameID = g_FileName2Id(szPlayerName);
  2849. sCreate.m_btPlayerNameLength = strlen(szPlayerName);
  2850. sCreate.m_btTongNameLength = strlen(sApply.m_szTongName);
  2851. memcpy(sCreate.m_szBuffer, sApply.m_szTongName, sCreate.m_btTongNameLength);
  2852. memcpy(&sCreate.m_szBuffer[sCreate.m_btTongNameLength], szPlayerName, sCreate.m_btPlayerNameLength);
  2853. sCreate.m_wLength = sizeof(STONG_CREATE_COMMAND) - sizeof(sCreate.m_szBuffer) + sCreate.m_btTongNameLength + sCreate.m_btPlayerNameLength;
  2854. if (m_pTongClient)
  2855. m_pTongClient->SendPackToServer((const void*)&sCreate, sCreate.m_wLength);
  2856. break;
  2857. }
  2858. // 创建帮会条件不成立
  2859. else
  2860. {
  2861. int nNetID;
  2862. TONG_CREATE_FAIL_SYNC sFail;
  2863. sFail.ProtocolType = s2c_extendtong;
  2864. sFail.m_btMsgId = enumTONG_SYNC_ID_CREATE_FAIL;
  2865. sFail.m_btFailId = nRet;
  2866. sFail.m_wLength = sizeof(sFail) - 1;
  2867. nNetID = m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NETID, nPlayerIdx, 0);
  2868. if (m_pServer)
  2869. m_pServer->PackDataToClient(nNetID, &sFail, sFail.m_wLength + 1);
  2870. break;
  2871. }
  2872. }
  2873. break;
  2874. case enumTONG_COMMAND_ID_APPLY_ADD:
  2875. {
  2876. TONG_APPLY_ADD_COMMAND *pApply = (TONG_APPLY_ADD_COMMAND*)pData;
  2877. if (pApply->m_wLength != sizeof(TONG_APPLY_ADD_COMMAND) - 1)
  2878. break;
  2879. STONG_SERVER_TO_CORE_APPLY_ADD sAdd;
  2880. sAdd.m_nPlayerIdx = nPlayerIdx;
  2881. sAdd.m_dwNpcID = pApply->m_dwNpcID;
  2882. if (m_pCoreServerShell)
  2883. m_pCoreServerShell->GetGameData(SGDI_TONG_APPLY_ADD, (unsigned int)&sAdd, 0);
  2884. }
  2885. break;
  2886. case enumTONG_COMMAND_ID_ACCEPT_ADD:
  2887. {
  2888. TONG_ACCEPT_MEMBER_COMMAND *pAccept = (TONG_ACCEPT_MEMBER_COMMAND*)pData;
  2889. if (pAccept->m_wLength != sizeof(TONG_ACCEPT_MEMBER_COMMAND) - 1)
  2890. break;
  2891. if (pAccept->m_btFlag == 0)
  2892. {
  2893. STONG_SERVER_TO_CORE_REFUSE_ADD sRefuse;
  2894. sRefuse.m_nSelfIdx = nPlayerIdx;
  2895. sRefuse.m_nTargetIdx = pAccept->m_nPlayerIdx;
  2896. sRefuse.m_dwNameID = pAccept->m_dwNameID;
  2897. m_pCoreServerShell->OperationRequest(SSOI_TONG_REFUSE_ADD, (unsigned int)&sRefuse, 0);
  2898. break;
  2899. }
  2900. else
  2901. {
  2902. char szTongName[32];
  2903. STONG_SERVER_TO_CORE_CHECK_ADD_CONDITION sAdd;
  2904. sAdd.m_nSelfIdx = nPlayerIdx;
  2905. sAdd.m_nTargetIdx = pAccept->m_nPlayerIdx;
  2906. sAdd.m_dwNameID = pAccept->m_dwNameID;
  2907. int nRet = FALSE;
  2908. if (m_pCoreServerShell)
  2909. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_CHECK_ADD_CONDITION, (unsigned int)szTongName, (unsigned int)&sAdd);
  2910. // 向 relay 申请添加帮众
  2911. if (nRet)
  2912. {
  2913. char szPlayerName[32];
  2914. STONG_ADD_MEMBER_COMMAND sTong;
  2915. szPlayerName[0] = 0;
  2916. m_pCoreServerShell->GetGameData(SGDI_CHARACTER_NAME, (unsigned int)szPlayerName, sAdd.m_nTargetIdx);
  2917. sTong.ProtocolFamily = pf_tong;
  2918. sTong.ProtocolID = enumC2S_TONG_ADD_MEMBER;
  2919. sTong.m_dwParam = sAdd.m_nTargetIdx;
  2920. sTong.m_dwPlayerNameID = sAdd.m_dwNameID;
  2921. sTong.m_btPlayerNameLength = strlen(szPlayerName);
  2922. sTong.m_btTongNameLength = strlen(szTongName);
  2923. memcpy(sTong.m_szBuffer, szTongName, sTong.m_btTongNameLength);
  2924. memcpy(&sTong.m_szBuffer[sTong.m_btTongNameLength], szPlayerName, sTong.m_btPlayerNameLength);
  2925. sTong.m_wLength = sizeof(STONG_ADD_MEMBER_COMMAND) - sizeof(sTong.m_szBuffer) + sTong.m_btTongNameLength + sTong.m_btPlayerNameLength;
  2926. if (m_pTongClient)
  2927. m_pTongClient->SendPackToServer((const void*)&sTong, sTong.m_wLength);
  2928. }
  2929. else
  2930. {
  2931. break;
  2932. }
  2933. }
  2934. }
  2935. break;
  2936. case enumTONG_COMMAND_ID_APPLY_INFO:
  2937. {
  2938. TONG_APPLY_INFO_COMMAND *pInfo = (TONG_APPLY_INFO_COMMAND*)pData;
  2939. if (pInfo->m_wLength < sizeof(TONG_APPLY_INFO_COMMAND) - 1 - sizeof(pInfo->m_szBuf))
  2940. break;
  2941. STONG_SERVER_TO_CORE_GET_INFO sGet;
  2942. switch (pInfo->m_btInfoID)
  2943. {
  2944. case enumTONG_APPLY_INFO_ID_SELF:
  2945. sGet.m_nSelfIdx = nPlayerIdx;
  2946. sGet.m_nInfoID = pInfo->m_btInfoID;
  2947. sGet.m_nParam1 = pInfo->m_nParam1;
  2948. sGet.m_nParam2 = pInfo->m_nParam2;
  2949. sGet.m_nParam3 = pInfo->m_nParam3;
  2950. sGet.m_szName[0] = 0;
  2951. if (m_pCoreServerShell)
  2952. m_pCoreServerShell->GetGameData(SGDI_TONG_GET_INFO, (unsigned int)&sGet, 0);
  2953. break;
  2954. case enumTONG_APPLY_INFO_ID_MASTER:
  2955. break;
  2956. case enumTONG_APPLY_INFO_ID_DIRECTOR:
  2957. break;
  2958. case enumTONG_APPLY_INFO_ID_MANAGER:
  2959. {
  2960. int nRet = 0;
  2961. sGet.m_nSelfIdx = nPlayerIdx;
  2962. sGet.m_nInfoID = pInfo->m_btInfoID;
  2963. sGet.m_nParam1 = pInfo->m_nParam1;
  2964. sGet.m_nParam2 = pInfo->m_nParam2;
  2965. sGet.m_nParam3 = pInfo->m_nParam3;
  2966. sGet.m_szName[0] = 0;
  2967. if (m_pCoreServerShell)
  2968. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_GET_INFO, (unsigned int)&sGet, 0);
  2969. if (nRet == 0)
  2970. break;
  2971. STONG_GET_MANAGER_INFO_COMMAND sGet;
  2972. sGet.ProtocolFamily = pf_tong;
  2973. sGet.ProtocolID = enumC2S_TONG_GET_MANAGER_INFO;
  2974. sGet.m_dwParam = nPlayerIdx;
  2975. sGet.m_nParam1 = pInfo->m_nParam1;
  2976. sGet.m_nParam2 = pInfo->m_nParam2;
  2977. sGet.m_nParam3 = pInfo->m_nParam3;
  2978. if (m_pTongClient)
  2979. m_pTongClient->SendPackToServer((const void*)&sGet, sizeof(sGet));
  2980. }
  2981. break;
  2982. case enumTONG_APPLY_INFO_ID_MEMBER:
  2983. {
  2984. int nRet = 0;
  2985. sGet.m_nSelfIdx = nPlayerIdx;
  2986. sGet.m_nInfoID = pInfo->m_btInfoID;
  2987. sGet.m_nParam1 = pInfo->m_nParam1;
  2988. sGet.m_nParam2 = pInfo->m_nParam2;
  2989. sGet.m_nParam3 = pInfo->m_nParam3;
  2990. sGet.m_szName[0] = 0;
  2991. if (m_pCoreServerShell)
  2992. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_GET_INFO, (unsigned int)&sGet, 0);
  2993. if (nRet == 0)
  2994. break;
  2995. STONG_GET_MEMBER_INFO_COMMAND sGet;
  2996. sGet.ProtocolFamily = pf_tong;
  2997. sGet.ProtocolID = enumC2S_TONG_GET_MEMBER_INFO;
  2998. sGet.m_dwParam = nPlayerIdx;
  2999. sGet.m_nParam1 = pInfo->m_nParam1;
  3000. sGet.m_nParam2 = pInfo->m_nParam2;
  3001. sGet.m_nParam3 = pInfo->m_nParam3;
  3002. if (m_pTongClient)
  3003. m_pTongClient->SendPackToServer((const void*)&sGet, sizeof(sGet));
  3004. }
  3005. break;
  3006. case enumTONG_APPLY_INFO_ID_ONE:
  3007. break;
  3008. case enumTONG_APPLY_INFO_ID_TONG_HEAD:
  3009. {
  3010. DWORD dwTongNameID = 0;
  3011. sGet.m_nSelfIdx = nPlayerIdx;
  3012. sGet.m_nInfoID = pInfo->m_btInfoID;
  3013. sGet.m_nParam1 = pInfo->m_nParam1;
  3014. sGet.m_nParam2 = pInfo->m_nParam2;
  3015. sGet.m_nParam3 = pInfo->m_nParam3;
  3016. sGet.m_szName[0] = 0;
  3017. if (m_pCoreServerShell)
  3018. dwTongNameID = m_pCoreServerShell->GetGameData(SGDI_TONG_GET_INFO, (unsigned int)&sGet, 0);
  3019. if (dwTongNameID == 0)
  3020. break;
  3021. STONG_GET_TONG_HEAD_INFO_COMMAND sGet;
  3022. sGet.ProtocolFamily = pf_tong;
  3023. sGet.ProtocolID = enumC2S_TONG_GET_HEAD_INFO;
  3024. sGet.m_dwParam = nPlayerIdx;
  3025. sGet.m_dwNpcID = pInfo->m_nParam1;
  3026. sGet.m_dwTongNameID = dwTongNameID;
  3027. if (m_pTongClient)
  3028. m_pTongClient->SendPackToServer((const void*)&sGet, sizeof(sGet));
  3029. }
  3030. break;
  3031. }
  3032. }
  3033. break;
  3034. case enumTONG_COMMAND_ID_APPLY_INSTATE:
  3035. {
  3036. TONG_APPLY_INSTATE_COMMAND *pApply = (TONG_APPLY_INSTATE_COMMAND*)pData;
  3037. if (pApply->m_wLength + 1 != sizeof(TONG_APPLY_INSTATE_COMMAND))
  3038. break;
  3039. int nRet = 0;
  3040. if (m_pCoreServerShell)
  3041. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_INSTATE_POWER, (unsigned int)pApply, nPlayerIdx);
  3042. if (nRet == 0)
  3043. break;
  3044. STONG_INSTATE_COMMAND sInstate;
  3045. sInstate.ProtocolFamily = pf_tong;
  3046. sInstate.ProtocolID = enumC2S_TONG_INSTATE;
  3047. sInstate.m_btCurFigure = pApply->m_btCurFigure;
  3048. sInstate.m_btCurPos = pApply->m_btCurPos;
  3049. sInstate.m_btNewFigure = pApply->m_btNewFigure;
  3050. sInstate.m_btNewPos = pApply->m_btNewPos;
  3051. sInstate.m_dwParam = nPlayerIdx;
  3052. sInstate.m_dwTongNameID = pApply->m_dwTongNameID;
  3053. memset(sInstate.m_szName, 0, sizeof(sInstate.m_szName));
  3054. memcpy(sInstate.m_szName, pApply->m_szName, pApply->m_wLength + 1 + sizeof(pApply->m_szName) - sizeof(TONG_APPLY_INSTATE_COMMAND));
  3055. if (m_pTongClient)
  3056. m_pTongClient->SendPackToServer((const void*)&sInstate, sizeof(sInstate));
  3057. }
  3058. break;
  3059. case enumTONG_COMMAND_ID_APPLY_KICK:
  3060. {
  3061. TONG_APPLY_KICK_COMMAND *pKick = (TONG_APPLY_KICK_COMMAND*)pData;
  3062. if (pKick->m_wLength + 1 != sizeof(TONG_APPLY_KICK_COMMAND))
  3063. break;
  3064. int nRet = 0;
  3065. if (m_pCoreServerShell)
  3066. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_KICK_POWER, (unsigned int)pKick, nPlayerIdx);
  3067. if (nRet == 0)
  3068. break;
  3069. STONG_KICK_COMMAND sKick;
  3070. sKick.ProtocolFamily = pf_tong;
  3071. sKick.ProtocolID = enumC2S_TONG_KICK;
  3072. sKick.m_dwParam = nPlayerIdx;
  3073. sKick.m_dwTongNameID = pKick->m_dwTongNameID;
  3074. sKick.m_btFigure = pKick->m_btFigure;
  3075. sKick.m_btPos = pKick->m_btPos;
  3076. memcpy(sKick.m_szName, pKick->m_szName, sizeof(pKick->m_szName));
  3077. if (m_pTongClient)
  3078. m_pTongClient->SendPackToServer((const void*)&sKick, sizeof(sKick));
  3079. }
  3080. break;
  3081. case enumTONG_COMMAND_ID_APPLY_LEAVE:
  3082. {
  3083. TONG_APPLY_LEAVE_COMMAND *pLeave = (TONG_APPLY_LEAVE_COMMAND*)pData;
  3084. if (pLeave->m_wLength + 1 != sizeof(TONG_APPLY_LEAVE_COMMAND))
  3085. break;
  3086. int nRet = 0;
  3087. if (m_pCoreServerShell)
  3088. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_LEAVE_POWER, (unsigned int)pLeave, nPlayerIdx);
  3089. if (nRet == 0)
  3090. break;
  3091. STONG_LEAVE_COMMAND sLeave;
  3092. sLeave.ProtocolFamily = pf_tong;
  3093. sLeave.ProtocolID = enumC2S_TONG_LEAVE;
  3094. sLeave.m_dwParam = nPlayerIdx;
  3095. sLeave.m_dwTongNameID = pLeave->m_dwTongNameID;
  3096. sLeave.m_btFigure = pLeave->m_btFigure;
  3097. sLeave.m_btPos = pLeave->m_btPos;
  3098. memcpy(sLeave.m_szName, pLeave->m_szName, sizeof(pLeave->m_szName));
  3099. if (m_pTongClient)
  3100. m_pTongClient->SendPackToServer((const void*)&sLeave, sizeof(sLeave));
  3101. }
  3102. break;
  3103. case enumTONG_COMMAND_ID_APPLY_CHANGE_MASTER:
  3104. {
  3105. TONG_APPLY_CHANGE_MASTER_COMMAND *pChange = (TONG_APPLY_CHANGE_MASTER_COMMAND*)pData;
  3106. if (pChange->m_wLength + 1 != sizeof(TONG_APPLY_CHANGE_MASTER_COMMAND))
  3107. break;
  3108. int nRet = 0;
  3109. if (m_pCoreServerShell)
  3110. nRet = m_pCoreServerShell->GetGameData(SGDI_TONG_CHANGE_MASTER_POWER, (unsigned int)pChange, nPlayerIdx);
  3111. if (nRet == 0)
  3112. break;
  3113. STONG_CHANGE_MASTER_COMMAND sChange;
  3114. sChange.ProtocolFamily = pf_tong;
  3115. sChange.ProtocolID = enumC2S_TONG_CHANGE_MASTER;
  3116. sChange.m_btFigure = pChange->m_btFigure;
  3117. sChange.m_btPos = pChange->m_btPos;
  3118. sChange.m_dwParam = nPlayerIdx;
  3119. sChange.m_dwTongNameID = pChange->m_dwTongNameID;
  3120. memcpy(sChange.m_szName, pChange->m_szName, sizeof(pChange->m_szName));
  3121. if (m_pTongClient)
  3122. m_pTongClient->SendPackToServer((const void*)&sChange, sizeof(sChange));
  3123. }
  3124. break;
  3125. default:
  3126. break;
  3127. }
  3128. }
  3129. BOOL KSwordOnLineSever::CheckPlayerID(unsigned long netidx, DWORD nameid)
  3130. {
  3131. if (netidx < 0 || netidx >= m_nMaxPlayer)
  3132. return FALSE;
  3133. int idx = m_pGameStatus[netidx].nPlayerIndex;
  3134. if (idx <= 0 || idx >= MAX_PLAYER)
  3135. return FALSE;
  3136. if (nameid != m_pCoreServerShell->GetGameData(SGDI_CHARACTER_ID, idx, 0))
  3137. return FALSE;
  3138. return TRUE;
  3139. }