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

模拟服务器

开发平台:

C/C++

  1. // ClientClone.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <objbase.h>
  5. #include <initguid.h>
  6. #include "....Sword3PaySysS3AccServerAccountLoginDef.h"
  7. #include "....s3clientloginLoginDef.h"
  8. #include "....RoleDBManagerkroledbheader.h"
  9. #include "S3DBInterface.h"
  10. #include "KProtocolDef.h"
  11. #include "KProtocol.h"
  12. #include "IClient.h"
  13. #include "RainbowInterface.h"
  14. #include "Macro.h"
  15. #include "Inifile.h"
  16. #include "Utils.h"
  17. #include "tstring.h"
  18. #include "Library.h"
  19. #include "Console.h"
  20. using OnlineGameLib::Win32::CIniFile;
  21. using OnlineGameLib::Win32::GetAppFullPath;
  22. using OnlineGameLib::Win32::_tstring;
  23. using OnlineGameLib::Win32::CLibrary;
  24. using OnlineGameLib::Win32::ToString;
  25. using OnlineGameLib::Win32::Console::setcolor;
  26. using OnlineGameLib::Win32::Console::gotoxy;
  27. using OnlineGameLib::Win32::Console::getxy;
  28. const size_t g_nTitleSpace = 3;
  29. const size_t g_nBufferSize = 256;
  30. _tstring g_sServerIPAddress;
  31. unsigned short g_nServerPort = 5622;
  32. _tstring g_sAccount, g_sPassword, g_sRoleName;
  33. CLibrary g_theRainbowLibrary( "rainbow.dll" );
  34. IClient *pClient = NULL;
  35. IClient *pGameSvrClient = NULL;
  36. #define CONFIG_FILENAME _T( "config.ini" )
  37. #define SVR_SETTING_FILENAME _T( "settings\ServerList.ini" )
  38. #include <iostream>
  39. #include <strstream>
  40. using namespace std;
  41. /*
  42.  * Using directives
  43.  */
  44. using std::auto_ptr;
  45. using std::endl;
  46. typedef HRESULT ( __stdcall * pfnCreateClientInterface )(
  47. REFIID riid,
  48. void **ppv
  49. );
  50. void __stdcall ClientEventNotify(
  51. LPVOID lpParam,
  52. const unsigned long &ulnEventType )
  53. {
  54. switch( ulnEventType )
  55. {
  56. case enumServerConnectCreate:
  57. cout << "Connection was created!" << endl;
  58. break;
  59. case enumServerConnectClose:
  60. cout << "Connection was destroy!" << endl;
  61. break;
  62. }
  63. }
  64. /*
  65.  * Global variable
  66.  */
  67. _tstring g_sAppFullPath;
  68. /*
  69.  * Helper function
  70.  */
  71. void ShowTitle( const char *pTile );
  72. void SelectServerList();
  73. void InputAccountAndPassword();
  74. UINT Login();
  75. int SelectRoleList();
  76. void WaitForEnterGameServer();
  77. void ConnectToGameSvr( const char *pIPAddr, unsigned short nPort, GUID &guid );
  78. void PlayGame();
  79. void InitGameWorld();
  80. void ConnectServer();
  81. void DisconnectServer();
  82. int g_nProtocolSize[MAX_PROTOCOL_NUM] = 
  83. {
  84. -1, // s2c_login,
  85. -1, // s2c_logout,
  86. sizeof(BYTE), // s2c_syncend,
  87. sizeof(CURPLAYER_SYNC), // s2c_synccurplayer,
  88. -1, // s2c_synccurplayerskill
  89. sizeof(CURPLAYER_NORMAL_SYNC),// s2c_synccurplayernormal
  90. -1, // s2c_newplayer,
  91. -1, // s2c_removeplayer,
  92. sizeof(WORLD_SYNC), // s2c_syncworld,
  93. sizeof(PLAYER_SYNC), // s2c_syncplayer,
  94. sizeof(PLAYER_NORMAL_SYNC), // s2c_syncplayermin,
  95. -1, //sizeof(NPC_SYNC), // s2c_syncnpc,
  96. sizeof(NPC_NORMAL_SYNC), // s2c_syncnpcmin,
  97. sizeof(NPC_PLAYER_TYPE_NORMAL_SYNC), // s2c_syncnpcminplayer,
  98. sizeof(OBJ_SYNC_ADD), // s2c_objadd,
  99. sizeof(OBJ_SYNC_STATE), // s2c_syncobjstate,
  100. sizeof(OBJ_SYNC_DIR), // s2c_syncobjdir,
  101. sizeof(OBJ_SYNC_REMOVE), // s2c_objremove,
  102. sizeof(OBJ_SYNC_TRAP_ACT), // s2c_objTrapAct,
  103. sizeof(NPC_REMOVE_SYNC), // s2c_npcremove,
  104. sizeof(NPC_WALK_SYNC), // s2c_npcwalk,
  105. sizeof(NPC_RUN_SYNC), // s2c_npcrun,
  106. -1, // s2c_npcattack,
  107. -1, // s2c_npcmagic,
  108. sizeof(NPC_JUMP_SYNC), // s2c_npcjump,
  109. -1, // s2c_npctalk,
  110. sizeof(NPC_HURT_SYNC), // s2c_npchurt,
  111. sizeof(NPC_DEATH_SYNC), // s2c_npcdeath,
  112. sizeof(NPC_CHGCURCAMP_SYNC),// s2c_npcchgcurcamp,
  113. sizeof(NPC_CHGCAMP_SYNC), // s2c_npcchgcamp,
  114. sizeof(NPC_SKILL_SYNC), // s2c_skillcast,
  115. -1, // s2c_playertalk,
  116. sizeof(PLAYER_EXP_SYNC), // s2c_playerexp,
  117. sizeof(PLAYER_SEND_TEAM_INFO), // s2c_teaminfo,
  118. sizeof(PLAYER_SEND_SELF_TEAM_INFO), // s2c_teamselfinfo,
  119. sizeof(PLAYER_APPLY_TEAM_INFO_FALSE), // s2c_teamapplyinfofalse,
  120. sizeof(PLAYER_SEND_CREATE_TEAM_SUCCESS),// s2c_teamcreatesuccess,
  121. sizeof(PLAYER_SEND_CREATE_TEAM_FALSE), // s2c_teamcreatefalse,
  122. sizeof(PLAYER_TEAM_OPEN_CLOSE), // s2c_teamopenclose,
  123. sizeof(PLAYER_APPLY_ADD_TEAM), // s2c_teamgetapply,
  124. sizeof(PLAYER_TEAM_ADD_MEMBER), // s2c_teamaddmember,
  125. sizeof(PLAYER_LEAVE_TEAM), // s2c_teamleave,
  126. sizeof(PLAYER_TEAM_CHANGE_CAPTAIN), // s2c_teamchangecaptain,
  127. sizeof(PLAYER_FACTION_DATA), // s2c_playerfactiondata,
  128. sizeof(PLAYER_LEAVE_FACTION), // s2c_playerleavefaction,
  129. sizeof(PLAYER_FACTION_SKILL_LEVEL), // s2c_playerfactionskilllevel,
  130. -1,//sizeof(PLAYER_SEND_CHAT_SYNC), // s2c_playersendchat,
  131. sizeof(PLAYER_LEAD_EXP_SYNC), // s2c_playersyncleadexp
  132. sizeof(PLAYER_LEVEL_UP_SYNC), // s2c_playerlevelup
  133. sizeof(PLAYER_TEAMMATE_LEVEL_SYNC), // s2c_teammatelevel
  134. sizeof(PLAYER_ATTRIBUTE_SYNC), // s2c_playersyncattribute
  135. sizeof(PLAYER_SKILL_LEVEL_SYNC), // s2c_playerskilllevel
  136. sizeof(ITEM_SYNC), // s2c_syncitem
  137. sizeof(ITEM_REMOVE_SYNC), // s2c_removeitem
  138. sizeof(PLAYER_MONEY_SYNC), // s2c_syncmoney
  139. sizeof(PLAYER_MOVE_ITEM_SYNC), // s2c_playermoveitem
  140. -1, // s2c_playershowui
  141. sizeof(CHAT_APPLY_ADD_FRIEND_SYNC), // s2c_chatapplyaddfriend
  142. sizeof(CHAT_ADD_FRIEND_SYNC), // s2c_chataddfriend
  143. -1,//sizeof(CHAT_REFUSE_FRIEND_SYNC), // s2c_chatrefusefriend
  144. sizeof(CHAT_ADD_FRIEND_FAIL_SYNC), // s2c_chataddfriendfail
  145. sizeof(CHAT_LOGIN_FRIEND_NONAME_SYNC), // s2c_chatloginfriendnoname
  146. -1,//sizeof(CHAT_LOGIN_FRIEND_NAME_SYNC), // s2c_chatloginfriendname
  147. sizeof(CHAT_ONE_FRIEND_DATA_SYNC), // s2c_chatonefrienddata
  148. sizeof(CHAT_FRIEND_ONLINE_SYNC), // s2c_chatfriendinline
  149. sizeof(CHAT_DELETE_FRIEND_SYNC), // s2c_chatdeletefriend
  150. sizeof(CHAT_FRIEND_OFFLINE_SYNC), // s2c_chatfriendoffline
  151. sizeof(ROLE_LIST_SYNC), // s2c_syncrolelist
  152. sizeof(TRADE_CHANGE_STATE_SYNC), // s2c_tradechangestate
  153. -1, // NPC_SET_MENU_STATE_SYNC    s2c_npcsetmenustate
  154. sizeof(TRADE_MONEY_SYNC), // s2c_trademoneysync
  155. sizeof(TRADE_DECISION_SYNC), // s2c_tradedecision
  156. -1, // sizeof(CHAT_SCREENSINGLE_ERROR_SYNC)s2c_chatscreensingleerror
  157. sizeof(NPC_SYNC_STATEINFO), // s2c_syncnpcstate,
  158. -1, // sizeof(TEAM_INVITE_ADD_SYNC)    s2c_teaminviteadd
  159. sizeof(TRADE_STATE_SYNC), // s2c_tradepressoksync
  160. sizeof(PING_COMMAND), // s2c_ping
  161. sizeof(NPC_SIT_SYNC), // s2c_npcsit
  162. sizeof(SALE_BOX_SYNC), // s2c_opensalebox
  163. sizeof(NPC_SKILL_SYNC), // s2cDirectlyCastSkill
  164. -1, // s2c_msgshow
  165. -1, // s2c_syncstateeffect
  166. sizeof(BYTE), // s2c_openstorebox
  167. sizeof(NPC_REVIVE_SYNC), // s2c_playerrevive
  168. sizeof(NPC_REQUEST_FAIL), // s2c_requestnpcfail
  169. sizeof(TRADE_APPLY_START_SYNC), // s2c_tradeapplystart
  170. sizeof(tagNewDelRoleResponse), // s3c_rolenewdelresponse
  171. };
  172. int main(int argc, char* argv[])
  173. {
  174. ShowTitle( "Welcome to JX.MMOG client" );
  175. g_sAppFullPath = GetAppFullPath( NULL );
  176. cout << "The current directory : " 
  177. << endl 
  178. << _strlwr( const_cast< char * >( g_sAppFullPath.c_str() ) )
  179. << endl;
  180. SelectServerList();
  181. ConnectServer();
  182. _LOGIN_ROUTE:
  183. InputAccountAndPassword();
  184. switch ( Login() )
  185. {
  186. case LOGIN_R_ACCOUNT_OR_PASSWORD_ERROR:
  187. case LOGIN_R_TIMEOUT:
  188. case LOGIN_R_ACCOUNT_EXIST:
  189. case LOGIN_R_FAILED:
  190. goto _LOGIN_ROUTE;
  191. break;
  192. case LOGIN_R_SUCCESS:
  193. break;
  194. }
  195. int nRoleIndex = SelectRoleList();
  196. WaitForEnterGameServer();
  197. InitGameWorld();
  198. PlayGame();
  199. DisconnectServer();
  200. return 0;
  201. }
  202. void PlayGame()
  203. {
  204. ShowTitle( "Enter the game" );
  205. cout << "Input 'Exit' or 'exit' to quit." << endl;
  206. _tstring sCmd;
  207. while ( sCmd.compare( "exit" ) && sCmd.compare( "Exit" ) )
  208. {
  209. cin >> sCmd;
  210. }
  211. }
  212. void InitGameWorld()
  213. {
  214. bool ok = false;
  215. while ( !ok )
  216. {
  217. size_t datalength = 0;
  218. const char *pData = ( const char * )pGameSvrClient->GetPackFromServer( datalength );
  219. const char *pMsg = pData;
  220. while ( pMsg < ( pData + datalength ) )
  221. {
  222. BYTE cProtocol = *( BYTE * )pMsg;
  223. ASSERT( cProtocol > s2c_clientbegin && cProtocol < s2c_end );
  224. int nProtocolSize = g_nProtocolSize[ cProtocol - s2c_clientbegin - 1 ];
  225. cout << nProtocolSize << "; ";
  226. switch ( cProtocol )
  227. {
  228. case s2c_syncend:
  229. {
  230. BYTE cData = c2s_syncend;
  231. pGameSvrClient->SendPackToServer( ( const void * )&cData, sizeof( BYTE ) );
  232. cout << endl << "Init game finish!" << endl;
  233. ok = true;
  234. }
  235. break;
  236. }
  237. if ( ok )
  238. {
  239. break;
  240. }
  241. /*
  242.  * Get next protocol
  243.  */
  244. if ( nProtocolSize >= 0 )
  245. {
  246. pMsg += nProtocolSize;
  247. }
  248. else
  249. {
  250. unsigned short usMsgSize = *( unsigned short * )( pMsg + sizeof( BYTE ) );
  251. pMsg += sizeof( BYTE ) + usMsgSize;
  252. }
  253. }
  254. ::Sleep( 1 );
  255. }
  256. }
  257. void ShowTitle( const char *pTile )
  258. {
  259. ASSERT( pTile );
  260. setcolor( enumBlackonGray );
  261. int count = strlen( pTile );
  262. int size = 1;
  263. _tstring sLine;
  264. sLine = '+';
  265. sLine.resize( g_nTitleSpace * 2 + count + size, '-' );
  266. sLine += '+';
  267. _tstring sTitle;
  268. sTitle = '|';
  269. sTitle.resize( g_nTitleSpace + size, ' ' );
  270. sTitle += pTile;
  271. size = sTitle.size();
  272. sTitle.resize( g_nTitleSpace + size, ' ' );
  273. sTitle += '|';
  274. /*
  275.  * Show some information for user
  276.  */
  277. cout << sLine << endl;
  278. cout << sTitle << endl;
  279. cout << sLine << endl;
  280. setcolor( enumDefault );
  281. }
  282. void SelectServerList()
  283. {
  284. _BEGIN_SELECT_SERVER_LIST:
  285. ShowTitle( "Select server" );
  286. /*
  287.  * Get setup infomation from ini-file
  288.  */
  289. _tstring sConfigFile, buffer, key, result;
  290. sConfigFile = g_sAppFullPath + SVR_SETTING_FILENAME;
  291. CIniFile iniFile( sConfigFile.c_str() );
  292. key.resize( g_nBufferSize );
  293. buffer.resize( g_nBufferSize );
  294. size_t nSvrCount = iniFile.ReadInteger( "List", "Count", 0 );
  295. setcolor( enumYellowonBlack );
  296. int i = 0;
  297. for ( ; i<nSvrCount; i++ )
  298. {
  299. sprintf( const_cast< char * >( key.c_str() ), "%d", i );
  300. iniFile.ReadString( key.c_str(), "Title", const_cast< char * >( buffer.c_str() ), g_nBufferSize, "" );
  301. result = buffer.c_str();
  302. result += _T(" [");
  303. iniFile.ReadString( key.c_str(), "Address", const_cast< char * >( buffer.c_str() ), g_nBufferSize, "" );
  304. result += buffer.c_str();
  305. result += "]";
  306. cout << i << ") " << result << endl;
  307. }
  308. cout << i << ") " << "Exit game" << endl;
  309. setcolor( enumDefault );
  310. cout << "What are you select? Press number to be continue!" << endl;
  311. int nSelRoleIndex = 0;
  312. while ( true )
  313. {
  314. cin >> buffer;
  315. nSelRoleIndex = atoi( buffer.c_str() );
  316. /*
  317.  * The last one is option that you can exit application
  318.  */
  319. if ( nSelRoleIndex >= 0 && nSelRoleIndex < nSvrCount + 1 )
  320. {
  321. break;
  322. }
  323. cout << "Input incorrect, please input again." << endl;
  324. }
  325. /*
  326.  * Exit game if you select the last option
  327.  */
  328. if ( nSelRoleIndex == nSvrCount )
  329. {
  330. cout << "Are you exit game? < Y : yes>";
  331. _tstring sInformation;
  332. cin >> sInformation;
  333. if ( 0 == sInformation.compare( "Y" ) || 0 == sInformation.compare( "y" ) )
  334. {
  335. exit( 0 );
  336. }
  337. /*
  338.  * Repeat this route
  339.  */
  340. goto _BEGIN_SELECT_SERVER_LIST;
  341. }
  342. /*
  343.  * Read information by you select
  344.  */
  345. sprintf( const_cast< char * >( key.c_str() ), "%d", nSelRoleIndex );
  346. iniFile.ReadString( key.c_str(), "Title", const_cast< char * >( buffer.c_str() ), g_nBufferSize, "" );
  347. result = buffer.c_str();
  348. result += _T(" [");
  349. iniFile.ReadString( key.c_str(), "Address", const_cast< char * >( buffer.c_str() ), g_nBufferSize, "" );
  350. g_sServerIPAddress = buffer.c_str();
  351. result += buffer.c_str();
  352. /* sConfigFile = g_sAppFullPath + CONFIG_FILENAME;
  353. iniFile.SetFile( sConfigFile.c_str() );
  354. g_nServerPort = iniFile.ReadInteger( "Server", "GameServPort", g_nServerPort );
  355. */
  356. result += _T( ":" ) + ToString(g_nServerPort) + _T( "]" );
  357. cout << "Your select " << result << endl;
  358. //cout << "Please wait for a moment..." << endl;
  359. }
  360. UINT Login()
  361. {
  362. _tstring buffer;
  363. UINT nRetValue = ( UINT )( -1 );
  364. /*
  365.  * Send login command to gateway
  366.  */
  367. KLoginAccountInfo lai;
  368. ZeroMemory( &lai, sizeof( KLoginAccountInfo ) );
  369. /*
  370.  * Account
  371.  */
  372. size_t used = sizeof( lai.Account );
  373. used = ( used > g_sAccount.length() ) ? g_sAccount.length() : used;
  374. memcpy( lai.Account, g_sAccount.c_str(), used );
  375. /*
  376.  * Password
  377.  */
  378. used = sizeof( lai.Password );
  379. used = ( used > g_sPassword.length() ) ? g_sPassword.length() : used;
  380. memcpy( lai.Password, g_sPassword.c_str(), used );
  381. /*
  382.  * The other info
  383.  */
  384. lai.Param = LOGIN_A_LOGIN | LOGIN_R_REQUEST;
  385. lai.Size = sizeof( lai );
  386. buffer.resize( lai.Size + 1/* protocol id */ );
  387. BYTE bProtocol = c2s_login;
  388. memcpy( const_cast< char * >( buffer.c_str() ), &bProtocol, sizeof( BYTE ) );
  389. memcpy( const_cast< char * >( buffer.c_str() + 1 ), &lai, sizeof( lai ) );
  390. pClient->SendPackToServer( buffer.c_str(), buffer.size() );
  391. /*
  392.  * Get verify info get from gateway
  393.  */
  394. bool ok = false;
  395. int xPos = 0;
  396. int yPos = 0;
  397. getxy( xPos, yPos );
  398. DWORD dwOlderTimer = GetTickCount();
  399. while ( true )
  400. {
  401. static int s_nPos = 0;
  402. DWORD dwNewTimer = GetTickCount();
  403. if ( dwNewTimer - dwOlderTimer > 300 )
  404. {
  405. dwOlderTimer = dwNewTimer;
  406. if ( s_nPos++ > 5 )
  407. {
  408. s_nPos = 0;
  409. }
  410. }
  411. _tstring info;
  412. strstream str;
  413. gotoxy( xPos, yPos );
  414. info.resize( s_nPos, '.' );
  415. cout << "Wait for purify your account" << info.c_str() << "      " << endl;
  416.  
  417. size_t datalength = 0;
  418. const void *pData = pClient->GetPackFromServer( datalength );
  419. while ( pData && datalength )
  420. {
  421. BYTE cProtocol = *( const BYTE * )pData;
  422. switch ( cProtocol )
  423. {
  424. case s2c_login:
  425. {
  426. KLoginAccountInfo *pLAI = ( KLoginAccountInfo * )( ( const BYTE * )pData + 1 );
  427. if ( ( pLAI->Param | LOGIN_A_LOGIN ) &&
  428.  0 == g_sAccount.compare( pLAI->Account ) &&
  429.  0 == g_sPassword.compare( pLAI->Password ) )
  430. {
  431. if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_ACCOUNT_OR_PASSWORD_ERROR )
  432. {
  433. nRetValue = LOGIN_R_ACCOUNT_OR_PASSWORD_ERROR;
  434. cout << "An incorrect account or password!" << endl;
  435. }
  436. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_FAILED )
  437. {
  438. nRetValue = LOGIN_R_FAILED;
  439. cout << "Login failed!" << endl;
  440. }
  441. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_SUCCESS )
  442. {
  443. nRetValue = LOGIN_R_SUCCESS;
  444. cout << "Login successful!" << endl;
  445. }
  446. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_ACCOUNT_EXIST )
  447. {
  448. nRetValue = LOGIN_R_ACCOUNT_EXIST;
  449. cout << "Login failed! your accout was existed!" << endl;
  450. }
  451. else if ( ( pLAI->Param & 0xFFFF ) == LOGIN_R_TIMEOUT )
  452. {
  453. nRetValue = LOGIN_R_TIMEOUT;
  454. cout << "Login failed! no money!" << endl;
  455. }
  456. ok = true;
  457. }
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. pData = pClient->GetPackFromServer( datalength );
  464. }
  465. if ( ok )
  466. {
  467. break;
  468. }
  469. Sleep( 1 );
  470. }
  471. return nRetValue;
  472. }
  473. void InputAccountAndPassword()
  474. {
  475. ShowTitle( "Login" );
  476. /*
  477.  * Wait for a correct account and password
  478.  */
  479. _tstring sInformation;
  480. while ( true )
  481. {
  482. cout << "Please input your account : ";
  483. cin >> g_sAccount;
  484. cout << "please input your password for this account : ";
  485. cin >> g_sPassword;
  486. cout << endl;
  487. cout << "Account : [" << g_sAccount << "] Password : [" << g_sPassword << "]" << endl;
  488. cout << "Is it right? < Y : yes>";
  489. cin >> sInformation;
  490. if ( 0 == sInformation.compare( "Y" ) || 0 == sInformation.compare( "y" ) )
  491. {
  492. break;
  493. }
  494. }
  495. }
  496. int SelectRoleList()
  497. {
  498. int nRoleCount = 0;
  499. const S3DBI_RoleBaseInfo *pRoleList = NULL;
  500. size_t datalength = 0;
  501. const void *pData = pClient->GetPackFromServer( datalength );
  502. while ( true )
  503. {
  504. if ( pData && datalength )
  505. {
  506. BYTE cProtocol = *( BYTE * )pData;
  507. if ( s2c_roleserver_getrolelist_result == cProtocol )
  508. {
  509. TProcessData *pPA = ( TProcessData * )pData;
  510. nRoleCount = *( char * )( pPA->pDataBuffer );
  511. pRoleList = ( const S3DBI_RoleBaseInfo * )( ( const char * )( pPA->pDataBuffer ) + 1/* sizeof( char ) */ );
  512. const size_t datalength = sizeof( S3DBI_RoleBaseInfo ) * nRoleCount;
  513. break;
  514. }
  515. }
  516. pData = pClient->GetPackFromServer( datalength );
  517. ::Sleep( 1 );
  518. }
  519. ShowTitle( "Select role" );
  520. setcolor( enumYellowonBlack );
  521. int nIndex = 0;
  522. while ( nIndex < nRoleCount && pRoleList )
  523. {
  524. cout << nIndex << " ) " << pRoleList[nIndex].szRoleName << endl;
  525. nIndex ++;
  526. }
  527. setcolor( enumDefault );
  528. _tstring sRoleIndex;
  529. cout << "What are you select? Press number to be continue!" << endl;
  530. int nSelRoleIndex = -1;
  531. while ( true )
  532. {
  533. cin >> sRoleIndex;
  534. nSelRoleIndex = atoi( sRoleIndex.c_str() );
  535. /*
  536.  * The last one is option that you can exit application
  537.  */
  538. if ( nSelRoleIndex >= 0 && nSelRoleIndex < nRoleCount )
  539. {
  540. break;
  541. }
  542. cout << "Input incorrect, please input again." << endl;
  543. }
  544. /*
  545.  * Send this info to gateway
  546.  */
  547. tagDBSelPlayer tDBSP;
  548. tDBSP.cProtocol = c2s_dbplayerselect;
  549. /*
  550.  * nSelRoleIndex >= 0 && 
  551.  * nSelRoleIndex < nRoleCount && 
  552.  * sizeof( dpc.szRoleName ) >= strlen( pRoleList[nSelRoleIndex].szRoleName )
  553.  */
  554. size_t nRoleNameLen = strlen( pRoleList[nSelRoleIndex].szRoleName );
  555. memcpy( tDBSP.szRoleName, pRoleList[nSelRoleIndex].szRoleName, nRoleNameLen );
  556. tDBSP.szRoleName[nRoleNameLen] = '';
  557. pClient->SendPackToServer( &tDBSP, sizeof( tagDBSelPlayer ) );
  558. return nSelRoleIndex;
  559. }
  560. void WaitForEnterGameServer()
  561. {
  562. cout << endl << "Wait for a respond for log on to gameserver..." << endl;
  563. size_t datalength = 0;
  564. const void *pData = pClient->GetPackFromServer( datalength );
  565. while ( true )
  566. {
  567. if ( pData && datalength )
  568. {
  569. BYTE cProtocol = *( BYTE * )pData;
  570. if ( s2c_notifyplayerlogin == cProtocol )
  571. {
  572. tagNotifyPlayerLogin *pNPL = ( tagNotifyPlayerLogin * )pData;
  573. cout << "Find a valid game server and connect to it!" << endl;
  574. const char *pIPAddr = OnlineGameLib::Win32::net_ntoa( pNPL->nIPAddr );
  575. cout << "IP : " << pIPAddr << " PORT : " << pNPL->nPort << endl;
  576. g_sRoleName = ( const char * )pNPL->szRoleName;
  577. ConnectToGameSvr( pIPAddr, pNPL->nPort, pNPL->guid );
  578. break;
  579. }
  580. }
  581. pData = pClient->GetPackFromServer( datalength );
  582. ::Sleep( 1 );
  583. }
  584. }
  585. void ConnectToGameSvr( const char *pIPAddr, unsigned short nPort, GUID &guid )
  586. {
  587. /*
  588.  * Query network interface
  589.  */
  590. pfnCreateClientInterface pFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  591. IClientFactory *pClientFactory = NULL;
  592. if ( pFactroyFun && SUCCEEDED( pFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  593. {
  594. pClientFactory->SetEnvironment( 50, 1024 * 16 );
  595. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &pGameSvrClient ) );
  596. pClientFactory->Release();
  597. }
  598. if ( !pGameSvrClient )
  599. {
  600. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  601. exit( -1 );
  602. }
  603. /*
  604.  * Create a connection
  605.  */
  606. pGameSvrClient->Startup();
  607. //pGameSvrClient->RegisterMsgFilter( reinterpret_cast< void * >( pClient ), NULL );
  608. if ( FAILED( pGameSvrClient->ConnectTo( pIPAddr, nPort ) ) )
  609. {
  610. cout << "Connection failed! Don't connect to game server." << endl;
  611. exit( -1 );
  612. }
  613. cout << "It's successful in connection! Prepare to enter this game..." << endl;
  614. tagLogicLogin ll;
  615. ll.cProtocol = c2s_logiclogin;
  616. memcpy( &ll.guid, &guid, sizeof( GUID ) );
  617. pGameSvrClient->SendPackToServer( &ll, sizeof( tagLogicLogin ) );
  618. }
  619. void ConnectServer()
  620. {
  621. /*
  622.  * Query network interface
  623.  */
  624. pfnCreateClientInterface pFactroyFun = ( pfnCreateClientInterface )( g_theRainbowLibrary.GetProcAddress( "CreateInterface" ) );
  625. IClientFactory *pClientFactory = NULL;
  626. if ( pFactroyFun && SUCCEEDED( pFactroyFun( IID_IClientFactory, reinterpret_cast< void ** >( &pClientFactory ) ) ) )
  627. {
  628. pClientFactory->SetEnvironment( 50, 1024 * 16 );
  629. pClientFactory->CreateClientInterface( IID_IESClient, reinterpret_cast< void ** >( &pClient ) );
  630. pClientFactory->Release();
  631. }
  632. if ( !pClient )
  633. {
  634. cout << "Initialization failed! Don't find a correct rainbow.dll" << endl;
  635. exit( -1 );
  636. }
  637. /*
  638.  * Create a connection
  639.  */
  640. pClient->Startup();
  641. pClient->RegisterMsgFilter( reinterpret_cast< void * >( pClient ), ClientEventNotify );
  642. if ( FAILED( pClient->ConnectTo( g_sServerIPAddress.c_str(), g_nServerPort ) ) )
  643. {
  644. cout << "Connection failed! Don't connect to server." << endl;
  645. exit( -1 );
  646. }
  647. cout << "It's successful in connection! Prepare to enter this game..." << endl;
  648. }
  649. void DisconnectServer()
  650. {
  651. /*
  652.  * Destroy the connection
  653.  */
  654. pGameSvrClient->Shutdown();
  655. pGameSvrClient->Cleanup();
  656. pGameSvrClient->Release();
  657. pClient->Shutdown();
  658. pClient->Cleanup();
  659. pClient->Release();
  660. }