Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.3 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1994 - 1999
  3. Module Name:
  4. server.c
  5. Abstract:
  6. Server side of post message test. Note, this guy is a hidden
  7. win32 app. The client controls it via PostMessage. It's ugly.
  8. Author:
  9. Mario Goertzel (mariogo) 31-Mar-1994
  10. Revision History:
  11. --*/
  12. #include <rpcperf.h>
  13. #include <pmsgtest.h>
  14. HANDLE hRequestEvent;
  15. HANDLE hReplyEvent;
  16. HANDLE hWorkerEvent;
  17. HANDLE hWorkerThread;
  18. BOOL fShutdown = FALSE;
  19. LONG lIterations;
  20. LONG lTestCase;
  21. BOOL fMsgMO = 0;
  22. BOOL APIENTRY InitInstance(HINSTANCE, INT);
  23. LRESULT APIENTRY MainWndProc(HWND, UINT, WPARAM, LPARAM);
  24. DWORD WINAPI Worker (LPVOID);
  25. int APIENTRY WinMain(
  26. HINSTANCE hInstance,
  27. HINSTANCE hPrevInstance,
  28. LPSTR lpCmdLine,
  29. int nCmdShow
  30. )
  31. {
  32. ULONG cCount = 1;
  33. ULONG status;
  34. int fStop;
  35. MSG msg;
  36. UNREFERENCED_PARAMETER( lpCmdLine );
  37. if (hPrevInstance)
  38. {
  39. return FALSE;
  40. }
  41. if (!InitInstance(hInstance, nCmdShow))
  42. return FALSE;
  43. fStop = 0;
  44. while (!fStop)
  45. {
  46. if (fMsgMO)
  47. {
  48. status = MsgWaitForMultipleObjects(cCount,
  49. &hRequestEvent,
  50. FALSE,
  51. 2000,
  52. QS_ALLINPUT);
  53. if (status == WAIT_OBJECT_0)
  54. {
  55. if (lTestCase == 11)
  56. SetEvent(hWorkerEvent);
  57. else
  58. SetEvent(hReplyEvent);
  59. continue;
  60. }
  61. else if (status == WAIT_OBJECT_0 + 1)
  62. {
  63. // Fall through and do a GetMessage and DispatchMessage.
  64. }
  65. else
  66. {
  67. *(long *)status = 10; //GPF on error.
  68. }
  69. }
  70. if (GetMessage(&msg, NULL, 0, 0) == FALSE)
  71. {
  72. break;
  73. }
  74. DispatchMessage(&msg);
  75. }
  76. return (int) msg.wParam;
  77. }
  78. BOOL APIENTRY InitInstance( HINSTANCE hInstance, INT nCmdShow )
  79. {
  80. HWND hWnd;
  81. WNDCLASS wc;
  82. DWORD dwThreadId;
  83. wc.style = 0; //CS_HREDRAW | CS_VREDRAW; // redraw if size changes
  84. wc.lpfnWndProc = MainWndProc; // points to window proc.
  85. wc.cbClsExtra = 0; // no extra class memory
  86. wc.cbWndExtra = 0; // no extra window memory
  87. wc.hInstance = hInstance; // handle of instance
  88. wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); // predefined app. icon
  89. wc.hCursor = LoadCursor(NULL,IDC_ARROW); // predefined arrow
  90. wc.hbrBackground = GetStockObject(WHITE_BRUSH); // white background brush
  91. wc.lpszMenuName = 0; //"MainMenu"; // name of menu resource
  92. wc.lpszClassName = CLASS; // name of window class
  93. if (RegisterClass(&wc) == 0)
  94. return !GetLastError();
  95. hWnd = CreateWindow(
  96. CLASS,
  97. TITLE,
  98. 0,
  99. CW_USEDEFAULT,
  100. CW_USEDEFAULT,
  101. CW_USEDEFAULT,
  102. CW_USEDEFAULT,
  103. NULL,
  104. NULL,
  105. hInstance,
  106. NULL
  107. );
  108. if (!hWnd) {
  109. return !GetLastError();
  110. }
  111. hRequestEvent = CreateEvent(0,
  112. FALSE,
  113. FALSE,
  114. REQUEST_EVENT);
  115. if (!hRequestEvent) return FALSE;
  116. hReplyEvent = CreateEvent(0,
  117. FALSE,
  118. FALSE,
  119. REPLY_EVENT);
  120. if (!hReplyEvent) return FALSE;
  121. hWorkerEvent = CreateEvent(0,
  122. FALSE,
  123. FALSE,
  124. WORKER_EVENT);
  125. if (!hWorkerEvent) return FALSE;
  126. hWorkerThread = CreateThread(0,
  127. 0,
  128. Worker,
  129. 0,
  130. 0,
  131. &dwThreadId
  132. );
  133. if (!hWorkerThread) return FALSE;
  134. return TRUE;
  135. }
  136. LRESULT APIENTRY MainWndProc(
  137. HWND hWnd,
  138. UINT message,
  139. WPARAM wParam,
  140. LPARAM lParam)
  141. {
  142. switch(message)
  143. {
  144. case WM_DESTROY:
  145. CloseHandle(hReplyEvent);
  146. fShutdown = TRUE;
  147. SetEvent(hWorkerEvent);
  148. CloseHandle(hWorkerThread);
  149. CloseHandle(hWorkerEvent);
  150. CloseHandle(hReplyEvent);
  151. CloseHandle(hRequestEvent);
  152. PostQuitMessage(0);
  153. break;
  154. case MSG_PERF_MESSAGE:
  155. {
  156. switch(wParam)
  157. {
  158. case 3:
  159. case 9:
  160. case 11:
  161. fMsgMO = TRUE;
  162. // Fall through
  163. case 1:
  164. case 5:
  165. case 7:
  166. lIterations = (long)lParam;
  167. lTestCase = (long)wParam;
  168. SetEvent(hWorkerEvent);
  169. // Worker now completes required test and tells the
  170. // client when finished.
  171. break;
  172. case 4:
  173. fMsgMO = TRUE;
  174. // Fall through
  175. case 2:
  176. if (lParam == 0)
  177. {
  178. // Client sends many messages (queues them up),
  179. // server must signal when the last message arrives.
  180. SetEvent(hReplyEvent);
  181. }
  182. break;
  183. case 6: // send message
  184. return 69;
  185. case 10:
  186. fMsgMO = TRUE;
  187. // Fall through
  188. case 8:
  189. SetEvent(hReplyEvent);
  190. break;
  191. case 12:
  192. fMsgMO = TRUE;
  193. default:
  194. break;
  195. }
  196. }
  197. case MSG_PERF_MESSAGE2:
  198. {
  199. // lParam is the number of iterations left
  200. // wParam the test case.
  201. if ( (lParam == 1)
  202. || (wParam == 7)
  203. || (wParam == 9) )
  204. {
  205. // Finished all the iterations, or we are running
  206. // the force context-switch version. Let the worker know.
  207. SetEvent(hWorkerEvent);
  208. }
  209. break;
  210. }
  211. case WM_COPYDATA:
  212. {
  213. COPYDATASTRUCT *pData = (COPYDATASTRUCT *)lParam;
  214. return TRUE;
  215. }
  216. default:
  217. return DefWindowProc(hWnd, message, wParam, lParam);
  218. }
  219. return TRUE;
  220. }
  221. DWORD WINAPI Worker (LPVOID pArg)
  222. {
  223. LONG i;
  224. HWND hWnd = FindWindow(CLASS, TITLE);
  225. UNREFERENCED_PARAMETER(pArg);
  226. WaitForSingleObject(hWorkerEvent, INFINITE);
  227. if (fShutdown) return 0;
  228. if (lTestCase == 5)
  229. {
  230. // SendMessage test case.
  231. for(i = lIterations; i >= 0; i--)
  232. SendMessage(hWnd, MSG_PERF_MESSAGE2, lTestCase, i);
  233. goto worker_done;
  234. }
  235. if (lTestCase == 11)
  236. {
  237. // SetEvent/MsgWaitForMultipleObjects
  238. for(i = lIterations; i; i--)
  239. {
  240. SetEvent(hRequestEvent);
  241. WaitForSingleObject(hWorkerEvent, INFINITE);
  242. }
  243. goto worker_done;
  244. }
  245. for (i = lIterations; i; i--)
  246. {
  247. // One of the PostMessage test cases.
  248. PostMessage(hWnd, MSG_PERF_MESSAGE2, lTestCase, i);
  249. if (lTestCase > 4)
  250. {
  251. // Forced context switch version, wait for server
  252. // to process the last message.
  253. WaitForSingleObject(hWorkerEvent, INFINITE);
  254. }
  255. }
  256. if (lTestCase <= 4)
  257. {
  258. // Wait for the server to finish processing the queued-up messages.
  259. WaitForSingleObject(hWorkerEvent, INFINITE);
  260. }
  261. worker_done:
  262. SetEvent(hReplyEvent); // Tell the client that we finished this
  263. // in process test.
  264. return 0;
  265. }