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.

113 lines
4.1 KiB

  1. #if !defined(_FUSION_INC_ASYNCHELP_H_INCLUDED_)
  2. #define _FUSION_INC_ASYNCHELP_H_INCLUDED_
  3. #pragma once
  4. class CAsyncContext : public OVERLAPPED
  5. {
  6. public:
  7. CAsyncContext() { }
  8. virtual ~CAsyncContext() { }
  9. // Public handler for async operations which are finished via an I/O completion port
  10. static VOID OnQueuedCompletion(HANDLE hCompletionPort, DWORD cbTransferred, ULONG_PTR ulCompletionKey, LPOVERLAPPED lpo)
  11. {
  12. CAsyncContext *pThis = reinterpret_cast<CAsyncContext *>(ulCompletionKey);
  13. INVOCATION_CONTEXT ic;
  14. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eCompletionPort;
  15. ic.m_dwErrorCode = ERROR_SUCCESS;
  16. ic.m_hCompletionPort = hCompletionPort;
  17. ic.m_lpo = lpo;
  18. ic.m_cbTransferred = cbTransferred;
  19. pThis->OnCompletion(ic);
  20. }
  21. // Public handler for async operations which are signalled via an APC.
  22. static VOID CALLBACK OnUserAPC(DWORD_PTR dwParam)
  23. {
  24. CAsyncContext *pThis = reinterpret_cast<CAsyncContext *>(dwParam);
  25. INVOCATION_CONTEXT ic;
  26. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eUserAPC;
  27. ic.m_dwErrorCode = ERROR_SUCCESS;
  28. pThis->OnCompletion(ic);
  29. }
  30. // Public handler for async operations which are signalled via a thread message
  31. static VOID OnThreadMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  32. {
  33. CAsyncContext *pThis = reinterpret_cast<CAsyncContext *>(wParam);
  34. INVOCATION_CONTEXT ic;
  35. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eThreadMessage;
  36. ic.m_dwErrorCode = ERROR_SUCCESS;
  37. ic.m_uMsg = uMsg;
  38. ic.m_lParam = lParam;
  39. pThis->OnCompletion(ic);
  40. }
  41. // Public handler for async operations which are signalled via a window message
  42. static VOID OnWindowMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  43. {
  44. CAsyncContext *pThis = reinterpret_cast<CAsyncContext *>(wParam);
  45. INVOCATION_CONTEXT ic;
  46. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eWindowMessage;
  47. ic.m_dwErrorCode = ERROR_SUCCESS;
  48. ic.m_hwnd = hwnd;
  49. ic.m_uMsg = uMsg;
  50. ic.m_lParam = lParam;
  51. pThis->OnCompletion(ic);
  52. }
  53. // Public handler for async operations which are signalled via an overlapped completion routine
  54. // (e.g. ReadFileEx(), WriteFileEx()).
  55. static VOID CALLBACK OnOverlappedCompletion(DWORD dwErrorCode, DWORD cbTransferred, LPOVERLAPPED lpo)
  56. {
  57. CAsyncContext *pThis = static_cast<CAsyncContext *>(lpo);
  58. INVOCATION_CONTEXT ic;
  59. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eOverlappedCompletionRoutine;
  60. ic.m_lpo = lpo;
  61. ic.m_dwErrorCode = dwErrorCode;
  62. ic.m_cbTransferred = cbTransferred;
  63. pThis->OnCompletion(ic);
  64. }
  65. // Call this member function when an asynch I/O completes immediately
  66. VOID OnImmediateCompletion(DWORD dwErrorCode, DWORD cbTransferred)
  67. {
  68. INVOCATION_CONTEXT ic;
  69. ic.m_it = CAsyncContext::INVOCATION_CONTEXT::eDirectCall;
  70. ic.m_lpo = this;
  71. ic.m_dwErrorCode = dwErrorCode;
  72. ic.m_cbTransferred = cbTransferred;
  73. this->OnCompletion(ic);
  74. }
  75. protected:
  76. struct INVOCATION_CONTEXT
  77. {
  78. enum InvocationType
  79. {
  80. eCompletionPort,
  81. eUserAPC,
  82. eThreadMessage,
  83. eWindowMessage,
  84. eDirectCall,
  85. eOverlappedCompletionRoutine,
  86. } m_it;
  87. DWORD m_dwErrorCode; // Win32 error code - valid for all invocation types
  88. HANDLE m_hCompletionPort; // valid for: eCompletionPort
  89. LPOVERLAPPED m_lpo; // valid for: eCompletionPort, eOverlappedCompletionRoutine, eDirectCall
  90. DWORD m_cbTransferred; // valid for: eCompletionPort, eOverlappedCompletionRoutine, eDirectCall
  91. LPARAM m_lParam; // valid for: eThreadMessage, eWindowMessage
  92. HWND m_hwnd; // valid for: eWindowMessage
  93. UINT m_uMsg; // valid for: eThreadMessage, eWindowMessage
  94. };
  95. // Derived classes override OnCompletion to do what's necessary.
  96. virtual VOID OnCompletion(const INVOCATION_CONTEXT &ric) = 0;
  97. };
  98. #endif