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.

259 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. atqbmon.hxx
  5. Abstract:
  6. ATQ Backlog Monitor
  7. Author:
  8. 01-Dec-1998 MCourage
  9. Revision History:
  10. --*/
  11. #ifndef __ATQBMON_HXX__
  12. #define __ATQBMON_HXX__
  13. #define ATQ_BMON_ENTRY_SIGNATURE ((DWORD) 'NOMB') // BMON
  14. #define ATQ_FREE_BMON_ENTRY_SIGNATURE ((DWORD) 'NOMX') // XMON
  15. #define ATQ_BMON_WAKEUP_MESSAGE ((DWORD) 'EKAW') // WAKE
  16. #define ATQ_BMON_WAKEUP_PORT (3456)
  17. #define ATQ_BMON_WAKEUP_PORT_MAX (3556)
  18. //
  19. // forwards
  20. //
  21. class ATQ_BACKLOG_MONITOR;
  22. class ATQ_BMON_ENTRY;
  23. class ATQ_BMON_WAKEUP_ENTRY;
  24. class ATQ_BMON_SET;
  25. //
  26. // globals
  27. //
  28. extern ATQ_BACKLOG_MONITOR * g_pAtqBacklogMonitor;
  29. //
  30. // Helper function
  31. //
  32. DWORD WINAPI BmonThreadFunc(LPVOID lpBmonSet);
  33. //
  34. // ATQ_BACKLOG_MONITOR
  35. //
  36. // This class is the main interface to the backlog monitor.
  37. //
  38. class ATQ_BACKLOG_MONITOR {
  39. public:
  40. ATQ_BACKLOG_MONITOR();
  41. ~ATQ_BACKLOG_MONITOR();
  42. BOOL AddEntry(ATQ_BMON_ENTRY * pBmonEntry);
  43. BOOL RemoveEntry(ATQ_BMON_ENTRY * pBmonEntry);
  44. BOOL PauseEntry(ATQ_BMON_ENTRY * pBmonEntry);
  45. BOOL ResumeEntry(ATQ_BMON_ENTRY * pBmonEntry);
  46. private:
  47. VOID Lock()
  48. { EnterCriticalSection( &m_csLock ); }
  49. VOID Unlock()
  50. { LeaveCriticalSection( &m_csLock ); }
  51. CRITICAL_SECTION m_csLock;
  52. LIST_ENTRY m_ListHead;
  53. };
  54. //
  55. // enum BMON_ENTRY_OPCODE
  56. //
  57. // Tells the select thread what it should
  58. // do with a particular BMON_ENTRY
  59. //
  60. typedef enum _BMON_OPCODE {
  61. BMON_INVALID,
  62. BMON_WAIT,
  63. BMON_NOWAIT,
  64. BMON_PAUSE,
  65. BMON_RESUME,
  66. BMON_ADD,
  67. BMON_REMOVE
  68. } BMON_OPCODE;
  69. //
  70. // ATQ_BMON_ENTRY
  71. //
  72. // The client of the backlog monitor provides an instance
  73. // of this class which contains a socket descriptor and
  74. // a callback function which the client overrides.
  75. //
  76. class ATQ_BMON_ENTRY {
  77. public:
  78. ATQ_BMON_ENTRY(SOCKET s);
  79. virtual ~ATQ_BMON_ENTRY();
  80. virtual BOOL Callback() = 0;
  81. //
  82. // these functions are used internally
  83. // by the monitor
  84. //
  85. BOOL InitEvent();
  86. VOID SignalAddRemove(DWORD dwError);
  87. BOOL WaitForAddRemove();
  88. VOID SetOpcode(BMON_OPCODE bmonOpcode)
  89. { m_BmonOpcode = bmonOpcode; }
  90. BMON_OPCODE GetOpcode()
  91. { return m_BmonOpcode; }
  92. VOID SetContainingBmonSet(ATQ_BMON_SET * pBmonSet)
  93. { m_pBmonSet = pBmonSet; }
  94. ATQ_BMON_SET * GetContainingBmonSet()
  95. { return m_pBmonSet; }
  96. DWORD GetError()
  97. { return m_dwErr; }
  98. SOCKET GetSocket()
  99. { return m_Socket; }
  100. BOOL CheckSignature()
  101. { return (m_Signature == ATQ_BMON_ENTRY_SIGNATURE); }
  102. private:
  103. DWORD m_Signature;
  104. SOCKET m_Socket;
  105. HANDLE m_hAddRemoveEvent;
  106. BMON_OPCODE m_BmonOpcode;
  107. ATQ_BMON_SET * m_pBmonSet;
  108. DWORD m_dwErr;
  109. };
  110. //
  111. // ATQ_BMON_WAKEUP_ENTRY
  112. //
  113. // This is a special entry whose purpose is to
  114. // wake up an ATQ_BMON_SET's thread so it can
  115. // call SynchronizeSets.
  116. //
  117. class BMON_WAKEUP_ENTRY : public ATQ_BMON_ENTRY {
  118. public:
  119. BMON_WAKEUP_ENTRY(SOCKET s) : ATQ_BMON_ENTRY(s) {}
  120. virtual ~BMON_WAKEUP_ENTRY() {}
  121. virtual BOOL Callback();
  122. };
  123. #define BMON_SELECT_ERROR 0x80000000
  124. #define BMON_NOTIFY_ERROR 0x40000000
  125. #define BMON_SENDTO_ERROR 0x20000000
  126. //
  127. // ATQ_BMON_SET
  128. //
  129. // This class owns a set of listen sockets. It uses
  130. // select to see which listeners are out of AcceptEx
  131. // contexts.
  132. //
  133. class ATQ_BMON_SET {
  134. public:
  135. ATQ_BMON_SET();
  136. ~ATQ_BMON_SET();
  137. BOOL Initialize();
  138. BOOL Cleanup();
  139. BOOL IsEmpty();
  140. BOOL IsNotFull();
  141. BOOL AddEntry(ATQ_BMON_ENTRY * pBmonEntry);
  142. BOOL RemoveEntry(ATQ_BMON_ENTRY * pBmonEntry);
  143. BOOL PauseEntry(ATQ_BMON_ENTRY * pBmonEntry);
  144. BOOL ResumeEntry(ATQ_BMON_ENTRY * pBmonEntry);
  145. //
  146. // Called to tell thread to sleep after notifying
  147. //
  148. VOID DoSleep( BOOL fDoSleep )
  149. {
  150. m_fDoSleep = fDoSleep;
  151. }
  152. //
  153. // Has the thread gone away?
  154. //
  155. BOOL ThreadFinished( VOID ) const
  156. {
  157. return m_fThreadFinished;
  158. }
  159. //
  160. // called (indirectly) by Initialize
  161. //
  162. VOID BmonThreadFunc();
  163. LIST_ENTRY m_SetList;
  164. private:
  165. VOID Lock()
  166. { EnterCriticalSection( &m_csLock ); }
  167. VOID Unlock()
  168. { LeaveCriticalSection( &m_csLock ); }
  169. VOID Wakeup();
  170. BOOL SynchronizeSets();
  171. BOOL NotifyEntries();
  172. CRITICAL_SECTION m_csLock;
  173. DWORD m_SetSize;
  174. ATQ_BMON_ENTRY * m_apEntrySet[FD_SETSIZE];
  175. FD_SET m_ListenSet;
  176. BMON_WAKEUP_ENTRY * m_pWakeupEntry;
  177. WORD m_Port;
  178. BOOL m_fCleanup;
  179. HANDLE m_hThread;
  180. BOOL m_fDoSleep;
  181. BOOL m_fThreadFinished;
  182. DWORD m_dwError;
  183. };
  184. #endif // __ATQBMON_HXX__