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.

219 lines
5.6 KiB

  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2001 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dp8simendpoint.h
  6. *
  7. * Content: Header for endpoint object class.
  8. *
  9. * History:
  10. * Date By Reason
  11. * ======== ======== =========
  12. * 05/08/01 VanceO Created.
  13. *
  14. ***************************************************************************/
  15. //=============================================================================
  16. // Object flags
  17. //=============================================================================
  18. #define DP8SIMENDPOINTOBJ_DISCONNECTING 0x01 // the endpoint is disconnecting
  19. //=============================================================================
  20. // Endpoint object class
  21. //=============================================================================
  22. class CDP8SimEndpoint
  23. {
  24. public:
  25. inline BOOL IsValidObject(void)
  26. {
  27. if ((this == NULL) || (IsBadWritePtr(this, sizeof(CDP8SimEndpoint))))
  28. {
  29. return FALSE;
  30. }
  31. if (*((DWORD*) (&this->m_Sig)) != 0x454d4953) // 0x45 0x4d 0x49 0x53 = 'EMIS' = 'SIME' in Intel order
  32. {
  33. return FALSE;
  34. }
  35. return TRUE;
  36. };
  37. static BOOL FPMAlloc(void* pvItem, void* pvContext)
  38. {
  39. CDP8SimEndpoint * pDP8SimEndpoint = (CDP8SimEndpoint*) pvItem;
  40. pDP8SimEndpoint->m_Sig[0] = 'S';
  41. pDP8SimEndpoint->m_Sig[1] = 'I';
  42. pDP8SimEndpoint->m_Sig[2] = 'M';
  43. pDP8SimEndpoint->m_Sig[3] = 'e'; // start with lower case so we can tell when it's in the pool or not
  44. pDP8SimEndpoint->m_lRefCount = 0;
  45. if (! DNInitializeCriticalSection(&pDP8SimEndpoint->m_csLock))
  46. {
  47. return FALSE;
  48. }
  49. //
  50. // Don't allow critical section re-entry.
  51. //
  52. DebugSetCriticalSectionRecursionCount(&pDP8SimEndpoint->m_csLock, 0);
  53. pDP8SimEndpoint->m_dwFlags = 0;
  54. pDP8SimEndpoint->m_hRealSPEndpoint = NULL;
  55. pDP8SimEndpoint->m_pvUserContext = NULL;
  56. return TRUE;
  57. }
  58. #undef DPF_MODNAME
  59. #define DPF_MODNAME "CDP8SimEndpoint::FPMInitialize"
  60. static void FPMInitialize(void* pvItem, void* pvContext)
  61. {
  62. CDP8SimEndpoint * pDP8SimEndpoint = (CDP8SimEndpoint*) pvItem;
  63. pDP8SimEndpoint->m_lRefCount++; // somebody is getting a pointer to this object
  64. DNASSERT(pDP8SimEndpoint->m_lRefCount == 1);
  65. //
  66. // Reset the flags.
  67. //
  68. pDP8SimEndpoint->m_dwFlags = 0;
  69. //
  70. // Save the real SP's endpoint handle.
  71. //
  72. pDP8SimEndpoint->m_hRealSPEndpoint = (HANDLE) pvContext;
  73. //
  74. // Change the signature before handing it out.
  75. //
  76. pDP8SimEndpoint->m_Sig[3] = 'E';
  77. }
  78. #undef DPF_MODNAME
  79. #define DPF_MODNAME "CDP8SimEndpoint::FPMRelease"
  80. static void FPMRelease(void* pvItem)
  81. {
  82. CDP8SimEndpoint * pDP8SimEndpoint = (CDP8SimEndpoint*) pvItem;
  83. DNASSERT(pDP8SimEndpoint->m_lRefCount == 0);
  84. //
  85. // Change the signature before putting the object back in the pool.
  86. //
  87. pDP8SimEndpoint->m_Sig[3] = 'e';
  88. }
  89. #undef DPF_MODNAME
  90. #define DPF_MODNAME "CDP8SimEndpoint::FPMDealloc"
  91. static void FPMDealloc(void* pvItem)
  92. {
  93. CDP8SimEndpoint * pDP8SimEndpoint = (CDP8SimEndpoint*) pvItem;
  94. DNASSERT(pDP8SimEndpoint->m_lRefCount == 0);
  95. DNDeleteCriticalSection(&pDP8SimEndpoint->m_csLock);
  96. }
  97. #undef DPF_MODNAME
  98. #define DPF_MODNAME "CDP8SimEndpoint::AddRef"
  99. inline void AddRef(void)
  100. {
  101. LONG lResult;
  102. lResult = InterlockedIncrement(&this->m_lRefCount);
  103. DNASSERT(lResult > 0);
  104. DPFX(DPFPREP, 9, "Endpoint 0x%p refcount = %u.", this, lResult);
  105. };
  106. #undef DPF_MODNAME
  107. #define DPF_MODNAME "CDP8SimEndpoint::Release"
  108. inline void Release(void)
  109. {
  110. LONG lResult;
  111. lResult = InterlockedDecrement(&this->m_lRefCount);
  112. DNASSERT(lResult >= 0);
  113. if (lResult == 0)
  114. {
  115. DPFX(DPFPREP, 9, "Endpoint 0x%p refcount = 0, returning to pool.", this);
  116. //
  117. // Time to return this object to the pool.
  118. //
  119. g_FPOOLEndpoint.Release(this);
  120. }
  121. else
  122. {
  123. DPFX(DPFPREP, 9, "Endpoint 0x%p refcount = %u.", this, lResult);
  124. }
  125. };
  126. inline void Lock(void) { DNEnterCriticalSection(&this->m_csLock); };
  127. inline void Unlock(void) { DNLeaveCriticalSection(&this->m_csLock); };
  128. #undef DPF_MODNAME
  129. #define DPF_MODNAME "CDP8SimEndpoint::IsDisconnecting"
  130. inline BOOL IsDisconnecting(void)
  131. {
  132. AssertCriticalSectionIsTakenByThisThread(&this->m_csLock, TRUE);
  133. return ((this->m_dwFlags & DP8SIMENDPOINTOBJ_DISCONNECTING) ? TRUE: FALSE);
  134. };
  135. #undef DPF_MODNAME
  136. #define DPF_MODNAME "CDP8SimEndpoint::NoteDisconnecting"
  137. inline void NoteDisconnecting(void)
  138. {
  139. AssertCriticalSectionIsTakenByThisThread(&this->m_csLock, TRUE);
  140. #pragma TODO(vanceo, "Have separate upper/lower layer disconnect flags")
  141. //DNASSERT(! (this->m_dwFlags & DP8SIMENDPOINTOBJ_DISCONNECTING));
  142. this->m_dwFlags |= DP8SIMENDPOINTOBJ_DISCONNECTING;
  143. };
  144. inline HANDLE GetRealSPEndpoint(void) { return this->m_hRealSPEndpoint; };
  145. inline PVOID GetUserContext(void) { return this->m_pvUserContext; };
  146. inline void SetUserContext(PVOID pvUserContext) { this->m_pvUserContext = pvUserContext; };
  147. private:
  148. BYTE m_Sig[4]; // debugging signature ('SIME')
  149. LONG m_lRefCount; // number of references for this object
  150. DNCRITICAL_SECTION m_csLock; // lock protecting the endpoint data
  151. DWORD m_dwFlags; // flags for this endpoint
  152. HANDLE m_hRealSPEndpoint; // real service provider's endpoint handle
  153. PVOID m_pvUserContext; // upper layer user context for endpoint
  154. };