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.

269 lines
5.4 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. // 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, SOCKET s2) : ATQ_BMON_ENTRY(s), m_ExclusiveEnforce(s2) {}
  120. virtual ~BMON_WAKEUP_ENTRY()
  121. {
  122. if (m_ExclusiveEnforce != INVALID_SOCKET)
  123. {
  124. closesocket( m_ExclusiveEnforce);
  125. m_ExclusiveEnforce = INVALID_SOCKET;
  126. }
  127. }
  128. virtual BOOL Callback();
  129. private:
  130. SOCKET m_ExclusiveEnforce;
  131. };
  132. #define BMON_SELECT_ERROR 0x80000000
  133. #define BMON_NOTIFY_ERROR 0x40000000
  134. #define BMON_SENDTO_ERROR 0x20000000
  135. //
  136. // ATQ_BMON_SET
  137. //
  138. // This class owns a set of listen sockets. It uses
  139. // select to see which listeners are out of AcceptEx
  140. // contexts.
  141. //
  142. class ATQ_BMON_SET {
  143. public:
  144. ATQ_BMON_SET();
  145. ~ATQ_BMON_SET();
  146. BOOL Initialize();
  147. BOOL Cleanup();
  148. BOOL IsEmpty();
  149. BOOL IsNotFull();
  150. BOOL AddEntry(ATQ_BMON_ENTRY * pBmonEntry);
  151. BOOL RemoveEntry(ATQ_BMON_ENTRY * pBmonEntry);
  152. BOOL PauseEntry(ATQ_BMON_ENTRY * pBmonEntry);
  153. BOOL ResumeEntry(ATQ_BMON_ENTRY * pBmonEntry);
  154. //
  155. // Called to tell thread to sleep after notifying
  156. //
  157. VOID DoSleep( BOOL fDoSleep )
  158. {
  159. m_fDoSleep = fDoSleep;
  160. }
  161. //
  162. // Has the thread gone away?
  163. //
  164. BOOL ThreadFinished( VOID ) const
  165. {
  166. return m_fThreadFinished;
  167. }
  168. //
  169. // called (indirectly) by Initialize
  170. //
  171. VOID BmonThreadFunc();
  172. LIST_ENTRY m_SetList;
  173. private:
  174. VOID Lock()
  175. { EnterCriticalSection( &m_csLock ); }
  176. VOID Unlock()
  177. { LeaveCriticalSection( &m_csLock ); }
  178. VOID Wakeup();
  179. BOOL SynchronizeSets();
  180. BOOL NotifyEntries();
  181. CRITICAL_SECTION m_csLock;
  182. DWORD m_SetSize;
  183. ATQ_BMON_ENTRY * m_apEntrySet[FD_SETSIZE];
  184. FD_SET m_ListenSet;
  185. BMON_WAKEUP_ENTRY * m_pWakeupEntry;
  186. WORD m_Port;
  187. BOOL m_fCleanup;
  188. HANDLE m_hThread;
  189. BOOL m_fDoSleep;
  190. BOOL m_fThreadFinished;
  191. DWORD m_dwError;
  192. };
  193. #endif // __ATQBMON_HXX__