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.

344 lines
9.4 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: SPData.h
  6. * Content: Global information for the DPNWSOCK service provider in class
  7. * format.
  8. *
  9. *
  10. * History:
  11. * Date By Reason
  12. * ==== == ======
  13. * 03/15/1999 jtk Derived from Locals.h
  14. * 03/22/2000 jtk Updated with changes to interface names
  15. ***************************************************************************/
  16. #ifndef __SPDATA_H__
  17. #define __SPDATA_H__
  18. //**********************************************************************
  19. // Constant definitions
  20. //**********************************************************************
  21. #define DEFAULT_ADDRESS_BUFFER_SIZE 151
  22. //
  23. // enumeration of the states the SP can be in
  24. //
  25. typedef enum
  26. {
  27. SPSTATE_UNKNOWN = 0, // uninitialized state
  28. SPSTATE_UNINITIALIZED = 0, // uninitialized state
  29. SPSTATE_INITIALIZED, // service provider has been initialized
  30. SPSTATE_CLOSING // service provider is closing
  31. } SPSTATE;
  32. //**********************************************************************
  33. // Macro definitions
  34. //**********************************************************************
  35. //**********************************************************************
  36. // Structure definitions
  37. //**********************************************************************
  38. // forward structure and class references
  39. class CCommandData;
  40. class CEndpoint;
  41. class CSocketAddress;
  42. class CSocketPort;
  43. class CThreadPool;
  44. typedef enum _ENDPOINT_TYPE ENDPOINT_TYPE;
  45. typedef enum _GATEWAY_BIND_TYPE GATEWAY_BIND_TYPE;
  46. typedef struct _SPRECEIVEDBUFFER SPRECEIVEDBUFFER;
  47. //**********************************************************************
  48. // Class definitions
  49. //**********************************************************************
  50. //
  51. // class for information used by the provider
  52. //
  53. class CSPData
  54. {
  55. public:
  56. CSPData() {};
  57. ~CSPData() {};
  58. #undef DPF_MODNAME
  59. #define DPF_MODNAME "CSPData::AddRef"
  60. LONG AddRef( void )
  61. {
  62. LONG lResult;
  63. lResult = DNInterlockedIncrement( const_cast<LONG*>(&m_lRefCount) );
  64. DPFX(DPFPREP, 9, "(0x%p) Refcount = %i.", this, lResult);
  65. return lResult;
  66. }
  67. #undef DPF_MODNAME
  68. #define DPF_MODNAME "CSPData::DecRef"
  69. LONG DecRef( void )
  70. {
  71. LONG lResult;
  72. DNASSERT( m_lRefCount != 0 );
  73. lResult = DNInterlockedDecrement( const_cast<LONG*>(&m_lRefCount) );
  74. if ( lResult == 0 )
  75. {
  76. DPFX(DPFPREP, 9, "(0x%p) Refcount = 0, destroying this object.", this);
  77. //
  78. // WARNING, the following function deletes this object!!!
  79. //
  80. DestroyThisObject();
  81. }
  82. else
  83. {
  84. DPFX(DPFPREP, 9, "(0x%p) Refcount = %i.", this, lResult);
  85. }
  86. return lResult;
  87. }
  88. #undef DPF_MODNAME
  89. #define DPF_MODNAME "CSPData::ObjectAddRef"
  90. void ObjectAddRef( void )
  91. {
  92. LONG lResult;
  93. AddRef();
  94. Lock();
  95. //
  96. // This is actually bogus on 95, since you can only count on
  97. // negative, 0, or positive. Doesn't seem to hurt, though.
  98. //
  99. lResult = DNInterlockedIncrement( const_cast<LONG*>(&m_lObjectRefCount) );
  100. if ( lResult == 1 )
  101. {
  102. DPFX(DPFPREP, 8, "(0x%p) Resetting shutdown event.",
  103. this);
  104. DNASSERT( m_hShutdownEvent != NULL );
  105. if ( DNResetEvent( m_hShutdownEvent ) == FALSE )
  106. {
  107. DWORD dwError;
  108. dwError = GetLastError();
  109. DPFX(DPFPREP, 0, "Failed to reset shutdown event!");
  110. DisplayErrorCode( 0, dwError );
  111. }
  112. }
  113. else
  114. {
  115. DPFX(DPFPREP, 9, "(0x%p) Not resetting shutdown event, refcount = %i.",
  116. this, lResult);
  117. }
  118. Unlock();
  119. }
  120. #undef DPF_MODNAME
  121. #define DPF_MODNAME "CSPData::ObjectDecRef"
  122. void ObjectDecRef( void )
  123. {
  124. LONG lResult;
  125. Lock();
  126. lResult = DNInterlockedDecrement( const_cast<LONG*>(&m_lObjectRefCount) );
  127. if ( lResult == 0 )
  128. {
  129. DPFX(DPFPREP, 8, "(0x%p) Setting shutdown event.",
  130. this);
  131. if ( DNSetEvent( m_hShutdownEvent ) == FALSE )
  132. {
  133. DWORD dwError;
  134. dwError = GetLastError();
  135. DPFX(DPFPREP, 0, "Failed to set shutdown event!");
  136. DisplayErrorCode( 0, dwError );
  137. }
  138. }
  139. else
  140. {
  141. DPFX(DPFPREP, 9, "(0x%p) Not setting shutdown event, refcount = %i.",
  142. this, lResult);
  143. }
  144. Unlock();
  145. DecRef();
  146. }
  147. HRESULT Initialize(
  148. IDP8ServiceProviderVtbl *const pVtbl
  149. #if ((! defined(DPNBUILD_NOIPV6)) || (! defined(DPNBUILD_NOIPX)))
  150. ,const short sSPType
  151. #endif // ! DPNBUILD_NOIPV6 or ! DPNBUILD_NOIPX
  152. #ifdef DPNBUILD_PREALLOCATEDMEMORYMODEL
  153. ,const XDP8CREATE_PARAMS * const pDP8CreateParams
  154. #endif // DPNBUILD_PREALLOCATEDMEMORYMODEL
  155. );
  156. void Deinitialize( void );
  157. HRESULT Startup( SPINITIALIZEDATA *pInitializeData );
  158. void Shutdown( void );
  159. void Lock( void ) { DNEnterCriticalSection( &m_Lock ); }
  160. void Unlock( void ) { DNLeaveCriticalSection( &m_Lock ); }
  161. #if ((! defined(DPNBUILD_NOIPV6)) || (! defined(DPNBUILD_NOIPX)))
  162. short GetType( void ) const { return m_sSPType; }
  163. #endif // ! DPNBUILD_NOIPV6 or ! DPNBUILD_NOIPX
  164. const SPSTATE GetState( void ) const { return m_SPState; }
  165. #undef DPF_MODNAME
  166. #define DPF_MODNAME "CSPData::SetState"
  167. void SetState( const SPSTATE NewState )
  168. {
  169. DNASSERT( ( NewState == SPSTATE_UNINITIALIZED ) ||
  170. ( NewState == SPSTATE_INITIALIZED ) ||
  171. ( NewState == SPSTATE_CLOSING ) );
  172. m_SPState = NewState;
  173. }
  174. CThreadPool *GetThreadPool( void ) const { return m_pThreadPool; }
  175. #undef DPF_MODNAME
  176. #define DPF_MODNAME "CSPData::SetThreadPool"
  177. void SetThreadPool( CThreadPool *const pThreadPool )
  178. {
  179. DNASSERT( ( m_pThreadPool == NULL ) || ( pThreadPool == NULL ) );
  180. m_pThreadPool = pThreadPool;
  181. }
  182. //
  183. // functions to manage the endpoint list
  184. //
  185. HRESULT BindEndpoint( CEndpoint *const pEndpoint,
  186. IDirectPlay8Address *const pDeviceAddress,
  187. const CSocketAddress *const pSocketAddress,
  188. const GATEWAY_BIND_TYPE GatewayBindType );
  189. void UnbindEndpoint( CEndpoint *const pEndpoint );
  190. #ifndef DPNBUILD_NOMULTICAST
  191. HRESULT GetEndpointFromAddress( IDirectPlay8Address *const pHostAddress,
  192. IDirectPlay8Address *const pDeviceAddress,
  193. HANDLE * phEndpoint,
  194. PVOID * ppvEndpointContext );
  195. #endif // ! DPNBUILD_NOMULTICAST
  196. //
  197. // endpoint pool management
  198. //
  199. CEndpoint *GetNewEndpoint( void );
  200. CEndpoint *EndpointFromHandle( const HANDLE hEndpoint );
  201. void CloseEndpointHandle( CEndpoint *const pEndpoint );
  202. CEndpoint *GetEndpointAndCloseHandle( const HANDLE hEndpoint );
  203. #ifndef DPNBUILD_NONATHELP
  204. void MungePublicAddress( const CSocketAddress * const pDeviceBaseAddress, CSocketAddress * const pPublicAddress, const BOOL fEnum );
  205. #endif // !DPNBUILD_NONATHELP
  206. IDP8SPCallback *DP8SPCallbackInterface( void ) { return reinterpret_cast<IDP8SPCallback*>( m_InitData.pIDP ); }
  207. IDP8ServiceProvider *COMInterface( void ) { return reinterpret_cast<IDP8ServiceProvider*>( &m_COMInterface ); }
  208. #undef DPF_MODNAME
  209. #define DPF_MODNAME "CSPData::SPDataFromCOMInterface"
  210. static CSPData *SPDataFromCOMInterface( IDP8ServiceProvider *const pCOMInterface )
  211. {
  212. CSPData * pResult;
  213. DNASSERT( pCOMInterface != NULL );
  214. DBG_CASSERT( sizeof( BYTE* ) == sizeof( pCOMInterface ) );
  215. DBG_CASSERT( sizeof( CSPData* ) == sizeof( BYTE* ) );
  216. pResult = reinterpret_cast<CSPData*>( &reinterpret_cast<BYTE*>( pCOMInterface )[ -OFFSETOF( CSPData, m_COMInterface ) ] );
  217. // Verify signature is 'TDPS' DWORD a.k.a. 'SPDT' in bytes.
  218. DNASSERT(*((DWORD*) (&pResult->m_Sig)) == 0x54445053);
  219. return pResult;
  220. }
  221. #ifndef WINCE
  222. void SetWinsockBufferSizeOnAllSockets( const INT iBufferSize );
  223. #endif // ! WINCE
  224. #undef DPF_MODNAME
  225. #define DPF_MODNAME "CSPData::GetSocketData"
  226. CSocketData * GetSocketData( void )
  227. {
  228. return m_pSocketData;
  229. }
  230. #undef DPF_MODNAME
  231. #define DPF_MODNAME "CSPData::SetSocketData"
  232. void SetSocketData( CSocketData * const pSocketData )
  233. {
  234. AssertCriticalSectionIsTakenByThisThread( &m_Lock, TRUE );
  235. m_pSocketData = pSocketData;
  236. }
  237. CSocketData * GetSocketDataRef( void );
  238. private:
  239. BYTE m_Sig[4]; // debugging signature ('SPDT')
  240. #ifndef DPNBUILD_ONLYONETHREAD
  241. DNCRITICAL_SECTION m_Lock; // lock
  242. #endif // !DPNBUILD_ONLYONETHREAD
  243. volatile LONG m_lRefCount; // reference count
  244. volatile LONG m_lObjectRefCount; // reference count of outstanding objects (CEndpoint, CSocketPort, etc.)
  245. DNHANDLE m_hShutdownEvent; // handle for shutdown
  246. #if ((! defined(DPNBUILD_NOIPV6)) || (! defined(DPNBUILD_NOIPX)))
  247. short m_sSPType; // type of SP (AF_xxx)
  248. #endif // ! DPNBUILD_NOIPV6 or ! DPNBUILD_NOIPX
  249. SPSTATE m_SPState; // what state is the SP in?
  250. SPINITIALIZEDATA m_InitData; // initialization data
  251. //
  252. // job management
  253. //
  254. CThreadPool *m_pThreadPool;
  255. CSocketData *m_pSocketData; // pointer to socket port data
  256. struct
  257. {
  258. IDP8ServiceProviderVtbl *m_pCOMVtbl;
  259. } m_COMInterface;
  260. void DestroyThisObject( void );
  261. #ifdef DBG
  262. #ifndef DPNBUILD_ONLYONEADAPTER
  263. void DebugPrintOutstandingAdapterEntries( void );
  264. #endif // ! DPNBUILD_ONLYONEADAPTER
  265. #endif // DBG
  266. //
  267. // prevent unwarranted copies
  268. //
  269. CSPData( const CSPData & );
  270. CSPData& operator=( const CSPData & );
  271. };
  272. #undef DPF_MODNAME
  273. #endif // __SPDATA_H__