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

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. LPARAM OnServerSockMsg(WPARAM wParam, LPARAM lParam);
  3. LPARAM OnClientSockMsg(WPARAM wParam, LPARAM lParam);
  4. BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  5. BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
  6. BOOL CALLBACK ConfigDlgFunc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. extern HINSTANCE g_hInst;
  8. extern HWND g_hMainWnd;
  9. extern HWND g_hLogMsgWnd;
  10. extern HWND g_hToolBar;
  11. extern HWND g_hStatusBar;
  12. extern HANDLE g_hThreadForComm;
  13. SOCKET g_ssock = INVALID_SOCKET;
  14. SOCKADDR_IN g_saddr;
  15. SOCKET g_csock = INVALID_SOCKET;
  16. SOCKADDR_IN g_caddr;
  17. extern CWHAbusive g_xAbusive;
  18. void SwitchMenuItem(BOOL fFlag)
  19. {
  20. HMENU hMainMenu = GetMenu(g_hMainWnd);
  21. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  22. if (fFlag)
  23. {
  24. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
  25. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
  26. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  27. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  28. InsertLogMsg(IDS_STARTSERVICE);
  29. }
  30. else
  31. {
  32. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
  33. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
  34. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  35. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  36. InsertLogMsg(IDS_STOPSERVICE);
  37. }
  38. }
  39. void OnCommand(WPARAM wParam, LPARAM lParam)
  40. {
  41. switch (LOWORD(wParam))
  42. {
  43. case IDM_STARTSERVICE:
  44. {
  45. DWORD dwIP = 0;
  46. int nPort = 0;
  47. g_fTerminated = FALSE;
  48. if (!jRegGetKey(_GAMEGATE_SERVER_REGISTRY, _TEXT("LocalPort"), (LPBYTE)&nPort))
  49. nPort = 7200;
  50. InitServerSocket(g_ssock, &g_saddr, _IDM_SERVERSOCK_MSG, nPort, 2);
  51. jRegGetKey(_GAMEGATE_SERVER_REGISTRY, _TEXT("RemoteIP"), (LPBYTE)&dwIP);
  52. if (!jRegGetKey(_GAMEGATE_SERVER_REGISTRY, _TEXT("RemotePort"), (LPBYTE)&nPort))
  53. nPort = 5000;
  54. ConnectToServer(g_csock, &g_caddr, _IDM_CLIENTSOCK_MSG, NULL, dwIP, nPort, FD_CONNECT|FD_READ|FD_CLOSE);
  55. SwitchMenuItem(TRUE);
  56. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_TEXT("Ready"));
  57. g_xAbusive.LoadAbusiveList();
  58. return;
  59. }
  60. case IDM_STOPSERVICE:
  61. {
  62. g_fTerminated = TRUE;
  63. ClearSocket(g_ssock);
  64. ClearSocket(g_csock);
  65. SwitchMenuItem(FALSE);
  66. SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_TEXT(""));
  67. return;
  68. }
  69. case IDM_CONFIG:
  70. {
  71. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), g_hMainWnd, (DLGPROC)ConfigDlgFunc);
  72. return;
  73. }
  74. }
  75. }
  76. // **************************************************************************************
  77. //
  78. //
  79. //
  80. // **************************************************************************************
  81. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  82. {
  83. switch (nMsg)
  84. {
  85. #ifdef _SOCKET_ASYNC_IO
  86. case _IDM_SERVERSOCK_MSG:
  87. return OnServerSockMsg(wParam, lParam);
  88. #endif
  89. case _IDM_CLIENTSOCK_MSG:
  90. return OnClientSockMsg(wParam, lParam);
  91. case WM_COMMAND:
  92. OnCommand(wParam, lParam);
  93. break;
  94. case WM_SIZE:
  95. {
  96. if (g_hToolBar && g_hMainWnd && g_hStatusBar) 
  97. {
  98. RECT rcToolBar, rcMain, rcStatusBar;
  99. GetWindowRect(g_hToolBar, &rcToolBar);
  100. GetClientRect(g_hMainWnd, &rcMain);
  101. GetWindowRect(g_hStatusBar, &rcStatusBar);
  102. MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
  103. MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top), 
  104. LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
  105. MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top), (rcMain.right - rcMain.left), 
  106. (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top), 
  107. TRUE);
  108. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  109. int nCnt = 0;
  110. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
  111. nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
  112. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  113. }
  114. break;
  115. }
  116. case WM_CLOSE:
  117. {
  118. TCHAR szMsg[128];
  119. TCHAR szTitle[128];
  120. LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
  121. LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
  122. if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONSTOP|MB_YESNO) == IDYES)
  123. {
  124. if (SendMessage(g_hToolBar, TB_GETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)0L) == TBSTATE_INDETERMINATE)
  125. OnCommand(IDM_STOPSERVICE, 0L);
  126. WSACleanup();
  127. PostQuitMessage(0);
  128. }
  129. return 0L;
  130. }
  131. }
  132. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  133. }