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.

287 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1999
  6. //
  7. // File: server.c
  8. //
  9. //--------------------------------------------------------------------------
  10. /*++
  11. LPC Performance Test Server
  12. Copyright (C) 1993 Microsoft Corp
  13. Mario Goertzel
  14. Date Name Comments
  15. 2/26/93 MarioGo Created
  16. --*/
  17. #include <nt.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windows.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <rpcperf.h>
  24. #include <lpctest.h>
  25. const char *USAGE = "-n worker-threads (clients), default 1.\n"
  26. "All other options are set by the individual clients.";
  27. static HANDLE portPerf = 0;
  28. long Worker (long *plThreadNumber)
  29. {
  30. HANDLE portClient = 0;
  31. LPC_PERF_MESSAGE *pMessage, *pReplyMessage;
  32. int lTestNumber = 1;
  33. int status;
  34. BOOLEAN fExit = 0;
  35. unsigned long RequestSize, ReplySize;
  36. char *BigBuffer;
  37. SIZE_T Bytes;
  38. pMessage = malloc(sizeof(LPC_PERF_MESSAGE));
  39. pReplyMessage = 0;
  40. if (pMessage == 0)
  41. {
  42. printf("Error: Thread %ld - malloc failed\n", *plThreadNumber);
  43. return -1;
  44. }
  45. for(;;)
  46. {
  47. status =
  48. NtReplyWaitReceivePort(
  49. // portPerf,
  50. (portClient != 0) ? portClient : portPerf,
  51. 0,
  52. (PPORT_MESSAGE)pReplyMessage,
  53. (PPORT_MESSAGE)pMessage
  54. );
  55. pReplyMessage = pMessage;
  56. if (!NT_SUCCESS(status))
  57. {
  58. printf("NtReplyWaitReceivePort failed - %ld:%8lX\n",
  59. *plThreadNumber, status);
  60. return -1;
  61. }
  62. if (pMessage->Lpc.u2.s2.Type == LPC_CONNECTION_REQUEST)
  63. {
  64. RequestSize = pMessage->Bind.BufferLengthIn;
  65. ReplySize = pMessage->Bind.BufferLengthOut;
  66. BigBuffer = malloc((RequestSize>ReplySize)?RequestSize:ReplySize);
  67. printf("New Client Connection: %ld %ld\n", RequestSize, ReplySize);
  68. if (BigBuffer == 0)
  69. {
  70. printf("Malloc failed\n");
  71. return -1;
  72. }
  73. status =
  74. NtAcceptConnectPort(&portClient,
  75. 0,
  76. (PPORT_MESSAGE)pMessage,
  77. TRUE, // I accept the charges operator
  78. 0,
  79. 0);
  80. if (!NT_SUCCESS(status))
  81. {
  82. printf("NtAcceptConnectPort failed - %ld:%8lX\n",
  83. *plThreadNumber, status);
  84. return -1;
  85. }
  86. status =
  87. NtCompleteConnectPort(portClient);
  88. if (!NT_SUCCESS(status))
  89. {
  90. printf("NtCompleteConnectPort failed - %ld:%8lX\n",
  91. *plThreadNumber, status);
  92. return -1;
  93. }
  94. pReplyMessage = 0;
  95. }
  96. else
  97. if (pMessage->Lpc.u2.s2.Type == LPC_PORT_CLOSED)
  98. {
  99. printf("Client disconnected.\n");
  100. portClient = 0;
  101. pReplyMessage = 0;
  102. free(BigBuffer);
  103. }
  104. else
  105. {
  106. #if 0
  107. printf("Request: %ld %ld\n", pMessage->Lpc.u2.s2.Type,
  108. pMessage->Common.MsgType);
  109. #endif
  110. switch(pMessage->Common.MsgType)
  111. {
  112. case PERF_REQUEST:
  113. {
  114. pMessage->Common.MsgType = PERF_REPLY;
  115. if (pMessage->Packet.Buffer[0] == 'X')
  116. {
  117. printf("Shutdown received.\n");
  118. pMessage->Packet.Buffer[0] = 'Z';
  119. }
  120. break;
  121. }
  122. case PERF_SHARED_REQUEST:
  123. {
  124. break;
  125. }
  126. case PERF_READ_CLIENT_BUFFER:
  127. {
  128. status =
  129. NtReadRequestData(portClient,
  130. (PPORT_MESSAGE)pMessage,
  131. 0,
  132. BigBuffer,
  133. RequestSize,
  134. &Bytes);
  135. if (!NT_SUCCESS(status))
  136. {
  137. printf("NtReadRequestData failed - %ld:%8lX\n",
  138. *plThreadNumber, status);
  139. return -1;
  140. }
  141. status =
  142. NtWriteRequestData(portClient,
  143. (PPORT_MESSAGE)pMessage,
  144. 0,
  145. BigBuffer,
  146. ReplySize,
  147. &Bytes);
  148. if (!NT_SUCCESS(status))
  149. {
  150. printf("NtWriteRequestData failed - %ld:%8lX\n",
  151. *plThreadNumber, status);
  152. return -1;
  153. }
  154. pReplyMessage->Buffer.MsgType = PERF_READ_SERVER_BUFFER;
  155. break;
  156. }
  157. default:
  158. {
  159. printf("Invalid message: %ld\n", pMessage->Common.MsgType);
  160. break;
  161. }
  162. } /* Perf Message Type */
  163. } /* Lpc Message Type */
  164. } /* Message loop */
  165. return -1;
  166. }
  167. int __cdecl
  168. main(int argc, char **argv)
  169. {
  170. int i;
  171. HANDLE WorkerThreads[32];
  172. OBJECT_ATTRIBUTES oaPort;
  173. ANSI_STRING ansiPortName;
  174. UNICODE_STRING unicodePortName;
  175. char pszPortName[100];
  176. NTSTATUS status;
  177. ULONG lTemp, lClients;
  178. /* Create Port */
  179. ParseArgv(argc, argv);
  180. strcpy(pszPortName, DEFAULT_PORT_DIR);
  181. strcat(pszPortName, DEFAULT_PORT_NAME);
  182. RtlInitAnsiString(&ansiPortName, pszPortName);
  183. RtlAnsiStringToUnicodeString(&unicodePortName,
  184. &ansiPortName,
  185. TRUE);
  186. InitializeObjectAttributes(&oaPort,
  187. &unicodePortName,
  188. OBJ_CASE_INSENSITIVE,
  189. 0,
  190. 0);
  191. status =
  192. NtCreatePort(&portPerf,
  193. &oaPort,
  194. sizeof(LPC_PERF_BIND),
  195. PORT_MAXIMUM_MESSAGE_LENGTH,
  196. 0);
  197. CHECK_STATUS(status, "NtCreatePort");
  198. /* Spawn threads to listen to the port */
  199. lClients = Options[0];
  200. if (lClients < 0 || lClients > 32) lClients = 1;
  201. printf("Starting %d worker threads\n", lClients);
  202. for(i = 0; i < (signed)lClients ; i++)
  203. {
  204. WorkerThreads[i] = CreateThread(0,
  205. 0,
  206. (LPTHREAD_START_ROUTINE)Worker,
  207. &i,
  208. 0,
  209. &lTemp);
  210. if (WorkerThreads[i] == 0)
  211. {
  212. printf("Error: CreateThread failed - %ld:%8xd\n",
  213. i, status);
  214. return -1;
  215. }
  216. }
  217. printf("LPC Perf Server Ready...\n");
  218. /* Wait for all the threads to finish */
  219. status =
  220. WaitForMultipleObjects(lClients,
  221. WorkerThreads,
  222. TRUE,
  223. INFINITE);
  224. /* The Workers shouldn't quit...hmmm */
  225. printf("Workers have all gone home..\n");
  226. return 0;
  227. }