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.

4072 lines
88 KiB

  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: spwrapper.cpp
  6. *
  7. * Content: DP8SIM main SP interface wrapper object class.
  8. *
  9. * History:
  10. * Date By Reason
  11. * ======== ======== =========
  12. * 04/23/01 VanceO Created.
  13. *
  14. ***************************************************************************/
  15. #include "dp8simi.h"
  16. //=============================================================================
  17. // Dynamically loaded function prototypes
  18. //=============================================================================
  19. typedef HRESULT (WINAPI * PFN_DLLGETCLASSOBJECT)(REFCLSID, REFIID, LPVOID *);
  20. #undef DPF_MODNAME
  21. #define DPF_MODNAME "CDP8SimSP::CDP8SimSP"
  22. //=============================================================================
  23. // CDP8SimSP constructor
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Description: Initializes the new CDP8SimSP object.
  27. //
  28. // Arguments:
  29. // GUID * pguidFakeSP - Pointer to guid of fake SP.
  30. // GUID * pguidRealSP - Pointer to guid of real SP being wrapped.
  31. //
  32. // Returns: None (the object).
  33. //=============================================================================
  34. CDP8SimSP::CDP8SimSP(const GUID * const pguidFakeSP, const GUID * const pguidRealSP)
  35. {
  36. this->m_blList.Initialize();
  37. this->m_Sig[0] = 'S';
  38. this->m_Sig[1] = 'P';
  39. this->m_Sig[2] = 'W';
  40. this->m_Sig[3] = 'P';
  41. this->m_lRefCount = 1; // someone must have a pointer to this object
  42. this->m_dwFlags = 0;
  43. CopyMemory(&this->m_guidFakeSP, pguidFakeSP, sizeof(GUID));
  44. CopyMemory(&this->m_guidRealSP, pguidRealSP, sizeof(GUID));
  45. this->m_pDP8SimCB = NULL;
  46. this->m_pDP8SP = NULL;
  47. this->m_dwSendsPending = 0;
  48. this->m_hLastPendingSendEvent = NULL;
  49. this->m_dwReceivesPending = 0;
  50. //this->m_hLastPendingReceiveEvent = NULL;
  51. } // CDP8SimSP::CDP8SimSP
  52. #undef DPF_MODNAME
  53. #define DPF_MODNAME "CDP8SimSP::~CDP8SimSP"
  54. //=============================================================================
  55. // CDP8SimSP destructor
  56. //-----------------------------------------------------------------------------
  57. //
  58. // Description: Frees the CDP8SimSP object.
  59. //
  60. // Arguments: None.
  61. //
  62. // Returns: None.
  63. //=============================================================================
  64. CDP8SimSP::~CDP8SimSP(void)
  65. {
  66. DNASSERT(this->m_blList.IsEmpty());
  67. DNASSERT(this->m_lRefCount == 0);
  68. DNASSERT(this->m_dwFlags == 0);
  69. DNASSERT(this->m_pDP8SimCB == NULL);
  70. DNASSERT(this->m_pDP8SP == NULL);
  71. DNASSERT(this->m_dwSendsPending == 0);
  72. DNASSERT(this->m_hLastPendingSendEvent == NULL);
  73. DNASSERT(this->m_dwReceivesPending == 0);
  74. //DNASSERT(this->m_hLastPendingReceiveEvent == NULL);
  75. //
  76. // For grins, change the signature before deleting the object.
  77. //
  78. this->m_Sig[3] = 'p';
  79. } // CDP8SimSP::~CDP8SimSP
  80. #undef DPF_MODNAME
  81. #define DPF_MODNAME "CDP8SimSP::QueryInterface"
  82. //=============================================================================
  83. // CDP8SimSP::QueryInterface
  84. //-----------------------------------------------------------------------------
  85. //
  86. // Description: Retrieves a new reference for an interfaces supported by this
  87. // CDP8SimSP object.
  88. //
  89. // Arguments:
  90. // REFIID riid - Reference to interface ID GUID.
  91. // LPVOID * ppvObj - Place to store pointer to object.
  92. //
  93. // Returns: HRESULT
  94. // S_OK - Returning a valid interface pointer.
  95. // DPNHERR_INVALIDOBJECT - The interface object is invalid.
  96. // DPNHERR_INVALIDPOINTER - The destination pointer is invalid.
  97. // E_NOINTERFACE - Invalid interface was specified.
  98. //=============================================================================
  99. STDMETHODIMP CDP8SimSP::QueryInterface(REFIID riid, LPVOID * ppvObj)
  100. {
  101. HRESULT hr = DPN_OK;
  102. DPFX(DPFPREP, 3, "(0x%p) Parameters: (REFIID, 0x%p)", this, ppvObj);
  103. //
  104. // Validate the object.
  105. //
  106. if (! this->IsValidObject())
  107. {
  108. DPFX(DPFPREP, 0, "Invalid DP8Sim object!");
  109. hr = DPNERR_INVALIDOBJECT;
  110. goto Failure;
  111. }
  112. //
  113. // Validate the parameters.
  114. //
  115. if ((! IsEqualIID(riid, IID_IUnknown)) &&
  116. (! IsEqualIID(riid, IID_IDP8ServiceProvider)))
  117. {
  118. DPFX(DPFPREP, 0, "Unsupported interface!");
  119. hr = E_NOINTERFACE;
  120. goto Failure;
  121. }
  122. if ((ppvObj == NULL) ||
  123. (IsBadWritePtr(ppvObj, sizeof(void*))))
  124. {
  125. DPFX(DPFPREP, 0, "Invalid interface pointer specified!");
  126. hr = DPNERR_INVALIDPOINTER;
  127. goto Failure;
  128. }
  129. //
  130. // Add a reference, and return the interface pointer (which is actually
  131. // just the object pointer, they line up because CDP8SimSP inherits from
  132. // the interface declaration).
  133. //
  134. this->AddRef();
  135. (*ppvObj) = this;
  136. Exit:
  137. DPFX(DPFPREP, 3, "(0x%p) Returning: [0x%lx]", this, hr);
  138. return hr;
  139. Failure:
  140. goto Exit;
  141. } // CDP8SimSP::QueryInterface
  142. #undef DPF_MODNAME
  143. #define DPF_MODNAME "CDP8SimSP::AddRef"
  144. //=============================================================================
  145. // CDP8SimSP::AddRef
  146. //-----------------------------------------------------------------------------
  147. //
  148. // Description: Adds a reference to this CDP8SimSP object.
  149. //
  150. // Arguments: None.
  151. //
  152. // Returns: New refcount.
  153. //=============================================================================
  154. STDMETHODIMP_(ULONG) CDP8SimSP::AddRef(void)
  155. {
  156. LONG lRefCount;
  157. DNASSERT(this->IsValidObject());
  158. //
  159. // There must be at least 1 reference to this object, since someone is
  160. // calling AddRef.
  161. //
  162. DNASSERT(this->m_lRefCount > 0);
  163. lRefCount = InterlockedIncrement(&this->m_lRefCount);
  164. DPFX(DPFPREP, 3, "[0x%p] RefCount [0x%lx]", this, lRefCount);
  165. return lRefCount;
  166. } // CDP8SimSP::AddRef
  167. #undef DPF_MODNAME
  168. #define DPF_MODNAME "CDP8SimSP::Release"
  169. //=============================================================================
  170. // CDP8SimSP::Release
  171. //-----------------------------------------------------------------------------
  172. //
  173. // Description: Removes a reference to this CDP8SimSP object. When the
  174. // refcount reaches 0, this object is destroyed.
  175. // You must NULL out your pointer to this object after calling
  176. // this function.
  177. //
  178. // Arguments: None.
  179. //
  180. // Returns: New refcount.
  181. //=============================================================================
  182. STDMETHODIMP_(ULONG) CDP8SimSP::Release(void)
  183. {
  184. LONG lRefCount;
  185. DNASSERT(this->IsValidObject());
  186. //
  187. // There must be at least 1 reference to this object, since someone is
  188. // calling Release.
  189. //
  190. DNASSERT(this->m_lRefCount > 0);
  191. lRefCount = InterlockedDecrement(&this->m_lRefCount);
  192. //
  193. // Was that the last reference? If so, we're going to destroy this object.
  194. //
  195. if (lRefCount == 0)
  196. {
  197. DPFX(DPFPREP, 3, "[0x%p] RefCount hit 0, destroying object.", this);
  198. //
  199. // First pull it off the global list.
  200. //
  201. DNEnterCriticalSection(&g_csGlobalsLock);
  202. this->m_blList.RemoveFromList();
  203. DNASSERT(g_lOutstandingInterfaceCount > 0);
  204. g_lOutstandingInterfaceCount--; // update count so DLL can unload now works correctly
  205. DNLeaveCriticalSection(&g_csGlobalsLock);
  206. //
  207. // Make sure it's closed.
  208. //
  209. if (this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED)
  210. {
  211. //
  212. // Assert so that the user can fix his/her broken code!
  213. //
  214. DNASSERT(! "DP8SimSP object being released without calling Close first!");
  215. //
  216. // Then go ahead and do the right thing. Ignore error, we can't do
  217. // much about it.
  218. //
  219. this->Close();
  220. }
  221. //
  222. // Then uninitialize the object.
  223. //
  224. this->UninitializeObject();
  225. //
  226. // Finally delete this (!) object.
  227. //
  228. delete this;
  229. }
  230. else
  231. {
  232. DPFX(DPFPREP, 3, "[0x%p] RefCount [0x%lx]", this, lRefCount);
  233. }
  234. return lRefCount;
  235. } // CDP8SimSP::Release
  236. #undef DPF_MODNAME
  237. #define DPF_MODNAME "CDP8SimSP::Initialize"
  238. //=============================================================================
  239. // CDP8SimSP::Initialize
  240. //-----------------------------------------------------------------------------
  241. //
  242. // Description: ?
  243. //
  244. // Arguments:
  245. // PSPINITIALIZEDATA pspid - Pointer to parameter block to use when
  246. // initializing.
  247. //
  248. // Returns: HRESULT
  249. //=============================================================================
  250. STDMETHODIMP CDP8SimSP::Initialize(PSPINITIALIZEDATA pspid)
  251. {
  252. HRESULT hr;
  253. BOOL fHaveLock = FALSE;
  254. BOOL fInitializedIPCObject = FALSE;
  255. SPINITIALIZEDATA spidModified;
  256. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspid);
  257. //
  258. // Validate (actually assert) the object.
  259. //
  260. DNASSERT(this->IsValidObject());
  261. //
  262. // Assert the parameters.
  263. //
  264. DNASSERT(pspid != NULL);
  265. DNEnterCriticalSection(&this->m_csLock);
  266. fHaveLock = TRUE;
  267. //
  268. // Assert the object state.
  269. //
  270. DNASSERT(this->m_dwFlags == 0);
  271. //
  272. // Connect the shared memory.
  273. //
  274. hr = this->m_DP8SimIPC.Initialize();
  275. if (hr != DPN_OK)
  276. {
  277. DPFX(DPFPREP, 0, "Couldn't initialize IPC object!");
  278. goto Failure;
  279. }
  280. fInitializedIPCObject = TRUE;
  281. //
  282. // Create a wrapper for the callback interface.
  283. //
  284. this->m_pDP8SimCB = new CDP8SimCB(this, pspid->pIDP);
  285. if (this->m_pDP8SimCB == NULL)
  286. {
  287. hr = DPNERR_OUTOFMEMORY;
  288. goto Failure;
  289. }
  290. //
  291. // Initialize the callback interface wrapper object.
  292. //
  293. hr = this->m_pDP8SimCB->InitializeObject();
  294. if (hr != DPN_OK)
  295. {
  296. DPFX(DPFPREP, 0, "Couldn't initialize callback interface wrapper object!");
  297. delete this->m_pDP8SimCB;
  298. this->m_pDP8SimCB = NULL;
  299. goto Failure;
  300. }
  301. //
  302. // Instantiate the real SP.
  303. //
  304. hr = CoCreateInstance(this->m_guidRealSP,
  305. NULL,
  306. CLSCTX_INPROC_SERVER,
  307. IID_IDP8ServiceProvider,
  308. (PVOID*) (&this->m_pDP8SP));
  309. if (hr != S_OK)
  310. {
  311. DPFX(DPFPREP, 0, "Couldn't instantiate real SP object (pointer = 0x%p)!",
  312. this->m_pDP8SP);
  313. goto Failure;
  314. }
  315. DPFX(DPFPREP, 1, "Object 0x%p wrapping real SP 0x%p, inserting callback interface 0x%p before 0x%p.",
  316. this, this->m_pDP8SP, this->m_pDP8SimCB, pspid->pIDP);
  317. //
  318. // Initialize the real SP.
  319. //
  320. ZeroMemory(&spidModified, sizeof(spidModified));
  321. spidModified.pIDP = this->m_pDP8SimCB;
  322. spidModified.dwFlags = pspid->dwFlags;
  323. hr = this->m_pDP8SP->Initialize(&spidModified);
  324. if (hr != DPN_OK)
  325. {
  326. DPFX(DPFPREP, 0, "Failed initializing real SP object (0x%p)!",
  327. this->m_pDP8SP);
  328. goto Failure;
  329. }
  330. //
  331. // We're now initialized.
  332. //
  333. this->m_dwFlags |= DP8SIMSPOBJ_INITIALIZED;
  334. Exit:
  335. if (fHaveLock)
  336. {
  337. DNLeaveCriticalSection(&this->m_csLock);
  338. }
  339. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  340. return hr;
  341. Failure:
  342. if (this->m_pDP8SP != NULL)
  343. {
  344. this->m_pDP8SP->Release();
  345. this->m_pDP8SP = NULL;
  346. }
  347. if (this->m_pDP8SimCB != NULL)
  348. {
  349. this->m_pDP8SimCB->Release();
  350. this->m_pDP8SimCB = NULL;
  351. }
  352. if (fInitializedIPCObject)
  353. {
  354. this->m_DP8SimIPC.Close();
  355. fInitializedIPCObject = FALSE;
  356. }
  357. goto Exit;
  358. } // CDP8SimSP::Initialize
  359. #undef DPF_MODNAME
  360. #define DPF_MODNAME "CDP8SimSP::Close"
  361. //=============================================================================
  362. // CDP8SimSP::Close
  363. //-----------------------------------------------------------------------------
  364. //
  365. // Description: ?
  366. //
  367. // Arguments: None.
  368. //
  369. // Returns: HRESULT
  370. //=============================================================================
  371. STDMETHODIMP CDP8SimSP::Close(void)
  372. {
  373. HRESULT hr;
  374. //BOOL fHaveLock = FALSE;
  375. BOOL fWait = FALSE;
  376. DPFX(DPFPREP, 2, "(0x%p) Enter", this);
  377. //
  378. // Validate (actually assert) the object.
  379. //
  380. DNASSERT(this->IsValidObject());
  381. DNEnterCriticalSection(&this->m_csLock);
  382. //fHaveLock = TRUE;
  383. //
  384. // Assert the object state.
  385. //
  386. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  387. //
  388. // Figure out if we need to wait for all sends to complete.
  389. //
  390. if (this->m_dwSendsPending > 0)
  391. {
  392. DNASSERT(this->m_hLastPendingSendEvent == NULL);
  393. this->m_hLastPendingSendEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  394. if (this->m_hLastPendingSendEvent == NULL)
  395. {
  396. hr = GetLastError();
  397. DPFX(DPFPREP, 0, "Couldn't create last send pending event (err = %u)!", hr);
  398. }
  399. else
  400. {
  401. fWait = TRUE;
  402. }
  403. }
  404. this->m_dwFlags |= DP8SIMSPOBJ_CLOSING;
  405. //
  406. // Drop the lock, nobody should be touching this object now.
  407. //
  408. DNLeaveCriticalSection(&this->m_csLock);
  409. //fHaveLock = FALSE;
  410. if (fWait)
  411. {
  412. DPFX(DPFPREP, 1, "Waiting for ~%u pending sends to complete.",
  413. this->m_dwSendsPending);
  414. //
  415. // Wait for all the sends to complete. Nobody should touch
  416. // m_hLastPendingSendEvent except the thread triggering it, so
  417. // referring to it without the lock should be safe.
  418. // Ignore any errors.
  419. //
  420. WaitForSingleObject(this->m_hLastPendingSendEvent, INFINITE);
  421. //
  422. // Take the lock while removing the handle, to be paranoid.
  423. //
  424. DNEnterCriticalSection(&this->m_csLock);
  425. //
  426. // Remove the handle.
  427. //
  428. CloseHandle(this->m_hLastPendingSendEvent);
  429. this->m_hLastPendingSendEvent = NULL;
  430. //
  431. // Drop the lock again.
  432. //
  433. DNLeaveCriticalSection(&this->m_csLock);
  434. }
  435. //
  436. // Shutdown the global worker thread if we launched it.
  437. //
  438. if (this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD)
  439. {
  440. StopGlobalWorkerThread();
  441. this->m_dwFlags &= ~DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD;
  442. }
  443. //
  444. // Close the real SP.
  445. //
  446. hr = this->m_pDP8SP->Close();
  447. if (hr != DPN_OK)
  448. {
  449. DPFX(DPFPREP, 0, "Failed closing real SP object (0x%p)!",
  450. this->m_pDP8SP);
  451. //
  452. // Continue...
  453. //
  454. }
  455. //
  456. // Release the real SP object.
  457. //
  458. this->m_pDP8SP->Release();
  459. this->m_pDP8SP = NULL;
  460. //
  461. // Release the callback interceptor object.
  462. //
  463. this->m_pDP8SimCB->Release();
  464. this->m_pDP8SimCB = NULL;
  465. //
  466. // Disconnect the shared memory.
  467. //
  468. this->m_DP8SimIPC.Close();
  469. //
  470. // Turn off the initialized and closing flags.
  471. //
  472. this->m_dwFlags &= ~(DP8SIMSPOBJ_INITIALIZED | DP8SIMSPOBJ_CLOSING);
  473. DNASSERT(this->m_dwFlags == 0);
  474. //Exit:
  475. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  476. return hr;
  477. /*
  478. Failure:
  479. if (fHaveLock)
  480. {
  481. DNLeaveCriticalSection(&this->m_csLock);
  482. }
  483. goto Exit;
  484. */
  485. } // CDP8SimSP::Close
  486. #undef DPF_MODNAME
  487. #define DPF_MODNAME "CDP8SimSP::Connect"
  488. //=============================================================================
  489. // CDP8SimSP::Connect
  490. //-----------------------------------------------------------------------------
  491. //
  492. // Description: ?
  493. //
  494. // Arguments:
  495. // PSPCONNECTDATA pspcd - Pointer to parameter block to use when
  496. // connecting.
  497. //
  498. // Returns: HRESULT
  499. //=============================================================================
  500. STDMETHODIMP CDP8SimSP::Connect(PSPCONNECTDATA pspcd)
  501. {
  502. HRESULT hr;
  503. BOOL fHaveLock = FALSE;
  504. SPCONNECTDATA spcdModified;
  505. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  506. CDP8SimCommand * pDP8SimCommand = NULL;
  507. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspcd);
  508. ZeroMemory(&spcdModified, sizeof(spcdModified));
  509. //
  510. // Validate (actually assert) the object.
  511. //
  512. DNASSERT(this->IsValidObject());
  513. //
  514. // Assert the parameters.
  515. //
  516. DNASSERT(pspcd != NULL);
  517. DNASSERT(pspcd->pAddressHost != NULL);
  518. DNASSERT(pspcd->pAddressDeviceInfo != NULL);
  519. DNEnterCriticalSection(&this->m_csLock);
  520. fHaveLock = TRUE;
  521. //
  522. // Assert the object state.
  523. //
  524. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  525. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  526. //
  527. // Fire up the global worker thread, if it hasn't been already.
  528. //
  529. if (! (this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD))
  530. {
  531. hr = StartGlobalWorkerThread();
  532. if (hr != DPN_OK)
  533. {
  534. DPFX(DPFPREP, 0, "Failed starting global worker thread!");
  535. goto Failure;
  536. }
  537. this->m_dwFlags |= DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD;
  538. }
  539. DNLeaveCriticalSection(&this->m_csLock);
  540. fHaveLock = FALSE;
  541. //
  542. // Prepare a command object.
  543. //
  544. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  545. CommandFPMContext.dwType = CMDTYPE_CONNECT;
  546. CommandFPMContext.pvUserContext = pspcd->pvContext;
  547. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  548. if (pDP8SimCommand == NULL)
  549. {
  550. hr = DPNERR_OUTOFMEMORY;
  551. goto Failure;
  552. }
  553. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  554. //
  555. // Copy the parameter block, modifying as necessary.
  556. //
  557. /*
  558. //
  559. // Duplicate the host address.
  560. //
  561. hr = pspcd->pAddressHost->Duplicate(&spcdModified.pAddressHost);
  562. if (hr != DPN_OK)
  563. {
  564. DPFX(DPFPREP, 0, "Couldn't duplicate host address!");
  565. goto Failure;
  566. }
  567. */
  568. spcdModified.pAddressHost = pspcd->pAddressHost;
  569. //
  570. // Change the service provider GUID so it matches the one we're
  571. // calling.
  572. //
  573. hr = spcdModified.pAddressHost->SetSP(&this->m_guidRealSP);
  574. if (hr != DPN_OK)
  575. {
  576. DPFX(DPFPREP, 0, "Couldn't change host address' SP!");
  577. goto Failure;
  578. }
  579. /*
  580. //
  581. // Duplicate the host address.
  582. //
  583. hr = pspcd->pAddressDeviceInfo->Duplicate(&spcdModified.pAddressDeviceInfo);
  584. if (hr != DPN_OK)
  585. {
  586. DPFX(DPFPREP, 0, "Couldn't duplicate device info address!");
  587. goto Failure;
  588. }
  589. */
  590. spcdModified.pAddressDeviceInfo = pspcd->pAddressDeviceInfo;
  591. //
  592. // Change the service provider GUID so it matches the one we're
  593. // calling.
  594. //
  595. hr = spcdModified.pAddressDeviceInfo->SetSP(&this->m_guidRealSP);
  596. if (hr != DPN_OK)
  597. {
  598. DPFX(DPFPREP, 0, "Couldn't change device info address' SP!");
  599. goto Failure;
  600. }
  601. //
  602. // Add a reference for the connect command.
  603. //
  604. pDP8SimCommand->AddRef();
  605. DNASSERT(pspcd->dwReserved == 0);
  606. //spcdModified.dwReserved = pspcd->dwReserved;
  607. spcdModified.dwFlags = pspcd->dwFlags;
  608. spcdModified.pvContext = pDP8SimCommand;
  609. //spcdModified.hCommand = pspcd->hCommand; // filled in by real SP
  610. //spcdModified.dwCommandDescriptor = pspcd->dwCommandDescriptor; // filled in by real SP
  611. //
  612. // Start connecting with the real service provider.
  613. //
  614. hr = this->m_pDP8SP->Connect(&spcdModified);
  615. if (FAILED(hr))
  616. {
  617. DPFX(DPFPREP, 0, "Failed starting to connect with real SP object (0x%p)!",
  618. this->m_pDP8SP);
  619. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  620. pDP8SimCommand->Release();
  621. goto Failure;
  622. }
  623. #pragma BUGBUG(vanceo, "Handle DPN_OK and investigate command completing before this function returns")
  624. DNASSERT(spcdModified.hCommand != NULL);
  625. //
  626. // Save the output parameters.
  627. //
  628. pDP8SimCommand->SetRealSPCommand(spcdModified.hCommand,
  629. spcdModified.dwCommandDescriptor);
  630. //
  631. // Generate the output parameters for the caller.
  632. //
  633. pspcd->hCommand = (HANDLE) pDP8SimCommand;
  634. pspcd->dwCommandDescriptor = 0;
  635. Exit:
  636. //
  637. // Give up local reference.
  638. //
  639. if (pDP8SimCommand != NULL)
  640. {
  641. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  642. pDP8SimCommand->Release();
  643. pDP8SimCommand = NULL;
  644. }
  645. /*
  646. if (spcdModified.pAddressDeviceInfo != NULL)
  647. {
  648. spcdModified.pAddressDeviceInfo->Release();
  649. spcdModified.pAddressDeviceInfo = NULL;
  650. }
  651. if (spcdModified.pAddressHost != NULL)
  652. {
  653. spcdModified.pAddressHost->Release();
  654. spcdModified.pAddressHost = NULL;
  655. }
  656. */
  657. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  658. return hr;
  659. Failure:
  660. if (fHaveLock)
  661. {
  662. DNLeaveCriticalSection(&this->m_csLock);
  663. }
  664. goto Exit;
  665. } // CDP8SimSP::Connect
  666. #undef DPF_MODNAME
  667. #define DPF_MODNAME "CDP8SimSP::Disconnect"
  668. //=============================================================================
  669. // CDP8SimSP::Disconnect
  670. //-----------------------------------------------------------------------------
  671. //
  672. // Description: ?
  673. //
  674. // Arguments:
  675. // PSPDISCONNECTDATA pspdd - Pointer to parameter block to use when
  676. // disconnecting.
  677. //
  678. // Returns: HRESULT
  679. //=============================================================================
  680. STDMETHODIMP CDP8SimSP::Disconnect(PSPDISCONNECTDATA pspdd)
  681. {
  682. HRESULT hr;
  683. BOOL fHaveLock = FALSE;
  684. CDP8SimEndpoint * pDP8SimEndpoint;
  685. SPDISCONNECTDATA spddModified;
  686. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  687. CDP8SimCommand * pDP8SimCommand = NULL;
  688. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspdd);
  689. ZeroMemory(&spddModified, sizeof(spddModified));
  690. //
  691. // Validate (actually assert) the object.
  692. //
  693. DNASSERT(this->IsValidObject());
  694. //
  695. // Assert the parameters.
  696. //
  697. DNASSERT(pspdd != NULL);
  698. DNEnterCriticalSection(&this->m_csLock);
  699. fHaveLock = TRUE;
  700. //
  701. // Assert the object state.
  702. //
  703. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  704. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  705. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  706. DNLeaveCriticalSection(&this->m_csLock);
  707. fHaveLock = FALSE;
  708. pDP8SimEndpoint = (CDP8SimEndpoint*) pspdd->hEndpoint;
  709. DNASSERT(pDP8SimEndpoint->IsValidObject());
  710. //
  711. // Mark the endpoint as disconnecting to prevent additional sends/receives.
  712. //
  713. pDP8SimEndpoint->Lock();
  714. pDP8SimEndpoint->NoteDisconnecting();
  715. pDP8SimEndpoint->Unlock();
  716. //
  717. // Flush any delayed sends that were already going to this endpoint to make
  718. // sure they hit the wire.
  719. //
  720. FlushAllDelayedSendsToEndpoint(pDP8SimEndpoint, FALSE);
  721. //
  722. // Drop any delayed receives from this endpoint, the upper layer doesn't
  723. // want to receive anything else after disconnecting.
  724. //
  725. FlushAllDelayedReceivesFromEndpoint(pDP8SimEndpoint, TRUE);
  726. //
  727. // Prepare a command object.
  728. //
  729. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  730. CommandFPMContext.dwType = CMDTYPE_DISCONNECT;
  731. CommandFPMContext.pvUserContext = pspdd->pvContext;
  732. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  733. if (pDP8SimCommand == NULL)
  734. {
  735. hr = DPNERR_OUTOFMEMORY;
  736. goto Failure;
  737. }
  738. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  739. //
  740. // Add a reference for the disconnect command.
  741. //
  742. pDP8SimCommand->AddRef();
  743. //
  744. // Copy the parameter block, modifying as necessary.
  745. //
  746. spddModified.hEndpoint = pDP8SimEndpoint->GetRealSPEndpoint();
  747. spddModified.dwFlags = pspdd->dwFlags;
  748. spddModified.pvContext = pDP8SimCommand;
  749. //spddModified.hCommand = pspdd->hCommand; // filled in by real SP
  750. //spddModified.dwCommandDescriptor = pspdd->dwCommandDescriptor; // filled in by real SP
  751. //
  752. // Tell the real service provider to disconnect.
  753. //
  754. hr = this->m_pDP8SP->Disconnect(&spddModified);
  755. if (FAILED(hr))
  756. {
  757. DPFX(DPFPREP, 0, "Failed having real SP object (0x%p) disconnect!",
  758. this->m_pDP8SP);
  759. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  760. pDP8SimCommand->Release();
  761. goto Failure;
  762. }
  763. if (hr == DPNSUCCESS_PENDING)
  764. {
  765. DNASSERT(spddModified.hCommand != NULL);
  766. //
  767. // Save the output parameters.
  768. //
  769. pDP8SimCommand->SetRealSPCommand(spddModified.hCommand,
  770. spddModified.dwCommandDescriptor);
  771. //
  772. // Generate the output parameters for the caller.
  773. //
  774. pspdd->hCommand = (HANDLE) pDP8SimCommand;
  775. pspdd->dwCommandDescriptor = 0;
  776. }
  777. else
  778. {
  779. DNASSERT(spddModified.hCommand == NULL);
  780. DPFX(DPFPREP, 7, "Releasing completed command 0x%p.", pDP8SimCommand);
  781. pDP8SimCommand->Release();
  782. }
  783. Exit:
  784. //
  785. // Give up local reference.
  786. //
  787. if (pDP8SimCommand != NULL)
  788. {
  789. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  790. pDP8SimCommand->Release();
  791. pDP8SimCommand = NULL;
  792. }
  793. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  794. return hr;
  795. Failure:
  796. if (fHaveLock)
  797. {
  798. DNLeaveCriticalSection(&this->m_csLock);
  799. }
  800. goto Exit;
  801. } // CDP8SimSP::Disconnect
  802. #undef DPF_MODNAME
  803. #define DPF_MODNAME "CDP8SimSP::Listen"
  804. //=============================================================================
  805. // CDP8SimSP::Listen
  806. //-----------------------------------------------------------------------------
  807. //
  808. // Description: ?
  809. //
  810. // Arguments:
  811. // PSPLISTENDATA pspld - Pointer to parameter block to use when listening.
  812. //
  813. // Returns: HRESULT
  814. //=============================================================================
  815. STDMETHODIMP CDP8SimSP::Listen(PSPLISTENDATA pspld)
  816. {
  817. HRESULT hr;
  818. BOOL fHaveLock = FALSE;
  819. SPLISTENDATA spldModified;
  820. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  821. CDP8SimCommand * pDP8SimCommand = NULL;
  822. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspld);
  823. ZeroMemory(&spldModified, sizeof(spldModified));
  824. //
  825. // Validate (actually assert) the object.
  826. //
  827. DNASSERT(this->IsValidObject());
  828. //
  829. // Assert the parameters.
  830. //
  831. DNASSERT(pspld != NULL);
  832. DNASSERT(pspld->pAddressDeviceInfo != NULL);
  833. DNEnterCriticalSection(&this->m_csLock);
  834. fHaveLock = TRUE;
  835. //
  836. // Assert the object state.
  837. //
  838. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  839. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  840. //
  841. // Fire up the global worker thread, if it hasn't been already.
  842. //
  843. if (! (this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD))
  844. {
  845. hr = StartGlobalWorkerThread();
  846. if (hr != DPN_OK)
  847. {
  848. DPFX(DPFPREP, 0, "Failed starting global worker thread!");
  849. goto Failure;
  850. }
  851. this->m_dwFlags |= DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD;
  852. }
  853. DNLeaveCriticalSection(&this->m_csLock);
  854. fHaveLock = FALSE;
  855. //
  856. // Prepare a command object.
  857. //
  858. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  859. CommandFPMContext.dwType = CMDTYPE_LISTEN;
  860. CommandFPMContext.pvUserContext = pspld->pvContext;
  861. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  862. if (pDP8SimCommand == NULL)
  863. {
  864. hr = DPNERR_OUTOFMEMORY;
  865. goto Failure;
  866. }
  867. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  868. //
  869. // Copy the parameter block, modifying as necessary.
  870. //
  871. /*
  872. //
  873. // Duplicate the host address.
  874. //
  875. hr = pspld->pAddressDeviceInfo->Duplicate(&spldModified.pAddressDeviceInfo);
  876. if (hr != DPN_OK)
  877. {
  878. DPFX(DPFPREP, 0, "Couldn't duplicate device info address!");
  879. goto Failure;
  880. }
  881. */
  882. spldModified.pAddressDeviceInfo = pspld->pAddressDeviceInfo;
  883. //
  884. // Change the service provider GUID so it matches the one we're
  885. // calling.
  886. //
  887. hr = spldModified.pAddressDeviceInfo->SetSP(&this->m_guidRealSP);
  888. if (hr != DPN_OK)
  889. {
  890. DPFX(DPFPREP, 0, "Couldn't change device info address' SP!");
  891. goto Failure;
  892. }
  893. //
  894. // Add a reference for the listen command.
  895. //
  896. pDP8SimCommand->AddRef();
  897. spldModified.dwFlags = pspld->dwFlags;
  898. spldModified.pvContext = pDP8SimCommand;
  899. //spldModified.hCommand = pspld->hCommand; // filled in by real SP
  900. //spldModified.dwCommandDescriptor = pspld->dwCommandDescriptor; // filled in by real SP
  901. //
  902. // Start listening with the real service provider.
  903. //
  904. hr = this->m_pDP8SP->Listen(&spldModified);
  905. if (FAILED(hr))
  906. {
  907. DPFX(DPFPREP, 0, "Failed to start listening with the real SP object (0x%p)!",
  908. this->m_pDP8SP);
  909. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  910. pDP8SimCommand->Release();
  911. goto Failure;
  912. }
  913. DNASSERT(spldModified.hCommand != NULL);
  914. //
  915. // Save the output parameters.
  916. //
  917. pDP8SimCommand->SetRealSPCommand(spldModified.hCommand,
  918. spldModified.dwCommandDescriptor);
  919. //
  920. // Generate the output parameters for the caller.
  921. //
  922. pspld->hCommand = (HANDLE) pDP8SimCommand;
  923. pspld->dwCommandDescriptor = 0;
  924. Exit:
  925. //
  926. // Give up local reference.
  927. //
  928. if (pDP8SimCommand != NULL)
  929. {
  930. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  931. pDP8SimCommand->Release();
  932. pDP8SimCommand = NULL;
  933. }
  934. /*
  935. if (spldModified.pAddressDeviceInfo != NULL)
  936. {
  937. spldModified.pAddressDeviceInfo->Release();
  938. spldModified.pAddressDeviceInfo = NULL;
  939. }
  940. */
  941. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  942. return hr;
  943. Failure:
  944. if (fHaveLock)
  945. {
  946. DNLeaveCriticalSection(&this->m_csLock);
  947. }
  948. goto Exit;
  949. } // CDP8SimSP::Listen
  950. #undef DPF_MODNAME
  951. #define DPF_MODNAME "CDP8SimSP::SendData"
  952. //=============================================================================
  953. // CDP8SimSP::SendData
  954. //-----------------------------------------------------------------------------
  955. //
  956. // Description: ?
  957. //
  958. // Arguments:
  959. // PSPSENDDATA pspsd - Pointer to parameter block to use when sending.
  960. //
  961. // Returns: HRESULT
  962. //=============================================================================
  963. STDMETHODIMP CDP8SimSP::SendData(PSPSENDDATA pspsd)
  964. {
  965. HRESULT hr;
  966. DP8SIM_PARAMETERS dp8sp;
  967. CDP8SimEndpoint * pDP8SimEndpoint;
  968. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  969. CDP8SimCommand * pDP8SimCommand = NULL;
  970. SPSENDDATA spsdModified;
  971. CDP8SimSend * pDP8SimSend = NULL;
  972. IDP8SPCallback * pDP8SPCB;
  973. DWORD dwMsgSize;
  974. DWORD dwTemp;
  975. DWORD dwBandwidthDelay;
  976. DWORD dwLatencyDelay;
  977. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspsd);
  978. //
  979. // Validate (actually assert) the object.
  980. //
  981. DNASSERT(this->IsValidObject());
  982. //
  983. // Assert the parameters.
  984. //
  985. DNASSERT(pspsd != NULL);
  986. #ifdef DEBUG
  987. DNEnterCriticalSection(&this->m_csLock);
  988. //
  989. // Assert the object state.
  990. //
  991. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  992. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  993. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  994. DNLeaveCriticalSection(&this->m_csLock);
  995. #endif // DEBUG
  996. //
  997. // Determine the total size of the message.
  998. //
  999. dwMsgSize = 0;
  1000. for(dwTemp = 0; dwTemp < pspsd->dwBufferCount; dwTemp++)
  1001. {
  1002. DNASSERT((pspsd->pBuffers[dwTemp].pBufferData != NULL) && (pspsd->pBuffers[dwTemp].dwBufferSize > 0));
  1003. dwMsgSize += pspsd->pBuffers[dwTemp].dwBufferSize;
  1004. }
  1005. //
  1006. // Get the current send settings.
  1007. //
  1008. ZeroMemory(&dp8sp, sizeof(dp8sp));
  1009. dp8sp.dwSize = sizeof(dp8sp);
  1010. this->m_DP8SimIPC.GetAllSendParameters(&dp8sp);
  1011. //
  1012. // Determine if we need to drop this send.
  1013. //
  1014. if (this->ShouldDrop(dp8sp.fPacketLossPercent))
  1015. {
  1016. //
  1017. // Update the statistics.
  1018. //
  1019. this->IncrementStatsSendDropped(dwMsgSize);
  1020. //
  1021. // Indicate send completion (with a bogus handle) immediately.
  1022. //
  1023. pDP8SPCB = this->m_pDP8SimCB->GetRealCallbackInterface();
  1024. DPFX(DPFPREP, 2, "Indicating successful send completion (dropped, context = 0x%p) to interface 0x%p.",
  1025. pspsd->pvContext, pDP8SPCB);
  1026. hr = pDP8SPCB->CommandComplete(NULL, DPN_OK, pspsd->pvContext);
  1027. DPFX(DPFPREP, 2, "Returning from command complete [0x%lx].", hr);
  1028. //
  1029. // Ignore any error and return DPNSUCCESS_PENDING, even though we've
  1030. // completed the send already.
  1031. //
  1032. hr = DPNSUCCESS_PENDING;
  1033. //
  1034. // Return bogus output parameters for the caller, it's already complete
  1035. // from their perspective.
  1036. //
  1037. pspsd->hCommand = NULL;
  1038. pspsd->dwCommandDescriptor = 0;
  1039. //
  1040. // We're done here.
  1041. //
  1042. goto Exit;
  1043. }
  1044. //
  1045. // Figure out how much latency needs to be added based on the bandwidth
  1046. // and random latency settings.
  1047. //
  1048. // If we're not supposed to delay the sends, fire it off right away.
  1049. // Otherwise submit a timed job to be performed later.
  1050. //
  1051. if (! this->GetDelay(dp8sp.dwBandwidthBPS,
  1052. dp8sp.dwPacketHeaderSize,
  1053. dwMsgSize,
  1054. dp8sp.dwMinLatencyMS,
  1055. dp8sp.dwMaxLatencyMS,
  1056. &dwBandwidthDelay,
  1057. &dwLatencyDelay))
  1058. {
  1059. pDP8SimEndpoint = (CDP8SimEndpoint*) pspsd->hEndpoint;
  1060. DNASSERT(pDP8SimEndpoint->IsValidObject());
  1061. //
  1062. // If the endpoint is disconnecting, don't try to send.
  1063. //
  1064. pDP8SimEndpoint->Lock();
  1065. if (pDP8SimEndpoint->IsDisconnecting())
  1066. {
  1067. pDP8SimEndpoint->Unlock();
  1068. DPFX(DPFPREP, 0, "Endpoint 0x%p is disconnecting, can't send!",
  1069. pDP8SimEndpoint);
  1070. hr = DPNERR_NOCONNECTION;
  1071. goto Failure;
  1072. }
  1073. pDP8SimEndpoint->Unlock();
  1074. DPFX(DPFPREP, 6, "Sending %u bytes of data immmediately.", dwMsgSize);
  1075. //
  1076. // Prepare a command object.
  1077. //
  1078. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  1079. CommandFPMContext.dwType = CMDTYPE_SENDDATA_IMMEDIATE;
  1080. CommandFPMContext.pvUserContext = pspsd->pvContext;
  1081. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  1082. if (pDP8SimCommand == NULL)
  1083. {
  1084. hr = DPNERR_OUTOFMEMORY;
  1085. goto Failure;
  1086. }
  1087. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  1088. //
  1089. // Save the message's size for incrementing stats at send completion.
  1090. //
  1091. pDP8SimCommand->SetMessageSize(dwMsgSize);
  1092. //
  1093. // Copy the parameter block, modifying as necessary.
  1094. //
  1095. ZeroMemory(&spsdModified, sizeof(spsdModified));
  1096. spsdModified.hEndpoint = pDP8SimEndpoint->GetRealSPEndpoint();
  1097. spsdModified.pBuffers = pspsd->pBuffers;
  1098. spsdModified.dwBufferCount = pspsd->dwBufferCount;
  1099. spsdModified.dwFlags = pspsd->dwFlags;
  1100. spsdModified.pvContext = pDP8SimCommand;
  1101. //spsdModified.hCommand = NULL; // filled in by real SP
  1102. //spsdModified.dwCommandDescriptor = 0; // filled in by real SP
  1103. //
  1104. // Add a reference for the send command.
  1105. //
  1106. pDP8SimCommand->AddRef();
  1107. //
  1108. // Increase the pending sends counter.
  1109. //
  1110. this->IncSendsPending();
  1111. //
  1112. // Issue the send to the real SP.
  1113. //
  1114. hr = this->m_pDP8SP->SendData(&spsdModified);
  1115. if (FAILED(hr))
  1116. {
  1117. DPFX(DPFPREP, 0, "Failed sending immediate data (err = 0x%lx)!", hr);
  1118. //
  1119. // Remove the send counter.
  1120. //
  1121. this->DecSendsPending();
  1122. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  1123. pDP8SimCommand->Release();
  1124. //
  1125. // Continue.
  1126. //
  1127. }
  1128. else
  1129. {
  1130. if (hr != DPNSUCCESS_PENDING)
  1131. {
  1132. //
  1133. // The command completed right away.
  1134. //
  1135. DNASSERT(hr == DPN_OK);
  1136. hr = this->m_pDP8SimCB->CommandComplete(spsdModified.hCommand,
  1137. hr,
  1138. pDP8SimCommand);
  1139. DNASSERT(hr == DPN_OK);
  1140. //
  1141. // Be sure to still return pending for the caller even though
  1142. // we just completed it to him.
  1143. //
  1144. hr = DPNSUCCESS_PENDING;
  1145. }
  1146. else
  1147. {
  1148. //
  1149. // Save the output parameters returned by the SP.
  1150. //
  1151. pDP8SimCommand->SetRealSPCommand(spsdModified.hCommand,
  1152. spsdModified.dwCommandDescriptor);
  1153. }
  1154. }
  1155. //
  1156. // Give up local reference.
  1157. //
  1158. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  1159. pDP8SimCommand->Release();
  1160. pDP8SimCommand = NULL;
  1161. //
  1162. // We're done here.
  1163. //
  1164. goto Exit;
  1165. }
  1166. //
  1167. // If we're here, we must be delaying the send.
  1168. //
  1169. DPFX(DPFPREP, 6, "Delaying %u byte send for %u + %u ms.",
  1170. dwMsgSize, dwBandwidthDelay, dwLatencyDelay);
  1171. //
  1172. // Get a send object, duplicating the send data given to us by our caller
  1173. // for submission some time in the future.
  1174. //
  1175. pDP8SimSend = (CDP8SimSend*)g_FPOOLSend.Get(pspsd);
  1176. if (pDP8SimSend == NULL)
  1177. {
  1178. hr = DPNERR_OUTOFMEMORY;
  1179. goto Failure;
  1180. }
  1181. DPFX(DPFPREP, 7, "New send 0x%p.", pDP8SimSend);
  1182. //
  1183. // Store the latency that is about be added to this send.
  1184. //
  1185. pDP8SimSend->SetLatencyAdded(dwBandwidthDelay + dwLatencyDelay);
  1186. //
  1187. // Transfer local pDP8SimSend reference to the job queue.
  1188. //
  1189. //
  1190. // Increment the send counter.
  1191. //
  1192. this->IncSendsPending();
  1193. //
  1194. // Queue it to be sent at a later time, depending on the latency value
  1195. // requested. If there's a bandwidth restriction, enforce the sending
  1196. // order as well so that earlier sends that are still ongoing hold up later
  1197. // ones.
  1198. //
  1199. hr = AddWorkerJob(DP8SIMJOBTYPE_DELAYEDSEND,
  1200. pDP8SimSend,
  1201. this,
  1202. dwBandwidthDelay,
  1203. dwLatencyDelay,
  1204. DP8SIMJOBFLAG_PERFORMBLOCKINGPHASEFIRST);
  1205. if (hr != DPN_OK)
  1206. {
  1207. DPFX(DPFPREP, 0, "Couldn't add delayed send worker job (0x%p)!",
  1208. pDP8SimSend);
  1209. //
  1210. // Remove the send counter.
  1211. //
  1212. this->DecSendsPending();
  1213. goto Failure;
  1214. }
  1215. //
  1216. // Indicate send completion (with a bogus handle) immediately.
  1217. //
  1218. pDP8SPCB = this->m_pDP8SimCB->GetRealCallbackInterface();
  1219. DPFX(DPFPREP, 2, "Indicating successful send completion (delayed, context = 0x%p) to interface 0x%p.",
  1220. pspsd->pvContext, pDP8SPCB);
  1221. hr = pDP8SPCB->CommandComplete(NULL, DPN_OK, pspsd->pvContext);
  1222. DPFX(DPFPREP, 2, "Returning from command complete [0x%lx].", hr);
  1223. //
  1224. // Ignore any error and return DPNSUCCESS_PENDING, even though we've
  1225. // completed the send already.
  1226. //
  1227. hr = DPNSUCCESS_PENDING;
  1228. //
  1229. // Return bogus output parameters for the caller, it's already complete
  1230. // from their perspective.
  1231. //
  1232. pspsd->hCommand = NULL;
  1233. pspsd->dwCommandDescriptor = 0;
  1234. Exit:
  1235. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1236. return hr;
  1237. Failure:
  1238. if (pDP8SimSend != NULL)
  1239. {
  1240. pDP8SimSend->Release();
  1241. pDP8SimSend = NULL;
  1242. }
  1243. goto Exit;
  1244. } // CDP8SimSP::SendData
  1245. #undef DPF_MODNAME
  1246. #define DPF_MODNAME "CDP8SimSP::EnumQuery"
  1247. //=============================================================================
  1248. // CDP8SimSP::EnumQuery
  1249. //-----------------------------------------------------------------------------
  1250. //
  1251. // Description: ?
  1252. //
  1253. // Arguments:
  1254. // PSPENUMQUERYDATA pspeqd - Pointer to parameter block to use when
  1255. // enumerating.
  1256. //
  1257. // Returns: HRESULT
  1258. //=============================================================================
  1259. STDMETHODIMP CDP8SimSP::EnumQuery(PSPENUMQUERYDATA pspeqd)
  1260. {
  1261. HRESULT hr;
  1262. BOOL fHaveLock = FALSE;
  1263. SPENUMQUERYDATA speqdModified;
  1264. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  1265. CDP8SimCommand * pDP8SimCommand = NULL;
  1266. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspeqd);
  1267. ZeroMemory(&speqdModified, sizeof(speqdModified));
  1268. //
  1269. // Validate (actually assert) the object.
  1270. //
  1271. DNASSERT(this->IsValidObject());
  1272. //
  1273. // Assert the parameters.
  1274. //
  1275. DNASSERT(pspeqd != NULL);
  1276. DNASSERT(pspeqd->pAddressHost != NULL);
  1277. DNASSERT(pspeqd->pAddressDeviceInfo != NULL);
  1278. DNEnterCriticalSection(&this->m_csLock);
  1279. fHaveLock = TRUE;
  1280. //
  1281. // Assert the object state.
  1282. //
  1283. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1284. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1285. //
  1286. // Fire up the global worker thread, if it hasn't been already.
  1287. //
  1288. if (! (this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD))
  1289. {
  1290. hr = StartGlobalWorkerThread();
  1291. if (hr != DPN_OK)
  1292. {
  1293. DPFX(DPFPREP, 0, "Failed starting global worker thread!");
  1294. goto Failure;
  1295. }
  1296. this->m_dwFlags |= DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD;
  1297. }
  1298. DNLeaveCriticalSection(&this->m_csLock);
  1299. fHaveLock = FALSE;
  1300. //
  1301. // Prepare a command object.
  1302. //
  1303. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  1304. CommandFPMContext.dwType = CMDTYPE_ENUMQUERY;
  1305. CommandFPMContext.pvUserContext = pspeqd->pvContext;
  1306. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  1307. if (pDP8SimCommand == NULL)
  1308. {
  1309. hr = DPNERR_OUTOFMEMORY;
  1310. goto Failure;
  1311. }
  1312. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  1313. //
  1314. // Copy the parameter block, modifying as necessary.
  1315. //
  1316. /*
  1317. //
  1318. // Duplicate the host address.
  1319. //
  1320. hr = pspeqd->pAddressHost->Duplicate(&speqdModified.pAddressHost);
  1321. if (hr != DPN_OK)
  1322. {
  1323. DPFX(DPFPREP, 0, "Couldn't duplicate host address!");
  1324. goto Failure;
  1325. }
  1326. */
  1327. speqdModified.pAddressHost = pspeqd->pAddressHost;
  1328. //
  1329. // Change the service provider GUID so it matches the one we're
  1330. // calling.
  1331. //
  1332. hr = speqdModified.pAddressHost->SetSP(&this->m_guidRealSP);
  1333. if (hr != DPN_OK)
  1334. {
  1335. DPFX(DPFPREP, 0, "Couldn't change host address' SP!");
  1336. goto Failure;
  1337. }
  1338. /*
  1339. //
  1340. // Duplicate the host address.
  1341. //
  1342. hr = pspeqd->pAddressDeviceInfo->Duplicate(&speqdModified.pAddressDeviceInfo);
  1343. if (hr != DPN_OK)
  1344. {
  1345. DPFX(DPFPREP, 0, "Couldn't duplicate device info address!");
  1346. goto Failure;
  1347. }
  1348. */
  1349. speqdModified.pAddressDeviceInfo = pspeqd->pAddressDeviceInfo;
  1350. //
  1351. // Change the service provider GUID so it matches the one we're
  1352. // calling.
  1353. //
  1354. hr = speqdModified.pAddressDeviceInfo->SetSP(&this->m_guidRealSP);
  1355. if (hr != DPN_OK)
  1356. {
  1357. DPFX(DPFPREP, 0, "Couldn't change device info address' SP!");
  1358. goto Failure;
  1359. }
  1360. //
  1361. // Add a reference for the enum query command.
  1362. //
  1363. pDP8SimCommand->AddRef();
  1364. speqdModified.pBuffers = pspeqd->pBuffers;
  1365. speqdModified.dwBufferCount = pspeqd->dwBufferCount;
  1366. speqdModified.dwTimeout = pspeqd->dwTimeout;
  1367. speqdModified.dwRetryCount = pspeqd->dwRetryCount;
  1368. speqdModified.dwRetryInterval = pspeqd->dwRetryInterval;
  1369. speqdModified.dwFlags = pspeqd->dwFlags;
  1370. speqdModified.pvContext = pDP8SimCommand;
  1371. //speqdModified.hCommand = pspeqd->hCommand; // filled in by real SP
  1372. //speqdModified.dwCommandDescriptor = pspeqd->dwCommandDescriptor; // filled in by real SP
  1373. //
  1374. // Start the enumeration via the real service provider.
  1375. //
  1376. hr = this->m_pDP8SP->EnumQuery(&speqdModified);
  1377. if (FAILED(hr))
  1378. {
  1379. DPFX(DPFPREP, 0, "Failed starting the enumeration via the real SP object (0x%p)!",
  1380. this->m_pDP8SP);
  1381. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  1382. pDP8SimCommand->Release();
  1383. goto Failure;
  1384. }
  1385. DNASSERT(speqdModified.hCommand != NULL);
  1386. //
  1387. // Save the output parameters.
  1388. //
  1389. pDP8SimCommand->SetRealSPCommand(speqdModified.hCommand,
  1390. speqdModified.dwCommandDescriptor);
  1391. //
  1392. // Generate the output parameters for the caller.
  1393. //
  1394. pspeqd->hCommand = (HANDLE) pDP8SimCommand;
  1395. pspeqd->dwCommandDescriptor = 0;
  1396. Exit:
  1397. //
  1398. // Give up local reference.
  1399. //
  1400. if (pDP8SimCommand != NULL)
  1401. {
  1402. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  1403. pDP8SimCommand->Release();
  1404. pDP8SimCommand = NULL;
  1405. }
  1406. /*
  1407. if (speqdModified.pAddressDeviceInfo != NULL)
  1408. {
  1409. speqdModified.pAddressDeviceInfo->Release();
  1410. speqdModified.pAddressDeviceInfo = NULL;
  1411. }
  1412. if (speqdModified.pAddressHost != NULL)
  1413. {
  1414. speqdModified.pAddressHost->Release();
  1415. speqdModified.pAddressHost = NULL;
  1416. }
  1417. */
  1418. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1419. return hr;
  1420. Failure:
  1421. if (fHaveLock)
  1422. {
  1423. DNLeaveCriticalSection(&this->m_csLock);
  1424. }
  1425. goto Exit;
  1426. } // CDP8SimSP::EnumQuery
  1427. #undef DPF_MODNAME
  1428. #define DPF_MODNAME "CDP8SimSP::EnumRespond"
  1429. //=============================================================================
  1430. // CDP8SimSP::EnumRespond
  1431. //-----------------------------------------------------------------------------
  1432. //
  1433. // Description: ?
  1434. //
  1435. // Arguments:
  1436. // PSPENUMRESPONDDATA psperd - Pointer to parameter block to use when
  1437. // responding.
  1438. //
  1439. // Returns: HRESULT
  1440. //=============================================================================
  1441. STDMETHODIMP CDP8SimSP::EnumRespond(PSPENUMRESPONDDATA psperd)
  1442. {
  1443. HRESULT hr;
  1444. SPENUMRESPONDDATA sperdModified;
  1445. ENUMQUERYDATAWRAPPER * pEnumQueryDataWrapper;
  1446. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  1447. CDP8SimCommand * pDP8SimCommand = NULL;
  1448. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, psperd);
  1449. ZeroMemory(&sperdModified, sizeof(sperdModified));
  1450. //
  1451. // Validate (actually assert) the object.
  1452. //
  1453. DNASSERT(this->IsValidObject());
  1454. //
  1455. // Assert the parameters.
  1456. //
  1457. DNASSERT(psperd != NULL);
  1458. #ifdef DEBUG
  1459. DNEnterCriticalSection(&this->m_csLock);
  1460. //
  1461. // Assert the object state.
  1462. //
  1463. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1464. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  1465. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1466. DNLeaveCriticalSection(&this->m_csLock);
  1467. #endif // DEBUG
  1468. //
  1469. // Prepare a command object.
  1470. //
  1471. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  1472. CommandFPMContext.dwType = CMDTYPE_ENUMRESPOND;
  1473. CommandFPMContext.pvUserContext = psperd->pvContext;
  1474. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  1475. if (pDP8SimCommand == NULL)
  1476. {
  1477. hr = DPNERR_OUTOFMEMORY;
  1478. goto Failure;
  1479. }
  1480. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  1481. //
  1482. // Copy the parameter block, modifying as necessary.
  1483. //
  1484. //
  1485. // We wrapped the enum query data structure, get the original object.
  1486. //
  1487. pEnumQueryDataWrapper = ENUMQUERYEVENTWRAPPER_FROM_SPIEQUERY(psperd->pQuery);
  1488. DNASSERT(*((DWORD*) (&pEnumQueryDataWrapper->m_Sig)) == 0x57455145); // 0x57 0x45 0x51 0x45 = 'WEQE' = 'EQEW' in Intel order
  1489. sperdModified.pQuery = pEnumQueryDataWrapper->pOriginalQuery;
  1490. //
  1491. // Add a reference for the enum respond command.
  1492. //
  1493. pDP8SimCommand->AddRef();
  1494. sperdModified.pBuffers = psperd->pBuffers;
  1495. sperdModified.dwBufferCount = psperd->dwBufferCount;
  1496. sperdModified.dwFlags = psperd->dwFlags;
  1497. sperdModified.pvContext = pDP8SimCommand;
  1498. //sperdModified.hCommand = psperd->hCommand; // filled in by real SP
  1499. //sperdModified.dwCommandDescriptor = psperd->dwCommandDescriptor; // filled in by real SP
  1500. //
  1501. // Respond to the enumeration via the real service provider.
  1502. //
  1503. hr = this->m_pDP8SP->EnumRespond(&sperdModified);
  1504. if (FAILED(hr))
  1505. {
  1506. DPFX(DPFPREP, 0, "Failed responding to enumeration via the real SP object (0x%p)!",
  1507. this->m_pDP8SP);
  1508. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  1509. pDP8SimCommand->Release();
  1510. goto Failure;
  1511. }
  1512. //
  1513. // Save the output parameters.
  1514. //
  1515. pDP8SimCommand->SetRealSPCommand(sperdModified.hCommand,
  1516. sperdModified.dwCommandDescriptor);
  1517. //
  1518. // Generate the output parameters for the caller.
  1519. //
  1520. psperd->hCommand = (HANDLE) pDP8SimCommand;
  1521. psperd->dwCommandDescriptor = 0;
  1522. Exit:
  1523. //
  1524. // Give up local reference.
  1525. //
  1526. if (pDP8SimCommand != NULL)
  1527. {
  1528. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  1529. pDP8SimCommand->Release();
  1530. pDP8SimCommand = NULL;
  1531. }
  1532. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1533. return hr;
  1534. Failure:
  1535. goto Exit;
  1536. } // CDP8SimSP::EnumRespond
  1537. #undef DPF_MODNAME
  1538. #define DPF_MODNAME "CDP8SimSP::CancelCommand"
  1539. //=============================================================================
  1540. // CDP8SimSP::CancelCommand
  1541. //-----------------------------------------------------------------------------
  1542. //
  1543. // Description: ?
  1544. //
  1545. // Arguments:
  1546. // HANDLE hCommand - Handle to command to cancel.
  1547. // DWORD dwCommandDescriptor - Unique descriptor of command to cancel.
  1548. //
  1549. // Returns: HRESULT
  1550. //=============================================================================
  1551. STDMETHODIMP CDP8SimSP::CancelCommand(HANDLE hCommand, DWORD dwCommandDescriptor)
  1552. {
  1553. HRESULT hr;
  1554. CDP8SimCommand * pDP8SimCommand = NULL;
  1555. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p, %u)",
  1556. this, hCommand, dwCommandDescriptor);
  1557. //
  1558. // Validate (actually assert) the object.
  1559. //
  1560. DNASSERT(this->IsValidObject());
  1561. //
  1562. // Assert the parameters.
  1563. //
  1564. DNASSERT(hCommand != NULL);
  1565. DNASSERT(dwCommandDescriptor == 0);
  1566. #ifdef DEBUG
  1567. DNEnterCriticalSection(&this->m_csLock);
  1568. //
  1569. // Assert the object state.
  1570. //
  1571. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1572. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  1573. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1574. DNLeaveCriticalSection(&this->m_csLock);
  1575. #endif // DEBUG
  1576. pDP8SimCommand = (CDP8SimCommand*) hCommand;
  1577. DNASSERT(pDP8SimCommand->IsValidObject());
  1578. //
  1579. // Cancel the real service provider's command.
  1580. //
  1581. hr = this->m_pDP8SP->CancelCommand(pDP8SimCommand->GetRealSPCommand(),
  1582. pDP8SimCommand->GetRealSPCommandDescriptor());
  1583. if (hr != DPN_OK)
  1584. {
  1585. DPFX(DPFPREP, 0, "Failed cancelling real SP object (0x%p)'s command!",
  1586. this->m_pDP8SP);
  1587. //
  1588. // Continue...
  1589. //
  1590. }
  1591. //Exit:
  1592. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1593. return hr;
  1594. //Failure:
  1595. // goto Exit;
  1596. } // CDP8SimSP::CancelCommand
  1597. #undef DPF_MODNAME
  1598. #define DPF_MODNAME "CDP8SimSP::EnumMulticastScopes"
  1599. //=============================================================================
  1600. // CDP8SimSP::EnumMulticastScopes
  1601. //-----------------------------------------------------------------------------
  1602. //
  1603. // Description: ?
  1604. //
  1605. // Arguments:
  1606. // PSPENUMMULTICASTSCOPESDATA pspemsd - Pointer to parameter block to use
  1607. // when enumerating scopes.
  1608. //
  1609. // Returns: HRESULT
  1610. //=============================================================================
  1611. STDMETHODIMP CDP8SimSP::EnumMulticastScopes(PSPENUMMULTICASTSCOPESDATA pspemsd)
  1612. {
  1613. HRESULT hr;
  1614. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspemsd);
  1615. //
  1616. // Validate (actually assert) the object.
  1617. //
  1618. DNASSERT(this->IsValidObject());
  1619. //
  1620. // Assert the parameters.
  1621. //
  1622. DNASSERT(pspemsd != NULL);
  1623. #ifdef DEBUG
  1624. DNEnterCriticalSection(&this->m_csLock);
  1625. //
  1626. // Assert the object state.
  1627. //
  1628. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1629. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1630. DNLeaveCriticalSection(&this->m_csLock);
  1631. #endif // DEBUG
  1632. //
  1633. // Have the real service provider enumerate multicast scopes.
  1634. //
  1635. hr = this->m_pDP8SP->EnumMulticastScopes(pspemsd);
  1636. if (hr != DPN_OK)
  1637. {
  1638. if (hr != DPNERR_BUFFERTOOSMALL)
  1639. {
  1640. DPFX(DPFPREP, 0, "Failed enumerating multicast scopes on real SP object (0x%p)!",
  1641. this->m_pDP8SP);
  1642. }
  1643. //
  1644. // Continue...
  1645. //
  1646. }
  1647. //Exit:
  1648. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1649. return hr;
  1650. //Failure:
  1651. // goto Exit;
  1652. } // CDP8SimSP::EnumMulticastScopes
  1653. #undef DPF_MODNAME
  1654. #define DPF_MODNAME "CDP8SimSP::ShareEndpointInfo"
  1655. //=============================================================================
  1656. // CDP8SimSP::ShareEndpointInfo
  1657. //-----------------------------------------------------------------------------
  1658. //
  1659. // Description: ?
  1660. //
  1661. // Arguments:
  1662. // PSPSHAREENDPOINTINFODATA pspseid - Pointer to parameter block.
  1663. //
  1664. // Returns: HRESULT
  1665. //=============================================================================
  1666. STDMETHODIMP CDP8SimSP::ShareEndpointInfo(PSPSHAREENDPOINTINFODATA pspseid)
  1667. {
  1668. HRESULT hr;
  1669. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspseid);
  1670. //
  1671. // Validate (actually assert) the object.
  1672. //
  1673. DNASSERT(this->IsValidObject());
  1674. //
  1675. // Assert the parameters.
  1676. //
  1677. DNASSERT(pspseid != NULL);
  1678. #ifdef DEBUG
  1679. DNEnterCriticalSection(&this->m_csLock);
  1680. //
  1681. // Assert the object state.
  1682. //
  1683. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1684. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1685. DNLeaveCriticalSection(&this->m_csLock);
  1686. #endif // DEBUG
  1687. //
  1688. // Have the real service provider share the endpoint info.
  1689. //
  1690. hr = this->m_pDP8SP->ShareEndpointInfo(pspseid);
  1691. if (hr != DPN_OK)
  1692. {
  1693. DPFX(DPFPREP, 0, "Failed sharing endpoint info on real SP object (0x%p)!",
  1694. this->m_pDP8SP);
  1695. //
  1696. // Continue...
  1697. //
  1698. }
  1699. //Exit:
  1700. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1701. return hr;
  1702. //Failure:
  1703. // goto Exit;
  1704. } // CDP8SimSP::ShareEndpointInfo
  1705. #undef DPF_MODNAME
  1706. #define DPF_MODNAME "CDP8SimSP::GetEndpointByAddress"
  1707. //=============================================================================
  1708. // CDP8SimSP::GetEndpointByAddress
  1709. //-----------------------------------------------------------------------------
  1710. //
  1711. // Description: ?
  1712. //
  1713. // Arguments:
  1714. // PSPGETENDPOINTBYADDRESSDATA pspgebad - Pointer to parameter block.
  1715. //
  1716. // Returns: HRESULT
  1717. //=============================================================================
  1718. STDMETHODIMP CDP8SimSP::GetEndpointByAddress(PSPGETENDPOINTBYADDRESSDATA pspgebad)
  1719. {
  1720. HRESULT hr;
  1721. #ifndef DPNBUILD_NOMULTICAST
  1722. SPGETENDPOINTBYADDRESSDATA spgebadModified;
  1723. ZeroMemory(&spgebadModified, sizeof(spgebadModified));
  1724. #endif // ! DPNBUILD_NOMULTICAST
  1725. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspgebad);
  1726. //
  1727. // Validate (actually assert) the object.
  1728. //
  1729. DNASSERT(this->IsValidObject());
  1730. //
  1731. // Assert the parameters.
  1732. //
  1733. DNASSERT(pspgebad != NULL);
  1734. #ifdef DEBUG
  1735. DNEnterCriticalSection(&this->m_csLock);
  1736. //
  1737. // Assert the object state.
  1738. //
  1739. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1740. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  1741. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1742. DNLeaveCriticalSection(&this->m_csLock);
  1743. #endif // DEBUG
  1744. #ifdef DPNBUILD_NOMULTICAST
  1745. hr = DPNERR_UNSUPPORTED;
  1746. goto Failure;
  1747. #else // ! DPNBUILD_NOMULTICAST
  1748. //
  1749. // Copy the parameter block, modifying as necessary.
  1750. //
  1751. /*
  1752. //
  1753. // Duplicate the host address.
  1754. //
  1755. hr = pspgebad->pAddressHost->Duplicate(&spgebadModified.pAddressHost);
  1756. if (hr != DPN_OK)
  1757. {
  1758. DPFX(DPFPREP, 0, "Couldn't duplicate host address!");
  1759. goto Failure;
  1760. }
  1761. */
  1762. spgebadModified.pAddressHost = pspgebad->pAddressHost;
  1763. //
  1764. // Change the service provider GUID so it matches the one we're
  1765. // calling.
  1766. //
  1767. hr = spgebadModified.pAddressHost->SetSP(&this->m_guidRealSP);
  1768. if (hr != DPN_OK)
  1769. {
  1770. DPFX(DPFPREP, 0, "Couldn't change host address' SP!");
  1771. goto Failure;
  1772. }
  1773. /*
  1774. //
  1775. // Duplicate the host address.
  1776. //
  1777. hr = pspgebad->pAddressDeviceInfo->Duplicate(&spgebadModified.pAddressDeviceInfo);
  1778. if (hr != DPN_OK)
  1779. {
  1780. DPFX(DPFPREP, 0, "Couldn't duplicate device info address!");
  1781. goto Failure;
  1782. }
  1783. */
  1784. spgebadModified.pAddressDeviceInfo = pspgebad->pAddressDeviceInfo;
  1785. //
  1786. // Change the service provider GUID so it matches the one we're
  1787. // calling.
  1788. //
  1789. hr = spgebadModified.pAddressDeviceInfo->SetSP(&this->m_guidRealSP);
  1790. if (hr != DPN_OK)
  1791. {
  1792. DPFX(DPFPREP, 0, "Couldn't change device info address' SP!");
  1793. goto Failure;
  1794. }
  1795. //
  1796. // Retrieve the real service provider's endpoint.
  1797. //
  1798. hr = this->m_pDP8SP->GetEndpointByAddress(&spgebadModified);
  1799. if (hr == DPN_OK)
  1800. {
  1801. CDP8SimEndpoint * pDP8SimEndpoint;
  1802. //
  1803. // Convert our user context into the real user's context, and
  1804. // return the endpoint handle that our user should see.
  1805. //
  1806. pDP8SimEndpoint = (CDP8SimEndpoint*) spgebadModified.pvEndpointContext;
  1807. pspgebad->hEndpoint = pDP8SimEndpoint;
  1808. pspgebad->pvEndpointContext = pDP8SimEndpoint->GetUserContext();
  1809. }
  1810. else
  1811. {
  1812. DPFX(DPFPREP, 0, "Failed getting endpoint by address on real SP object (0x%p)!",
  1813. this->m_pDP8SP);
  1814. //
  1815. // Continue...
  1816. //
  1817. }
  1818. #endif // ! DPNBUILD_NOMULTICAST
  1819. Exit:
  1820. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1821. return hr;
  1822. Failure:
  1823. /*
  1824. #ifndef DPNBUILD_NOMULTICAST
  1825. if (spgebadModified.pAddressDeviceInfo != NULL)
  1826. {
  1827. spgebadModified.pAddressDeviceInfo->Release();
  1828. spgebadModified.pAddressDeviceInfo = NULL;
  1829. }
  1830. if (spgebadModified.pAddressHost != NULL)
  1831. {
  1832. spgebadModified.pAddressHost->Release();
  1833. spgebadModified.pAddressHost = NULL;
  1834. }
  1835. #endif // ! DPNBUILD_NOMULTICAST
  1836. */
  1837. goto Exit;
  1838. } // CDP8SimSP::GetEndpointByAddress
  1839. #undef DPF_MODNAME
  1840. #define DPF_MODNAME "CDP8SimSP::Update"
  1841. //=============================================================================
  1842. // CDP8SimSP::Update
  1843. //-----------------------------------------------------------------------------
  1844. //
  1845. // Description: ?
  1846. //
  1847. // Arguments:
  1848. // PSPUNUSEDDATA pspud - Pointer to parameter block.
  1849. //
  1850. // Returns: HRESULT
  1851. //=============================================================================
  1852. STDMETHODIMP CDP8SimSP::Update(PSPUPDATEDATA pspud)
  1853. {
  1854. HRESULT hr;
  1855. SPUPDATEDATA spudModified;
  1856. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspud);
  1857. //
  1858. // Validate (actually assert) the object.
  1859. //
  1860. DNASSERT(this->IsValidObject());
  1861. //
  1862. // Assert the parameters.
  1863. //
  1864. DNASSERT(pspud != NULL);
  1865. #ifdef DEBUG
  1866. DNEnterCriticalSection(&this->m_csLock);
  1867. //
  1868. // Assert the object state.
  1869. //
  1870. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1871. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  1872. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1873. DNLeaveCriticalSection(&this->m_csLock);
  1874. #endif // DEBUG
  1875. //
  1876. // Update the real service provider's as appropriate.
  1877. //
  1878. spudModified.UpdateType = pspud->UpdateType;
  1879. switch (spudModified.UpdateType)
  1880. {
  1881. case SP_UPDATE_HOST_MIGRATE:
  1882. case SP_UPDATE_ALLOW_ENUMS:
  1883. case SP_UPDATE_DISALLOW_ENUMS:
  1884. {
  1885. CDP8SimEndpoint * pDP8SimEndpoint;
  1886. //
  1887. // Convert our endpoint into the real SP's endpoint.
  1888. //
  1889. pDP8SimEndpoint = (CDP8SimEndpoint*) pspud->hEndpoint;
  1890. spudModified.hEndpoint = pDP8SimEndpoint->GetRealSPEndpoint();
  1891. hr = this->m_pDP8SP->Update(&spudModified);
  1892. if (hr != DPN_OK)
  1893. {
  1894. DPFX(DPFPREP, 0, "Failed updating real SP object (0x%p)!",
  1895. this->m_pDP8SP);
  1896. //
  1897. // Continue...
  1898. //
  1899. }
  1900. break;
  1901. }
  1902. default:
  1903. {
  1904. DPFX(DPFPREP, 0, "Unrecognized update type %u!", spudModified.UpdateType);
  1905. DNASSERT(! "Unrecognized update type!");
  1906. hr = DPNERR_UNSUPPORTED;
  1907. break;
  1908. }
  1909. }
  1910. //Exit:
  1911. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1912. return hr;
  1913. //Failure:
  1914. // goto Exit;
  1915. } // CDP8SimSP::Update
  1916. #undef DPF_MODNAME
  1917. #define DPF_MODNAME "CDP8SimSP::GetCaps"
  1918. //=============================================================================
  1919. // CDP8SimSP::GetCaps
  1920. //-----------------------------------------------------------------------------
  1921. //
  1922. // Description: ?
  1923. //
  1924. // Arguments:
  1925. // PSPGETCAPSDATA pspgcd - Pointer to parameter block to use when retrieving
  1926. // the capabilities.
  1927. //
  1928. // Returns: HRESULT
  1929. //=============================================================================
  1930. STDMETHODIMP CDP8SimSP::GetCaps(PSPGETCAPSDATA pspgcd)
  1931. {
  1932. HRESULT hr;
  1933. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspgcd);
  1934. //
  1935. // Validate (actually assert) the object.
  1936. //
  1937. DNASSERT(this->IsValidObject());
  1938. //
  1939. // Assert the parameters.
  1940. //
  1941. DNASSERT(pspgcd != NULL);
  1942. #ifdef DEBUG
  1943. DNEnterCriticalSection(&this->m_csLock);
  1944. //
  1945. // Assert the object state.
  1946. //
  1947. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  1948. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  1949. DNLeaveCriticalSection(&this->m_csLock);
  1950. #endif // DEBUG
  1951. //
  1952. // Retrieve the capabilities of the real service provider.
  1953. //
  1954. hr = this->m_pDP8SP->GetCaps(pspgcd);
  1955. if (hr != DPN_OK)
  1956. {
  1957. DPFX(DPFPREP, 0, "Failed getting caps on real SP object (0x%p)!",
  1958. this->m_pDP8SP);
  1959. //
  1960. // Continue...
  1961. //
  1962. }
  1963. else
  1964. {
  1965. //
  1966. // Add in the network simulator flag.
  1967. //
  1968. pspgcd->dwFlags |= DPNSPCAPS_NETWORKSIMULATOR;
  1969. }
  1970. //Exit:
  1971. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  1972. return hr;
  1973. //Failure:
  1974. // goto Exit;
  1975. } // CDP8SimSP::GetCaps
  1976. #undef DPF_MODNAME
  1977. #define DPF_MODNAME "CDP8SimSP::SetCaps"
  1978. //=============================================================================
  1979. // CDP8SimSP::SetCaps
  1980. //-----------------------------------------------------------------------------
  1981. //
  1982. // Description: ?
  1983. //
  1984. // Arguments:
  1985. // PSPSETCAPSDATA pspscd - Pointer to parameter block to use when setting
  1986. // the capabilities.
  1987. //
  1988. // Returns: HRESULT
  1989. //=============================================================================
  1990. STDMETHODIMP CDP8SimSP::SetCaps(PSPSETCAPSDATA pspscd)
  1991. {
  1992. HRESULT hr;
  1993. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspscd);
  1994. //
  1995. // Validate (actually assert) the object.
  1996. //
  1997. DNASSERT(this->IsValidObject());
  1998. //
  1999. // Assert the parameters.
  2000. //
  2001. DNASSERT(pspscd != NULL);
  2002. #ifdef DEBUG
  2003. DNEnterCriticalSection(&this->m_csLock);
  2004. //
  2005. // Assert the object state.
  2006. //
  2007. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2008. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2009. DNLeaveCriticalSection(&this->m_csLock);
  2010. #endif // DEBUG
  2011. //
  2012. // Store the capabilities of the real service provider.
  2013. //
  2014. hr = this->m_pDP8SP->SetCaps(pspscd);
  2015. if (hr != DPN_OK)
  2016. {
  2017. DPFX(DPFPREP, 0, "Failed setting caps on real SP object (0x%p)!",
  2018. this->m_pDP8SP);
  2019. //
  2020. // Continue...
  2021. //
  2022. }
  2023. //Exit:
  2024. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2025. return hr;
  2026. //Failure:
  2027. // goto Exit;
  2028. } // CDP8SimSP::SetCaps
  2029. #undef DPF_MODNAME
  2030. #define DPF_MODNAME "CDP8SimSP::ReturnReceiveBuffers"
  2031. //=============================================================================
  2032. // CDP8SimSP::ReturnReceiveBuffers
  2033. //-----------------------------------------------------------------------------
  2034. //
  2035. // Description: ?
  2036. //
  2037. // Arguments:
  2038. // PSPRECEIVEDBUFFER psprb - Array of receive buffers to return.
  2039. //
  2040. // Returns: HRESULT
  2041. //=============================================================================
  2042. STDMETHODIMP CDP8SimSP::ReturnReceiveBuffers(PSPRECEIVEDBUFFER psprb)
  2043. {
  2044. HRESULT hr;
  2045. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, psprb);
  2046. //
  2047. // Validate (actually assert) the object.
  2048. //
  2049. DNASSERT(this->IsValidObject());
  2050. //
  2051. // Assert the parameters.
  2052. //
  2053. DNASSERT(psprb != NULL);
  2054. #ifdef DEBUG
  2055. DNEnterCriticalSection(&this->m_csLock);
  2056. //
  2057. // Assert the object state.
  2058. //
  2059. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2060. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2061. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2062. DNLeaveCriticalSection(&this->m_csLock);
  2063. #endif // DEBUG
  2064. //
  2065. // Return the receive buffers to the real service provider.
  2066. //
  2067. hr = this->m_pDP8SP->ReturnReceiveBuffers(psprb);
  2068. if (hr != DPN_OK)
  2069. {
  2070. DPFX(DPFPREP, 0, "Failed returning receive buffers to real SP object (0x%p)!",
  2071. this->m_pDP8SP);
  2072. //
  2073. // Continue...
  2074. //
  2075. }
  2076. //Exit:
  2077. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2078. return hr;
  2079. //Failure:
  2080. // goto Exit;
  2081. } // CDP8SimSP::ReturnReceiveBuffers
  2082. #undef DPF_MODNAME
  2083. #define DPF_MODNAME "CDP8SimSP::GetAddressInfo"
  2084. //=============================================================================
  2085. // CDP8SimSP::GetAddressInfo
  2086. //-----------------------------------------------------------------------------
  2087. //
  2088. // Description: ?
  2089. //
  2090. // Arguments:
  2091. // PSPGETADDRESSINFODATA pspgaid - Pointer to parameter block to use when
  2092. // getting address info.
  2093. //
  2094. // Returns: HRESULT
  2095. //=============================================================================
  2096. STDMETHODIMP CDP8SimSP::GetAddressInfo(PSPGETADDRESSINFODATA pspgaid)
  2097. {
  2098. HRESULT hr;
  2099. CDP8SimEndpoint * pDP8SimEndpoint;
  2100. SPGETADDRESSINFODATA spgaidModified;
  2101. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspgaid);
  2102. ZeroMemory(&spgaidModified, sizeof(spgaidModified));
  2103. //
  2104. // Validate (actually assert) the object.
  2105. //
  2106. DNASSERT(this->IsValidObject());
  2107. //
  2108. // Assert the parameters.
  2109. //
  2110. DNASSERT(pspgaid != NULL);
  2111. #ifdef DEBUG
  2112. DNEnterCriticalSection(&this->m_csLock);
  2113. //
  2114. // Assert the object state.
  2115. //
  2116. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2117. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2118. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2119. DNLeaveCriticalSection(&this->m_csLock);
  2120. #endif // DEBUG
  2121. pDP8SimEndpoint = (CDP8SimEndpoint*) pspgaid->hEndpoint;
  2122. DNASSERT(pDP8SimEndpoint->IsValidObject());
  2123. //
  2124. // Initialize return value to NULL.
  2125. //
  2126. pspgaid->pAddress = NULL;
  2127. //
  2128. // If the endpoint is disconnecting, don't try to get the address info.
  2129. //
  2130. pDP8SimEndpoint->Lock();
  2131. if (pDP8SimEndpoint->IsDisconnecting())
  2132. {
  2133. pDP8SimEndpoint->Unlock();
  2134. DPFX(DPFPREP, 0, "Endpoint 0x%p is disconnecting, can't get address info!",
  2135. pDP8SimEndpoint);
  2136. hr = DPNERR_NOCONNECTION;
  2137. goto Failure;
  2138. }
  2139. pDP8SimEndpoint->Unlock();
  2140. //
  2141. // Copy the parameter block, modifying as necessary.
  2142. //
  2143. spgaidModified.hEndpoint = pDP8SimEndpoint->GetRealSPEndpoint();
  2144. spgaidModified.pAddress = NULL; // filled in by real SP
  2145. spgaidModified.Flags = pspgaid->Flags;
  2146. //
  2147. // Get real service provider address info.
  2148. //
  2149. hr = this->m_pDP8SP->GetAddressInfo(&spgaidModified);
  2150. if (hr != DPN_OK)
  2151. {
  2152. DPFX(DPFPREP, 0, "Failed getting real SP object (0x%p) address info!",
  2153. this->m_pDP8SP);
  2154. goto Failure;
  2155. }
  2156. //
  2157. // Modify the address so that the SP uses our GUID, if there was an address
  2158. // returned.
  2159. //
  2160. if (spgaidModified.pAddress != NULL)
  2161. {
  2162. hr = spgaidModified.pAddress->SetSP(&this->m_guidFakeSP);
  2163. if (hr != DPN_OK)
  2164. {
  2165. DPFX(DPFPREP, 0, "Couldn't change address' SP!");
  2166. goto Failure;
  2167. }
  2168. }
  2169. //
  2170. // Return the modified address to the user.
  2171. //
  2172. pspgaid->pAddress = spgaidModified.pAddress;
  2173. Exit:
  2174. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2175. return hr;
  2176. Failure:
  2177. if (spgaidModified.pAddress != NULL)
  2178. {
  2179. spgaidModified.pAddress->Release();
  2180. spgaidModified.pAddress = NULL;
  2181. }
  2182. goto Exit;
  2183. } // CDP8SimSP::GetAddressInfo
  2184. #undef DPF_MODNAME
  2185. #define DPF_MODNAME "CDP8SimSP::IsApplicationSupported"
  2186. //=============================================================================
  2187. // CDP8SimSP::IsApplicationSupported
  2188. //-----------------------------------------------------------------------------
  2189. //
  2190. // Description: ?
  2191. //
  2192. // Arguments:
  2193. // PSPISAPPLICATIONSUPPORTEDDATA pspiasd - Pointer to parameter block to use
  2194. // when checking application
  2195. // support.
  2196. //
  2197. // Returns: HRESULT
  2198. //=============================================================================
  2199. STDMETHODIMP CDP8SimSP::IsApplicationSupported(PSPISAPPLICATIONSUPPORTEDDATA pspiasd)
  2200. {
  2201. HRESULT hr;
  2202. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspiasd);
  2203. //
  2204. // Validate (actually assert) the object.
  2205. //
  2206. DNASSERT(this->IsValidObject());
  2207. //
  2208. // Assert the parameters.
  2209. //
  2210. DNASSERT(pspiasd != NULL);
  2211. #ifdef DEBUG
  2212. DNEnterCriticalSection(&this->m_csLock);
  2213. //
  2214. // Assert the object state.
  2215. //
  2216. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2217. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2218. DNLeaveCriticalSection(&this->m_csLock);
  2219. #endif // DEBUG
  2220. //
  2221. // Check availability with the real service provider.
  2222. //
  2223. hr = this->m_pDP8SP->IsApplicationSupported(pspiasd);
  2224. if (hr != DPN_OK)
  2225. {
  2226. DPFX(DPFPREP, 0, "Failed checking if application is supported by real SP object (0x%p)!",
  2227. this->m_pDP8SP);
  2228. //
  2229. // Continue...
  2230. //
  2231. }
  2232. //Exit:
  2233. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2234. return hr;
  2235. //Failure:
  2236. // goto Exit;
  2237. } // CDP8SimSP::IsApplicationSupported
  2238. #undef DPF_MODNAME
  2239. #define DPF_MODNAME "CDP8SimSP::EnumAdapters"
  2240. //=============================================================================
  2241. // CDP8SimSP::EnumAdapters
  2242. //-----------------------------------------------------------------------------
  2243. //
  2244. // Description: ?
  2245. //
  2246. // Arguments:
  2247. // PSPENUMADAPTERSDATA pspead - Pointer to parameter block to use when
  2248. // enumerating the adapters.
  2249. //
  2250. // Returns: HRESULT
  2251. //=============================================================================
  2252. STDMETHODIMP CDP8SimSP::EnumAdapters(PSPENUMADAPTERSDATA pspead)
  2253. {
  2254. HRESULT hr;
  2255. DWORD dwTemp;
  2256. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, pspead);
  2257. //
  2258. // Validate (actually assert) the object.
  2259. //
  2260. DNASSERT(this->IsValidObject());
  2261. //
  2262. // Assert the parameters.
  2263. //
  2264. DNASSERT(pspead != NULL);
  2265. #ifdef DEBUG
  2266. DNEnterCriticalSection(&this->m_csLock);
  2267. //
  2268. // Assert the object state.
  2269. //
  2270. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2271. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2272. DNLeaveCriticalSection(&this->m_csLock);
  2273. #endif // DEBUG
  2274. //
  2275. // Enumerate the adapters available to the real service provider.
  2276. //
  2277. hr = this->m_pDP8SP->EnumAdapters(pspead);
  2278. if (hr == DPN_OK)
  2279. {
  2280. //
  2281. // Set the NETWORKSIMULATORDEVICE flag for all of the adapters.
  2282. //
  2283. for(dwTemp = 0; dwTemp < pspead->dwAdapterCount; dwTemp++)
  2284. {
  2285. pspead->pAdapterData[dwTemp].dwFlags |= DPNSPINFO_NETWORKSIMULATORDEVICE;
  2286. }
  2287. }
  2288. else
  2289. {
  2290. if (hr != DPNERR_BUFFERTOOSMALL)
  2291. {
  2292. DPFX(DPFPREP, 0, "Failed enumerating adapters on real SP object (0x%p)!",
  2293. this->m_pDP8SP);
  2294. }
  2295. //
  2296. // Continue...
  2297. //
  2298. }
  2299. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2300. return hr;
  2301. } // CDP8SimSP::EnumAdapters
  2302. #undef DPF_MODNAME
  2303. #define DPF_MODNAME "CDP8SimSP::ProxyEnumQuery"
  2304. //=============================================================================
  2305. // CDP8SimSP::ProxyEnumQuery
  2306. //-----------------------------------------------------------------------------
  2307. //
  2308. // Description: ?
  2309. //
  2310. // Arguments:
  2311. // PSPPROXYENUMQUERYDATA psppeqd - Pointer to parameter block to use when
  2312. // proxying the enum query.
  2313. //
  2314. // Returns: HRESULT
  2315. //=============================================================================
  2316. STDMETHODIMP CDP8SimSP::ProxyEnumQuery(PSPPROXYENUMQUERYDATA psppeqd)
  2317. {
  2318. HRESULT hr;
  2319. SPPROXYENUMQUERYDATA sppeqdModified;
  2320. ENUMQUERYDATAWRAPPER * pEnumQueryDataWrapper;
  2321. DPFX(DPFPREP, 2, "(0x%p) Parameters: (0x%p)", this, psppeqd);
  2322. ZeroMemory(&sppeqdModified, sizeof(sppeqdModified));
  2323. //
  2324. // Validate (actually assert) the object.
  2325. //
  2326. DNASSERT(this->IsValidObject());
  2327. //
  2328. // Assert the parameters.
  2329. //
  2330. DNASSERT(psppeqd != NULL);
  2331. DNASSERT(psppeqd->pDestinationAdapter != NULL);
  2332. #ifdef DEBUG
  2333. DNEnterCriticalSection(&this->m_csLock);
  2334. //
  2335. // Assert the object state.
  2336. //
  2337. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2338. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2339. DNASSERT(! (this->m_dwFlags & DP8SIMSPOBJ_CLOSING));
  2340. DNLeaveCriticalSection(&this->m_csLock);
  2341. #endif // DEBUG
  2342. //
  2343. // Copy the parameter block, modifying as necessary.
  2344. //
  2345. /*
  2346. //
  2347. // Duplicate the host address.
  2348. //
  2349. hr = psppeqd->pDestinationAdapter->Duplicate(&sppeqdModified.pDestinationAdapter);
  2350. if (hr != DPN_OK)
  2351. {
  2352. DPFX(DPFPREP, 0, "Couldn't duplicate destination adapter address!");
  2353. goto Failure;
  2354. }
  2355. */
  2356. sppeqdModified.pDestinationAdapter = psppeqd->pDestinationAdapter;
  2357. //
  2358. // Change the service provider GUID so it matches the one we're
  2359. // calling.
  2360. //
  2361. hr = sppeqdModified.pDestinationAdapter->SetSP(&this->m_guidRealSP);
  2362. if (hr != DPN_OK)
  2363. {
  2364. DPFX(DPFPREP, 0, "Couldn't change destination adapter address' SP!");
  2365. goto Failure;
  2366. }
  2367. //
  2368. // We wrapped the enum query data structure, get the original object.
  2369. //
  2370. pEnumQueryDataWrapper = ENUMQUERYEVENTWRAPPER_FROM_SPIEQUERY(psppeqd->pIncomingQueryData);
  2371. DNASSERT(*((DWORD*) (&pEnumQueryDataWrapper->m_Sig)) == 0x57455145); // 0x57 0x45 0x51 0x45 = 'WEQE' = 'EQEW' in Intel order
  2372. sppeqdModified.pIncomingQueryData = pEnumQueryDataWrapper->pOriginalQuery;
  2373. sppeqdModified.dwFlags = psppeqd->dwFlags;
  2374. //
  2375. // Proxy the enum query through the real service provider.
  2376. //
  2377. hr = this->m_pDP8SP->ProxyEnumQuery(&sppeqdModified);
  2378. if (FAILED(hr))
  2379. {
  2380. DPFX(DPFPREP, 0, "Failed proxying enum query through real SP object (0x%p)!",
  2381. this->m_pDP8SP);
  2382. goto Failure;
  2383. }
  2384. Exit:
  2385. /*
  2386. if (sppeqdModified.pDestinationAdapter != NULL)
  2387. {
  2388. sppeqdModified.pDestinationAdapter->Release();
  2389. sppeqdModified.pDestinationAdapter = NULL;
  2390. }
  2391. */
  2392. DPFX(DPFPREP, 2, "(0x%p) Returning: [0x%lx]", this, hr);
  2393. return hr;
  2394. Failure:
  2395. goto Exit;
  2396. } // CDP8SimSP::ProxyEnumQuery
  2397. #undef DPF_MODNAME
  2398. #define DPF_MODNAME "CDP8SimSP::InitializeObject"
  2399. //=============================================================================
  2400. // CDP8SimSP::InitializeObject
  2401. //-----------------------------------------------------------------------------
  2402. //
  2403. // Description: Sets up the object for use like the constructor, but may
  2404. // fail with OUTOFMEMORY. Should only be called by class factory
  2405. // creation routine.
  2406. //
  2407. // Arguments: None.
  2408. //
  2409. // Returns: HRESULT
  2410. // S_OK - Initialization was successful.
  2411. // E_OUTOFMEMORY - There is not enough memory to initialize.
  2412. //=============================================================================
  2413. HRESULT CDP8SimSP::InitializeObject(void)
  2414. {
  2415. HRESULT hr;
  2416. DPFX(DPFPREP, 5, "(0x%p) Enter", this);
  2417. DNASSERT(this->IsValidObject());
  2418. //
  2419. // Create the lock.
  2420. //
  2421. if (! DNInitializeCriticalSection(&this->m_csLock))
  2422. {
  2423. hr = E_OUTOFMEMORY;
  2424. goto Failure;
  2425. }
  2426. //
  2427. // Don't allow critical section reentry.
  2428. //
  2429. DebugSetCriticalSectionRecursionCount(&this->m_csLock, 0);
  2430. hr = S_OK;
  2431. Exit:
  2432. DPFX(DPFPREP, 5, "(0x%p) Returning: [0x%lx]", this, hr);
  2433. return hr;
  2434. Failure:
  2435. goto Exit;
  2436. } // CDP8SimSP::InitializeObject
  2437. #undef DPF_MODNAME
  2438. #define DPF_MODNAME "CDP8SimSP::UninitializeObject"
  2439. //=============================================================================
  2440. // CDP8SimSP::UninitializeObject
  2441. //-----------------------------------------------------------------------------
  2442. //
  2443. // Description: Cleans up the object like the destructor, mostly to balance
  2444. // InitializeObject.
  2445. //
  2446. // Arguments: None.
  2447. //
  2448. // Returns: None.
  2449. //=============================================================================
  2450. void CDP8SimSP::UninitializeObject(void)
  2451. {
  2452. DPFX(DPFPREP, 5, "(0x%p) Enter", this);
  2453. DNASSERT(this->IsValidObject());
  2454. DNDeleteCriticalSection(&this->m_csLock);
  2455. DPFX(DPFPREP, 5, "(0x%p) Leave", this);
  2456. } // CDP8SimSP::UninitializeObject
  2457. #undef DPF_MODNAME
  2458. #define DPF_MODNAME "CDP8SimSP::PerformDelayedSend"
  2459. //=============================================================================
  2460. // CDP8SimSP::PerformDelayedSend
  2461. //-----------------------------------------------------------------------------
  2462. //
  2463. // Description: Performs a delayed send.
  2464. //
  2465. // Arguments:
  2466. // PVOID pvContext - Pointer to context to use when performing delayed
  2467. // send.
  2468. //
  2469. // Returns: None.
  2470. //=============================================================================
  2471. void CDP8SimSP::PerformDelayedSend(PVOID const pvContext)
  2472. {
  2473. HRESULT hr;
  2474. CDP8SimSend * pDP8SimSend = (CDP8SimSend*) pvContext;
  2475. DP8SIMCOMMAND_FPMCONTEXT CommandFPMContext;
  2476. CDP8SimCommand * pDP8SimCommand = NULL;
  2477. DPFX(DPFPREP, 5, "(0x%p) Parameters: (0x%p)", this, pvContext);
  2478. //
  2479. // Validate (actually assert) the object.
  2480. //
  2481. DNASSERT(this->IsValidObject());
  2482. //
  2483. // Assert the parameters.
  2484. //
  2485. DNASSERT(pvContext != NULL);
  2486. #ifdef DEBUG
  2487. DNEnterCriticalSection(&this->m_csLock);
  2488. //
  2489. // Assert the object state.
  2490. //
  2491. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2492. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2493. DNLeaveCriticalSection(&this->m_csLock);
  2494. #endif // DEBUG
  2495. //
  2496. // Prepare a command object.
  2497. //
  2498. ZeroMemory(&CommandFPMContext, sizeof(CommandFPMContext));
  2499. CommandFPMContext.dwType = CMDTYPE_SENDDATA_DELAYED;
  2500. CommandFPMContext.pvUserContext = pDP8SimSend;
  2501. pDP8SimCommand = (CDP8SimCommand*)g_FPOOLCommand.Get(&CommandFPMContext);
  2502. if (pDP8SimCommand == NULL)
  2503. {
  2504. DPFX(DPFPREP, 0, "Couldn't allocate memory for new command object!");
  2505. }
  2506. else
  2507. {
  2508. DPFX(DPFPREP, 7, "New command 0x%p.", pDP8SimCommand);
  2509. //
  2510. // Add a reference for the send command.
  2511. //
  2512. pDP8SimCommand->AddRef();
  2513. pDP8SimSend->SetSendDataBlockContext(pDP8SimCommand);
  2514. //
  2515. // Issue the send to the real SP. Essentially ignore the return value
  2516. // since we already indicated completion to the upper layer.
  2517. //
  2518. hr = this->m_pDP8SP->SendData(pDP8SimSend->GetSendDataBlockPtr());
  2519. if (FAILED(hr))
  2520. {
  2521. DPFX(DPFPREP, 0, "Failed sending delayed data (err = 0x%lx)!", hr);
  2522. DPFX(DPFPREP, 7, "Releasing aborted command 0x%p.", pDP8SimCommand);
  2523. pDP8SimCommand->Release();
  2524. //
  2525. // Remove the send counter.
  2526. //
  2527. this->DecSendsPending();
  2528. DPFX(DPFPREP, 7, "Releasing aborted send 0x%p.", pDP8SimSend);
  2529. pDP8SimSend->Release();
  2530. //
  2531. // Continue.
  2532. //
  2533. }
  2534. else
  2535. {
  2536. if (hr != DPNSUCCESS_PENDING)
  2537. {
  2538. //
  2539. // The command completed right away.
  2540. //
  2541. DNASSERT(hr == DPN_OK);
  2542. hr = this->m_pDP8SimCB->CommandComplete(pDP8SimSend->GetSendDataBlockCommand(),
  2543. hr,
  2544. pDP8SimCommand);
  2545. DNASSERT(hr == DPN_OK);
  2546. }
  2547. else
  2548. {
  2549. //
  2550. // Save the output parameters returned by the SP.
  2551. //
  2552. pDP8SimCommand->SetRealSPCommand(pDP8SimSend->GetSendDataBlockCommand(),
  2553. pDP8SimSend->GetSendDataBlockCommandDescriptor());
  2554. }
  2555. }
  2556. //
  2557. // Give up local reference.
  2558. //
  2559. DPFX(DPFPREP, 7, "Releasing command 0x%p local reference.", pDP8SimCommand);
  2560. pDP8SimCommand->Release();
  2561. pDP8SimCommand = NULL;
  2562. }
  2563. DPFX(DPFPREP, 5, "(0x%p) Leave", this);
  2564. } // CDP8SimSP::PerformDelayedSend
  2565. #undef DPF_MODNAME
  2566. #define DPF_MODNAME "CDP8SimSP::PerformDelayedReceive"
  2567. //=============================================================================
  2568. // CDP8SimSP::PerformDelayedReceive
  2569. //-----------------------------------------------------------------------------
  2570. //
  2571. // Description: Performs a delayed receive.
  2572. //
  2573. // Arguments:
  2574. // PVOID pvContext - Pointer to context to use when performing delayed
  2575. // receive.
  2576. //
  2577. // Returns: None.
  2578. //=============================================================================
  2579. void CDP8SimSP::PerformDelayedReceive(PVOID const pvContext)
  2580. {
  2581. HRESULT hr;
  2582. CDP8SimReceive * pDP8SimReceive = (CDP8SimReceive*) pvContext;
  2583. IDP8SPCallback * pDP8SPCallback;
  2584. SPIE_DATA * pData;
  2585. DPFX(DPFPREP, 5, "(0x%p) Parameters: (0x%p)", this, pvContext);
  2586. //
  2587. // Validate (actually assert) the object.
  2588. //
  2589. DNASSERT(this->IsValidObject());
  2590. //
  2591. // Assert the parameters.
  2592. //
  2593. DNASSERT(pvContext != NULL);
  2594. #ifdef DEBUG
  2595. DNEnterCriticalSection(&this->m_csLock);
  2596. //
  2597. // Assert the object state.
  2598. //
  2599. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2600. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2601. DNLeaveCriticalSection(&this->m_csLock);
  2602. #endif // DEBUG
  2603. pDP8SPCallback = this->m_pDP8SimCB->GetRealCallbackInterface();
  2604. pData = pDP8SimReceive->GetReceiveDataBlockPtr();
  2605. //
  2606. // Indicate the event to the real callback interface.
  2607. //
  2608. DPFX(DPFPREP, 2, "Indicating event SPEV_DATA (message = 0x%p) to interface 0x%p.",
  2609. pData, pDP8SPCallback);
  2610. hr = pDP8SPCallback->IndicateEvent(SPEV_DATA, pData);
  2611. DPFX(DPFPREP, 2, "Returning from event SPEV_DATA [0x%lx].", hr);
  2612. //
  2613. // Update the statistics.
  2614. //
  2615. this->IncrementStatsReceiveTransmitted(pData->pReceivedData->BufferDesc.dwBufferSize,
  2616. pDP8SimReceive->GetLatencyAdded());
  2617. //
  2618. // Return the buffers to the real SP unless the user wanted to keep them.
  2619. //
  2620. if (hr != DPNSUCCESS_PENDING)
  2621. {
  2622. DPFX(DPFPREP, 8, "Returning receive data 0x%p to real SP 0x%p.",
  2623. pData->pReceivedData, this->m_pDP8SP);
  2624. hr = this->m_pDP8SP->ReturnReceiveBuffers(pData->pReceivedData);
  2625. if (hr != DPN_OK)
  2626. {
  2627. DPFX(DPFPREP, 0, "Failed returning receive buffers 0x%p (err = 0x%lx)! Ignoring.",
  2628. pData->pReceivedData, hr);
  2629. //
  2630. // Ignore failure.
  2631. //
  2632. }
  2633. }
  2634. else
  2635. {
  2636. DPFX(DPFPREP, 8, "Callback interface 0x%p keeping receive data 0x%p.",
  2637. pDP8SPCallback, pData->pReceivedData);
  2638. //
  2639. // Our user needs to return the buffers at some point.
  2640. //
  2641. }
  2642. //
  2643. // Remove the receive counter.
  2644. //
  2645. this->DecReceivesPending();
  2646. //
  2647. // Release the delayed receive reference.
  2648. //
  2649. DPFX(DPFPREP, 7, "Releasing receive 0x%p.", pDP8SimReceive);
  2650. pDP8SimReceive->Release();
  2651. pDP8SimReceive = NULL;
  2652. DPFX(DPFPREP, 5, "(0x%p) Leave", this);
  2653. } // CDP8SimSP::PerformDelayedReceive
  2654. #undef DPF_MODNAME
  2655. #define DPF_MODNAME "CDP8SimSP::IncSendsPending"
  2656. //=============================================================================
  2657. // CDP8SimSP::IncSendsPending
  2658. //-----------------------------------------------------------------------------
  2659. //
  2660. // Description: Increments the counter tracking the number of sends pending.
  2661. //
  2662. // Arguments: None.
  2663. //
  2664. // Returns: None.
  2665. //=============================================================================
  2666. void CDP8SimSP::IncSendsPending(void)
  2667. {
  2668. DNEnterCriticalSection(&this->m_csLock);
  2669. //
  2670. // Assert the object state.
  2671. //
  2672. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2673. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2674. //
  2675. // Increment the counters.
  2676. //
  2677. this->m_dwSendsPending++;
  2678. DPFX(DPFPREP, 5, "(0x%p) Sends now pending = %u.",
  2679. this, this->m_dwSendsPending);
  2680. DNLeaveCriticalSection(&this->m_csLock);
  2681. } // CDP8SimSP::IncSendsPending
  2682. #undef DPF_MODNAME
  2683. #define DPF_MODNAME "CDP8SimSP::DecSendsPending"
  2684. //=============================================================================
  2685. // CDP8SimSP::DecSendsPending
  2686. //-----------------------------------------------------------------------------
  2687. //
  2688. // Description: Decrements the counter tracking the number of sends pending.
  2689. //
  2690. // Arguments: None.
  2691. //
  2692. // Returns: None.
  2693. //=============================================================================
  2694. void CDP8SimSP::DecSendsPending(void)
  2695. {
  2696. DNEnterCriticalSection(&this->m_csLock);
  2697. //
  2698. // Assert the object state.
  2699. //
  2700. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2701. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2702. //
  2703. // Decrement the counters.
  2704. //
  2705. DNASSERT(this->m_dwSendsPending > 0);
  2706. this->m_dwSendsPending--;
  2707. DPFX(DPFPREP, 5, "(0x%p) Sends now pending = %u.",
  2708. this, this->m_dwSendsPending);
  2709. //
  2710. // If that was the last send pending and someone is waiting for all of them
  2711. // to complete, notify him.
  2712. //
  2713. if ((this->m_dwSendsPending == 0) &&
  2714. (this->m_hLastPendingSendEvent != NULL))
  2715. {
  2716. DPFX(DPFPREP, 1, "Last pending send, notifying waiting thread.");
  2717. SetEvent(this->m_hLastPendingSendEvent);
  2718. }
  2719. DNLeaveCriticalSection(&this->m_csLock);
  2720. } // CDP8SimSP::DecSendsPending
  2721. #undef DPF_MODNAME
  2722. #define DPF_MODNAME "CDP8SimSP::IncReceivesPending"
  2723. //=============================================================================
  2724. // CDP8SimSP::IncReceivesPending
  2725. //-----------------------------------------------------------------------------
  2726. //
  2727. // Description: Increments the counter tracking the number of receives pending.
  2728. //
  2729. // Arguments: None.
  2730. //
  2731. // Returns: None.
  2732. //=============================================================================
  2733. void CDP8SimSP::IncReceivesPending(void)
  2734. {
  2735. DNEnterCriticalSection(&this->m_csLock);
  2736. //
  2737. // Assert the object state.
  2738. //
  2739. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2740. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2741. //
  2742. // Increment the counters.
  2743. //
  2744. this->m_dwReceivesPending++;
  2745. DPFX(DPFPREP, 5, "(0x%p) Receives now pending = %u.",
  2746. this, this->m_dwReceivesPending);
  2747. DNLeaveCriticalSection(&this->m_csLock);
  2748. } // CDP8SimSP::IncReceivesPending
  2749. #undef DPF_MODNAME
  2750. #define DPF_MODNAME "CDP8SimSP::DecReceivesPending"
  2751. //=============================================================================
  2752. // CDP8SimSP::DecReceivesPending
  2753. //-----------------------------------------------------------------------------
  2754. //
  2755. // Description: Decrements the counter tracking the number of receives pending.
  2756. //
  2757. // Arguments: None.
  2758. //
  2759. // Returns: None.
  2760. //=============================================================================
  2761. void CDP8SimSP::DecReceivesPending(void)
  2762. {
  2763. DNEnterCriticalSection(&this->m_csLock);
  2764. //
  2765. // Assert the object state.
  2766. //
  2767. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_INITIALIZED);
  2768. DNASSERT(this->m_dwFlags & DP8SIMSPOBJ_STARTEDGLOBALWORKERTHREAD);
  2769. //
  2770. // Decrement the counters.
  2771. //
  2772. DNASSERT(this->m_dwReceivesPending > 0);
  2773. this->m_dwReceivesPending--;
  2774. DPFX(DPFPREP, 5, "(0x%p) Receives now pending = %u.",
  2775. this, this->m_dwReceivesPending);
  2776. /*
  2777. //
  2778. // If that was the last receive pending and someone is waiting for all of
  2779. // them to complete, notify him.
  2780. //
  2781. if ((this->m_dwReceivesPending == 0) &&
  2782. (this->m_hLastPendingReceiveEvent != NULL))
  2783. {
  2784. DPFX(DPFPREP, 1, "Last pending receive, notifying waiting thread.");
  2785. SetEvent(this->m_hLastPendingReceiveEvent);
  2786. }
  2787. */
  2788. DNLeaveCriticalSection(&this->m_csLock);
  2789. } // CDP8SimSP::DecReceivesPending
  2790. #undef DPF_MODNAME
  2791. #define DPF_MODNAME "CDP8SimSP::ShouldDrop"
  2792. //=============================================================================
  2793. // CDP8SimSP::ShouldDrop
  2794. //-----------------------------------------------------------------------------
  2795. //
  2796. // Description: Returns TRUE if it is determined that the packet should be
  2797. // dropped, or FALSE if not.
  2798. //
  2799. // Arguments:
  2800. // FLOAT fDropPercentage - Percentage chance that the packet should be
  2801. // dropped.
  2802. //
  2803. // Returns: BOOL
  2804. //=============================================================================
  2805. BOOL CDP8SimSP::ShouldDrop(const FLOAT fDropPercentage)
  2806. {
  2807. double dRand;
  2808. if (fDropPercentage == 0.0)
  2809. {
  2810. return FALSE;
  2811. }
  2812. DNASSERT((fDropPercentage >= 0.0) && (fDropPercentage <= 100.0));
  2813. dRand = GetGlobalRand() * 100.0;
  2814. return ((dRand < fDropPercentage) ? TRUE: FALSE);
  2815. } // CDP8SimSP::ShouldDrop
  2816. #undef DPF_MODNAME
  2817. #define DPF_MODNAME "CDP8SimSP::GetDelay"
  2818. //=============================================================================
  2819. // CDP8SimSP::GetDelay
  2820. //-----------------------------------------------------------------------------
  2821. //
  2822. // Description: Determines a delay factors based on the given bandwidth,
  2823. // data size, and random latency values.
  2824. //
  2825. // This function returns TRUE if some delay should be added,
  2826. // FALSE if not.
  2827. //
  2828. // Arguments:
  2829. // DWORD dwBandwidthBPS - Bandwidth settings.
  2830. // DWORD dwPacketHeaderSize - Size of fixed transport header.
  2831. // DWORD dwDataSize - Size of packet being sent/received.
  2832. // DWORD dwMinRandMS - Minimum random latency value.
  2833. // DWORD dwMaxRandMS - Maximum random latency value.
  2834. // DWORD * pdwBandwidthDelay - Place to store delay caused by bandwidth.
  2835. // DWORD * pdwLatencyDelay - Place to store delay caused by latency.
  2836. //
  2837. // Returns: BOOL
  2838. //=============================================================================
  2839. BOOL CDP8SimSP::GetDelay(const DWORD dwBandwidthBPS,
  2840. const DWORD dwPacketHeaderSize,
  2841. const DWORD dwDataSize,
  2842. const DWORD dwMinRandMS,
  2843. const DWORD dwMaxRandMS,
  2844. DWORD * const pdwBandwidthDelay,
  2845. DWORD * const pdwLatencyDelay)
  2846. {
  2847. BOOL fResult = FALSE;
  2848. double dTransferTime;
  2849. double dHalfDistance;
  2850. double dRand1;
  2851. double dRand2;
  2852. double dTemp;
  2853. //
  2854. // If there's no bandwidth limit, there's no delay.
  2855. //
  2856. if (dwBandwidthBPS == 0)
  2857. {
  2858. (*pdwBandwidthDelay) = 0;
  2859. }
  2860. else
  2861. {
  2862. //
  2863. // Otherwise, find out how many seconds it will take to transfer the
  2864. // data and add it to the base random latency.
  2865. //
  2866. dTransferTime = dwPacketHeaderSize + dwDataSize;
  2867. dTransferTime /= dwBandwidthBPS;
  2868. dTransferTime *= 1000;
  2869. //
  2870. // Round the value down to an even number of milliseconds.
  2871. //
  2872. (*pdwBandwidthDelay) = (DWORD) dTransferTime;
  2873. fResult = TRUE;
  2874. }
  2875. //
  2876. // If the min and max are equal, we can use either as the latency.
  2877. // If it's not zero, then we need to note the delay.
  2878. //
  2879. if (dwMinRandMS == dwMaxRandMS)
  2880. {
  2881. (*pdwLatencyDelay) = dwMinRandMS;
  2882. if (dwMinRandMS > 0)
  2883. {
  2884. fResult = TRUE;
  2885. }
  2886. }
  2887. else
  2888. {
  2889. //
  2890. // First store half the distance between the min and max.
  2891. //
  2892. dHalfDistance = dwMaxRandMS - dwMinRandMS;
  2893. dHalfDistance /= 2;
  2894. //
  2895. // Now pick a number using a normal (bell curve) distribution.
  2896. // This requires two randomly generated numbers and some fancy math.
  2897. //
  2898. do
  2899. {
  2900. dRand1 = 2.0 * GetGlobalRand() - 1.0;
  2901. dRand2 = 2.0 * GetGlobalRand() - 1.0;
  2902. dTemp = (dRand1 * dRand1) + (dRand2 * dRand2);
  2903. }
  2904. while ((dTemp >= 1.0) || (dTemp == 0.0));
  2905. dTemp = sqrt(-2.0 * log(dTemp) / dTemp);
  2906. //dTemp = dHalfDistance + (dRand1 * dTemp) * (dHalfDistance * 0.25);
  2907. dTemp = dHalfDistance + (dRand1 * dTemp) * (dHalfDistance * 0.36666);
  2908. //
  2909. // Cap the values, because our bell curve fattening factor (0.36666
  2910. // instead of 0.25) causes the distribution to leak out past the edges.
  2911. //
  2912. if (dTemp < 0.0)
  2913. {
  2914. dTemp = 0.0;
  2915. }
  2916. else if (dTemp > (dwMaxRandMS - dwMinRandMS))
  2917. {
  2918. dTemp = dwMaxRandMS - dwMinRandMS;
  2919. }
  2920. //
  2921. // Round the normally distributed value down to an even number of
  2922. // milliseconds and add it to the minimum for the final base latency.
  2923. //
  2924. (*pdwLatencyDelay) = dwMinRandMS + (DWORD) dTemp;
  2925. fResult = TRUE;
  2926. }
  2927. return fResult;
  2928. } // CDP8SimSP::GetDelay