PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Sources/MultiServer/Intercessor/Intercessor.cpp

http://mmo-resourse.googlecode.com/
C++ | 552 lines | 342 code | 165 blank | 45 comment | 38 complexity | ab88161965ac754b6e4a87000f0da0b2 MD5 | raw file
Possible License(s): Zlib, LGPL-2.1, LGPL-2.0
  1. // Intercessor.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "Network.h"
  6. #include "PlayerManager.h"
  7. #include "KProtocolDef.h"
  8. #include "../../Sword3PaySys/S3AccServer/AccountLoginDef.h"
  9. #include "macro.h"
  10. #include "Event.h"
  11. #include "string.h"
  12. #include "Buffer.h"
  13. using OnlineGameLib::Win32::CEvent;
  14. using OnlineGameLib::Win32::_tstring;
  15. using OnlineGameLib::Win32::CBuffer;
  16. using OnlineGameLib::Win32::CPackager;
  17. #define WM_ENTER_AFFIRM ( WM_USER + 0x100 )
  18. #define WM_LEAVE_AFFIRM ( WM_USER + 0x200 )
  19. #define WM_SERVER_LOGIN_SUCCESSFUL ( WM_USER + 0x300 )
  20. #define WM_SERVER_LOGIN_FAILED ( WM_USER + 0x400 )
  21. #define BUFFER_LENGTH 100
  22. enum enumServerLoginErrorCode
  23. {
  24. enumConnectFailed = 0xA1,
  25. enumUsrNamePswdErr,
  26. enumIPPortErr,
  27. enumException
  28. };
  29. const DWORD g_dwServerIdentify = 0xAEFC07B5;
  30. /*
  31. * Global variable
  32. */
  33. HINSTANCE g_hInst;
  34. CEvent g_theWorkingTutorEvent( NULL, false, false, _T("Working_Tutor_Event") );
  35. CNetwork g_theNetwork;
  36. CPlayerManager *g_pPlayerManager = NULL;
  37. /*
  38. * Helper function
  39. */
  40. bool LoginSystem( HINSTANCE hInstance );
  41. BOOL CALLBACK MainWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam );
  42. UINT MainDialog();
  43. void InitMainDlg( HWND hDlg );
  44. void CloseMainDlg( HWND hDlg );
  45. BOOL CALLBACK LoginDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
  46. /*
  47. * WinMain
  48. */
  49. int APIENTRY WinMain(HINSTANCE hInstance,
  50. HINSTANCE hPrevInstance,
  51. LPSTR lpCmdLine,
  52. int nCmdShow)
  53. {
  54. int nRet = 0;
  55. g_hInst = hInstance;
  56. g_theNetwork.Create();
  57. /*
  58. * To affirm account that it is used to check the application
  59. */
  60. if ( LoginSystem( hInstance ) )
  61. {
  62. g_pPlayerManager = new CPlayerManager( 50, g_theNetwork );
  63. ASSERT( g_pPlayerManager );
  64. MainDialog();
  65. }
  66. g_theNetwork.Destroy();
  67. return nRet;
  68. }
  69. /*
  70. * Log on to system
  71. */
  72. bool LoginSystem( HINSTANCE hInstance )
  73. {
  74. if ( WM_SERVER_LOGIN_SUCCESSFUL == ::DialogBox( hInstance,
  75. MAKEINTRESOURCE( IDD_DLG_LOGIN ),
  76. NULL,
  77. ( DLGPROC )LoginDlgProc ) )
  78. {
  79. return true;
  80. }
  81. return false;
  82. }
  83. UINT MainDialog()
  84. {
  85. /*
  86. * Create the main window as dialog.
  87. */
  88. HWND hwndMain = ::CreateDialog( g_hInst,
  89. MAKEINTRESOURCE( IDD_DLG_INTERCESSOR ),
  90. NULL,
  91. (DLGPROC) MainWndProc );
  92. ASSERT( hwndMain );
  93. ::ShowWindow( hwndMain, SW_SHOWNORMAL );
  94. ::UpdateWindow( hwndMain );
  95. BOOL bRet;
  96. MSG msg;
  97. while ( ( bRet = ::GetMessage( &msg, NULL, 0, 0 ) ) != 0 )
  98. {
  99. if ( !IsWindow( hwndMain ) || !IsDialogMessage( hwndMain, &msg ) )
  100. {
  101. ::TranslateMessage( &msg );
  102. ::DispatchMessage( &msg );
  103. }
  104. }
  105. return msg.wParam;
  106. }
  107. void InitMainDlg( HWND hDlg )
  108. {
  109. ::SetDlgItemText( hDlg, IDC_EDIT_ACCSVRIP, g_theNetwork.GetAccSvrIP() );
  110. ::SetDlgItemInt( hDlg, IDC_EDIT_ACCSVRPORT, g_theNetwork.GetAccSvrPort(), FALSE );
  111. ::SetDlgItemText( hDlg, IDC_EDIT_ROLESVRIP, g_theNetwork.GetRoleSvrIP() );
  112. ::SetDlgItemInt( hDlg, IDC_EDIT_ROLESVRPORT, g_theNetwork.GetRoleSvrPort(), FALSE );
  113. ::SetDlgItemInt( hDlg, IDC_EDIT_CLIENT_PORT, g_theNetwork.GetClientOpenPort(), FALSE );
  114. ::SetDlgItemInt( hDlg, IDC_EDIT_GAMESVR_PORT, g_theNetwork.GetGameSvrOpenPort(), FALSE );
  115. }
  116. void CloseMainDlg( HWND hDlg )
  117. {
  118. _tstring sBuffer;
  119. sBuffer.resize( BUFFER_LENGTH );
  120. ::GetDlgItemText( hDlg, IDC_EDIT_ROLESVRIP, const_cast< char * >( sBuffer.c_str() ), BUFFER_LENGTH );
  121. g_theNetwork.SetRoleSvrIP( sBuffer.c_str() );
  122. UINT nValue = 0;
  123. BOOL bTranslated = TRUE;
  124. nValue = ::GetDlgItemInt( hDlg, IDC_EDIT_ROLESVRPORT, &bTranslated, FALSE );
  125. g_theNetwork.SetRoleSvrPort( nValue );
  126. nValue = ::GetDlgItemInt( hDlg, IDC_EDIT_CLIENT_PORT, &bTranslated, FALSE );
  127. g_theNetwork.SetClientOpenPort( nValue );
  128. nValue = ::GetDlgItemInt( hDlg, IDC_EDIT_GAMESVR_PORT, &bTranslated, FALSE );
  129. g_theNetwork.SetGameSvrOpenPort( nValue );
  130. }
  131. DWORD WINAPI ServerLoginRoutine( LPVOID lpParam )
  132. {
  133. HWND hwndDlg = ( HWND )lpParam;
  134. ASSERT( hwndDlg );
  135. /*
  136. * Ask for log on to the account server
  137. */
  138. UINT nPort = 0;
  139. _tstring sBuffer, sUsername, sPassword;
  140. sBuffer.resize( MAX_PATH + 1 );
  141. ::GetDlgItemText( hwndDlg, IDC_EDIT_LOGONTO_IP, const_cast< char * >( sBuffer.c_str() ), MAX_PATH );
  142. BOOL bTranslated = TRUE;
  143. nPort = ::GetDlgItemInt( hwndDlg, IDC_EDIT_LOGONTO_PORT, &bTranslated, TRUE );
  144. IClient *pAccSvrClient = g_theNetwork.CreateAccSvrClient( sBuffer.c_str(), nPort );
  145. if ( !pAccSvrClient )
  146. {
  147. ::PostMessage( hwndDlg, WM_SERVER_LOGIN_FAILED, enumConnectFailed, 0L );
  148. SAFE_RELEASE( pAccSvrClient );
  149. return 1L;
  150. }
  151. sUsername.resize( BUFFER_LENGTH );
  152. ::GetDlgItemText( hwndDlg, IDC_EDIT_USERNAME, const_cast< char * >( sUsername.c_str() ), BUFFER_LENGTH );
  153. sPassword.resize( BUFFER_LENGTH );
  154. ::GetDlgItemText( hwndDlg, IDC_EDIT_PASSWORD, const_cast< char * >( sPassword.c_str() ), BUFFER_LENGTH );
  155. /*
  156. * Send a login command
  157. */
  158. CBuffer *pBuffer = CNetwork::m_theGlobalAllocator.Allocate();
  159. BYTE *pData = const_cast< BYTE * >( pBuffer->GetBuffer() );
  160. const size_t datalength = sizeof( KAccountUserLoginInfo ) + 1;
  161. KAccountUserLoginInfo serlogin;
  162. serlogin.Size = sizeof( KAccountUserLoginInfo );
  163. serlogin.Type = AccountUserLoginInfo;
  164. serlogin.Version = ACCOUNT_CURRENT_VERSION;
  165. serlogin.Operate = g_dwServerIdentify;
  166. strcpy( serlogin.Account, sUsername.c_str() );
  167. strcpy( serlogin.Password, sPassword.c_str() );
  168. *pData = c2s_gatewayverify;
  169. memcpy( pData + 1, &serlogin, sizeof( KAccountUserLoginInfo ) );
  170. pBuffer->Use( datalength );
  171. pAccSvrClient->SendPackToServer( ( const void * )pData, datalength );
  172. SAFE_RELEASE( pBuffer );
  173. /*
  174. * Wait for write back
  175. */
  176. while ( pAccSvrClient )
  177. {
  178. size_t nLen = 0;
  179. const void *pData = pAccSvrClient->GetPackFromServer( nLen );
  180. if ( !pData || 0 == nLen )
  181. {
  182. ::Sleep( 1 );
  183. continue;
  184. }
  185. BYTE cProtocol = CPackager::Peek( pData );
  186. switch ( cProtocol )
  187. {
  188. case s2c_gatewayverify:
  189. {
  190. KAccountUserReturn* pReturn = ( KAccountUserReturn * )( ( ( char * )pData ) + 1 );
  191. nLen--;
  192. if ( pReturn &&
  193. ( g_dwServerIdentify == pReturn->Operate ) &&
  194. ( nLen == sizeof( KAccountUserReturn ) ) )
  195. {
  196. if ( ACTION_SUCCESS == pReturn->nReturn )
  197. {
  198. ::PostMessage( hwndDlg, WM_SERVER_LOGIN_SUCCESSFUL, 0L, 0L );
  199. }
  200. else if ( E_ACCOUNT_OR_PASSWORD == pReturn->nReturn )
  201. {
  202. ::PostMessage( hwndDlg, WM_SERVER_LOGIN_FAILED, enumUsrNamePswdErr, 0L );
  203. }
  204. else if ( E_ADDRESS_OR_PORT == pReturn->nReturn )
  205. {
  206. ::PostMessage( hwndDlg, WM_SERVER_LOGIN_FAILED, enumIPPortErr, 0L );
  207. }
  208. else
  209. {
  210. ::PostMessage( hwndDlg, WM_SERVER_LOGIN_FAILED, enumException, 0L );
  211. }
  212. SAFE_RELEASE( pAccSvrClient );
  213. return 1L;
  214. }
  215. }
  216. break;
  217. default:
  218. ASSERT( FALSE );
  219. break;
  220. }
  221. }
  222. SAFE_RELEASE( pAccSvrClient );
  223. return 0L;
  224. }
  225. void EnterToAffirm( HWND hwndDlg )
  226. {
  227. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_USERNAME ), FALSE );
  228. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_PASSWORD ), FALSE );
  229. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_IP ), FALSE );
  230. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_PORT ), FALSE );
  231. ::EnableWindow( ::GetDlgItem( hwndDlg, IDOK ), FALSE );
  232. ::EnableWindow( ::GetDlgItem( hwndDlg, IDCANCEL ), FALSE );
  233. /*
  234. * Start up a listening thread to wait a required confirm from the account server
  235. */
  236. DWORD dwThreadID = 0;
  237. HANDLE hThread = ::CreateThread( NULL, 0, ServerLoginRoutine, ( void * )hwndDlg, 0, &dwThreadID );
  238. SAFE_CLOSEHANDLE( hThread );
  239. }
  240. void LeaveToAffirm( HWND hwndDlg )
  241. {
  242. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_USERNAME ), TRUE );
  243. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_PASSWORD ), TRUE );
  244. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_IP ), TRUE );
  245. ::EnableWindow( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_PORT ), TRUE );
  246. ::EnableWindow( ::GetDlgItem( hwndDlg, IDOK ), TRUE );
  247. ::EnableWindow( ::GetDlgItem( hwndDlg, IDCANCEL ), TRUE );
  248. }
  249. bool CheckUserInfo( HWND hwndDlg )
  250. {
  251. UINT nLen = 0;
  252. _tstring sBuffer;
  253. sBuffer.resize( MAX_PATH + 1 );
  254. if ( 0 == ( nLen = ::GetDlgItemText( hwndDlg, IDC_EDIT_USERNAME, const_cast< char * >( sBuffer.c_str() ), MAX_PATH ) ) )
  255. {
  256. ::SetFocus( ::GetDlgItem( hwndDlg, IDC_EDIT_USERNAME ) );
  257. ::MessageBox( hwndDlg, "Please input a valid username!", "Warning", MB_ICONASTERISK );
  258. return false;
  259. }
  260. if ( 0 == ( nLen = ::GetDlgItemText( hwndDlg, IDC_EDIT_LOGONTO_IP, const_cast< char * >( sBuffer.c_str() ), MAX_PATH ) ) )
  261. {
  262. ::SetFocus( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_IP ) );
  263. ::MessageBox( hwndDlg, "Please input a valid IP!", "Warning", MB_ICONASTERISK );
  264. return false;
  265. }
  266. BOOL bTranslated = FALSE;
  267. if ( 0 == ( nLen = ::GetDlgItemInt( hwndDlg, IDC_EDIT_LOGONTO_PORT, &bTranslated, TRUE ) ) )
  268. {
  269. ::SetFocus( ::GetDlgItem( hwndDlg, IDC_EDIT_LOGONTO_PORT ) );
  270. ::MessageBox( hwndDlg, "Please input a valid port!", "Warning", MB_ICONASTERISK );
  271. return false;
  272. }
  273. return true;
  274. }
  275. BOOL CALLBACK LoginDlgProc( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam )
  276. {
  277. _tstring sBuffer;
  278. switch (message)
  279. {
  280. case WM_SERVER_LOGIN_SUCCESSFUL:
  281. ::EndDialog( hwndDlg, WM_SERVER_LOGIN_SUCCESSFUL );
  282. break;
  283. case WM_SERVER_LOGIN_FAILED:
  284. switch( wParam )
  285. {
  286. case enumUsrNamePswdErr:
  287. sBuffer.resize( MAX_PATH );
  288. ::LoadString( g_hInst, IDS_NAMEPSWD_ERROR, const_cast< char * >( sBuffer.c_str() ), MAX_PATH );
  289. ::MessageBox( hwndDlg, sBuffer.c_str(), NULL, MB_ICONEXCLAMATION );
  290. ::PostMessage( hwndDlg, WM_LEAVE_AFFIRM, 0L, 0L );
  291. break;
  292. case enumConnectFailed:
  293. case enumIPPortErr:
  294. sBuffer.resize( MAX_PATH );
  295. ::LoadString( g_hInst, IDS_NETWORK_ERROR, const_cast< char * >( sBuffer.c_str() ), MAX_PATH );
  296. ::MessageBox( hwndDlg, sBuffer.c_str(), NULL, MB_ICONEXCLAMATION );
  297. ::PostMessage( hwndDlg, WM_LEAVE_AFFIRM, 0L, 0L );
  298. break;
  299. case enumException:
  300. default:
  301. ::MessageBox( hwndDlg, "Exception!", NULL, MB_ICONEXCLAMATION );
  302. ::EndDialog( hwndDlg, WM_SERVER_LOGIN_FAILED );
  303. break;
  304. }
  305. break;
  306. case WM_ENTER_AFFIRM:
  307. EnterToAffirm( hwndDlg );
  308. break;
  309. case WM_LEAVE_AFFIRM:
  310. LeaveToAffirm( hwndDlg );
  311. break;
  312. case WM_INITDIALOG:
  313. ::SetFocus( ::GetDlgItem( hwndDlg, IDC_EDIT_USERNAME ) );
  314. // TEMPORAL
  315. ::SetDlgItemText( hwndDlg, IDC_EDIT_LOGONTO_IP, "192.168.26.1" );
  316. ::SetDlgItemInt( hwndDlg, IDC_EDIT_LOGONTO_PORT, 5002, FALSE );
  317. break;
  318. case WM_COMMAND:
  319. switch ( LOWORD( wParam ) )
  320. {
  321. case IDOK:
  322. if ( CheckUserInfo( hwndDlg ) )
  323. {
  324. ::PostMessage( hwndDlg, WM_ENTER_AFFIRM, 0L, 0L );
  325. }
  326. break;
  327. case IDCANCEL:
  328. ::EndDialog( hwndDlg, WM_SERVER_LOGIN_FAILED );
  329. return TRUE;
  330. }
  331. }
  332. return FALSE;
  333. }
  334. /*
  335. *
  336. * MainWndProc() - Main window callback procedure.
  337. *
  338. */
  339. BOOL CALLBACK MainWndProc( HWND hwnd,
  340. UINT msg,
  341. WPARAM wParam,
  342. LPARAM lParam )
  343. {
  344. switch ( msg )
  345. {
  346. case WM_INITDIALOG:
  347. /*
  348. * Initialize
  349. */
  350. InitMainDlg( hwnd );
  351. return TRUE;
  352. break;
  353. case WM_CLOSE:
  354. CloseMainDlg( hwnd );
  355. if ( g_pPlayerManager )
  356. {
  357. g_pPlayerManager->Destroy();
  358. }
  359. ::DestroyWindow( hwnd );
  360. ::PostQuitMessage( 0 );
  361. return TRUE;
  362. break;
  363. case WM_COMMAND:
  364. switch (wParam)
  365. {
  366. case IDOK:
  367. ::EnableWindow( GetDlgItem( hwnd, IDOK ), FALSE );
  368. if ( !g_pPlayerManager->Create() )
  369. {
  370. ::EnableWindow( GetDlgItem( hwnd, IDOK ), TRUE );
  371. g_pPlayerManager->Destroy();
  372. }
  373. break;
  374. case IDCANCEL:
  375. ::PostMessage( hwnd, WM_CLOSE, 0L, 0L );
  376. break;
  377. }
  378. break;
  379. default:
  380. break;
  381. }
  382. /*
  383. * Clean up any unused messages by calling DefWindowProc
  384. */
  385. return FALSE;
  386. //return ::DefWindowProc( hwnd, msg, wParam, lParam );
  387. }