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

模拟服务器

开发平台:

Visual C++

  1. // LoginGate.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. // **************************************************************************************
  5. BOOL InitApplication(HANDLE hInstance);
  6. BOOL InitInstance(HANDLE hInstance, int nCmdShow);
  7. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
  8. BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  9. BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
  10. BOOL CALLBACK ConfigDlgFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  11. // **************************************************************************************
  12. //
  13. // Global Variables Definition
  14. //
  15. // **************************************************************************************
  16. HINSTANCE g_hInst = NULL; // Application instance
  17. HWND g_hMainWnd = NULL; // Main window handle
  18. HWND g_hLogMsgWnd = NULL;
  19. HWND g_hToolBar = NULL;
  20. HWND g_hStatusBar = NULL;
  21. BOOL g_fTerminated = FALSE;
  22. static WSADATA g_wsd;
  23. TBBUTTON tbButtons[] = 
  24. {
  25. { 0, IDM_STARTSERVICE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
  26. { 1, IDM_STOPSERVICE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0}
  27. };
  28. // **************************************************************************************
  29. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  30. {
  31.     MSG msg;
  32. #ifndef _SOCKET_ASYNC_IO
  33. if (CheckAvailableIOCP())
  34. {
  35. #endif
  36. if (!InitApplication(hInstance))
  37. return (FALSE);
  38. if (!InitInstance(hInstance, nCmdShow))
  39. return (FALSE);
  40. while (GetMessage(&msg, NULL, 0, 0))
  41. {
  42. TranslateMessage(&msg);
  43. DispatchMessage(&msg);
  44. }
  45. #ifndef _SOCKET_ASYNC_IO
  46. }
  47. else
  48. {
  49. TCHAR szMsg[1024];
  50. LoadString(hInstance, IDS_NOTWINNT, szMsg, sizeof(szMsg));
  51. MessageBox(NULL, szMsg, _GAMEGATE_SERVER_TITLE, MB_OK|MB_ICONINFORMATION);
  52. return -1;
  53. }
  54. #endif
  55.     
  56. return (msg.wParam);
  57. }
  58. // **************************************************************************************
  59. //
  60. //
  61. //
  62. // **************************************************************************************
  63. BOOL InitApplication(HANDLE hInstance)
  64. {
  65.     WNDCLASS  wc;
  66.     wc.style            = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW;
  67.     wc.lpfnWndProc      = (WNDPROC)MainWndProc;
  68.     wc.cbClsExtra       = 0;
  69.     wc.cbWndExtra       = 0;
  70.     wc.hIcon            = LoadIcon((HINSTANCE)hInstance, MAKEINTRESOURCE(IDI_MIR2));
  71.     wc.hInstance        = (HINSTANCE)hInstance;
  72.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
  73.     wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW + 1);
  74.     wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MAINMENU);
  75.     wc.lpszClassName    = _GAMEGATE_SERVER_CLASS;
  76. return RegisterClass(&wc);
  77. }
  78. // **************************************************************************************
  79. //
  80. //
  81. //
  82. // **************************************************************************************
  83. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  84. {
  85. g_hInst = (HINSTANCE)hInstance;
  86. INITCOMMONCONTROLSEX icex;
  87. icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  88. icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES | ICC_INTERNET_CLASSES;
  89. InitCommonControlsEx(&icex);
  90.     g_hMainWnd = CreateWindowEx(0, _GAMEGATE_SERVER_CLASS, _GAMEGATE_SERVER_TITLE, 
  91. WS_OVERLAPPEDWINDOW|WS_VISIBLE,
  92. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,                 
  93. NULL, NULL, (HINSTANCE)hInstance, NULL);
  94. g_hToolBar = CreateToolbarEx(g_hMainWnd, WS_CHILD|CCS_TOP|WS_VISIBLE|WS_BORDER,
  95. _IDW_TOOLBAR, sizeof(tbButtons) / sizeof(TBBUTTON), (HINSTANCE)hInstance, IDB_TOOLBAR,
  96. (LPCTBBUTTON)&tbButtons, sizeof(tbButtons) / sizeof(TBBUTTON),
  97. _BMP_CX, _BMP_CY, _BMP_CX, _BMP_CY, sizeof(TBBUTTON));
  98. RECT rcMainWnd, rcToolBar, rcStatusBar;
  99. GetClientRect(g_hMainWnd, &rcMainWnd);
  100. GetWindowRect(g_hToolBar, &rcToolBar);
  101. g_hStatusBar = CreateWindowEx(0L, STATUSCLASSNAME, _TEXT(""), WS_CHILD|WS_BORDER|WS_VISIBLE|SBS_SIZEGRIP,
  102. 0, rcMainWnd.bottom - _STATUS_HEIGHT, (rcMainWnd.right - rcMainWnd.left), _STATUS_HEIGHT, g_hMainWnd, (HMENU)_IDW_STATUSBAR, g_hInst, NULL);
  103. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  104. int nCnt = 0;
  105. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0; i--)
  106. nStatusPartsWidths[nCnt++] = (rcMainWnd.right - rcMainWnd.left) - (90 * i);
  107. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  108. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(1, 0), (LPARAM)_TEXT("Not Connected"));
  109. GetWindowRect(g_hStatusBar, &rcStatusBar);
  110.     g_hLogMsgWnd = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, _TEXT(""), 
  111. WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_EDITLABELS,
  112. 0, (rcToolBar.bottom - rcToolBar.top), (rcMainWnd.right - rcMainWnd.left), 
  113. (rcMainWnd.bottom - rcMainWnd.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top),
  114. g_hMainWnd, NULL, (HINSTANCE)hInstance, NULL);
  115. ListView_SetExtendedListViewStyleEx(g_hLogMsgWnd, 0, LVS_EX_FULLROWSELECT);
  116. LV_COLUMN lvc;
  117. TCHAR szText[64];
  118. lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  119. lvc.fmt = LVCFMT_LEFT;
  120. lvc.cx = 150;
  121. lvc.pszText = szText;
  122. for (i = 0; i < 3; i++)
  123. {
  124. lvc.iSubItem = i;
  125. LoadString((HINSTANCE)hInstance, IDS_LVS_LABEL1 + i, szText, sizeof(szText)/sizeof(TCHAR));
  126. ListView_InsertColumn(g_hLogMsgWnd, i, &lvc);
  127. }
  128. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  129. ShowWindow(g_hMainWnd, SW_SHOW);
  130. UpdateWindow(g_hMainWnd);
  131. if (WSAStartup(MAKEWORD(2, 2), &g_wsd) != 0)
  132. return (FALSE);
  133. BYTE btInstalled;
  134. if (!jRegGetKey(_GAMEGATE_SERVER_REGISTRY, _TEXT("Installed"), (LPBYTE)&btInstalled))
  135. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), NULL, (DLGPROC)ConfigDlgFunc);
  136. return TRUE;
  137. }
  138. // **************************************************************************************
  139. //
  140. //
  141. //
  142. // **************************************************************************************
  143. int AddNewLogMsg()
  144. {
  145. LV_ITEM lvi;
  146. TCHAR szText[64];
  147. int nCount = ListView_GetItemCount(g_hLogMsgWnd);
  148. if (nCount >= 50)
  149. {
  150. ListView_DeleteItem(g_hLogMsgWnd, 0);
  151. nCount--;
  152. }
  153. lvi.mask = LVIF_TEXT;
  154. lvi.iItem = nCount;
  155. lvi.iSubItem = 0;
  156. _tstrdate(szText);
  157. lvi.pszText = szText;
  158. ListView_InsertItem(g_hLogMsgWnd, &lvi);
  159. _tstrtime(szText);
  160. ListView_SetItemText(g_hLogMsgWnd, nCount, 1, szText);
  161. return nCount;
  162. }
  163. void InsertLogMsg(UINT nID)
  164. {
  165. TCHAR szText[256];
  166. int nCount = AddNewLogMsg();
  167. LoadString(g_hInst, nID, szText, sizeof(szText)/sizeof(TCHAR));
  168. ListView_SetItemText(g_hLogMsgWnd, nCount, 2, szText);
  169. ListView_Scroll(g_hLogMsgWnd, 0, 8);
  170. }
  171. void InsertLogMsg(LPTSTR lpszMsg)
  172. {
  173. int nCount = AddNewLogMsg();
  174. ListView_SetItemText(g_hLogMsgWnd, nCount, 2, lpszMsg);
  175. ListView_Scroll(g_hLogMsgWnd, 0, 8);
  176. }
  177. void InsertLogMsgParam(UINT nID, void *pParam, BYTE btFlags)
  178. {
  179. TCHAR szText[128];
  180. TCHAR szMsg[256];
  181. int nCount = AddNewLogMsg();
  182. LoadString(g_hInst, nID, szText, sizeof(szText)/sizeof(TCHAR));
  183. switch (btFlags)
  184. {
  185. case LOGPARAM_STR:
  186. _stprintf(szMsg, szText, (LPTSTR)pParam);
  187. break;
  188. case LOGPARAM_INT:
  189. _stprintf(szMsg, szText, *(int *)pParam);
  190. break;
  191. }
  192. if (lstrlen(szMsg) <= 256)
  193. {
  194. ListView_SetItemText(g_hLogMsgWnd, nCount, 2, szMsg);
  195. ListView_Scroll(g_hLogMsgWnd, 0, 8);
  196. }
  197. }