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

模拟服务器

开发平台:

C/C++

  1. // RoleDBManager.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "kdbprocessthread.h"
  6. #define MAX_LOADSTRING 100
  7. // Global Variables:
  8. HINSTANCE hInst; // current instance
  9. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  10. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  11. char g_SaveMarkString[200] = "";
  12. char g_RemoveMarkString[200] = "";
  13. char g_LoadMarkString[200] = "";
  14. // Foward declarations of functions included in this code module:
  15. ATOM MyRegisterClass(HINSTANCE hInstance);
  16. BOOL InitInstance(HINSTANCE, int);
  17. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  18. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  19. bool StartServer() ;
  20. int APIENTRY WinMain(HINSTANCE hInstance,
  21.                      HINSTANCE hPrevInstance,
  22.                      LPSTR     lpCmdLine,
  23.                      int       nCmdShow)
  24. {
  25.   // TODO: Place code here.
  26. MSG msg;
  27. HACCEL hAccelTable;
  28. // Initialize global strings
  29. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  30. LoadString(hInstance, IDC_ROLEDBMANAGER, szWindowClass, MAX_LOADSTRING);
  31. MyRegisterClass(hInstance);
  32. // Perform application initialization:
  33. if (!InitInstance (hInstance, nCmdShow)) 
  34. {
  35. return FALSE;
  36. }
  37. if (!StartServer()) 
  38. return FALSE;
  39. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ROLEDBMANAGER);
  40. // Main message loop:
  41. while (GetMessage(&msg, NULL, 0, 0)) 
  42. {
  43. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  44. {
  45. TranslateMessage(&msg);
  46. DispatchMessage(&msg);
  47. }
  48. }
  49. return msg.wParam;
  50. }
  51. //
  52. //  FUNCTION: MyRegisterClass()
  53. //
  54. //  PURPOSE: Registers the window class.
  55. //
  56. //  COMMENTS:
  57. //
  58. //    This function and its usage is only necessary if you want this code
  59. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  60. //    function that was added to Windows 95. It is important to call this function
  61. //    so that the application will get 'well formed' small icons associated
  62. //    with it.
  63. //
  64. ATOM MyRegisterClass(HINSTANCE hInstance)
  65. {
  66. WNDCLASSEX wcex;
  67. wcex.cbSize = sizeof(WNDCLASSEX); 
  68. wcex.style = CS_HREDRAW | CS_VREDRAW;
  69. wcex.lpfnWndProc = (WNDPROC)WndProc;
  70. wcex.cbClsExtra = 0;
  71. wcex.cbWndExtra = 0;
  72. wcex.hInstance = hInstance;
  73. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ROLEDBMANAGER);
  74. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  75. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  76. wcex.lpszMenuName = (LPCSTR)IDC_ROLEDBMANAGER;
  77. wcex.lpszClassName = szWindowClass;
  78. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  79. return RegisterClassEx(&wcex);
  80. }
  81. //
  82. //   FUNCTION: InitInstance(HANDLE, int)
  83. //
  84. //   PURPOSE: Saves instance handle and creates main window
  85. //
  86. //   COMMENTS:
  87. //
  88. //        In this function, we save the instance handle in a global variable and
  89. //        create and display the main program window.
  90. //
  91. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  92. {
  93.    HWND hWnd;
  94.    hInst = hInstance; // Store instance handle in our global variable
  95.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  96.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  97.    if (!hWnd)
  98.    {
  99.       return FALSE;
  100.    }
  101.    ShowWindow(hWnd, nCmdShow);
  102.    UpdateWindow(hWnd);
  103.    return TRUE;
  104. }
  105. //
  106. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  107. //
  108. //  PURPOSE:  Processes messages for the main window.
  109. //
  110. //  WM_COMMAND - process the application menu
  111. //  WM_PAINT - Paint the main window
  112. //  WM_DESTROY - post a quit message and return
  113. //
  114. //
  115. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  116. {
  117. int wmId, wmEvent;
  118. PAINTSTRUCT ps;
  119. HDC hdc;
  120. TCHAR szHello[MAX_MARK_COUNT + 400];
  121. // LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  122. switch (message) 
  123. {
  124. case WM_CREATE:
  125. ::SetTimer( hWnd, 1,  100, NULL );
  126. break;
  127. case WM_COMMAND:
  128. wmId    = LOWORD(wParam); 
  129. wmEvent = HIWORD(wParam); 
  130. // Parse the menu selections:
  131. switch (wmId)
  132. {
  133. case IDM_ABOUT:
  134.    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  135.    break;
  136. case IDM_EXIT:
  137.    DestroyWindow(hWnd);
  138.    break;
  139. default:
  140.    return DefWindowProc(hWnd, message, wParam, lParam);
  141. }
  142. break;
  143. case WM_PAINT:
  144. hdc = BeginPaint(hWnd, &ps);
  145. // TODO: Add any drawing code here...
  146. RECT rt;
  147. GetClientRect(hWnd, &rt);
  148. sprintf(szHello, "Recv%d, Sent%d", g_dwRecvLen , g_dwSendLen);
  149. DrawText(hdc, szHello, strlen(szHello), &rt, DT_LEFT);
  150. char Marks[MAX_MARK_COUNT +3];
  151. rt.top += 50;
  152. memset(Marks, MARK, g_MainLoopMark);
  153. Marks[g_MainLoopMark] = 0;
  154. sprintf(szHello, "MainLoop  %s", Marks);
  155. DrawText(hdc, szHello, strlen(szHello), &rt, DT_LEFT);
  156. rt.top += 50;
  157. memset(Marks, MARK, g_NetServerMark);
  158. Marks[g_NetServerMark] = 0;
  159. sprintf(szHello, "NetServer %s", Marks);
  160. DrawText(hdc, szHello, strlen(szHello), &rt, DT_LEFT);
  161. rt.top += 50;
  162. if (g_LoadDBThreadMark == MAX_MARK_COUNT + LOADMARK_NO)
  163. {
  164. strcpy(szHello, g_LoadMarkString);
  165. }
  166. else
  167. {
  168. memset(Marks, MARK, g_LoadDBThreadMark);
  169. Marks[g_LoadDBThreadMark] = 0;
  170. sprintf(szHello, "DBLoad    %s", Marks);
  171. }
  172. DrawText(hdc, szHello, strlen(szHello), &rt, DT_LEFT);
  173. rt.top += 50;
  174. if (g_SaveDBThreadMark == MAX_MARK_COUNT + SAVEMARK_NO)
  175. {
  176. strcpy(szHello, g_SaveMarkString);
  177. }
  178. else if (g_SaveDBThreadMark == MAX_MARK_COUNT + REMOVEMARK_NO)
  179. {
  180. strcpy(szHello, g_RemoveMarkString);
  181. }
  182. else
  183. {
  184. memset(Marks, MARK, g_SaveDBThreadMark);
  185. Marks[g_SaveDBThreadMark] = 0;
  186. sprintf(szHello, "DBSave    %s",Marks);
  187. }
  188. DrawText(hdc, szHello, strlen(szHello), &rt, DT_LEFT);
  189. rt.top = rt.bottom - 50;
  190. sprintf(szHello, "Mistake   %d", g_nMistakeCount);
  191. DrawText(hdc, szHello, strlen(szHello), &rt, DT_RIGHT);
  192. EndPaint(hWnd, &ps);
  193. break;
  194. case WM_DESTROY:
  195. ReleaseServer();
  196. PostQuitMessage(0);
  197. break;
  198. case WM_TIMER:
  199. ::InvalidateRect( hWnd, NULL, TRUE);
  200. break;
  201. default:
  202. return DefWindowProc(hWnd, message, wParam, lParam);
  203.    }
  204.    return 0;
  205. }
  206. // Mesage handler for about box.
  207. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  208. {
  209. switch (message)
  210. {
  211. case WM_INITDIALOG:
  212. return TRUE;
  213. case WM_COMMAND:
  214. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  215. {
  216. EndDialog(hDlg, LOWORD(wParam));
  217. return TRUE;
  218. }
  219. break;
  220. }
  221.     return FALSE;
  222. }
  223. extern TThreadData g_ThreadData[4];
  224. bool StartServer() 
  225. {
  226. // TODO: Add your control notification handler code here
  227. if (InitServer() == 0) return false;
  228.     g_ThreadData[0].hThread = CreateThread( 
  229.         NULL,                        // no security attributes 
  230.         0,                           // use default stack size  
  231.         RoleServerMainThreadFun,     // thread function 
  232. NULL,                // argument to thread function 
  233.         0,                           // use default creation flags 
  234.         &g_ThreadData[0].dwThreadId);                // returns the thread identifier 
  235. g_ThreadData[1].hThread = CreateThread( 
  236.         NULL,                        // no security attributes 
  237.         0,                           // use default stack size  
  238.         DatabaseLoadThreadFun,     // thread function 
  239. NULL,                // argument to thread function 
  240.         0,                           // use default creation flags 
  241.         &g_ThreadData[1].dwThreadId);                // returns the thread identifier 
  242. g_ThreadData[2].hThread = CreateThread( 
  243.         NULL,                        // no security attributes 
  244.         0,                           // use default stack size  
  245.         DatabaseSaveThreadFun,     // thread function 
  246. NULL,                // argument to thread function 
  247.         0,                           // use default creation flags 
  248.         &g_ThreadData[2].dwThreadId);                // returns the thread identifier 
  249. g_ThreadData[3].hThread = CreateThread( 
  250.         NULL,                        // no security attributes 
  251.         0,                           // use default stack size  
  252.         RoleNetWorkServiceThreadFun,     // thread function 
  253. NULL,                // argument to thread function 
  254.         0,                           // use default creation flags 
  255.         &g_ThreadData[3].dwThreadId);                // returns the thread identifier 
  256. return true;
  257. }