testAccServer.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 15k
Development Platform:

C/C++

  1. // testAccServer.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <objbase.h>
  5. #include <initguid.h>
  6. #pragma warning(disable : 4786)
  7. #include "KProtocolDef.h"
  8. #include "IClient.h"
  9. #include "RainbowInterface.h"
  10. #include "Library.h"
  11. #include "Macro.h"
  12. #include "Buffer.h"
  13. #include "Event.h"
  14. #include "Console.h"
  15. using OnlineGameLib::Win32::CLibrary;
  16. using OnlineGameLib::Win32::CPackager;
  17. using OnlineGameLib::Win32::CBuffer;
  18. using OnlineGameLib::Win32::CEvent;
  19. using OnlineGameLib::Win32::Console::setcolor;
  20. #include <string>
  21. #include <process.h>
  22. #include <iostream>
  23. #include <stdio.h>
  24. #include "../../Sword3PaySys/S3AccServer/AccountLoginDef.h"
  25. using namespace std;
  26. #pragma pack(push, 1)
  27. typedef struct
  28. {
  29. BYTE ProtocolType;
  30. DWORD m_dwTime;
  31. } PING_COMMAND;
  32. #pragma pack(pop)
  33. /*
  34.  * Global variable
  35.  */
  36. CLibrary g_theRainbowDll( "rainbow.dll" );
  37. CBuffer::Allocator m_theGlobalAllocator( 1024 * 1024, 10 );
  38. CEvent m_theQuitThreadEvent( NULL, true, false, NULL );
  39. CEvent m_theAfirmThreadQuitEvent( NULL, true, false, NULL );
  40. //const char *g_pServerIP = "192.168.22.105";
  41. const char *g_pServerIP = "192.168.20.15";
  42. const short g_nPort = 5002;
  43. typedef HRESULT ( __stdcall * pfnCreateClientInterface )(
  44. REFIID riid,
  45. void **ppv
  46. );
  47. void __stdcall ClientEventNotify(
  48. LPVOID lpParam,
  49. const unsigned long &ulnEventType )
  50. {
  51. switch( ulnEventType )
  52. {
  53. case enumServerConnectCreate:
  54. cout << "Server connection was created !" << endl;
  55. break;
  56. case enumServerConnectClose:
  57. cout << "Server connection was destroy !"  << endl;
  58. m_theQuitThreadEvent.Set();
  59. break;
  60. }
  61. }
  62. #define MAXFAST 4000
  63. unsigned __stdcall ThreadFunction( void *pParam )
  64. {
  65. IClient *pClient = ( IClient * )pParam;
  66. ASSERT( pClient );
  67. int nMax = 0;
  68. while ( !m_theQuitThreadEvent.Wait( 0 ) )
  69. {
  70. size_t dataLength = 0;
  71. const void *pData = pClient->GetPackFromServer( dataLength );
  72. if ( !pData || 0 == dataLength )
  73. {
  74. Sleep( 1 );
  75. continue;
  76. }
  77. nMax++;
  78. if (nMax < MAXFAST * 4)
  79. continue;
  80. BYTE cProtocol = CPackager::Peek( pData );
  81. switch ( cProtocol )
  82. {
  83. case s2c_gatewayverify:
  84. {
  85. KAccountUserReturn* pReturn = (KAccountUserReturn*)(((char*)pData) + 1);
  86. dataLength--;
  87. if (pReturn && dataLength == sizeof(KAccountUserReturn))
  88. {
  89. cout << "server " << pReturn->Account << " ";
  90. if (pReturn->nReturn == ACTION_SUCCESS)
  91. cout << "Login Success !";
  92. else if (pReturn->nReturn == E_ACCOUNT_OR_PASSWORD)
  93. cout << "Login Failed ! --- Name, Password";
  94. else if (pReturn->nReturn == E_ACCOUNT_EXIST)
  95. cout << "Login Failed ! --- Already login";
  96. else if (pReturn->nReturn == E_ADDRESS_OR_PORT)
  97. cout << "Login Failed ! --- Address, Port";
  98. else
  99. cout << "Login Failed !";
  100. cout << endl;
  101. }
  102. }
  103. break;
  104. case s2c_accountlogin:
  105. {
  106. KAccountUserReturn* pReturn = (KAccountUserReturn*)(((char*)pData) + 1);
  107. dataLength--;
  108. if (pReturn && dataLength == sizeof(KAccountUserReturn))
  109. {
  110. cout << "user " << pReturn->Account << " ";
  111. if (pReturn->nReturn == ACTION_SUCCESS)
  112. cout << "Login Success !";
  113. else if (pReturn->nReturn == E_ACCOUNT_OR_PASSWORD)
  114. cout << "Login Failed ! --- Name, Password";
  115. else if (pReturn->nReturn == E_ACCOUNT_EXIST)
  116. cout << "Login Failed ! --- Already login";
  117. else if (pReturn->nReturn == E_ACCOUNT_NODEPOSIT)
  118. cout << "Login Failed ! --- No Money";
  119. else
  120. cout << "Login Failed !";
  121. cout << endl;
  122. }
  123. }
  124. break;
  125. case s2c_gamelogin:
  126. {
  127. KAccountUserReturn* pReturn = (KAccountUserReturn*)(((char*)pData) + 1);
  128. dataLength--;
  129. if (pReturn && dataLength == sizeof(KAccountUserReturn))
  130. {
  131. cout << "user " << pReturn->Account << " ";
  132. if (pReturn->nReturn == ACTION_SUCCESS)
  133. cout << "Login Game Success !";
  134. else if (pReturn->nReturn == E_ACCOUNT_OR_PASSWORD)
  135. cout << "Login Game Failed ! --- Name, Password";
  136. else if (pReturn->nReturn == E_ACCOUNT_ACCESSDENIED)
  137. cout << "Login Game Failed ! --- login first";
  138. else
  139. cout << "Login Game Failed !";
  140. cout << endl;
  141. }
  142. }
  143. break;
  144. case s2c_accountlogout:
  145. {
  146. KAccountUserReturn* pReturn = (KAccountUserReturn*)(((char*)pData) + 1);
  147. dataLength--;
  148. if (pReturn && dataLength == sizeof(KAccountUserReturn))
  149. {
  150. cout << "user " << pReturn->Account << " ";
  151. if (pReturn->nReturn == ACTION_SUCCESS)
  152. cout << "Logout Success !";
  153. else if (pReturn->nReturn == E_ACCOUNT_OR_PASSWORD)
  154. cout << "Logout Failed ! --- Name, Password";
  155. else if (pReturn->nReturn == E_ACCOUNT_ACCESSDENIED)
  156. cout << "Logout Failed ! --- login first";
  157. else
  158. cout << "Logout Failed !";
  159. cout << endl;
  160. }
  161. }
  162. break;
  163. case s2c_gatewayinfo:
  164. {
  165. KServerInfo* pReturn = (KServerInfo*)(((char*)pData) + 1);
  166. dataLength--;
  167. if (pReturn && dataLength == sizeof(KServerInfo))
  168. {
  169. struct in_addr addr;
  170. switch (pReturn->nServerType)
  171. {
  172. case server_Login:
  173. cout << "server " << pReturn->Account << " ";
  174. addr.s_addr = pReturn->nValue;
  175. cout << "Login Success ! IP is " << inet_ntoa(addr);
  176. break;
  177. case server_Logout:
  178. cout << "server " << pReturn->Account << " ";
  179. addr.s_addr = pReturn->nValue;
  180. cout << "Logout Success ! IP is " << inet_ntoa(addr);
  181. break;
  182. case server_RegisterCount:
  183. cout << "server " << "total" << " ";
  184. cout << "Register User Count is " << pReturn->nValue;
  185. break;
  186. case server_OnlinePlayerCount:
  187. if (pReturn->Account[0] != 0)
  188. cout << "server " << pReturn->Account << " ";
  189. else
  190. cout << "server " << "total" << " ";
  191. cout << "Online Account Count is " << pReturn->nValue;
  192. break;
  193. case server_PlayerWhere:
  194. cout << "Player " << pReturn->Account << " ";
  195. addr.s_addr = pReturn->nValue;
  196. cout << "is login at Server " << inet_ntoa(addr);
  197. break;
  198. }
  199. cout << endl;
  200. }
  201. }
  202. break;
  203. case s2c_ping:
  204. {
  205. PING_COMMAND* pReturn = (PING_COMMAND*)pData;
  206. if (pReturn && dataLength == sizeof(PING_COMMAND))
  207. {
  208. cout << "server ping: " << pReturn->m_dwTime << " ";
  209. cout << endl;
  210. }
  211. }
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. cout << "The read thread was killed safely!" << endl;
  218. m_theAfirmThreadQuitEvent.Set();
  219. return 0;
  220. }
  221. void ServerLogin(IClient *pClient)
  222. {
  223. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  224. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  225. const size_t datalength = sizeof(KServerAccountUserLoginInfo) + 1;
  226. KServerAccountUserLoginInfo serlogin;
  227. serlogin.Size = sizeof(KServerAccountUserLoginInfo);
  228. serlogin.Type = ServerAccountUserLoginInfo;
  229. serlogin.Version = ACCOUNT_CURRENT_VERSION;
  230. strcpy(serlogin.Account, "wanli");
  231. strcpy(serlogin.Password, "48DFFCD3317D5A7B94B26CDCE8710CC7");
  232. *pData = c2s_gatewayverify;
  233. memcpy(pData + 1, &serlogin, sizeof(KServerAccountUserLoginInfo));
  234. pBuffer->Use(datalength);
  235. pClient->SendPackToServer((const void *)pData, datalength);
  236. SAFE_RELEASE(pBuffer);
  237. }
  238. void ServerLoginErr(IClient *pClient)
  239. {
  240. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  241. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  242. const size_t datalength = sizeof(KServerAccountUserLoginInfo) + 1;
  243. KServerAccountUserLoginInfo serlogin;
  244. serlogin.Size = sizeof(KServerAccountUserLoginInfo);
  245. serlogin.Type = ServerAccountUserLoginInfo;
  246. serlogin.Version = ACCOUNT_CURRENT_VERSION;
  247. strcpy(serlogin.Account, "wanli2");
  248. strcpy(serlogin.Password, "wanli");
  249. *pData = c2s_gatewayverify;
  250. memcpy(pData + 1, &serlogin, sizeof(KServerAccountUserLoginInfo));
  251. pBuffer->Use(datalength);
  252. pClient->SendPackToServer((const void *)pData, datalength);
  253. SAFE_RELEASE(pBuffer);
  254. }
  255. void ServerLoginAgain(IClient *pClient)
  256. {
  257. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  258. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  259. const size_t datalength = sizeof(KServerAccountUserLoginInfo) + 1;
  260. KServerAccountUserLoginInfo serlogin;
  261. serlogin.Size = sizeof(KServerAccountUserLoginInfo);
  262. serlogin.Type = ServerAccountUserLoginInfo;
  263. serlogin.Version = ACCOUNT_CURRENT_VERSION;
  264. strcpy(serlogin.Account, "wanli");
  265. strcpy(serlogin.Password, "48DFFCD3317D5A7B94B26CDCE8710CC7");
  266. *pData = c2s_gatewayverifyagain;
  267. memcpy(pData + 1, &serlogin, sizeof(KServerAccountUserLoginInfo));
  268. pBuffer->Use(datalength);
  269. pClient->SendPackToServer((const void *)pData, datalength);
  270. SAFE_RELEASE(pBuffer);
  271. }
  272. void UserLogin(IClient *pClient, const char* user, const char* password)
  273. {
  274. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  275. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  276. const size_t datalength = sizeof(KAccountUserLoginInfo) + 1;
  277. KAccountUserLoginInfo userlogin;
  278. userlogin.Size = sizeof(KAccountUserLoginInfo);
  279. userlogin.Type = AccountUserLoginInfo;
  280. userlogin.Version = ACCOUNT_CURRENT_VERSION;
  281. strcpy(userlogin.Account, user);
  282. strcpy(userlogin.Password, password);
  283. *pData = c2s_accountlogin;
  284. memcpy(pData + 1, &userlogin, sizeof(KAccountUserLoginInfo));
  285. pBuffer->Use(datalength);
  286. pClient->SendPackToServer((const void *)pData, datalength);
  287. SAFE_RELEASE(pBuffer);
  288. }
  289. void UserLogin1(IClient *pClient)
  290. {
  291. UserLogin(pClient, "wanlitest", "A63757F6C20852995151276AC40767EA");
  292. }
  293. void UserLogin2(IClient *pClient)
  294. {
  295. UserLogin(pClient, "wanli2", "wanli2");
  296. }
  297. void UserLoginAll(IClient *pClient)
  298. {
  299. char user[2];
  300. user[0] = 'a';
  301. user[1] = 0;
  302. char password[2];
  303. password[0] = 'a';
  304. password[1] = 0;
  305. for (int i = 0; i < 10; i++)
  306. {
  307. UserLogin(pClient, user, password);
  308. user[0]++;
  309. password[0]++;
  310. }
  311. }
  312. void UserLogout(IClient *pClient, const char* szAccount)
  313. {
  314. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  315. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  316. const size_t datalength = sizeof(KAccountUser) + 1;
  317. KAccountUser user;
  318. user.Size = sizeof(KAccountUser);
  319. user.Type = AccountUser;
  320. user.Version = ACCOUNT_CURRENT_VERSION;
  321. strcpy(user.Account, szAccount);
  322. *pData = c2s_accountlogout;
  323. memcpy(pData + 1, &user, sizeof(KAccountUser));
  324. pBuffer->Use(datalength);
  325. pClient->SendPackToServer((const void *)pData, datalength);
  326. SAFE_RELEASE(pBuffer);
  327. }
  328. void UserLogout1(IClient *pClient)
  329. {
  330. UserLogout(pClient, "wanlitest");
  331. }
  332. void UserGameLogin(IClient *pClient)
  333. {
  334. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  335. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  336. const size_t datalength = sizeof(KAccountUser) + 1;
  337. KAccountUser user;
  338. user.Size = sizeof(KAccountUser);
  339. user.Type = AccountUser;
  340. user.Version = ACCOUNT_CURRENT_VERSION;
  341. strcpy(user.Account, "wanlitest");
  342. *pData = c2s_gamelogin;
  343. memcpy(pData + 1, &user, sizeof(KAccountUser));
  344. pBuffer->Use(datalength);
  345. pClient->SendPackToServer((const void *)pData, datalength);
  346. SAFE_RELEASE(pBuffer);
  347. }
  348. void GatewayInfo(IClient *pClient, short nType, const char* szAccount)
  349. {
  350. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  351. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  352. const size_t datalength = sizeof(KServerInfo) + 1;
  353. KServerInfo user;
  354. user.Size = sizeof(KServerInfo);
  355. user.Type = ServerInfo;
  356. user.Version = ACCOUNT_CURRENT_VERSION;
  357. strcpy(user.Account, szAccount);
  358. user.nServerType = nType;
  359. *pData = c2s_gatewayinfo;
  360. memcpy(pData + 1, &user, sizeof(KServerInfo));
  361. pBuffer->Use(datalength);
  362. pClient->SendPackToServer((const void *)pData, datalength);
  363. SAFE_RELEASE(pBuffer);
  364. }
  365. void ServerPing(IClient *pClient)
  366. {
  367. CBuffer *pBuffer = m_theGlobalAllocator.Allocate();
  368. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  369. const size_t datalength = sizeof(PING_COMMAND);
  370. PING_COMMAND serping;
  371. serping.ProtocolType = c2s_ping;
  372. serping.m_dwTime = GetTickCount();
  373. memcpy(pData, &serping, sizeof(PING_COMMAND));
  374. pBuffer->Use(datalength);
  375. pClient->SendPackToServer((const void *)pData, datalength);
  376. SAFE_RELEASE(pBuffer);
  377. }
  378. /*
  379.  * main
  380.  */
  381. int main(int argc, char* argv[])
  382. {
  383. setcolor( enumCyanonBlack );
  384. cout << "Welcome to the example that it can be to test account server." << endl << endl;
  385. setcolor( enumDefault );
  386. IClient *pClient = NULL;
  387. pfnCreateClientInterface pFactroyFun = 
  388. (pfnCreateClientInterface)g_theRainbowDll.GetProcAddress("CreateInterface");
  389. IClientFactory *pClientFactory = NULL;
  390.  
  391. ASSERT(pFactroyFun);
  392. if (SUCCEEDED(pFactroyFun(IID_IClientFactory, reinterpret_cast< void ** >(&pClientFactory))))
  393. {
  394. pClientFactory->SetEnvironment(90 * 1024);
  395. pClientFactory->CreateClientInterface(IID_IESClient, reinterpret_cast< void ** >(&pClient ));
  396. pClientFactory->Release();
  397. }
  398. ASSERT(pClient);
  399. pClient->Startup();
  400. pClient->RegisterMsgFilter( reinterpret_cast< void * >( pClient ), ClientEventNotify );
  401. if ( FAILED( pClient->ConnectTo( g_pServerIP, g_nPort ) ) )
  402. {
  403. cout << "To connect the account server is failed!" << endl;
  404. exit( -1 );
  405. }
  406. IClient *pClonClient = NULL;
  407. pClient->QueryInterface( IID_IESClient, reinterpret_cast< void ** >( &pClonClient ) );
  408. unsigned int threadID = 0;
  409. HANDLE hThread = (HANDLE)::_beginthreadex(0,
  410. 0, 
  411. ThreadFunction,
  412. ( void * )pClonClient,
  413. 0, 
  414. &threadID );
  415. ASSERT( hThread );
  416. SAFE_CLOSEHANDLE( hThread );
  417. if (argc == 1)
  418. ServerLogin(pClient);
  419. else if (argc == 2)
  420. ServerLoginAgain(pClient);
  421. else
  422. ServerLoginErr(pClient);
  423. //m_theTaskEvent.Wait();
  424. /*
  425.  * System command
  426.  */
  427. _TRY_AGAIN:
  428. string sInfo;
  429. cout << "Command:" << endl;
  430. cin >> sInfo;
  431. if ( 0 == sInfo.compare( "userlogin1" ))
  432. {
  433. UserLogin1(pClient);
  434. }
  435. else if ( 0 == sInfo.compare( "userlogin" ))
  436. {
  437. string sUser;
  438. string sPassword;
  439. cout << "Account:" << endl;
  440. cin >> sUser;
  441. cout << "Password:" << endl;
  442. cin >> sPassword;
  443. UserLogin(pClient, sUser.c_str(), sPassword.c_str());
  444. }
  445. else if ( 0 == sInfo.compare( "userlogin2" ))
  446. {
  447. UserLogin2(pClient);
  448. }
  449. else if ( 0 == sInfo.compare( "userloginall" ))
  450. {
  451. UserLoginAll(pClient);
  452. }
  453. else if ( 0 == sInfo.compare( "fast" ))
  454. {
  455. for (int i = 0; i < MAXFAST; i++)
  456. {
  457. UserLogin1(pClient);
  458. UserGameLogin(pClient);
  459. UserLogout1(pClient);
  460. ServerPing(pClient);
  461. }
  462. }
  463. else if ( 0 == sInfo.compare( "usergamelogin" ))
  464. {
  465. UserGameLogin(pClient);
  466. }
  467. else if ( 0 == sInfo.compare( "userlogout" ))
  468. {
  469. string sUser;
  470. cout << "Account:" << endl;
  471. cin >> sUser;
  472. UserLogout(pClient, sUser.c_str());
  473. }
  474. else if ( 0 == sInfo.compare( "userlogout1" ))
  475. {
  476. UserLogout1(pClient);
  477. }
  478. else if ( 0 == sInfo.compare( "ping" ))
  479. {
  480. ServerPing(pClient);
  481. }
  482. else if ( 0 == sInfo.compare( "gatewayinfo" ))
  483. {
  484. string sUser;
  485. cout << "Account:" << endl;
  486. cin >> sUser;
  487. GatewayInfo(pClient, server_OnlinePlayerCount, sUser.c_str());
  488. GatewayInfo(pClient, server_RegisterCount, "");
  489. GatewayInfo(pClient, server_PlayerWhere, sUser.c_str());
  490. }
  491. if ( 0 != sInfo.compare( "exit" ))
  492. goto _TRY_AGAIN;
  493. m_theQuitThreadEvent.Set();
  494. m_theAfirmThreadQuitEvent.Wait();
  495. pClient->Shutdown();
  496. pClient->Cleanup();
  497. SAFE_RELEASE( pClient );
  498. return 0;
  499. }