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.

169 lines
4.6 KiB

  1. #include "globals.h"
  2. #if H323_USE_PRIVATE_IO_THREAD
  3. #define FINITE_WAIT_TIME 3000
  4. static HANDLE H323IoCompletionPort = NULL;
  5. static HANDLE H323IoThread = NULL;
  6. static DWORD H323IoThreadID = 0;
  7. static DWORD WINAPI H323IoThreadProc (
  8. IN PVOID ThreadParameter)
  9. {
  10. LPOVERLAPPED Overlapped;
  11. ULONG_PTR CompletionKey;
  12. DWORD Status;
  13. DWORD BytesTransferred;
  14. for (;;)
  15. {
  16. if( GetQueuedCompletionStatus( H323IoCompletionPort,
  17. &BytesTransferred,
  18. &CompletionKey,
  19. &Overlapped,
  20. INFINITE) == TRUE )
  21. {
  22. Status = ERROR_SUCCESS;
  23. }
  24. else
  25. {
  26. if( Overlapped )
  27. {
  28. Status = GetLastError();
  29. }
  30. else
  31. {
  32. H323DBG((DEBUG_LEVEL_ERROR, "failed to dequeue i/o completion "
  33. "packet: %d, quitting...", GetLastError() ));
  34. ExitThread (GetLastError());
  35. }
  36. }
  37. _ASSERTE( CompletionKey);
  38. ((LPOVERLAPPED_COMPLETION_ROUTINE) CompletionKey)( Status,
  39. BytesTransferred,
  40. Overlapped );
  41. }
  42. // never reached
  43. return EXIT_SUCCESS;
  44. }
  45. static void CALLBACK H323IoThreadExitCallback (
  46. IN DWORD Status,
  47. IN DWORD BytesTransferred,
  48. IN LPOVERLAPPED Overlapped)
  49. {
  50. H323DBG ((DEBUG_LEVEL_TRACE, "i/o completion thread is stopping"));
  51. ExitThread (EXIT_SUCCESS);
  52. }
  53. HRESULT H323IoThreadStart (void)
  54. {
  55. if( H323IoCompletionPort == NULL )
  56. {
  57. H323IoCompletionPort =
  58. CreateIoCompletionPort( INVALID_HANDLE_VALUE,
  59. NULL,
  60. 0,
  61. 0 );
  62. if( H323IoCompletionPort == NULL )
  63. {
  64. H323DBG(( DEBUG_LEVEL_ERROR,
  65. "failed to create i/o completion port: %d", GetLastError() ));
  66. return GetLastResult();
  67. }
  68. }
  69. H323IoThread = CreateThread(NULL,
  70. 0,
  71. H323IoThreadProc,
  72. NULL,
  73. 0,
  74. &H323IoThreadID );
  75. if( H323IoThread == NULL )
  76. {
  77. H323DBG(( DEBUG_LEVEL_ERROR,
  78. "failed to create i/o completion worker thread: %d",
  79. GetLastError() ));
  80. CloseHandle (H323IoCompletionPort);
  81. H323IoCompletionPort = NULL;
  82. return GetLastResult();
  83. }
  84. return S_OK;
  85. }
  86. void H323IoThreadStop (void)
  87. {
  88. DWORD dwWaitTime = INFINITE;
  89. H323DBG ((DEBUG_LEVEL_WARNING, "H323IoThreadStop entered."));
  90. if( H323IoThread != NULL )
  91. {
  92. _ASSERTE( H323IoCompletionPort != NULL );
  93. if( !PostQueuedCompletionStatus( H323IoCompletionPort, 0,
  94. (ULONG_PTR) H323IoThreadExitCallback, (LPOVERLAPPED) -1) )
  95. {
  96. H323DBG(( DEBUG_LEVEL_WARNING, "PostQueuedCompletionStatus failed" ));
  97. dwWaitTime = FINITE_WAIT_TIME;
  98. }
  99. H323DBG(( DEBUG_LEVEL_WARNING,
  100. "waiting for i/o completion port thread to finish..." ));
  101. WaitForSingleObject( H323IoThread, dwWaitTime );
  102. H323DBG(( DEBUG_LEVEL_WARNING,
  103. "i/o completion port thread is finished." ));
  104. CloseHandle (H323IoThread);
  105. H323IoThread = NULL;
  106. }
  107. if( H323IoCompletionPort != NULL )
  108. {
  109. CloseHandle( H323IoCompletionPort );
  110. H323IoCompletionPort = NULL;
  111. }
  112. H323DBG ((DEBUG_LEVEL_WARNING, "H323IoThreadStop exited"));
  113. }
  114. BOOL H323BindIoCompletionCallback (
  115. IN HANDLE ObjectHandle,
  116. IN LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine,
  117. IN ULONG Flags)
  118. {
  119. if( H323IoCompletionPort != NULL )
  120. {
  121. if (!CompletionRoutine)
  122. {
  123. SetLastError (ERROR_INVALID_PARAMETER);
  124. return FALSE;
  125. }
  126. return CreateIoCompletionPort( ObjectHandle,
  127. H323IoCompletionPort,
  128. (ULONG_PTR) CompletionRoutine,
  129. 0 ) != NULL;
  130. }
  131. else
  132. {
  133. H323DBG(( DEBUG_LEVEL_ERROR, "i/o completion port is not yet created" ));
  134. SetLastError( ERROR_GEN_FAILURE );
  135. return FALSE;
  136. }
  137. }
  138. #endif