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.

202 lines
4.2 KiB

  1. #ifndef __sipcli_timer_h__
  2. #define __sipcli_timer_h__
  3. #define TIMER_WINDOW_CLASS_NAME \
  4. _T("SipTimerWindowClassName-84b7f915-2389-4204-9eb5-16f4c522816f")
  5. class TIMER_MGR;
  6. struct TIMER_QUEUE_ENTRY;
  7. class __declspec(novtable) TIMER
  8. {
  9. public:
  10. inline TIMER(
  11. IN TIMER_MGR *pTimerMgr
  12. // IN HWND TimerWindow
  13. );
  14. inline ~TIMER();
  15. HRESULT StartTimer(
  16. IN UINT TimeoutValue
  17. );
  18. HRESULT KillTimer();
  19. void OnTimerExpireCommon();
  20. // Implemented by classes that inherit TIMER
  21. virtual void OnTimerExpire() = 0;
  22. inline BOOL IsTimerActive();
  23. inline TIMER_QUEUE_ENTRY *GetTimerQueueEntry();
  24. private:
  25. //UINT_PTR TimerId;
  26. //HWND m_TimerWindow;
  27. TIMER_MGR *m_pTimerMgr;
  28. TIMER_QUEUE_ENTRY *m_pTimerQEntry;
  29. // XXX Could probably get rid of the timeout value
  30. UINT m_TimeoutValue;
  31. };
  32. inline
  33. TIMER::TIMER(
  34. IN TIMER_MGR *pTimerMgr
  35. // IN HWND TimerWindow
  36. )
  37. {
  38. // m_TimerWindow = TimerWindow;
  39. m_pTimerMgr = pTimerMgr;
  40. m_pTimerQEntry = NULL;
  41. m_TimeoutValue = 0;
  42. }
  43. inline
  44. TIMER::~TIMER()
  45. {
  46. // ASSERT(!IsTimerActive());
  47. if (IsTimerActive())
  48. {
  49. KillTimer();
  50. }
  51. }
  52. inline BOOL
  53. TIMER::IsTimerActive()
  54. {
  55. return !(m_TimeoutValue == 0);
  56. }
  57. inline TIMER_QUEUE_ENTRY *
  58. TIMER::GetTimerQueueEntry()
  59. {
  60. return m_pTimerQEntry;
  61. }
  62. enum TIMERQ_STATE
  63. {
  64. TIMERQ_STATE_INIT = 0,
  65. TIMERQ_STATE_STARTED,
  66. // State when the timer expired and we have posted
  67. // a message to the window for processing the timer
  68. // callback.
  69. TIMERQ_STATE_EXPIRED,
  70. // State when the timer is killed and we have posted
  71. // a message to the window for processing the timer
  72. // callback.
  73. TIMERQ_STATE_KILLED
  74. };
  75. // StartTimer() adds this entry to the queue and KillTimer() removes
  76. // this entry from the queue.
  77. // Note that we can not reuse the TIMER structure for the TIMER_QUEUE_ENTRY
  78. // structure as sometimes the TIMER_QUEUE_ENTRY structure will have to
  79. // live beyond the lifetime of the TIMER structure.
  80. struct TIMER_QUEUE_ENTRY
  81. {
  82. TIMER_QUEUE_ENTRY(
  83. IN TIMER *pTimer,
  84. IN ULONG TimeoutValue
  85. );
  86. LIST_ENTRY m_ListEntry;
  87. ULONG m_ExpireTickCount;
  88. TIMER *m_pTimer;
  89. // Used to deal with the scenario where a timer is killed
  90. // when the windows message for calling the timer callback
  91. // for the timer is still in the message queue.
  92. //BOOL m_IsTimerKilled;
  93. TIMERQ_STATE m_TimerQState;
  94. };
  95. class TIMER_MGR
  96. {
  97. public:
  98. TIMER_MGR();
  99. ~TIMER_MGR();
  100. HRESULT Start();
  101. HRESULT Stop();
  102. HRESULT StartTimer(
  103. IN TIMER *pTimer,
  104. IN ULONG TimeoutValue,
  105. OUT TIMER_QUEUE_ENTRY **ppTimerQEntry
  106. );
  107. HRESULT KillTimer(
  108. IN TIMER *pTimer
  109. );
  110. VOID OnMainTimerExpire();
  111. inline VOID DecrementNumExpiredListEntries();
  112. inline ULONG GetNumExpiredListEntries();
  113. private:
  114. // Queue of timers (List of TIMER_QUEUE_ENTRY structures)
  115. // Sorted by m_ExpireTickCount
  116. LIST_ENTRY m_TimerQueue;
  117. ULONG m_NumTimerQueueEntries;
  118. LIST_ENTRY m_ExpiredList;
  119. ULONG m_NumExpiredListEntries;
  120. HWND m_TimerWindow;
  121. BOOL m_IsMainTimerActive;
  122. ULONG m_MainTimerTickCount;
  123. BOOL m_isTimerStopped;
  124. HRESULT ProcessTimerExpire(
  125. IN TIMER_QUEUE_ENTRY *pTimerQEntry
  126. );
  127. VOID AdjustMainTimer();
  128. TIMER_QUEUE_ENTRY *FindTimerQueueEntryInList(
  129. TIMER *pTimer,
  130. LIST_ENTRY *pListHead
  131. );
  132. BOOL IsTimerTickCountLessThanOrEqualTo(
  133. IN ULONG TickCount1,
  134. IN ULONG TickCount2
  135. );
  136. HRESULT CreateTimerWindow();
  137. VOID DebugPrintTimerQueue();
  138. };
  139. inline ULONG
  140. TIMER_MGR::GetNumExpiredListEntries()
  141. {
  142. return m_NumExpiredListEntries;
  143. }
  144. inline VOID
  145. TIMER_MGR::DecrementNumExpiredListEntries()
  146. {
  147. m_NumExpiredListEntries--;
  148. }
  149. #endif // __sipcli_timer_h__