AGCEventThread.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef __AGCEventThread_h__
  2. #define __AGCEventThread_h__
  3. /////////////////////////////////////////////////////////////////////////////
  4. // AGCEventThread : Declaration of the CAGCEventThread class.
  5. //
  6. #include <AGC.h>
  7. #include <..\TCLib\AutoHandle.h>
  8. #include <..\TCLib\WorkerThread.h>
  9. #include "AGCGlobal.h"
  10. #include "AGCEventData.h"
  11. /////////////////////////////////////////////////////////////////////////////
  12. //
  13. class CAGCEventThread :
  14. public TCWorkerThread
  15. {
  16. // Construction
  17. private:
  18. CAGCEventThread(const CAGCEventThread&); // Disable copy constructor
  19. public:
  20. CAGCEventThread(CAGCGlobal* pGlobal);
  21. // Operations
  22. public:
  23. void QueueEvent(HAGCLISTENERS hListeners, CAGCEventData& data);
  24. void QueueEventSynchronous(HAGCLISTENERS hListeners, CAGCEventData& data);
  25. // Overrides
  26. protected:
  27. virtual IUnknown* OnGetUnknown();
  28. virtual TC_WorkItemRelProc OnGetWorkItemRelProc();
  29. virtual void OnMessage(UINT idMsg, int cParams, LPARAM* rgParams);
  30. // Implementation
  31. protected:
  32. static void WINAPI ArgumentReleaseProc(UINT idMsg, int cParams,
  33. LPARAM* rgParams);
  34. // Types
  35. protected:
  36. // Event Registration
  37. typedef CAGCGlobal::XEventSinks XEventSinks;
  38. typedef CAGCGlobal::XEventSinksIt XEventSinksIt;
  39. // Worker Thread Identifiers
  40. enum {e_TriggerEvent};
  41. // Data Members
  42. protected:
  43. CAGCGlobal* m_pGlobal;
  44. };
  45. /////////////////////////////////////////////////////////////////////////////
  46. // Construction
  47. inline CAGCEventThread::CAGCEventThread(CAGCGlobal* pGlobal) :
  48. m_pGlobal(pGlobal)
  49. {
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Operations
  53. inline void CAGCEventThread::QueueEvent(HAGCLISTENERS hListeners,
  54. CAGCEventData& data)
  55. {
  56. // Detach the data and post it to the thread
  57. UINT cbData;
  58. BYTE* pbData = data.Detach(&cbData);
  59. PostMessage(e_TriggerEvent, 4, hListeners, cbData, pbData, NULL);
  60. }
  61. inline void CAGCEventThread::QueueEventSynchronous(HAGCLISTENERS hListeners,
  62. CAGCEventData& data)
  63. {
  64. TCHandle shEvent = ::CreateEvent(NULL, false, false, NULL);
  65. // Detach the data and post it to the thread
  66. UINT cbData;
  67. BYTE* pbData = data.Detach(&cbData);
  68. PostMessage(e_TriggerEvent, 4, hListeners, cbData, pbData, shEvent.GetHandle());
  69. WaitForSingleObject(shEvent, INFINITE);
  70. }
  71. /////////////////////////////////////////////////////////////////////////////
  72. // Overrides
  73. inline IUnknown* CAGCEventThread::OnGetUnknown()
  74. {
  75. return m_pGlobal->GetUnknown();
  76. }
  77. inline TC_WorkItemRelProc CAGCEventThread::OnGetWorkItemRelProc()
  78. {
  79. return ArgumentReleaseProc;
  80. }
  81. inline void CAGCEventThread::OnMessage(UINT idMsg, int cParams,
  82. LPARAM* rgParams)
  83. {
  84. switch (idMsg)
  85. {
  86. case e_TriggerEvent:
  87. {
  88. assert(4 <= cParams);
  89. XEventSinks* pSinks = reinterpret_cast<XEventSinks*>(rgParams[0]);
  90. UINT cbData = static_cast<UINT >(rgParams[1]);
  91. BYTE* pbData = reinterpret_cast<BYTE* >(rgParams[2]);
  92. HANDLE hevt = reinterpret_cast<HANDLE >(rgParams[3]);
  93. // Attach the parameters to a CAGCEventData object
  94. CAGCEventData data(cbData, pbData);
  95. // Create an event object
  96. CComObject<CAGCEvent>* pEvent = NULL;
  97. ZSucceeded(pEvent->CreateInstance(&pEvent));
  98. IAGCEventPtr spEvent(pEvent);
  99. // Initialize the AGCEvent object from the CAGCEventData object
  100. ZSucceeded(pEvent->Init(data));
  101. // Notify each registered event sink
  102. for (XEventSinksIt it = pSinks->begin(); it != pSinks->end(); ++it)
  103. {
  104. IAGCEventSinkPtr spSink;
  105. if (m_pGlobal->GetEventSinksAreGITCookies())
  106. {
  107. ZSucceeded(m_pGlobal->GetInterfaceFromGlobal(
  108. reinterpret_cast<DWORD>(*it), IID_IAGCEventSink, (void**)&spSink));
  109. }
  110. else
  111. {
  112. spSink = *it;
  113. }
  114. if (hevt)
  115. {
  116. IAGCEventSinkSynchronousPtr spSinkSync(spSink);
  117. if (spSinkSync != NULL)
  118. {
  119. spSinkSync->OnEventTriggeredSynchronous(spEvent);
  120. continue;
  121. }
  122. }
  123. spSink->OnEventTriggered(spEvent);
  124. }
  125. // Detach the parameters from the CAGCEventData object
  126. // (since they'll be properly destructed in ArgumentReleaseProc)
  127. data.Detach();
  128. return;
  129. }
  130. }
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. // Implementation
  134. inline void WINAPI CAGCEventThread::ArgumentReleaseProc(UINT idMsg,
  135. int cParams, LPARAM* rgParams)
  136. {
  137. switch (idMsg)
  138. {
  139. case e_TriggerEvent:
  140. {
  141. assert(4 <= cParams);
  142. XEventSinks* pSinks = reinterpret_cast<XEventSinks*>(rgParams[0]);
  143. UINT cbData = static_cast<UINT >(rgParams[1]);
  144. BYTE* pbData = reinterpret_cast<BYTE* >(rgParams[2]);
  145. HANDLE hevt = reinterpret_cast<HANDLE >(rgParams[3]);
  146. // Free the listeners array
  147. CAGCGlobal::FreeListenersImpl(reinterpret_cast<HAGCLISTENERS>(pSinks));
  148. // Attach the parameters to a CAGCEventData object
  149. // (deleted when CAGCEventData is destructed)
  150. CAGCEventData(cbData, pbData);
  151. // Signal the event, if synchronous
  152. if (hevt)
  153. ::SetEvent(hevt);
  154. return;
  155. }
  156. }
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. #endif // !__AGCEventThread_h__