Leaked source code of windows server 2003
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.

183 lines
3.8 KiB

  1. //
  2. // Test the quick return timeouts
  3. //
  4. // Assume that we are using a loopback connector.
  5. //
  6. // Assume that it isn't running on a stressed machine.
  7. //
  8. #include "windows.h"
  9. #include "stdio.h"
  10. #define FAILURE printf("FAIL: %d\n",__LINE__);exit(1)
  11. int __cdecl main(int argc, char *argv[]) {
  12. CHAR *myPort = "COM1";
  13. DCB myDcb;
  14. DWORD junk;
  15. COMMTIMEOUTS myTimeOuts;
  16. DWORD numberActuallyRead;
  17. DWORD numberActuallyWritten;
  18. UCHAR readBuff[1000];
  19. HANDLE comHandle;
  20. DWORD startingTicks;
  21. OVERLAPPED readOl;
  22. OVERLAPPED writeOl;
  23. UCHAR writeBuff[5] = {0,1,2,3,4};
  24. if (argc > 1) {
  25. myPort = argv[1];
  26. }
  27. if ((comHandle = CreateFile(
  28. myPort,
  29. GENERIC_READ | GENERIC_WRITE,
  30. 0,
  31. NULL,
  32. CREATE_ALWAYS,
  33. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  34. NULL
  35. )) == ((HANDLE)-1)) {
  36. FAILURE;
  37. }
  38. if (!(readOl.hEvent = CreateEvent(
  39. NULL,
  40. TRUE,
  41. FALSE,
  42. NULL
  43. ))) {
  44. FAILURE;
  45. }
  46. if (!GetCommState(
  47. comHandle,
  48. &myDcb
  49. )) {
  50. FAILURE;
  51. }
  52. myDcb.BaudRate = 19200;
  53. myDcb.ByteSize = 8;
  54. myDcb.StopBits = ONESTOPBIT;
  55. myDcb.Parity = NOPARITY;
  56. myDcb.fOutxCtsFlow = FALSE;
  57. myDcb.fOutxDsrFlow = FALSE;
  58. myDcb.fDsrSensitivity = FALSE;
  59. myDcb.fOutX = FALSE;
  60. myDcb.fInX = FALSE;
  61. myDcb.fRtsControl = RTS_CONTROL_ENABLE;
  62. myDcb.fDtrControl = DTR_CONTROL_ENABLE;
  63. if (!SetCommState(
  64. comHandle,
  65. &myDcb
  66. )) {
  67. FAILURE;
  68. }
  69. //
  70. // Make sure that the IO doesn't time out.
  71. //
  72. myTimeOuts.ReadIntervalTimeout = 0;
  73. myTimeOuts.ReadTotalTimeoutMultiplier = 0;
  74. myTimeOuts.ReadTotalTimeoutConstant = 0;
  75. myTimeOuts.WriteTotalTimeoutMultiplier = 0;
  76. myTimeOuts.WriteTotalTimeoutConstant = 0;
  77. if (!SetCommTimeouts(
  78. comHandle,
  79. &myTimeOuts
  80. )) {
  81. FAILURE;
  82. }
  83. //
  84. // Start off a read. It shouldn't complete
  85. //
  86. if (!ReadFile(
  87. comHandle,
  88. &readBuff[0],
  89. 1000,
  90. &numberActuallyRead,
  91. &readOl
  92. )) {
  93. if (GetLastError() != ERROR_IO_PENDING) {
  94. FAILURE;
  95. }
  96. if (GetOverlappedResult(
  97. comHandle,
  98. &readOl,
  99. &numberActuallyRead,
  100. FALSE
  101. )) {
  102. FAILURE;
  103. }
  104. } else {
  105. FAILURE;
  106. }
  107. //
  108. // The read should still be there. Now do a purge comm. We
  109. // should then do a sleep for 2 seconds to give the read time
  110. // to complete. Then we should first make sure that the
  111. // read has completed (via a get overlapped). Then we should
  112. // do a SetupComm (with a "large" value so that we will actually
  113. // allocate a new typeahead buffer). If there is still a "dangling"
  114. // read, then we should never return from SetupComm.
  115. //
  116. if (!PurgeComm(
  117. comHandle,
  118. PURGE_TXABORT | PURGE_RXABORT
  119. )) {
  120. FAILURE;
  121. }
  122. if (WaitForSingleObject(
  123. readOl.hEvent,
  124. 2000
  125. ) != WAIT_OBJECT_0) {
  126. FAILURE;
  127. }
  128. if (!SetupComm(
  129. comHandle,
  130. 20000,
  131. 20000
  132. )) {
  133. FAILURE;
  134. }
  135. return 1;
  136. }