MainWndProc.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:6k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "../def/dbmgr.h"
  3. LPARAM OnServerSockMsg(WPARAM wParam, LPARAM lParam);
  4. LPARAM OnClientSockMsg(WPARAM wParam, LPARAM lParam);
  5. BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  6. BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
  7. UINT WINAPI ProcessDBMsg(LPVOID lpParameter);
  8. UINT WINAPI ProcessGateMsg(LPVOID lpParameter);
  9. BOOL InitGateCommSocket(SOCKET &s, SOCKADDR_IN* addr, UINT nMsgID, int nPort, long lEvent);
  10. LPARAM OnGateCommSockMsg(WPARAM wParam, LPARAM lParam);
  11. BOOL CALLBACK ConfigDlgFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  12. extern HINSTANCE g_hInst;
  13. extern HWND g_hMainWnd;
  14. extern HWND g_hLogMsgWnd;
  15. extern HWND g_hToolBar;
  16. extern HWND g_hStatusBar;
  17. BOOL g_fTerminated = FALSE;
  18. SOCKET g_ssock = INVALID_SOCKET;
  19. SOCKADDR_IN g_saddr;
  20. SOCKET g_gssock = INVALID_SOCKET;
  21. SOCKADDR_IN g_gsaddr;
  22. CWHList<GAMESERVERINFO*> g_xGameServerList;
  23. void SwitchMenuItem(BOOL fFlag)
  24. {
  25. HMENU hMainMenu = GetMenu(g_hMainWnd);
  26. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  27. if (fFlag)
  28. {
  29. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
  30. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
  31. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  32. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  33. InsertLogMsg(IDS_STARTSERVICE);
  34. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_T("Ready"));
  35. }
  36. else
  37. {
  38. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
  39. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
  40. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  41. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  42. InsertLogMsg(IDS_STOPSERVICE);
  43. }
  44. }
  45. void LoadCharacterRecords()
  46. {
  47. InsertLogMsg(IDS_LOADACCOUNTRECORDS);
  48. CRecordset *pRec = GetDBManager()->CreateRecordset();
  49. if ( pRec->Execute( "SELECT * FROM TBL_GAMEGATEINFO" ) )
  50. {
  51. GAMESERVERINFO *pServerInfo;
  52. while ( pRec->Fetch() )
  53. {
  54. pServerInfo = new GAMESERVERINFO;
  55. if ( !pServerInfo )
  56. break;
  57. pServerInfo->index = atoi( pRec->Get( "FLD_SERVERIDX" ) );
  58. strcpy( pServerInfo->name, pRec->Get( "FLD_SERVERNAME" ) );
  59. strcpy( pServerInfo->ip,   pRec->Get( "FLD_SERVERIP" ) );
  60. pServerInfo->connCnt = 0;
  61. g_xGameServerList.AddNewNode( pServerInfo );
  62. }
  63. }
  64. GetDBManager()->DestroyRecordset( pRec );
  65. }
  66. void OnCommand(WPARAM wParam, LPARAM lParam)
  67. {
  68. int nPort;
  69. switch (LOWORD(wParam))
  70. {
  71. case IDM_STARTSERVICE:
  72. {
  73. g_fTerminated = FALSE;
  74. if (!jRegGetKey(_DB_SERVER_REGISTRY, _TEXT("LocalPort"), (LPBYTE)&nPort))
  75. nPort = 5000;
  76. LoadCharacterRecords();
  77. InitServerSocket(g_ssock, &g_saddr, _IDM_GATECOMMSOCK_MSG, 6000, 1);
  78. InitGateCommSocket(g_gssock, &g_gsaddr, _IDM_GATECOMMSOCK_MSG, 5100, FD_ACCEPT|FD_READ|FD_CLOSE);
  79. UINT dwThreadIDForMsg = 0;
  80. unsigned long hThreadForMsg = 0;
  81. // if (hThreadForMsg = _beginthreadex(NULL, 0, ProcessUserHuman, NULL, 0, &dwThreadIDForMsg))
  82. hThreadForMsg = _beginthreadex(NULL, 0, ProcessDBMsg, NULL, 0, &dwThreadIDForMsg);
  83. hThreadForMsg = _beginthreadex(NULL, 0, ProcessGateMsg, NULL, 0, &dwThreadIDForMsg);
  84. SwitchMenuItem(TRUE);
  85. return;
  86. }
  87. case IDM_STOPSERVICE:
  88. {
  89. g_fTerminated = TRUE;
  90. SwitchMenuItem(FALSE);
  91. return;
  92. }
  93. case IDM_CONFIG:
  94. {
  95. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), NULL, (DLGPROC)ConfigDlgFunc);
  96. return;
  97. }
  98. }
  99. }
  100. // **************************************************************************************
  101. //
  102. //
  103. //
  104. // **************************************************************************************
  105. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  106. {
  107. switch (nMsg)
  108. {
  109. case _IDM_GATECOMMSOCK_MSG:
  110. return OnGateCommSockMsg(wParam, lParam);
  111. case WM_COMMAND:
  112. OnCommand(wParam, lParam);
  113. break;
  114. case WM_SIZE:
  115. {
  116. if (g_hToolBar && g_hMainWnd && g_hStatusBar) 
  117. {
  118. RECT rcToolBar, rcMain, rcStatusBar;
  119. GetWindowRect(g_hToolBar, &rcToolBar);
  120. GetClientRect(g_hMainWnd, &rcMain);
  121. GetWindowRect(g_hStatusBar, &rcStatusBar);
  122. MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
  123. MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top), 
  124. LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
  125. MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top) - 2, (rcMain.right - rcMain.left), 
  126. (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top) + 2, 
  127. TRUE);
  128. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  129. int nCnt = 0;
  130. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
  131. nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
  132. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  133. }
  134. break;
  135. }
  136. case WM_CLOSE:
  137. {
  138. TCHAR szMsg[128];
  139. TCHAR szTitle[128];
  140. LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
  141. LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
  142. if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONSTOP|MB_YESNO) == IDYES)
  143. {
  144. if (SendMessage(g_hToolBar, TB_GETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)0L) == TBSTATE_INDETERMINATE)
  145. OnCommand(IDM_STOPSERVICE, 0L);
  146. ClearSocket(g_ssock);
  147. WSACleanup();
  148. CoUninitialize();
  149. PostQuitMessage(0);
  150. }
  151. return 0L;
  152. }
  153. }
  154. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  155. }