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.

4241 lines
112 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: Async.cpp
  6. * Content: Async operation FPM routines
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/27/99 mjn Created
  12. * 12/23/99 mjn Added HOST_MIGRATE functionality
  13. * 12/28/99 mjn Moved Async Op stuff to Async.h
  14. * 12/29/99 mjn Reformed DN_ASYNC_OP to use hParentOp instead of lpvUserContext
  15. * 01/06/00 mjn Moved NameTable stuff to NameTable.h
  16. * 01/07/00 mjn Handle NULL buffer descriptors during sends
  17. * 01/09/00 mjn Transfer Application Description at connect
  18. * 01/10/00 mjn Added support for DN_MSG_INTERNAL_UPDATE_APPLICATION_DESC
  19. * 01/12/00 jtk Added simple code to handle enum and enum response messages.
  20. * 01/11/00 mjn Moved AppDesc stuff to AppDesc.h
  21. * Moved connect/disconnect stuff to Connect.h
  22. * 01/14/00 mjn Added pvUserContext to DN_PerformListen
  23. * 01/15/00 mjn Replaced DN_COUNT_BUFFER with CRefCountBuffer
  24. * 01/16/00 mjn Moved User callback stuff to User.h
  25. * 01/17/00 mjn Fixed ConnectToPeer function names
  26. * 01/17/00 mjn Implemented send time
  27. * 01/19/00 mjn Fixed Parent Op refCount bug in MultiSend
  28. * 01/19/00 mjn Replaced DN_SYNC_EVENT with CSyncEvent
  29. * 01/20/00 mjn Route NameTable operations through NameTable operation list
  30. * 01/21/00 mjn Added DNProcessInternalOperation
  31. * 01/23/00 mjn Added support for DN_MSG_INTERNAL_HOST_DESTROY_PLAYER
  32. * 01/24/00 mjn Added support for DN_MSG_INTERNAL_NAMETABLE_VERSION
  33. * and DN_MSG_INTERNAL_RESYNC_VERSION
  34. * 01/25/00 mjn Added support for DN_MSG_INTERNAL_HOST_MIGRATE_COMPLETE
  35. * 01/26/00 mjn Implemented NameTable re-sync at host migration
  36. * 01/27/00 mjn Cleaned up switch/case statements
  37. * 01/27/00 mjn Added support for retention of receive buffers
  38. * 02/09/00 mjn Implemented DNSEND_COMPLETEONPROCESS
  39. * 02/18/00 mjn Converted DNADDRESS to IDirectPlayAddress8
  40. * 03/23/00 mjn Added phrSync and pvInternal
  41. * 03/24/00 mjn Add guidSP to DN_ASYNC_OP
  42. * 03/25/00 rmt Added code to unregister ourselves when listens are terminated
  43. * 04/04/00 rmt Added check for DPNSVR disable before attempting to unregister
  44. * 04/04/00 mjn Added DNProcessTerminateSession and related code
  45. * 04/06/00 rmt Added code to complete nocopy voice sends
  46. * 04/10/00 mjn Use CAsyncOp for CONNECTs, LISTENs and DISCONNECTs
  47. * 04/11/00 mjn Use CAsyncOp for ENUMs
  48. * 04/13/00 mjn Use Protocol Interface VTBL (replaces some functions)
  49. * 04/14/00 mjn DNPerformListen performs synchronous LISTENs
  50. * 04/16/00 mjn Use CAsyncOp for SENDs
  51. * 04/17/00 mjn Replaced BUFFERDESC with DPN_BUFFER_DESC
  52. * 04/17/00 mjn Added DNCompleteAsyncHandle
  53. * 04/20/00 mjn DNPerformChildSend set's child op flags to the parent's op flags
  54. * 04/21/00 mjn Added DNPerformDisconnect
  55. * 04/23/00 mjn Optionally return child AsyncOp in DNPerformChildSend()
  56. * mjn Removed DNSendCompleteOnProcess (better implementation)
  57. * 04/24/00 mjn Added DNCreateUserHandle()
  58. * 04/26/00 mjn Removed DN_ASYNC_OP and related functions
  59. * 04/28/00 mjn Clear unused buffer descriptions in DN_SendTo()
  60. * 05/02/00 mjn Keep a reference on the Connection during SEND's
  61. * 05/05/00 mjn Return DPN_OK from DNReceiveCompleteOnProcess() to prevent holding the buffer
  62. * 05/08/00 vpo Removed asserts when protocol returns non PENDING
  63. * 06/05/00 mjn Removed assert in DNSendMessage
  64. * 06/21/00 mjn Modified DNSendMessage() and DNCreateSendParent() to use protocol voice bit
  65. * 06/24/00 mjn Added CONNECT completions and fixed DN_MSG_INTERNAL_CONNECT_FAILED processing
  66. * mjn Added code to process DN_MSG_INTERNAL_INSTRUCTED_CONNECT_FAILED
  67. * 07/02/00 mjn Added DNSendGroupMessage() *@@END_MSINTERNAL
  68. * 07/05/00 mjn Removed references to DN_MSG_INTERNAL_ENUM_WITH_APPLICATION_GUID,DN_MSG_INTERNAL_ENUM,DN_MSG_INTERNAL_ENUM_RESPONSE
  69. * 07/06/00 mjn Only use CONNECTED connections in group sends
  70. * mjn Use SP handle instead of interface
  71. * 07/10/00 mjn Added DNPerformEnumQuery()
  72. * mjn Correctly flag parent ops in groups sends and added DPNIDs to async ops for better tracking
  73. * 07/11/00 mjn Added fNoLoopBack to DNSendGroupMessage()
  74. * mjn Added DNPerformNextEnumQuery(),DNPerformSPListen(),DNPerformNextListen(),DNEnumAdapterGuids(),DNPerformNextConnect()
  75. * 07/20/00 mjn Fixed DN_TerminateAllListens() to better use locks
  76. * mjn Fixed connect completions and added DNCompleteConnectOperation() and DNCompleteSendConnectInfo()
  77. * mjn Changed DNPerformDisconnect() to take a CConnection and hEndPt
  78. * mjn Revamped CONNECT process and associated refcounts
  79. * 07/21/00 mjn Process DN_MSG_INTERNAL_CONNECT_ATTEMPT_FAILED
  80. * 07/25/00 mjn Save result code on parent only if failure in DNCompleteSendConnectInfo()
  81. * 07/26/00 mjn DNPerformSPListen() fails if no valid device adapters
  82. * 07/26/00 mjn Fixed locking problem with CAsyncOp::MakeChild()
  83. * 07/28/00 mjn Added code to track send queue info on CConnection
  84. * 07/29/00 mjn Use DNUserConnectionTerminated() rather than DN_TerminateSession()
  85. * mjn Added HRESULT to DNUserReturnBuffer()
  86. * mjn Added fUseCachedCaps to DN_SPEnsureLoaded()
  87. * 07/30/00 mjn Use DNUserTerminateSession() rather than DNUserConnectionTerminated()
  88. * 07/31/00 mjn Removed DN_MSG_INTERNAL_HOST_DESTROY_PLAYER
  89. * 07/31/00 mjn Change DN_MSG_INTERNAL_DELETE_PLAYER to DN_MSG_INTERNAL_DESTROY_PLAYER
  90. * 08/02/00 mjn Added dwFlags to DNReceiveUserData()
  91. * 08/05/00 RichGr IA64: Use %p format specifier in DPFs for 32/64-bit pointers and handles.
  92. * 08/05/00 mjn Added pParent to DNSendGroupMessage and DNSendMessage()
  93. * mjn Ensure cancelled operations don't proceed
  94. * mjn Added m_bilinkActiveList to CAsyncOp
  95. * mjn Added fInternal to DNPerformChildSend()
  96. * mjn Removed DN_TerminateAllListens()
  97. * mjn Added DNProcessFailedRequest()
  98. * mjn Added DNCompleteRequest()
  99. * 08/07/00 mjn Added code to handle peer-peer integrity checking
  100. * 08/08/00 mjn Perform LISTENs on worker thread in DNPerformNextListen()
  101. * 08/14/00 mjn Handle failed LISTENs in DNPerformListen()
  102. * 08/15/00 mjn Changed registration with DPNSVR
  103. * mjn Allow NULL CConnection object pointer for DNPerformRequest()
  104. * 08/20/00 mjn Removed fUseCachedCaps from DN_SPEnsureLoaded()
  105. * 08/24/00 mjn Replace DN_NAMETABLE_OP with CNameTableOp
  106. * mjn DN_MSG_INTERNAL_INSTRUCT_CONNECT gets routed through DNNTAddOperation() in DNProcessInternalOperation()
  107. * 08/31/00 mjn Release DirectNetLock for failure cases in DNPerformRequest()
  108. * 09/04/00 mjn Added CApplicationDesc
  109. * 09/06/00 mjn Fixed register with DPNSVR problem
  110. * 09/14/2000 rmt Bug #44625: DPLAY8: CORE: Multihomed machines cannot always be enumerated
  111. * Moved registration to after point where listen completes.
  112. * 09/14/00 mjn AddRef Protocol refcount when invoking protocol
  113. * 09/21/00 mjn Allow NULL CConnection in DNPerformDisconnect()
  114. * 09/23/00 mjn Added CSyncEvent to DN_LISTEN_OP_DATA
  115. * 09/27/00 mjn Inform lobby of successfull connects from DNCompleteConnectOperation()
  116. * 10/11/00 mjn Save protocol handle on AsyncOp earlier in DNPerformListen()
  117. * mjn Clean up DirectNet object in failure cases in DNCompleteConnectToHost() and DNCompleteSendConnectInfo()
  118. * 10/17/00 mjn Fixed clean up for unreachable players
  119. * 12/11/00 mjn Added verification of internal messages
  120. * 01/10/01 mjn DNCompleteUserConnect() cancels ENUMs with DPNERR_CONNECTING
  121. * 01/22/01 mjn Set connection as INVALID in DNPerformDisconnect()
  122. * 01/25/01 mjn Fixed 64-bit alignment problem in received messages
  123. * 01/30/00 mjn Avoid sending requests during host migration in DNPerformRequest()
  124. * 02/11/01 mjn Allow complete on process requests during host migration in DNPerformRequest()
  125. * mjn Fixed CConnection::GetEndPt() to track calling thread
  126. * 03/30/01 mjn Changes to prevent multiple loading/unloading of SP's
  127. * 04/05/01 mjn Added DPNID parameter to DNProcessHostMigration3()
  128. * 04/11/01 mjn Propegate LISTEN flags from listen parents in DNPerformSPListen() and DNPerformListen()
  129. * 04/13/01 mjn Add requests to the request list in DNPerformRequest()
  130. * Remove requests from request list in DNCompleteSendRequest() and DNReceiveCompleteOnProcessReply()
  131. * 05/07/01 vpo Whistler 384350: "DPLAY8: CORE: Messages from server can be indicated before connect completes"
  132. * 05/11/01 mjn Ensure sends not canceled before storing protocol handle in DNSendMessage()
  133. * 05/14/01 mjn Fix client error handling when completing connect if server not available
  134. * 05/17/01 mjn Track number of threads performing NameTable operations
  135. * 05/22/01 mjn Properly set DirectNetObject as CONNECTED for successful client connect
  136. * 05/23/01 mjn Prevent LISTEN's from being cancelled before completing in DNPerformListen()
  137. * 06/03/01 mjn Make DISCONNECT's children of failed connect's in DNPerformDisconnect()
  138. * 06/08/01 mjn Disconnect connection to host if connect was rejected in DNConnectToHostFailed()
  139. * 07/22/01 mjn Added DPNBUILD_NOHOSTMIGRATE compile flag
  140. *@@END_MSINTERNAL
  141. *
  142. ***************************************************************************/
  143. #include "dncorei.h"
  144. #undef DPF_MODNAME
  145. #define DPF_MODNAME "DNCreateUserHandle"
  146. HRESULT DNCreateUserHandle(DIRECTNETOBJECT *const pdnObject,
  147. CAsyncOp **const ppAsyncOp)
  148. {
  149. HRESULT hResultCode;
  150. CAsyncOp *pAsyncOp;
  151. DPNHANDLE handle;
  152. DPFX(DPFPREP, 6,"Parameters: ppAsyncOp [0x%p]",ppAsyncOp);
  153. DNASSERT(pdnObject != NULL);
  154. DNASSERT(ppAsyncOp != NULL);
  155. pAsyncOp = NULL;
  156. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  157. {
  158. DPFERR("Could not create AsyncOp");
  159. DisplayDNError(0,hResultCode);
  160. goto Failure;
  161. }
  162. pAsyncOp->SetOpType( ASYNC_OP_USER_HANDLE );
  163. pAsyncOp->MakeParent();
  164. if ((hResultCode = pdnObject->HandleTable.Create(pAsyncOp, &handle)) != DPN_OK)
  165. {
  166. DPFERR("Could not create Handle");
  167. DisplayDNError(0,hResultCode);
  168. goto Failure;
  169. }
  170. else
  171. {
  172. // Add a reference for the HandleTable
  173. pAsyncOp->AddRef();
  174. pAsyncOp->Lock();
  175. pAsyncOp->SetHandle(handle);
  176. pAsyncOp->Unlock();
  177. }
  178. pAsyncOp->AddRef();
  179. *ppAsyncOp = pAsyncOp;
  180. pAsyncOp->Release();
  181. pAsyncOp = NULL;
  182. hResultCode = DPN_OK;
  183. Exit:
  184. DPFX(DPFPREP, 6,"Returning: [0x%lx] (handle = 0x%p)",hResultCode,*ppAsyncOp);
  185. return(hResultCode);
  186. Failure:
  187. if (pAsyncOp)
  188. {
  189. pAsyncOp->Release();
  190. pAsyncOp = NULL;
  191. }
  192. goto Exit;
  193. }
  194. #ifndef DPNBUILD_ONLYONEADAPTER
  195. // DNEnumAdapterGuids
  196. //
  197. // Generate a list of adapter GUIDs for multiple ENUMs,LISTENs,CONNECTs
  198. #undef DPF_MODNAME
  199. #define DPF_MODNAME "DNEnumAdapterGuids"
  200. HRESULT DNEnumAdapterGuids(DIRECTNETOBJECT *const pdnObject,
  201. GUID *const pguidSP,
  202. const DWORD dwMatchFlags,
  203. GUID **const ppAdapterList,
  204. DWORD *const pdwNumAdapters)
  205. {
  206. HRESULT hResultCode;
  207. GUID *pguid;
  208. DWORD dw;
  209. DWORD dwAdapterBufferSize;
  210. DWORD dwAdapterBufferCount;
  211. DWORD dwNumAdapters;
  212. void *pvAdapterBuffer;
  213. void *pvBlock;
  214. DPN_SERVICE_PROVIDER_INFO *pSPInfo;
  215. DPFX(DPFPREP, 6,"Parameters: pguidSP [0x%p], dwMatchFlags [0x%x], ppAdapterList [0x%p], pdwNumAdapters [0x%p]",
  216. pguidSP,dwMatchFlags,ppAdapterList,pdwNumAdapters);
  217. DNASSERT(pdnObject != NULL);
  218. DNASSERT(ppAdapterList != NULL);
  219. DNASSERT(pdwNumAdapters != NULL);
  220. pvBlock = NULL;
  221. pvAdapterBuffer = NULL;
  222. dwAdapterBufferSize = 0;
  223. dwAdapterBufferCount = 0;
  224. dwNumAdapters = 0;
  225. hResultCode = DN_EnumAdapters( pdnObject,
  226. 0,
  227. pguidSP,
  228. NULL,
  229. reinterpret_cast<DPN_SERVICE_PROVIDER_INFO*>(pvAdapterBuffer),
  230. &dwAdapterBufferSize,
  231. &dwAdapterBufferCount);
  232. if ((hResultCode == DPNERR_BUFFERTOOSMALL) && (dwAdapterBufferSize > 0))
  233. {
  234. if ((pvAdapterBuffer = DNMalloc(dwAdapterBufferSize)) == NULL)
  235. {
  236. DPFERR("Could not allocate space for adapter list");
  237. hResultCode = DPNERR_OUTOFMEMORY;
  238. goto Failure;
  239. }
  240. if ((pvBlock = MemoryBlockAlloc(pdnObject,dwAdapterBufferCount * sizeof(GUID))) == NULL)
  241. {
  242. DPFERR("Could not allocate MemoryBlock");
  243. hResultCode = DPNERR_OUTOFMEMORY;
  244. goto Failure;
  245. }
  246. pguid = reinterpret_cast<GUID*>(pvBlock);
  247. hResultCode = DN_EnumAdapters( pdnObject,
  248. 0,
  249. pguidSP,
  250. NULL,
  251. reinterpret_cast<DPN_SERVICE_PROVIDER_INFO*>(pvAdapterBuffer),
  252. &dwAdapterBufferSize,
  253. &dwAdapterBufferCount);
  254. if (hResultCode != DPN_OK)
  255. {
  256. DPFERR("Could not enumerate adapters");
  257. DisplayDNError(0,hResultCode);
  258. goto Failure;
  259. }
  260. DPFX(DPFPREP, 7,"dwAdapterBufferCount [%ld]",dwAdapterBufferCount);
  261. pSPInfo = reinterpret_cast<DPN_SERVICE_PROVIDER_INFO*>(pvAdapterBuffer);
  262. for ( dw = 0 ; dw < dwAdapterBufferCount ; dw++ )
  263. {
  264. static const GUID InvalidGuid = { 0 };
  265. if (!memcmp(&InvalidGuid,&pSPInfo->guid,sizeof(GUID)))
  266. {
  267. DPFX(DPFPREP, 1,"Ignoring invalid adapter GUID %u.",dw);
  268. pSPInfo++;
  269. continue;
  270. }
  271. if ((dwMatchFlags != 0) && (! (pSPInfo->dwFlags & dwMatchFlags)))
  272. {
  273. DPFX(DPFPREP, 1,"Ignoring adapter %u with invalid flags (0x%x doesn't match 0x%x).",dw,pSPInfo->dwFlags,dwMatchFlags);
  274. pSPInfo++;
  275. continue;
  276. }
  277. memcpy(pguid,&pSPInfo->guid,sizeof(GUID));
  278. pguid++;
  279. dwNumAdapters++;
  280. pSPInfo++;
  281. }
  282. DNFree(pvAdapterBuffer);
  283. pvAdapterBuffer = NULL;
  284. DPFX(DPFPREP, 7,"Number of adapters [%ld]",dwNumAdapters);
  285. }
  286. *pdwNumAdapters = dwNumAdapters;
  287. *ppAdapterList = reinterpret_cast<GUID*>(pvBlock);
  288. pvBlock = NULL;
  289. hResultCode = DPN_OK;
  290. Exit:
  291. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  292. return(hResultCode);
  293. Failure:
  294. if (pvBlock)
  295. {
  296. MemoryBlockFree(pdnObject,pvBlock);
  297. pvBlock = NULL;
  298. }
  299. if (pvAdapterBuffer)
  300. {
  301. DNFree(pvAdapterBuffer);
  302. pvAdapterBuffer = NULL;
  303. }
  304. goto Exit;
  305. }
  306. #endif // ! DPNBUILD_ONLYONEADAPTER
  307. // DNPerformSPListen
  308. //
  309. // LISTEN on a particular SP
  310. #undef DPF_MODNAME
  311. #define DPF_MODNAME "DNPerformSPListen"
  312. HRESULT DNPerformSPListen(DIRECTNETOBJECT *const pdnObject,
  313. IDirectPlay8Address *const pDeviceAddr,
  314. CAsyncOp *const pListenParent,
  315. CAsyncOp **const ppParent)
  316. {
  317. HRESULT hResultCode;
  318. CAsyncOp *pParent;
  319. #ifndef DPNBUILD_ONLYONESP
  320. GUID guidSP;
  321. #endif // ! DPNBUILD_ONLYONESP
  322. #ifndef DPNBUILD_ONLYONEADAPTER
  323. GUID guidAdapter;
  324. BOOL fEnumAdapters;
  325. DWORD dwMatchFlags;
  326. #endif // ! DPNBUILD_ONLYONEADAPTER
  327. DPN_SP_CAPS dnSPCaps;
  328. CServiceProvider *pSP;
  329. CSyncEvent *pSyncEvent;
  330. DPFX(DPFPREP, 6,"Parameters: pDeviceAddr [0x%p], pListenParent [0x%p], ppParent [0x%p]",
  331. pDeviceAddr,pListenParent,ppParent);
  332. DNASSERT(pdnObject != NULL);
  333. pParent = NULL;
  334. pSP = NULL;
  335. pSyncEvent = NULL;
  336. #if ((defined(DPNBUILD_ONLYONESP)) && (defined(DPNBUILD_LIBINTERFACE)))
  337. DNASSERT(pdnObject->pOnlySP != NULL);
  338. pdnObject->pOnlySP->AddRef();
  339. pSP = pdnObject->pOnlySP;
  340. #else // ! DPNBUILD_ONLYONESP or ! DPNBUILD_LIBINTERFACE
  341. #ifndef DPNBUILD_ONLYONESP
  342. //
  343. // Extract SP guid as we will probably need it
  344. //
  345. if ((hResultCode = IDirectPlay8Address_GetSP(pDeviceAddr,&guidSP)) != DPN_OK)
  346. {
  347. DPFERR("SP not specified in Device address");
  348. DisplayDNError(0,hResultCode);
  349. goto Failure;
  350. }
  351. #endif // ! DPNBUILD_ONLYONESP
  352. //
  353. // Ensure SP specified in Device address is loaded
  354. //
  355. hResultCode = DN_SPEnsureLoaded(pdnObject,
  356. #ifndef DPNBUILD_ONLYONESP
  357. &guidSP,
  358. #endif // ! DPNBUILD_ONLYONESP
  359. #ifndef DPNBUILD_LIBINTERFACE
  360. NULL,
  361. #endif // ! DPNBUILD_LIBINTERFACE
  362. &pSP);
  363. if (hResultCode != DPN_OK)
  364. {
  365. DPFERR("Could not ensure SP is loaded!" );
  366. DisplayDNError(0,hResultCode);
  367. goto Failure;
  368. }
  369. #endif // ! DPNBUILD_ONLYONESP or ! DPNBUILD_LIBINTERFACE
  370. //
  371. // Get SP caps (to later see if we can ENUM on all adapters)
  372. //
  373. if ((hResultCode = DNGetActualSPCaps(pSP,&dnSPCaps)) != DPN_OK)
  374. {
  375. DPFERR("Could not get SP caps");
  376. DisplayDNError(0,hResultCode);
  377. goto Failure;
  378. }
  379. //
  380. // Create a parent op for LISTENs on this SP
  381. //
  382. if ((hResultCode = AsyncOpNew(pdnObject,&pParent)) != DPN_OK)
  383. {
  384. DPFERR("Could not create SP parent listen op");
  385. DisplayDNError(0,hResultCode);
  386. goto Failure;
  387. }
  388. pParent->SetOpType( pListenParent->GetOpType() );
  389. pParent->SetCompletion( DNCompleteListen );
  390. pParent->SetOpFlags( pListenParent->GetOpFlags() );
  391. pParent->MakeParent();
  392. if (pListenParent)
  393. {
  394. pListenParent->Lock();
  395. if (pListenParent->IsCancelled())
  396. {
  397. pListenParent->Unlock();
  398. pParent->SetResult( DPNERR_USERCANCEL );
  399. hResultCode = DPNERR_USERCANCEL;
  400. goto Failure;
  401. }
  402. pParent->MakeChild( pListenParent );
  403. pListenParent->Unlock();
  404. }
  405. //
  406. // Set SP on parent
  407. //
  408. pParent->SetSP( pSP );
  409. #ifndef DPNBUILD_ONLYONEADAPTER
  410. //
  411. // If there is no adapter specified in the device address,
  412. // we will attempt to enum on each individual adapter if the SP supports it
  413. //
  414. fEnumAdapters = FALSE;
  415. dwMatchFlags = 0;
  416. if ((hResultCode = IDirectPlay8Address_GetDevice( pDeviceAddr, &guidAdapter )) != DPN_OK)
  417. {
  418. DPFX(DPFPREP,1,"Could not determine adapter");
  419. DisplayDNError(1,hResultCode);
  420. if (dnSPCaps.dwFlags & DPNSPCAPS_SUPPORTSALLADAPTERS)
  421. {
  422. DPFX(DPFPREP, 3,"SP supports ENUMing on all adapters");
  423. fEnumAdapters = TRUE;
  424. #ifndef DPNBUILD_NOMULTICAST
  425. //
  426. // For multicast listens where the user did not specify an adapter,
  427. // let the SP tell us which of its devices would be the best to use.
  428. //
  429. if (pListenParent->GetOpType() == ASYNC_OP_LISTEN_MULTICAST)
  430. {
  431. dwMatchFlags = DPNSPINFO_DEFAULTMULTICASTDEVICE;
  432. }
  433. #endif // ! DPNBUILD_NOMULTICAST
  434. }
  435. }
  436. #endif // ! DPNBUILD_ONLYONEADAPTER
  437. pSP->Release();
  438. pSP = NULL;
  439. #ifndef DPNBUILD_ONLYONEADAPTER
  440. if(fEnumAdapters)
  441. {
  442. DWORD dwNumAdapters;
  443. GUID *pAdapterList = NULL;;
  444. DN_LISTEN_OP_DATA *pListenOpData = NULL;
  445. if ((hResultCode = DNEnumAdapterGuids( pdnObject,
  446. &guidSP,
  447. dwMatchFlags,
  448. &pAdapterList,
  449. &dwNumAdapters)) != DPN_OK)
  450. {
  451. DPFERR("Could not enum adapters for this SP");
  452. DisplayDNError(0,hResultCode);
  453. goto Failure;
  454. }
  455. if (dwNumAdapters == 0)
  456. {
  457. DPFERR("No adapters were found for this SP");
  458. hResultCode = DPNERR_INVALIDDEVICEADDRESS;
  459. goto Failure;
  460. }
  461. pListenOpData = pParent->GetLocalListenOpData();
  462. pListenOpData->dwNumAdapters = dwNumAdapters;
  463. pListenOpData->dwCurrentAdapter = 0;
  464. pListenOpData->dwCompleteAdapters = 0;
  465. //
  466. // Choose first adapter for initial LISTEN call
  467. //
  468. if ((hResultCode = IDirectPlay8Address_SetDevice(pDeviceAddr,pAdapterList)) != DPN_OK)
  469. {
  470. DPFERR("Could not set device adapter");
  471. DisplayDNError(0,hResultCode);
  472. MemoryBlockFree(pdnObject,pAdapterList);
  473. goto Failure;
  474. }
  475. pListenOpData->dwCurrentAdapter++;
  476. pParent->SetOpData( pAdapterList );
  477. pAdapterList = NULL;
  478. //
  479. // Create a SyncEvent for multiple LISTENs
  480. //
  481. if ((hResultCode = SyncEventNew(pdnObject,&pSyncEvent)) != DPN_OK)
  482. {
  483. DPFERR("Could not create SyncEvent");
  484. DisplayDNError(0,hResultCode);
  485. DNASSERT(FALSE);
  486. goto Failure;
  487. }
  488. pListenOpData->pSyncEvent = pSyncEvent;
  489. pListenOpData = NULL;
  490. }
  491. #endif // ! DPNBUILD_ONLYONEADAPTER
  492. hResultCode = DNPerformListen(pdnObject,pDeviceAddr,pParent);
  493. if (hResultCode != DPNERR_PENDING)
  494. {
  495. DPFERR("Could not perform LISTEN");
  496. DisplayDNError(0,hResultCode);
  497. goto Failure;
  498. }
  499. //
  500. // If there is a SyncEvent, wait for it to be set and then return it
  501. //
  502. if (pSyncEvent)
  503. {
  504. pSyncEvent->WaitForEvent();
  505. pSyncEvent->ReturnSelfToPool();
  506. pSyncEvent = NULL;
  507. }
  508. //
  509. // Save enum frame size
  510. //
  511. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  512. if ((pdnObject->dwMaxFrameSize == 0) || (pdnObject->dwMaxFrameSize > (dnSPCaps.dwMaxEnumPayloadSize + sizeof(DN_ENUM_QUERY_PAYLOAD))))
  513. {
  514. pdnObject->dwMaxFrameSize = dnSPCaps.dwMaxEnumPayloadSize + sizeof(DN_ENUM_QUERY_PAYLOAD);
  515. }
  516. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  517. if (ppParent)
  518. {
  519. pParent->AddRef();
  520. *ppParent = pParent;
  521. }
  522. pParent->Release();
  523. pParent = NULL;
  524. hResultCode = DPN_OK;
  525. Exit:
  526. DNASSERT( pParent == NULL );
  527. DNASSERT( pSP == NULL );
  528. DNASSERT( pSyncEvent == NULL );
  529. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  530. return(hResultCode);
  531. Failure:
  532. if (pParent)
  533. {
  534. pParent->Release();
  535. pParent = NULL;
  536. }
  537. if (pSP)
  538. {
  539. pSP->Release();
  540. pSP = NULL;
  541. }
  542. if (pSyncEvent)
  543. {
  544. pSyncEvent->ReturnSelfToPool();
  545. pSyncEvent = NULL;
  546. }
  547. goto Exit;
  548. }
  549. // DNPerformListen
  550. //
  551. // IDirectPlayAddress8 *pDeviceInfo
  552. // CAsyncOp *pParent
  553. #undef DPF_MODNAME
  554. #define DPF_MODNAME "DNPerformListen"
  555. HRESULT DNPerformListen(DIRECTNETOBJECT *const pdnObject,
  556. IDirectPlay8Address *const pDeviceInfo,
  557. CAsyncOp *const pParent)
  558. {
  559. HANDLE hProtocol;
  560. HRESULT hResultCode;
  561. CAsyncOp *pAsyncOp;
  562. CSyncEvent *pSyncEvent;
  563. HRESULT hrListen;
  564. #ifdef DBG
  565. TCHAR DP8ABuffer[512] = {0};
  566. DWORD DP8ASize = 512;
  567. #endif // DBG
  568. DPFX(DPFPREP, 6,"Parameters: pDeviceInfo [0x%p], pParent [0x%p]",pDeviceInfo,pParent);
  569. DNASSERT(pdnObject != NULL);
  570. DNASSERT(pParent != NULL);
  571. hProtocol = NULL;
  572. pAsyncOp = NULL;
  573. pSyncEvent = NULL;
  574. // Try an initial check (might get lucky :)
  575. if (!(pdnObject->dwFlags & DN_OBJECT_FLAG_INITIALIZED))
  576. {
  577. DPFERR("Not initialized");
  578. return(DPNERR_UNINITIALIZED);
  579. }
  580. #ifdef DBG
  581. IDirectPlay8Address_GetURL(pDeviceInfo,DP8ABuffer,&DP8ASize);
  582. DPFX(DPFPREP, 7,"Device Info [%s]",DP8ABuffer);
  583. #endif // DBG
  584. //
  585. // Set up for LISTEN
  586. //
  587. // HRESULT
  588. hrListen = DPNERR_GENERIC;
  589. // SyncEvent
  590. if ((hResultCode = SyncEventNew(pdnObject,&pSyncEvent)) != DPN_OK)
  591. {
  592. DPFERR("Could not create SyncEvent");
  593. DisplayDNError(0,hResultCode);
  594. DNASSERT(FALSE);
  595. goto Failure;
  596. }
  597. //
  598. // Async op for LISTEN
  599. //
  600. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  601. {
  602. DPFERR("Could not create AsyncOp");
  603. DisplayDNError(0,hResultCode);
  604. DNASSERT(FALSE);
  605. goto Failure;
  606. }
  607. pAsyncOp->SetOpType( pParent->GetOpType() );
  608. pAsyncOp->SetSyncEvent(pSyncEvent);
  609. pAsyncOp->SetResultPointer( &hrListen );
  610. //
  611. // We will set the LISTEN as not cancellable (part of our contract with the Protocol)
  612. // We will set it as cancellable if the LISTEN completes successfully.
  613. //
  614. pAsyncOp->SetCannotCancel();
  615. pParent->Lock();
  616. if (pParent->IsCancelled())
  617. {
  618. pParent->Unlock();
  619. pAsyncOp->SetResult( DPNERR_USERCANCEL );
  620. hResultCode = DPNERR_USERCANCEL;
  621. goto Failure;
  622. }
  623. pAsyncOp->MakeChild(pParent);
  624. pParent->Unlock();
  625. //
  626. // Add to active async op list
  627. //
  628. DNEnterCriticalSection(&pdnObject->csActiveList);
  629. pAsyncOp->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkActiveList);
  630. DNLeaveCriticalSection(&pdnObject->csActiveList);
  631. //
  632. // Perform LISTEN
  633. //
  634. pAsyncOp->AddRef();
  635. hResultCode = DNPListen(pdnObject->pdnProtocolData,
  636. pDeviceInfo,
  637. pParent->GetSP()->GetHandle(),
  638. pParent->GetOpFlags(),
  639. static_cast<void*>(pAsyncOp),
  640. pdnObject->ApplicationDesc.GetReservedData(),
  641. pdnObject->ApplicationDesc.GetReservedDataSize(),
  642. &hProtocol);
  643. if (hResultCode != DPNERR_PENDING)
  644. {
  645. DPFERR("Could not Listen at Protocol layer !");
  646. DisplayDNError(0,hResultCode);
  647. pAsyncOp->Release();
  648. goto Failure;
  649. }
  650. //
  651. // Save Protocol handle
  652. //
  653. pAsyncOp->Lock();
  654. if (pAsyncOp->IsCancelled() && !pAsyncOp->IsComplete())
  655. {
  656. HRESULT hrCancel;
  657. pAsyncOp->Unlock();
  658. DPFX(DPFPREP, 7,"Operation marked for cancel");
  659. if ((hrCancel = DNPCancelCommand(pdnObject->pdnProtocolData,hProtocol)) == DPN_OK)
  660. {
  661. hResultCode = DPNERR_USERCANCEL;
  662. goto Failure;
  663. }
  664. DPFERR("Could not cancel operation");
  665. DisplayDNError(0,hrCancel);
  666. pAsyncOp->Lock();
  667. }
  668. pAsyncOp->SetProtocolHandle(hProtocol);
  669. pAsyncOp->Unlock();
  670. //
  671. // Wait for LISTEN to complete.
  672. // DNPICompleteListen() will set pSyncEvent and hrListen.
  673. // Clean up.
  674. //
  675. pSyncEvent->WaitForEvent();
  676. pAsyncOp->SetSyncEvent(NULL);
  677. pAsyncOp->SetResultPointer(NULL);
  678. if (hrListen != DPN_OK)
  679. {
  680. DPFERR("LISTEN did not succeed");
  681. DisplayDNError(0,hrListen);
  682. hResultCode = hrListen;
  683. goto Failure;
  684. }
  685. pAsyncOp->Release();
  686. pAsyncOp = NULL;
  687. pSyncEvent->ReturnSelfToPool();
  688. pSyncEvent = NULL;
  689. //
  690. // Flag object as LISTENing
  691. //
  692. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  693. pdnObject->dwFlags |= DN_OBJECT_FLAG_LISTENING;
  694. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  695. DNASSERT( hResultCode == DPNERR_PENDING );
  696. hResultCode = DPNERR_PENDING;
  697. Exit:
  698. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  699. return(hResultCode);
  700. Failure:
  701. if (pAsyncOp)
  702. {
  703. DNEnterCriticalSection(&pdnObject->csActiveList);
  704. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  705. DNLeaveCriticalSection(&pdnObject->csActiveList);
  706. pAsyncOp->Release();
  707. pAsyncOp = NULL;
  708. }
  709. if (pSyncEvent)
  710. {
  711. pSyncEvent->ReturnSelfToPool();
  712. pSyncEvent = NULL;
  713. }
  714. goto Exit;
  715. }
  716. #ifndef DPNBUILD_ONLYONEADAPTER
  717. // DNPerformNextListen
  718. //
  719. // This will attempt to perform the next LISTEN if multiple adapters were requested
  720. #undef DPF_MODNAME
  721. #define DPF_MODNAME "DNPerformNextListen"
  722. HRESULT DNPerformNextListen(DIRECTNETOBJECT *const pdnObject,
  723. CAsyncOp *const pAsyncOp,
  724. IDirectPlay8Address *const pDeviceAddr)
  725. {
  726. HRESULT hResultCode;
  727. CAsyncOp *pParent;
  728. DPFX(DPFPREP, 6,"Parameters: pAsyncOp [0x%p], pDeviceAddr [0x%p]",pAsyncOp,pDeviceAddr);
  729. pParent = NULL;
  730. pAsyncOp->Lock();
  731. if (pAsyncOp->GetParent())
  732. {
  733. pAsyncOp->GetParent()->AddRef();
  734. pParent = pAsyncOp->GetParent();
  735. }
  736. pAsyncOp->Unlock();
  737. //
  738. // If there are any LISTENs left to perform, we will move on to the next one
  739. //
  740. if (pParent)
  741. {
  742. DN_LISTEN_OP_DATA *pListenOpData;
  743. pListenOpData = pParent->GetLocalListenOpData();
  744. if ((pListenOpData->dwCurrentAdapter < pListenOpData->dwNumAdapters) && pParent->GetOpData())
  745. {
  746. GUID *pguid;
  747. CWorkerJob *pWorkerJob;
  748. pguid = reinterpret_cast<GUID*>(pParent->GetOpData());
  749. pguid += pListenOpData->dwCurrentAdapter;
  750. if ((hResultCode = IDirectPlay8Address_SetDevice(pDeviceAddr,pguid)) != DPN_OK)
  751. {
  752. DPFERR("Could not set device for next adapter");
  753. DisplayDNError(0,hResultCode);
  754. DNASSERT(FALSE);
  755. goto Failure;
  756. }
  757. pListenOpData->dwCurrentAdapter++;
  758. //
  759. // Perform LISTEN on worker thread
  760. //
  761. if ((hResultCode = WorkerJobNew(pdnObject,&pWorkerJob)) != DPN_OK)
  762. {
  763. DPFERR("Could not create WorkerJob");
  764. DisplayDNError(0,hResultCode);
  765. DNASSERT(FALSE);
  766. goto Failure;
  767. }
  768. pWorkerJob->SetJobType( WORKER_JOB_PERFORM_LISTEN );
  769. pWorkerJob->SetAddress( pDeviceAddr );
  770. pWorkerJob->SetAsyncOp( pParent );
  771. DNQueueWorkerJob(pdnObject,pWorkerJob);
  772. }
  773. pParent->Release();
  774. pParent = NULL;
  775. }
  776. hResultCode = DPN_OK;
  777. Exit:
  778. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  779. return(hResultCode);
  780. Failure:
  781. if (pParent)
  782. {
  783. pParent->Release();
  784. pParent = NULL;
  785. }
  786. goto Exit;
  787. }
  788. #endif // ! DPNBUILD_ONLYONEADAPTER
  789. #undef DPF_MODNAME
  790. #define DPF_MODNAME "DNCompleteListen"
  791. void DNCompleteListen(DIRECTNETOBJECT *const pdnObject,
  792. CAsyncOp *const pAsyncOp)
  793. {
  794. DNASSERT(pdnObject != NULL);
  795. DNASSERT(pAsyncOp != NULL);
  796. #ifndef DPNBUILD_ONLYONEADAPTER
  797. memset( pAsyncOp->GetLocalListenOpData(),0x00,sizeof(DN_LISTEN_OP_DATA) );
  798. #endif // ! DPNBUILD_ONLYONEADAPTER
  799. if (pAsyncOp->GetOpData() != NULL)
  800. {
  801. MemoryBlockFree(pdnObject,pAsyncOp->GetOpData());
  802. pAsyncOp->SetOpData( NULL );
  803. }
  804. if (pAsyncOp->IsChild())
  805. {
  806. DNASSERT(pAsyncOp->GetParent() != NULL);
  807. pAsyncOp->Orphan();
  808. }
  809. else
  810. {
  811. if (pAsyncOp->IsParent())
  812. {
  813. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  814. pdnObject->dwFlags &= (~DN_OBJECT_FLAG_LISTENING);
  815. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  816. }
  817. }
  818. }
  819. // DNPerformEnumQuery
  820. //
  821. // Initiate an ENUM and take care of the book keeping
  822. #undef DPF_MODNAME
  823. #define DPF_MODNAME "DNPerformEnumQuery"
  824. HRESULT DNPerformEnumQuery(DIRECTNETOBJECT *const pdnObject,
  825. IDirectPlay8Address *const pHost,
  826. IDirectPlay8Address *const pDevice,
  827. const HANDLE hSPHandle,
  828. const DWORD dwFlags,
  829. void *const pvContext,
  830. CAsyncOp *const pParent)
  831. {
  832. HRESULT hResultCode;
  833. CAsyncOp *pAsyncOp;
  834. HANDLE hProtocol;
  835. DN_ENUM_QUERY_OP_DATA *pEnumQueryOpData;
  836. DPFX(DPFPREP, 6,"Parameters: pHost [0x%p], pDevice [0x%p], hSPHandle [0x%p], dwFlags [0x%x], pvContext [0x%p], pParent [0x%p]",
  837. pHost,pDevice,hSPHandle,dwFlags,pvContext,pParent);
  838. DNASSERT(pdnObject != NULL);
  839. DNASSERT(hSPHandle != NULL);
  840. pAsyncOp = NULL;
  841. //
  842. // Create AsyncOp for ENUM
  843. //
  844. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  845. {
  846. DPFERR("Could not create AsyncOp");
  847. DisplayDNError(0,hResultCode);
  848. DNASSERT(FALSE);
  849. goto Failure;
  850. }
  851. pAsyncOp->SetOpType( ASYNC_OP_ENUM_QUERY );
  852. pAsyncOp->SetResult( DPNERR_GENERIC );
  853. pAsyncOp->SetCompletion( DNCompleteEnumQuery );
  854. pAsyncOp->SetContext( pvContext );
  855. DNASSERT(pParent != NULL);
  856. pParent->Lock();
  857. if (pParent->IsCancelled())
  858. {
  859. pParent->Unlock();
  860. pAsyncOp->SetResult( DPNERR_USERCANCEL );
  861. hResultCode = DPNERR_USERCANCEL;
  862. goto Failure;
  863. }
  864. pAsyncOp->MakeChild(pParent);
  865. pParent->Unlock();
  866. pEnumQueryOpData = pParent->GetLocalEnumQueryOpData();
  867. //
  868. // Add to active AsyncOp list
  869. //
  870. DNEnterCriticalSection(&pdnObject->csActiveList);
  871. pAsyncOp->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkActiveList);
  872. DNLeaveCriticalSection(&pdnObject->csActiveList);
  873. //
  874. // AddRef Protocol so that it won't go away until this completes
  875. //
  876. DNProtocolAddRef(pdnObject);
  877. pAsyncOp->AddRef();
  878. hResultCode = DNPEnumQuery(pdnObject->pdnProtocolData,
  879. pHost,
  880. pDevice,
  881. hSPHandle,
  882. &pEnumQueryOpData->BufferDesc[DN_ENUM_BUFFERDESC_QUERY_DN_PAYLOAD],
  883. pEnumQueryOpData->dwBufferCount,
  884. pEnumQueryOpData->dwRetryCount, // count of enumerations to send
  885. pEnumQueryOpData->dwRetryInterval, // interval between enumerations
  886. pEnumQueryOpData->dwTimeOut, // linger time after last enumeration is sent
  887. dwFlags,
  888. reinterpret_cast<void*>(pAsyncOp),
  889. ((pEnumQueryOpData->dwAppDescReservedDataSize > 0) ? pEnumQueryOpData->AppDescReservedData : NULL),
  890. pEnumQueryOpData->dwAppDescReservedDataSize,
  891. &hProtocol);
  892. if ( hResultCode != DPNERR_PENDING )
  893. {
  894. DPFERR( "Failed to start enuming!" );
  895. pAsyncOp->Release();
  896. DNProtocolRelease(pdnObject);
  897. goto Failure;
  898. }
  899. //
  900. // Setup for proper clean-up
  901. //
  902. pAsyncOp->Lock();
  903. if (pAsyncOp->IsCancelled() && !pAsyncOp->IsComplete())
  904. {
  905. HRESULT hrCancel;
  906. pAsyncOp->Unlock();
  907. DPFX(DPFPREP, 7,"Operation marked for cancel");
  908. if ((hrCancel = DNPCancelCommand(pdnObject->pdnProtocolData,hProtocol)) == DPN_OK)
  909. {
  910. hResultCode = DPNERR_USERCANCEL;
  911. goto Failure;
  912. }
  913. DPFERR("Could not cancel operation");
  914. DisplayDNError(0,hrCancel);
  915. pAsyncOp->Lock();
  916. }
  917. pAsyncOp->SetProtocolHandle( hProtocol );
  918. pAsyncOp->Unlock();
  919. pAsyncOp->Release();
  920. pAsyncOp = NULL;
  921. hResultCode = DPN_OK;
  922. Exit:
  923. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  924. return(hResultCode);
  925. Failure:
  926. if (pAsyncOp)
  927. {
  928. DNEnterCriticalSection(&pdnObject->csActiveList);
  929. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  930. DNLeaveCriticalSection(&pdnObject->csActiveList);
  931. pAsyncOp->Release();
  932. pAsyncOp = NULL;
  933. }
  934. goto Exit;
  935. }
  936. // DNPerformNextEnumQuery
  937. //
  938. // This will attempt to perform the next ENUM if multiple adapters were requested
  939. #undef DPF_MODNAME
  940. #define DPF_MODNAME "DNPerformNextEnumQuery"
  941. HRESULT DNPerformNextEnumQuery(DIRECTNETOBJECT *const pdnObject,
  942. CAsyncOp *const pAsyncOp,
  943. IDirectPlay8Address *const pHostAddr,
  944. IDirectPlay8Address *const pDeviceAddr)
  945. {
  946. HRESULT hResultCode;
  947. CAsyncOp *pParent;
  948. DPFX(DPFPREP, 6,"Parameters: pAsyncOp [0x%p], pHostAddr [0x%p], pDeviceAddr [0x%p]",pAsyncOp,pHostAddr,pDeviceAddr);
  949. pParent = NULL;
  950. pAsyncOp->Lock();
  951. if (pAsyncOp->GetParent())
  952. {
  953. pAsyncOp->GetParent()->AddRef();
  954. pParent = pAsyncOp->GetParent();
  955. }
  956. pAsyncOp->Unlock();
  957. //
  958. // If there are any ENUMs left to perform, we will move on to the next one
  959. //
  960. if (pParent)
  961. {
  962. #ifndef DPNBUILD_ONLYONEADAPTER
  963. DN_ENUM_QUERY_OP_DATA *pEnumQueryOpData;
  964. pEnumQueryOpData = pParent->GetLocalEnumQueryOpData();
  965. if ((pEnumQueryOpData->dwCurrentAdapter < pEnumQueryOpData->dwNumAdapters) && pParent->GetOpData())
  966. {
  967. GUID *pguid;
  968. DWORD dwMultiplexFlag;
  969. pguid = reinterpret_cast<GUID*>(pParent->GetOpData());
  970. pguid += pEnumQueryOpData->dwCurrentAdapter;
  971. if ((hResultCode = IDirectPlay8Address_SetDevice(pDeviceAddr,pguid)) != DPN_OK)
  972. {
  973. DPFERR("Could not set device for next adapter");
  974. DisplayDNError(0,hResultCode);
  975. DNASSERT(FALSE);
  976. goto Failure;
  977. }
  978. pEnumQueryOpData->dwCurrentAdapter++;
  979. if (pEnumQueryOpData->dwCurrentAdapter < pEnumQueryOpData->dwNumAdapters)
  980. {
  981. dwMultiplexFlag = DN_ENUMQUERYFLAGS_ADDITIONALMULTIPLEXADAPTERS;
  982. }
  983. else
  984. {
  985. dwMultiplexFlag = 0;
  986. }
  987. hResultCode = DNPerformEnumQuery( pdnObject,
  988. pHostAddr,
  989. pDeviceAddr,
  990. pParent->GetSP()->GetHandle(),
  991. pParent->GetOpFlags() | dwMultiplexFlag,
  992. pParent->GetContext(),
  993. pParent );
  994. if (hResultCode != DPN_OK)
  995. {
  996. DPFERR("Could not start ENUM");
  997. DisplayDNError(0,hResultCode);
  998. goto Failure;
  999. }
  1000. }
  1001. #endif // ! DPNBUILD_ONLYONEADAPTER
  1002. pParent->Release();
  1003. pParent = NULL;
  1004. }
  1005. hResultCode = DPN_OK;
  1006. #ifndef DPNBUILD_ONLYONEADAPTER
  1007. Exit:
  1008. #endif // ! DPNBUILD_ONLYONEADAPTER
  1009. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  1010. return(hResultCode);
  1011. #ifndef DPNBUILD_ONLYONEADAPTER
  1012. Failure:
  1013. if (pParent)
  1014. {
  1015. pParent->Release();
  1016. pParent = NULL;
  1017. }
  1018. goto Exit;
  1019. #endif // ! DPNBUILD_ONLYONEADAPTER
  1020. }
  1021. // DNCompleteEnumQuery
  1022. //
  1023. // Completion for AsyncOps for EnumQuery.
  1024. // This will:
  1025. // - free up the EnumQuery memory block associated with this AsyncOp
  1026. #undef DPF_MODNAME
  1027. #define DPF_MODNAME "DNCompleteEnumQuery"
  1028. void DNCompleteEnumQuery(DIRECTNETOBJECT *const pdnObject,
  1029. CAsyncOp *const pAsyncOp)
  1030. {
  1031. DNASSERT(pdnObject != NULL);
  1032. DNASSERT(pAsyncOp != NULL);
  1033. memset(pAsyncOp->GetLocalEnumQueryOpData(),0x00,sizeof(DN_ENUM_QUERY_OP_DATA));
  1034. if (pAsyncOp->GetOpData())
  1035. {
  1036. MemoryBlockFree(pdnObject,pAsyncOp->GetOpData());
  1037. pAsyncOp->SetOpData( NULL );
  1038. }
  1039. if ( pAsyncOp->IsChild() )
  1040. {
  1041. DNASSERT(pAsyncOp->GetParent() != NULL);
  1042. pAsyncOp->GetParent()->Lock();
  1043. //
  1044. // Save HRESULT
  1045. //
  1046. if (pAsyncOp->GetParent()->GetResult() != DPN_OK)
  1047. {
  1048. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1049. }
  1050. //
  1051. // Release parent Handle if it exists
  1052. //
  1053. if (SUCCEEDED(pdnObject->HandleTable.Destroy( pAsyncOp->GetParent()->GetHandle(), NULL )))
  1054. {
  1055. // Release the HandleTable reference
  1056. pAsyncOp->GetParent()->Release();
  1057. }
  1058. pAsyncOp->GetParent()->Unlock();
  1059. }
  1060. }
  1061. // DNCompleteEnumResponse
  1062. //
  1063. // Completion for AsyncOps for EnumResponse.
  1064. // This will:
  1065. // - generate a RETURN_BUFFER message if there was a user payload
  1066. // - free up the EnumResponse memory block associated with this AsyncOp
  1067. #undef DPF_MODNAME
  1068. #define DPF_MODNAME "DNCompleteEnumResponse"
  1069. void DNCompleteEnumResponse(DIRECTNETOBJECT *const pdnObject,
  1070. CAsyncOp *const pAsyncOp)
  1071. {
  1072. DN_ENUM_RESPONSE_OP_DATA *pEnumResponseOpData;
  1073. DNASSERT(pdnObject != NULL);
  1074. DNASSERT(pAsyncOp != NULL);
  1075. pEnumResponseOpData = pAsyncOp->GetLocalEnumResponseOpData();
  1076. if (pEnumResponseOpData->BufferDesc[DN_ENUM_BUFFERDESC_RESPONSE_USER_PAYLOAD].pBufferData != NULL)
  1077. {
  1078. DNUserReturnBuffer( pdnObject,
  1079. DPN_OK,
  1080. pEnumResponseOpData->BufferDesc[DN_ENUM_BUFFERDESC_RESPONSE_USER_PAYLOAD].pBufferData,
  1081. pEnumResponseOpData->pvUserContext);
  1082. }
  1083. memset(pEnumResponseOpData,0x00,sizeof(DN_ENUM_RESPONSE_OP_DATA));
  1084. }
  1085. // DNPerformConnect
  1086. //
  1087. // Initiate a connection and take care of the book keeping (create handle).
  1088. //
  1089. // DPNID dpnid Target player (for ExistingPlayers connect calls)
  1090. // IDirectPlayAddress8 *pDeviceInfo (may be NULL - no connect performed)
  1091. // IDirectPlayAddress8 *pRemoteAddr (may be NULL - no connect performed)
  1092. // DWORD dwFlags CONNECT op flags
  1093. // CAsyncOp *pParent Parent Async Op (if it exists)
  1094. #undef DPF_MODNAME
  1095. #define DPF_MODNAME "DNPerformConnect"
  1096. HRESULT DNPerformConnect(DIRECTNETOBJECT *const pdnObject,
  1097. const DPNID dpnid,
  1098. IDirectPlay8Address *const pDeviceInfo,
  1099. IDirectPlay8Address *const pRemoteAddr,
  1100. CServiceProvider *const pSP,
  1101. const DWORD dwConnectFlags,
  1102. CAsyncOp *const pParent)
  1103. {
  1104. HANDLE hProtocol;
  1105. HRESULT hResultCode;
  1106. CAsyncOp *pAsyncOp;
  1107. #ifdef DBG
  1108. TCHAR DP8ABuffer[512] = {0};
  1109. DWORD DP8ASize;
  1110. #endif // DBG
  1111. DPFX(DPFPREP, 6,"Parameters: dpnid [0x%p], pDeviceInfo [0x%p], pRemoteAddr [0x%p], dwConnectFlags [0x%lx], pParent [0x%p]",
  1112. dpnid,pDeviceInfo,pRemoteAddr,dwConnectFlags,pParent);
  1113. DNASSERT(pdnObject != NULL);
  1114. DNASSERT(pDeviceInfo != NULL);
  1115. DNASSERT(pRemoteAddr != NULL);
  1116. DNASSERT(pSP != NULL);
  1117. pAsyncOp = NULL;
  1118. #ifdef DBG
  1119. DP8ASize = 512;
  1120. IDirectPlay8Address_GetURL(pRemoteAddr,DP8ABuffer,&DP8ASize);
  1121. DPFX(DPFPREP, 7,"Remote Address [%s]",DP8ABuffer);
  1122. DP8ASize = 512;
  1123. IDirectPlay8Address_GetURL(pDeviceInfo,DP8ABuffer,&DP8ASize);
  1124. DPFX(DPFPREP, 7,"Device Info [%s]",DP8ABuffer);
  1125. #endif // DBG
  1126. //
  1127. // Create AsyncOp for CONNECT
  1128. //
  1129. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  1130. {
  1131. DPFERR("Could not create AsyncOp");
  1132. DisplayDNError(0,hResultCode);
  1133. DNASSERT(FALSE);
  1134. goto Failure;
  1135. }
  1136. pAsyncOp->SetDPNID( dpnid );
  1137. pAsyncOp->SetOpFlags( dwConnectFlags );
  1138. pAsyncOp->SetSP(pSP);
  1139. pAsyncOp->SetResult( DPNERR_NOCONNECTION );
  1140. pAsyncOp->SetCompletion( DNCompleteConnect );
  1141. if (pParent)
  1142. {
  1143. pAsyncOp->SetOpType( pParent->GetOpType() );
  1144. pParent->Lock();
  1145. if (pParent->IsCancelled())
  1146. {
  1147. pParent->Unlock();
  1148. pAsyncOp->SetResult( DPNERR_USERCANCEL );
  1149. hResultCode = DPNERR_USERCANCEL;
  1150. goto Failure;
  1151. }
  1152. pAsyncOp->MakeChild(pParent);
  1153. pParent->Unlock();
  1154. }
  1155. else
  1156. {
  1157. //
  1158. // Assume it's a regular connect.
  1159. //
  1160. pAsyncOp->SetOpType( ASYNC_OP_CONNECT );
  1161. }
  1162. //
  1163. // Add to active async op list
  1164. //
  1165. DNEnterCriticalSection(&pdnObject->csActiveList);
  1166. pAsyncOp->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkActiveList);
  1167. DNLeaveCriticalSection(&pdnObject->csActiveList);
  1168. //
  1169. // AddRef Protocol so that it won't go away until this completes
  1170. //
  1171. DNProtocolAddRef(pdnObject);
  1172. //
  1173. // Perform CONNECT
  1174. //
  1175. DPFX(DPFPREP, 7,"Performing connect");
  1176. pAsyncOp->AddRef();
  1177. hResultCode = DNPConnect( pdnObject->pdnProtocolData,
  1178. pDeviceInfo,
  1179. pRemoteAddr,
  1180. pSP->GetHandle(),
  1181. dwConnectFlags,
  1182. pAsyncOp,
  1183. pdnObject->ApplicationDesc.GetReservedData(),
  1184. pdnObject->ApplicationDesc.GetReservedDataSize(),
  1185. &hProtocol);
  1186. if (hResultCode != DPNERR_PENDING)
  1187. {
  1188. DPFERR("Could not CONNECT");
  1189. DisplayDNError(0,hResultCode);
  1190. pAsyncOp->Release();
  1191. DNProtocolRelease(pdnObject);
  1192. goto Failure;
  1193. }
  1194. pAsyncOp->Lock();
  1195. if (pAsyncOp->IsCancelled() && !pAsyncOp->IsComplete())
  1196. {
  1197. HRESULT hrCancel;
  1198. pAsyncOp->Unlock();
  1199. DPFX(DPFPREP, 7,"Operation marked for cancel");
  1200. if ((hrCancel = DNPCancelCommand(pdnObject->pdnProtocolData,hProtocol)) == DPN_OK)
  1201. {
  1202. //
  1203. // If the parent has DPN_OK, then another connect succeeded and we don't want to fail.
  1204. // This may happen in a dual (or more) adapter case.
  1205. //
  1206. if (pParent && pParent->GetResult() == DPN_OK)
  1207. {
  1208. hResultCode = DPN_OK;
  1209. }
  1210. else
  1211. {
  1212. hResultCode = DPNERR_USERCANCEL;
  1213. }
  1214. goto Failure;
  1215. }
  1216. DPFERR("Could not cancel operation");
  1217. DisplayDNError(0,hrCancel);
  1218. pAsyncOp->Lock();
  1219. }
  1220. pAsyncOp->SetProtocolHandle(hProtocol);
  1221. pAsyncOp->Unlock();
  1222. pAsyncOp->Release();
  1223. pAsyncOp = NULL;
  1224. hResultCode = DPN_OK;
  1225. Exit:
  1226. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  1227. return(hResultCode);
  1228. Failure:
  1229. if (pAsyncOp)
  1230. {
  1231. DNEnterCriticalSection(&pdnObject->csActiveList);
  1232. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  1233. DNLeaveCriticalSection(&pdnObject->csActiveList);
  1234. pAsyncOp->Release();
  1235. pAsyncOp = NULL;
  1236. }
  1237. goto Exit;
  1238. }
  1239. // DNPerformNextConnect
  1240. //
  1241. // This will attempt to perform the next CONNECT if multiple adapters were requested
  1242. #undef DPF_MODNAME
  1243. #define DPF_MODNAME "DNPerformNextConnect"
  1244. HRESULT DNPerformNextConnect(DIRECTNETOBJECT *const pdnObject,
  1245. CAsyncOp *const pAsyncOp,
  1246. IDirectPlay8Address *const pHostAddr,
  1247. IDirectPlay8Address *const pDeviceAddr)
  1248. {
  1249. HRESULT hResultCode;
  1250. CAsyncOp *pParent;
  1251. DPFX(DPFPREP, 6,"Parameters: pAsyncOp [0x%p], pHostAddr [0x%p], pDeviceAddr [0x%p]",pAsyncOp,pHostAddr,pDeviceAddr);
  1252. pParent = NULL;
  1253. pAsyncOp->Lock();
  1254. if (pAsyncOp->GetParent())
  1255. {
  1256. pAsyncOp->GetParent()->AddRef();
  1257. pParent = pAsyncOp->GetParent();
  1258. }
  1259. pAsyncOp->Unlock();
  1260. //
  1261. // If there are any CONNECTs left to perform, we will move on to the next one
  1262. //
  1263. if (pParent)
  1264. {
  1265. #ifndef DPNBUILD_ONLYONEADAPTER
  1266. DN_CONNECT_OP_DATA *pConnectOpData;
  1267. pConnectOpData = pParent->GetLocalConnectOpData();
  1268. if ((pConnectOpData->dwCurrentAdapter < pConnectOpData->dwNumAdapters) && pParent->GetOpData())
  1269. {
  1270. GUID *pguid;
  1271. DWORD dwMultiplexFlag;
  1272. pguid = reinterpret_cast<GUID*>(pParent->GetOpData());
  1273. pguid += pConnectOpData->dwCurrentAdapter;
  1274. if ((hResultCode = IDirectPlay8Address_SetDevice(pDeviceAddr,pguid)) != DPN_OK)
  1275. {
  1276. DPFERR("Could not set device for next adapter");
  1277. DisplayDNError(0,hResultCode);
  1278. DNASSERT(FALSE);
  1279. goto Failure;
  1280. }
  1281. pConnectOpData->dwCurrentAdapter++;
  1282. if (pConnectOpData->dwCurrentAdapter < pConnectOpData->dwNumAdapters)
  1283. {
  1284. dwMultiplexFlag = DN_CONNECTFLAGS_ADDITIONALMULTIPLEXADAPTERS;
  1285. }
  1286. else
  1287. {
  1288. dwMultiplexFlag = 0;
  1289. }
  1290. if ((hResultCode = DNPerformConnect(pdnObject,
  1291. NULL,
  1292. pDeviceAddr,
  1293. pHostAddr,
  1294. pParent->GetSP(),
  1295. pParent->GetOpFlags() | dwMultiplexFlag,
  1296. pParent)) != DPN_OK)
  1297. {
  1298. DPFERR("Could not perform CONNECT");
  1299. DisplayDNError(0,hResultCode);
  1300. goto Failure;
  1301. }
  1302. }
  1303. #endif // ! DPNBUILD_ONLYONEADAPTER
  1304. pParent->Release();
  1305. pParent = NULL;
  1306. }
  1307. hResultCode = DPN_OK;
  1308. #ifndef DPNBUILD_ONLYONEADAPTER
  1309. Exit:
  1310. #endif // ! DPNBUILD_ONLYONEADAPTER
  1311. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  1312. return(hResultCode);
  1313. #ifndef DPNBUILD_ONLYONEADAPTER
  1314. Failure:
  1315. if (pParent)
  1316. {
  1317. pParent->Release();
  1318. pParent = NULL;
  1319. }
  1320. goto Exit;
  1321. #endif // ! DPNBUILD_ONLYONEADAPTER
  1322. }
  1323. #undef DPF_MODNAME
  1324. #define DPF_MODNAME "DNCompleteConnect"
  1325. void DNCompleteConnect(DIRECTNETOBJECT *const pdnObject,
  1326. CAsyncOp *const pAsyncOp)
  1327. {
  1328. //
  1329. // Save the result code on the parent if it hasn't already been set (DPN_OK or DPNERR_HOSTREJECTEDCONNECTION)
  1330. //
  1331. if (pAsyncOp->GetParent() && (pAsyncOp->GetResult() != DPNERR_NOCONNECTION))
  1332. {
  1333. pAsyncOp->GetParent()->Lock();
  1334. if ((pAsyncOp->GetParent()->GetResult() != DPN_OK)
  1335. && (pAsyncOp->GetParent()->GetResult() != DPNERR_HOSTREJECTEDCONNECTION))
  1336. {
  1337. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1338. }
  1339. pAsyncOp->GetParent()->Unlock();
  1340. }
  1341. }
  1342. #undef DPF_MODNAME
  1343. #define DPF_MODNAME "DNCompleteConnectToHost"
  1344. void DNCompleteConnectToHost(DIRECTNETOBJECT *const pdnObject,
  1345. CAsyncOp *const pAsyncOp)
  1346. {
  1347. //
  1348. // Save the result code on the parent if there was a problem (this should be the connect operation parent)
  1349. //
  1350. if ((pAsyncOp->GetResult() != DPN_OK) && (pAsyncOp->GetResult() != DPNERR_NOCONNECTION))
  1351. {
  1352. if (pAsyncOp->GetParent())
  1353. {
  1354. pAsyncOp->GetParent()->Lock();
  1355. if ((pAsyncOp->GetParent()->GetResult() != DPN_OK) && (pAsyncOp->GetParent()->GetResult() != DPNERR_HOSTREJECTEDCONNECTION))
  1356. {
  1357. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1358. }
  1359. pAsyncOp->GetParent()->Unlock();
  1360. }
  1361. }
  1362. //
  1363. // Clean up DirectNet object if this fails
  1364. //
  1365. if (pAsyncOp->GetResult() != DPN_OK)
  1366. {
  1367. CAsyncOp *pConnectParent;
  1368. pConnectParent = NULL;
  1369. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1370. pdnObject->dwFlags &= (~(DN_OBJECT_FLAG_CONNECTED
  1371. | DN_OBJECT_FLAG_CONNECTING
  1372. | DN_OBJECT_FLAG_HOST_CONNECTED));
  1373. if (pdnObject->pConnectParent)
  1374. {
  1375. pConnectParent = pdnObject->pConnectParent;
  1376. pdnObject->pConnectParent = NULL;
  1377. }
  1378. if( pdnObject->pIDP8ADevice )
  1379. {
  1380. IDirectPlay8Address_Release( pdnObject->pIDP8ADevice );
  1381. pdnObject->pIDP8ADevice = NULL;
  1382. }
  1383. if( pdnObject->pConnectAddress )
  1384. {
  1385. IDirectPlay8Address_Release( pdnObject->pConnectAddress );
  1386. pdnObject->pConnectAddress = NULL;
  1387. }
  1388. if( pdnObject->pConnectSP )
  1389. {
  1390. pdnObject->pConnectSP->Release();
  1391. pdnObject->pConnectSP = NULL;
  1392. }
  1393. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1394. if (pConnectParent)
  1395. {
  1396. pConnectParent->Release();
  1397. pConnectParent = NULL;
  1398. }
  1399. DNASSERT(pConnectParent == NULL);
  1400. }
  1401. //
  1402. // Clean up CONNECT op data
  1403. //
  1404. #ifndef DPNBUILD_ONLYONEADAPTER
  1405. memset( pAsyncOp->GetLocalConnectOpData(),0x00,sizeof(DN_CONNECT_OP_DATA) );
  1406. #endif // ! DPNBUILD_ONLYONEADAPTER
  1407. if (pAsyncOp->GetOpData() != NULL)
  1408. {
  1409. MemoryBlockFree(pdnObject,pAsyncOp->GetOpData());
  1410. pAsyncOp->SetOpData( NULL );
  1411. }
  1412. //
  1413. // Detach from parent
  1414. // I'm not sure why we need to do this !
  1415. //
  1416. DNASSERT(pAsyncOp->IsChild());
  1417. DNASSERT(pAsyncOp->GetParent());
  1418. pAsyncOp->Orphan();
  1419. }
  1420. //
  1421. // Completion for connect parent
  1422. //
  1423. #undef DPF_MODNAME
  1424. #define DPF_MODNAME "DNCompleteConnectOperation"
  1425. void DNCompleteConnectOperation(DIRECTNETOBJECT *const pdnObject,
  1426. CAsyncOp *const pAsyncOp)
  1427. {
  1428. //
  1429. // Save the result code on the parent (if it exists - it will be the CONNECT handle)
  1430. //
  1431. if (pAsyncOp->GetParent())
  1432. {
  1433. pAsyncOp->GetParent()->Lock();
  1434. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1435. pAsyncOp->GetParent()->SetRefCountBuffer( pAsyncOp->GetRefCountBuffer() );
  1436. pAsyncOp->GetParent()->Unlock();
  1437. if (SUCCEEDED(pdnObject->HandleTable.Destroy( pAsyncOp->GetParent()->GetHandle(), NULL )))
  1438. {
  1439. // Release the HandleTable reference
  1440. pAsyncOp->GetParent()->Release();
  1441. }
  1442. }
  1443. //
  1444. // If the OpData of this AsyncOp is set it is a pointer to a RefCountBuffer pointer
  1445. // (sync connect call) and we will fill it in
  1446. //
  1447. if (pAsyncOp->GetOpData())
  1448. {
  1449. if (pAsyncOp->GetRefCountBuffer())
  1450. {
  1451. pAsyncOp->GetRefCountBuffer()->AddRef();
  1452. *(static_cast<CRefCountBuffer**>(pAsyncOp->GetOpData())) = pAsyncOp->GetRefCountBuffer();
  1453. }
  1454. pAsyncOp->SetOpData( NULL );
  1455. }
  1456. #ifndef DPNBUILD_NOLOBBY
  1457. //
  1458. // If this connect succeeded, we will inform the lobby
  1459. //
  1460. if (pAsyncOp->GetResult() == DPN_OK)
  1461. {
  1462. DNUpdateLobbyStatus(pdnObject,DPLSESSION_CONNECTED);
  1463. }
  1464. else
  1465. {
  1466. DNUpdateLobbyStatus(pdnObject,DPLSESSION_COULDNOTCONNECT);
  1467. }
  1468. #endif // ! DPNBUILD_NOLOBBY
  1469. //
  1470. // Clear DISCONNECTING flag (in case this was aborted)
  1471. //
  1472. DPFX(DPFPREP, 8,"Clearing DISCONNECTING flag");
  1473. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1474. pdnObject->dwFlags &= (~DN_OBJECT_FLAG_DISCONNECTING);
  1475. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1476. }
  1477. //
  1478. // Completion for connect handle given to user if Connect was called asynchronously
  1479. //
  1480. #undef DPF_MODNAME
  1481. #define DPF_MODNAME "DNCompleteUserConnect"
  1482. void DNCompleteUserConnect(DIRECTNETOBJECT *const pdnObject,
  1483. CAsyncOp *const pAsyncOp)
  1484. {
  1485. CNameTableEntry *pHostPlayer;
  1486. pHostPlayer = NULL;
  1487. //
  1488. // No longer connecting
  1489. //
  1490. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1491. pdnObject->dwFlags &= (~DN_OBJECT_FLAG_CONNECTING);
  1492. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1493. //
  1494. // Clients need to release all sends from the server that are
  1495. // queued once the CONNECT_COMPLETE gets indicated.
  1496. // We prepare to do that now.
  1497. //
  1498. if ((pAsyncOp->GetResult() == DPN_OK) && (pdnObject->dwFlags & DN_OBJECT_FLAG_CLIENT))
  1499. {
  1500. if (pdnObject->NameTable.GetHostPlayerRef( &pHostPlayer ) == DPN_OK)
  1501. {
  1502. pHostPlayer->Lock();
  1503. pHostPlayer->MakeAvailable();
  1504. pHostPlayer->NotifyAddRef();
  1505. pHostPlayer->SetInUse();
  1506. pHostPlayer->Unlock();
  1507. //
  1508. // We are now connected
  1509. //
  1510. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1511. pdnObject->dwFlags |= DN_OBJECT_FLAG_CONNECTED;
  1512. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1513. }
  1514. else
  1515. {
  1516. //
  1517. // If we couldn't get a reference on the server (host player),
  1518. // then either the server has disconnected, or we are being shut down.
  1519. // In either case, we should return an error
  1520. //
  1521. DPFX(DPFPREP, 0, "Couldn't get host player reference, failing CONNECT!");
  1522. pAsyncOp->SetResult( DPNERR_NOCONNECTION );
  1523. if (pAsyncOp->GetRefCountBuffer())
  1524. {
  1525. pAsyncOp->GetRefCountBuffer()->Release();
  1526. pAsyncOp->SetRefCountBuffer( NULL );
  1527. }
  1528. }
  1529. }
  1530. //
  1531. // Generate connect completion for player
  1532. //
  1533. DNUserConnectComplete( pdnObject,
  1534. pAsyncOp->GetHandle(),
  1535. pAsyncOp->GetContext(),
  1536. pAsyncOp->GetResult(),
  1537. pAsyncOp->GetRefCountBuffer() );
  1538. //
  1539. // Cancel ENUMs if the CONNECT succeeded and unload SP's
  1540. //
  1541. if (pAsyncOp->GetResult() == DPN_OK)
  1542. {
  1543. DNCancelActiveCommands(pdnObject,DN_CANCEL_FLAG_ENUM_QUERY,NULL,TRUE,DPNERR_CONNECTING);
  1544. #if ((! defined(DPNBUILD_LIBINTERFACE)) || (! defined(DPNBUILD_ONLYONESP)))
  1545. DN_SPReleaseAll(pdnObject);
  1546. #endif // ! DPNBUILD_LIBINTERFACE or ! DPNBUILD_ONLYONESP
  1547. //
  1548. // Actually release queued messages if necessary
  1549. //
  1550. if (pHostPlayer != NULL)
  1551. {
  1552. pHostPlayer->PerformQueuedOperations();
  1553. pHostPlayer->Release();
  1554. pHostPlayer = NULL;
  1555. }
  1556. }
  1557. DNASSERT( pHostPlayer == NULL );
  1558. }
  1559. //
  1560. // Completion for NewPlayer sending connect data to the Host
  1561. //
  1562. #undef DPF_MODNAME
  1563. #define DPF_MODNAME "DNCompleteSendConnectInfo"
  1564. void DNCompleteSendConnectInfo(DIRECTNETOBJECT *const pdnObject,
  1565. CAsyncOp *const pAsyncOp)
  1566. {
  1567. //
  1568. // Update result of this send on the CONNECT operation parent
  1569. //
  1570. DNASSERT(pAsyncOp->GetParent() != NULL);
  1571. pAsyncOp->GetParent()->Lock();
  1572. if (pAsyncOp->GetResult() != DPN_OK)
  1573. {
  1574. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1575. //
  1576. // Clean up CONNECT response buffer from Host
  1577. //
  1578. if (pAsyncOp->GetParent()->GetRefCountBuffer())
  1579. {
  1580. pAsyncOp->GetParent()->GetRefCountBuffer()->Release();
  1581. pAsyncOp->GetParent()->SetRefCountBuffer( NULL );
  1582. }
  1583. }
  1584. pAsyncOp->GetParent()->Unlock();
  1585. //
  1586. // Clean up DirectNet object if this fails
  1587. //
  1588. if (pAsyncOp->GetResult() != DPN_OK)
  1589. {
  1590. CAsyncOp *pConnectParent;
  1591. pConnectParent = NULL;
  1592. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1593. pdnObject->dwFlags &= (~(DN_OBJECT_FLAG_CONNECTED
  1594. | DN_OBJECT_FLAG_CONNECTING
  1595. | DN_OBJECT_FLAG_HOST_CONNECTED));
  1596. if (pdnObject->pConnectParent)
  1597. {
  1598. pConnectParent = pdnObject->pConnectParent;
  1599. pdnObject->pConnectParent = NULL;
  1600. }
  1601. if( pdnObject->pIDP8ADevice )
  1602. {
  1603. IDirectPlay8Address_Release( pdnObject->pIDP8ADevice );
  1604. pdnObject->pIDP8ADevice = NULL;
  1605. }
  1606. if( pdnObject->pConnectAddress )
  1607. {
  1608. IDirectPlay8Address_Release( pdnObject->pConnectAddress );
  1609. pdnObject->pConnectAddress = NULL;
  1610. }
  1611. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1612. if (pConnectParent)
  1613. {
  1614. pConnectParent->Release();
  1615. pConnectParent = NULL;
  1616. }
  1617. DNASSERT(pConnectParent == NULL);
  1618. }
  1619. //
  1620. // Clean up op data
  1621. //
  1622. if (pAsyncOp->GetOpData())
  1623. {
  1624. MemoryBlockFree(pdnObject,pAsyncOp->GetOpData());
  1625. pAsyncOp->SetOpData( NULL );
  1626. }
  1627. }
  1628. #undef DPF_MODNAME
  1629. #define DPF_MODNAME "DNPerformDisconnect"
  1630. HRESULT DNPerformDisconnect(DIRECTNETOBJECT *const pdnObject,
  1631. CConnection *const pConnection,
  1632. const HANDLE hEndPt,
  1633. const BOOL fImmediate)
  1634. {
  1635. HRESULT hResultCode;
  1636. HANDLE hProtocol;
  1637. CAsyncOp *pAsyncOp;
  1638. CAsyncOp *pConnectParent;
  1639. DWORD dwProtocolFlags;
  1640. DPFX(DPFPREP, 6,"Parameters: pConnection [0x%p], hEndPt [0x%lx], fImmediate [%d]",pConnection,hEndPt,fImmediate);
  1641. DNASSERT(pdnObject != NULL);
  1642. pAsyncOp = NULL;
  1643. pConnectParent = NULL;
  1644. dwProtocolFlags = 0;
  1645. if (hEndPt == NULL)
  1646. {
  1647. DPFERR("Ignoring NULL endpoint");
  1648. hResultCode = DPN_OK;
  1649. goto Failure;
  1650. }
  1651. //
  1652. // Create AsyncOp for this operation
  1653. //
  1654. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  1655. {
  1656. DPFERR("Could not create AsyncOp");
  1657. DisplayDNError(0,hResultCode);
  1658. DNASSERT(FALSE);
  1659. goto Failure;
  1660. }
  1661. pAsyncOp->SetOpType( ASYNC_OP_DISCONNECT );
  1662. pAsyncOp->SetConnection( pConnection );
  1663. pAsyncOp->SetCannotCancel(); // Cannot cancel DISCONNECT's
  1664. //
  1665. // AddRef Protocol so that it won't go away until this completes
  1666. //
  1667. DNProtocolAddRef(pdnObject);
  1668. //
  1669. // If there is a connect parent op, and it's hResultCode is not DPN_OK,
  1670. // then a connect is failing for some reason, and to prevent it from
  1671. // completing before this disconnect is complete, we will set the connect parent
  1672. // as the parent of this disconnect.
  1673. //
  1674. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  1675. if (pdnObject->pConnectParent && (pdnObject->pConnectParent->GetResult() != DPN_OK))
  1676. {
  1677. pdnObject->pConnectParent->AddRef();
  1678. pConnectParent = pdnObject->pConnectParent;
  1679. }
  1680. if ((pdnObject->dwFlags & DN_OBJECT_FLAG_CLOSING_IMMEDIATE) || (fImmediate))
  1681. {
  1682. //
  1683. // If Close() was called with DPNCLOSE_IMMEDIATE then DN_OBJET_FLAG_CLOSING_IMMEDIATE will be set
  1684. // and all disconnects should be done in immediate mode
  1685. //
  1686. dwProtocolFlags |= DN_DISCONNECTFLAGS_IMMEDIATE;
  1687. }
  1688. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  1689. if (pConnectParent)
  1690. {
  1691. pAsyncOp->MakeChild( pConnectParent );
  1692. pConnectParent->Release();
  1693. pConnectParent = NULL;
  1694. }
  1695. //
  1696. // Set connection as INVALID so that no other operations may use it anymore
  1697. //
  1698. pConnection->Lock();
  1699. pConnection->SetStatus( INVALID );
  1700. pConnection->Unlock();
  1701. //
  1702. // Perform DISCONNECT
  1703. //
  1704. pAsyncOp->AddRef();
  1705. hResultCode = DNPDisconnectEndPoint(pdnObject->pdnProtocolData,
  1706. hEndPt,
  1707. static_cast<void*>(pAsyncOp),
  1708. &hProtocol,
  1709. dwProtocolFlags);
  1710. if ((hResultCode != DPN_OK) && (hResultCode != DPNERR_PENDING))
  1711. {
  1712. DPFERR("Could not issue DISCONNECT");
  1713. DisplayDNError(0,hResultCode);
  1714. DNProtocolRelease(pdnObject);
  1715. pAsyncOp->Release();
  1716. goto Failure;
  1717. }
  1718. pAsyncOp->Lock();
  1719. pAsyncOp->SetProtocolHandle( hProtocol );
  1720. pAsyncOp->Unlock();
  1721. pAsyncOp->Release();
  1722. pAsyncOp = NULL;
  1723. Exit:
  1724. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  1725. return(hResultCode);
  1726. Failure:
  1727. if (pAsyncOp)
  1728. {
  1729. pAsyncOp->Release();
  1730. pAsyncOp = NULL;
  1731. }
  1732. if (pConnectParent)
  1733. {
  1734. pConnectParent->Release();
  1735. pConnectParent = NULL;
  1736. }
  1737. goto Exit;
  1738. }
  1739. #undef DPF_MODNAME
  1740. #define DPF_MODNAME "DNCompleteAsyncHandle"
  1741. void DNCompleteAsyncHandle(DIRECTNETOBJECT *const pdnObject,
  1742. CAsyncOp *const pAsyncOp)
  1743. {
  1744. DNASSERT(pdnObject != NULL);
  1745. DNASSERT(pAsyncOp != NULL);
  1746. DNUserAsyncComplete(pdnObject,
  1747. pAsyncOp->GetHandle(),
  1748. pAsyncOp->GetContext(),
  1749. pAsyncOp->GetResult() );
  1750. }
  1751. #undef DPF_MODNAME
  1752. #define DPF_MODNAME "DNCompleteSendHandle"
  1753. void DNCompleteSendHandle(DIRECTNETOBJECT *const pdnObject,
  1754. CAsyncOp *const pAsyncOp)
  1755. {
  1756. DNASSERT(pdnObject != NULL);
  1757. DNASSERT(pAsyncOp != NULL);
  1758. DNUserSendComplete( pdnObject,
  1759. pAsyncOp->GetHandle(),
  1760. pAsyncOp->GetContext(),
  1761. pAsyncOp->GetStartTime(),
  1762. pAsyncOp->GetResult(),
  1763. pAsyncOp->GetFirstFrameRTT(),
  1764. pAsyncOp->GetFirstFrameRetryCount());
  1765. }
  1766. #undef DPF_MODNAME
  1767. #define DPF_MODNAME "DNCompleteSendAsyncOp"
  1768. void DNCompleteSendAsyncOp(DIRECTNETOBJECT *const pdnObject,
  1769. CAsyncOp *const pAsyncOp)
  1770. {
  1771. DNASSERT(pdnObject != NULL);
  1772. DNASSERT(pAsyncOp != NULL);
  1773. //
  1774. // Update outstanding queue info - this is only performed on children or stand-alone ops (i.e. no parent)
  1775. //
  1776. if ( !pAsyncOp->IsParent() && (pAsyncOp->GetConnection() != NULL))
  1777. {
  1778. DN_SEND_OP_DATA *pSendOpData = NULL;
  1779. if (pAsyncOp->IsUseParentOpData())
  1780. {
  1781. if (pAsyncOp->IsChild() && pAsyncOp->GetParent())
  1782. {
  1783. pSendOpData = pAsyncOp->GetParent()->GetLocalSendOpData();
  1784. }
  1785. }
  1786. else
  1787. {
  1788. pSendOpData = pAsyncOp->GetLocalSendOpData();
  1789. }
  1790. if (pSendOpData && pSendOpData->dwMsgId == DN_MSG_USER_SEND)
  1791. {
  1792. DWORD dw;
  1793. DNASSERT( pAsyncOp->GetConnection() != NULL );
  1794. pAsyncOp->GetConnection()->Lock();
  1795. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  1796. {
  1797. if (pAsyncOp->GetOpFlags() & DN_SENDFLAGS_HIGH_PRIORITY)
  1798. {
  1799. pAsyncOp->GetConnection()->RemoveFromHighQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  1800. }
  1801. else if (pAsyncOp->GetOpFlags() & DN_SENDFLAGS_LOW_PRIORITY)
  1802. {
  1803. pAsyncOp->GetConnection()->RemoveFromLowQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  1804. }
  1805. else
  1806. {
  1807. pAsyncOp->GetConnection()->RemoveFromNormalQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  1808. }
  1809. }
  1810. pAsyncOp->GetConnection()->Unlock();
  1811. }
  1812. }
  1813. //
  1814. // Clean up
  1815. //
  1816. if (!pAsyncOp->IsUseParentOpData())
  1817. {
  1818. DN_SEND_OP_DATA *pSendOpData;
  1819. DWORD dw;
  1820. pSendOpData = pAsyncOp->GetLocalSendOpData();
  1821. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  1822. {
  1823. pSendOpData->BufferDesc[dw].dwBufferSize = 0;
  1824. pSendOpData->BufferDesc[dw].pBufferData = NULL;
  1825. }
  1826. pSendOpData->dwMsgId = 0;
  1827. pSendOpData->dwNumBuffers = 0;
  1828. }
  1829. if ( pAsyncOp->IsChild() )
  1830. {
  1831. DNASSERT(pAsyncOp->GetParent() != NULL);
  1832. pAsyncOp->GetParent()->Lock();
  1833. //
  1834. // Save HRESULT. Overwrite the parent's error while it's not DPN_OK.
  1835. //
  1836. if (pAsyncOp->GetParent()->GetResult() != DPN_OK)
  1837. {
  1838. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1839. }
  1840. //
  1841. // Release parent Handle if it exists
  1842. //
  1843. if (pAsyncOp->GetParent()->GetOpType() == ASYNC_OP_USER_HANDLE)
  1844. {
  1845. if (SUCCEEDED(pdnObject->HandleTable.Destroy( pAsyncOp->GetParent()->GetHandle(), NULL )))
  1846. {
  1847. // Release the HandleTable reference
  1848. pAsyncOp->GetParent()->Release();
  1849. }
  1850. }
  1851. pAsyncOp->GetParent()->Unlock();
  1852. }
  1853. }
  1854. #undef DPF_MODNAME
  1855. #define DPF_MODNAME "DNCompleteRequest"
  1856. void DNCompleteRequest(DIRECTNETOBJECT *const pdnObject,
  1857. CAsyncOp *const pAsyncOp)
  1858. {
  1859. DWORD dw;
  1860. DN_SEND_OP_DATA *pSendOpData;
  1861. DNASSERT(pdnObject != NULL);
  1862. DNASSERT(pAsyncOp != NULL);
  1863. //
  1864. // Clean up op data
  1865. //
  1866. pSendOpData = pAsyncOp->GetLocalSendOpData();
  1867. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  1868. {
  1869. pSendOpData->BufferDesc[dw].dwBufferSize = 0;
  1870. pSendOpData->BufferDesc[dw].pBufferData = NULL;
  1871. }
  1872. pSendOpData->dwMsgId = 0;
  1873. pSendOpData->dwNumBuffers = 0;
  1874. pSendOpData = NULL;
  1875. //
  1876. // If the parent exists, copy the result up, and then remove them from the HandleTable
  1877. //
  1878. if (pAsyncOp->GetParent() != NULL)
  1879. {
  1880. pAsyncOp->GetParent()->Lock();
  1881. pAsyncOp->GetParent()->SetResult( pAsyncOp->GetResult() );
  1882. pAsyncOp->GetParent()->Unlock();
  1883. if (SUCCEEDED(pdnObject->HandleTable.Destroy( pAsyncOp->GetParent()->GetHandle(), NULL )))
  1884. {
  1885. // Release the HandleTable reference
  1886. pAsyncOp->GetParent()->Release();
  1887. }
  1888. }
  1889. }
  1890. #undef DPF_MODNAME
  1891. #define DPF_MODNAME "DNCompleteSendRequest"
  1892. void DNCompleteSendRequest(DIRECTNETOBJECT *const pdnObject,
  1893. CAsyncOp *const pAsyncOp)
  1894. {
  1895. DNASSERT(pdnObject != NULL);
  1896. DNASSERT(pAsyncOp != NULL);
  1897. DNASSERT(pAsyncOp->GetParent() != NULL);
  1898. if (pAsyncOp->GetResult() != DPN_OK)
  1899. {
  1900. //
  1901. // If this operation was cancelled or was NOT internal (i.e. complete on process send)
  1902. // remove the parent (RequestChild) AsyncOp from the HandleTable and from the request list
  1903. //
  1904. if ((pAsyncOp->GetResult() == DPNERR_USERCANCEL) || !pAsyncOp->IsInternal())
  1905. {
  1906. DNASSERT(pAsyncOp->GetParent()->GetHandle() != 0);
  1907. DNASSERT(pAsyncOp->GetParent()->GetOpType() == ASYNC_OP_REQUEST);
  1908. DNEnterCriticalSection(&pdnObject->csActiveList);
  1909. pAsyncOp->GetParent()->m_bilinkActiveList.RemoveFromList();
  1910. DNLeaveCriticalSection(&pdnObject->csActiveList);
  1911. if (SUCCEEDED(pdnObject->HandleTable.Destroy( pAsyncOp->GetParent()->GetHandle(), NULL )))
  1912. {
  1913. // Release the HandleTable reference
  1914. pAsyncOp->GetParent()->Release();
  1915. }
  1916. }
  1917. }
  1918. else
  1919. {
  1920. //
  1921. // Mark this operation as PLAYERLOST. If we do get an operation complete message,
  1922. // this result will get overwritten.
  1923. //
  1924. pAsyncOp->GetParent()->Lock();
  1925. if (pAsyncOp->GetParent()->GetResult() == DPNERR_GENERIC)
  1926. {
  1927. pAsyncOp->GetParent()->SetResult( DPNERR_PLAYERLOST );
  1928. }
  1929. pAsyncOp->GetParent()->Unlock();
  1930. }
  1931. }
  1932. // DNSendMessage
  1933. //
  1934. // Send structured message to given endpoint. Internally generated sends have a header
  1935. // associated with them. The first few bytes of this header are a signature, indicating
  1936. // an internal message. User generated sends do not have a header, unless they contain
  1937. // the signature. In this case, the user message is escaped with a header indicating this.
  1938. //
  1939. // For internal sends:
  1940. // - Create message header (dwMsgId,dwParam1,dwParam2)
  1941. // - Send message header and supplied data buffer (lpBuffDesc)
  1942. // - Save Async Operation info to be unwound when send completes
  1943. // - If pdnCountBuffer is specified, pBuffDesc may or may not point to its contents.
  1944. //
  1945. #undef DPF_MODNAME
  1946. #define DPF_MODNAME "DNSendMessage"
  1947. HRESULT DNSendMessage(DIRECTNETOBJECT *const pdnObject,
  1948. CConnection *const pConnection, // Connection to send to
  1949. const DWORD dwMsgId, // Message ID
  1950. const DPNID dpnidTarget, // Target of this send (may be NULL)
  1951. const DPN_BUFFER_DESC *const pdnBufferDesc, // Array of buffer desc's
  1952. const DWORD cBufferDesc, // Number of buffer desc's
  1953. CRefCountBuffer *const pRefCountBuffer, // RefCountBuffer (may be NULL)
  1954. const DWORD dwTimeOut, // Time out
  1955. const DWORD dwSendFlags, // Send flags
  1956. CAsyncOp *const pParent, // Parent of this send
  1957. CAsyncOp **const ppAsyncOp) // CAsyncOp created for this send
  1958. {
  1959. DWORD dw;
  1960. HANDLE hProtocol;
  1961. HRESULT hResultCode;
  1962. HANDLE hEndPt;
  1963. CAsyncOp *pAsyncOp;
  1964. DN_SEND_OP_DATA *pSendOpData;
  1965. CCallbackThread CallbackThread;
  1966. DWORD dwStart;
  1967. DPFX(DPFPREP, 6,"Parameters: pConnection [0x%p], dwMsgId [0x%x], dpnidTarget [0x%x], pdnBufferDesc [0x%p], cBufferDesc [%ld], pRefCountBuffer [0x%p], dwTimeOut [%ld], dwSendFlags [0x%lx], pParent [0x%p], ppAsyncOp [0x%p]",
  1968. pConnection,dwMsgId,dpnidTarget,pdnBufferDesc,cBufferDesc,pRefCountBuffer,dwTimeOut,dwSendFlags,pParent,ppAsyncOp);
  1969. DNASSERT(pdnObject != NULL);
  1970. DNASSERT(pConnection != NULL);
  1971. pAsyncOp = NULL;
  1972. pSendOpData = NULL;
  1973. CallbackThread.Initialize();
  1974. //
  1975. // Create AsyncOp for SEND
  1976. //
  1977. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  1978. {
  1979. DPFERR("Could not create AsyncOp");
  1980. DisplayDNError(0,hResultCode);
  1981. goto Failure;
  1982. }
  1983. pAsyncOp->SetOpType( ASYNC_OP_SEND );
  1984. pAsyncOp->SetDPNID( dpnidTarget );
  1985. pAsyncOp->SetRefCountBuffer( pRefCountBuffer );
  1986. pAsyncOp->SetStartTime( GETTIMESTAMP() );
  1987. if (dwMsgId & DN_MSG_INTERNAL)
  1988. {
  1989. //
  1990. // We have to set this early (before it's in the active list or a child) so that it won't
  1991. // be prematurely cancelled (before we get a chance to mark it INTERNAL).
  1992. //
  1993. pAsyncOp->SetInternal();
  1994. }
  1995. //
  1996. // Make child if parent was supplied
  1997. //
  1998. if (pParent)
  1999. {
  2000. pParent->Lock();
  2001. if (pParent->IsCancelled())
  2002. {
  2003. pParent->Unlock();
  2004. pAsyncOp->SetResult( DPNERR_USERCANCEL );
  2005. hResultCode = DPNERR_USERCANCEL;
  2006. goto Failure;
  2007. }
  2008. pAsyncOp->MakeChild( pParent );
  2009. pParent->Unlock();
  2010. }
  2011. //
  2012. // Add to active async op list
  2013. //
  2014. DNEnterCriticalSection(&pdnObject->csActiveList);
  2015. pAsyncOp->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkActiveList);
  2016. DNLeaveCriticalSection(&pdnObject->csActiveList);
  2017. //
  2018. // Set-up SEND op data block
  2019. //
  2020. pSendOpData = pAsyncOp->GetLocalSendOpData();
  2021. pSendOpData->dwMsgId = dwMsgId;
  2022. if (dwMsgId & DN_MSG_INTERNAL)
  2023. {
  2024. #ifndef DPNBUILD_NOVOICE
  2025. if (dwMsgId == DN_MSG_INTERNAL_VOICE_SEND)
  2026. {
  2027. DNASSERT(pdnBufferDesc != NULL);
  2028. dwStart = 0;
  2029. pAsyncOp->SetOpFlags( dwSendFlags | DN_SENDFLAGS_SET_USER_FLAG | DN_SENDFLAGS_SET_USER_FLAG_TWO | DN_SENDFLAGS_COALESCE );
  2030. }
  2031. else
  2032. #endif // DPNBUILD_NOVOICE
  2033. {
  2034. pSendOpData->BufferDesc[0].pBufferData = reinterpret_cast<BYTE*>( &(pSendOpData->dwMsgId) );
  2035. pSendOpData->BufferDesc[0].dwBufferSize = sizeof( DWORD );
  2036. dwStart = 1;
  2037. pAsyncOp->SetOpFlags( dwSendFlags | DN_SENDFLAGS_SET_USER_FLAG | DN_SENDFLAGS_COALESCE );
  2038. }
  2039. }
  2040. else
  2041. {
  2042. DNASSERT(pdnBufferDesc != NULL);
  2043. dwStart = 0;
  2044. pAsyncOp->SetOpFlags( dwSendFlags );
  2045. }
  2046. for ( dw = 0 ; dw < cBufferDesc ; dw++ )
  2047. {
  2048. pSendOpData->BufferDesc[dwStart+dw].pBufferData = pdnBufferDesc[dw].pBufferData;
  2049. pSendOpData->BufferDesc[dwStart+dw].dwBufferSize = pdnBufferDesc[dw].dwBufferSize;
  2050. }
  2051. pSendOpData->dwNumBuffers = cBufferDesc + dwStart;
  2052. if ((hResultCode = pConnection->GetEndPt(&hEndPt,&CallbackThread)) != DPN_OK)
  2053. {
  2054. DPFERR("Could not get end point from connection");
  2055. DisplayDNError(0,hResultCode);
  2056. hResultCode = DPNERR_CONNECTIONLOST; // re-map this
  2057. goto Failure;
  2058. }
  2059. pAsyncOp->SetConnection( pConnection );
  2060. if (hEndPt == NULL) // Message for local player - Put on local message queue
  2061. {
  2062. //
  2063. // INTERNAL Message
  2064. //
  2065. DPFX(DPFPREP, 5,"INTERNAL Message");
  2066. //
  2067. // AddRef Protocol as SendComplete will release this reference
  2068. //
  2069. DNProtocolAddRef(pdnObject);
  2070. pAsyncOp->AddRef();
  2071. hResultCode = DNWTSendInternal( pdnObject,
  2072. pAsyncOp );
  2073. pConnection->ReleaseEndPt(&CallbackThread);
  2074. DNASSERT( hResultCode == DPNERR_PENDING );
  2075. }
  2076. else
  2077. {
  2078. //
  2079. // EXTERNAL Message
  2080. //
  2081. DPFX(DPFPREP, 5,"EXTERNAL Message");
  2082. //
  2083. // AddRef Protocol so that it won't go away until this completes
  2084. //
  2085. DNProtocolAddRef(pdnObject);
  2086. pAsyncOp->AddRef();
  2087. hResultCode = DNPSendData( pdnObject->pdnProtocolData,
  2088. hEndPt,
  2089. pSendOpData->dwNumBuffers,
  2090. &pSendOpData->BufferDesc[0],
  2091. dwTimeOut,
  2092. pAsyncOp->GetOpFlags(),
  2093. reinterpret_cast<void*>(pAsyncOp),
  2094. &hProtocol);
  2095. pConnection->ReleaseEndPt(&CallbackThread);
  2096. if (hResultCode != DPNERR_PENDING)
  2097. {
  2098. if (hResultCode != DPN_OK)
  2099. {
  2100. DPFERR("Could not send data");
  2101. DisplayDNError(0,hResultCode);
  2102. }
  2103. pAsyncOp->Release();
  2104. DNProtocolRelease(pdnObject);
  2105. goto Failure;
  2106. }
  2107. pAsyncOp->Lock();
  2108. if (pAsyncOp->IsCancelled() && !pAsyncOp->IsComplete())
  2109. {
  2110. HRESULT hrCancel;
  2111. pAsyncOp->Unlock();
  2112. DPFX(DPFPREP, 7,"Operation marked for cancel");
  2113. if ((hrCancel = DNPCancelCommand(pdnObject->pdnProtocolData,hProtocol)) == DPN_OK)
  2114. {
  2115. hResultCode = DPNERR_USERCANCEL;
  2116. goto Failure;
  2117. }
  2118. DPFERR("Could not cancel operation");
  2119. DisplayDNError(0,hrCancel);
  2120. pAsyncOp->Lock();
  2121. }
  2122. pAsyncOp->SetProtocolHandle( hProtocol );
  2123. pAsyncOp->Unlock();
  2124. }
  2125. DNASSERT( hResultCode == DPNERR_PENDING );
  2126. //
  2127. // Update outstanding queue info for USER sends (now guaranteed to be cleaned up by the completion)
  2128. // and set completion
  2129. //
  2130. if (!(dwMsgId & DN_MSG_INTERNAL))
  2131. {
  2132. pConnection->Lock();
  2133. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  2134. {
  2135. if (dwSendFlags & DN_SENDFLAGS_HIGH_PRIORITY)
  2136. {
  2137. pConnection->AddToHighQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2138. }
  2139. else if (dwSendFlags & DN_SENDFLAGS_LOW_PRIORITY)
  2140. {
  2141. pConnection->AddToLowQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2142. }
  2143. else
  2144. {
  2145. pConnection->AddToNormalQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2146. }
  2147. }
  2148. pConnection->Unlock();
  2149. }
  2150. pAsyncOp->SetCompletion( DNCompleteSendAsyncOp );
  2151. pSendOpData = NULL;
  2152. if (ppAsyncOp != NULL)
  2153. {
  2154. pAsyncOp->AddRef();
  2155. *ppAsyncOp = pAsyncOp;
  2156. }
  2157. pAsyncOp->Release();
  2158. pAsyncOp = NULL;
  2159. Exit:
  2160. CallbackThread.Deinitialize();
  2161. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  2162. return(hResultCode);
  2163. Failure:
  2164. if (pAsyncOp)
  2165. {
  2166. DNEnterCriticalSection(&pdnObject->csActiveList);
  2167. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  2168. DNLeaveCriticalSection(&pdnObject->csActiveList);
  2169. pAsyncOp->Release();
  2170. pAsyncOp = NULL;
  2171. }
  2172. if (pSendOpData)
  2173. {
  2174. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  2175. {
  2176. pSendOpData->BufferDesc[dw].dwBufferSize = 0;
  2177. pSendOpData->BufferDesc[dw].pBufferData = NULL;
  2178. }
  2179. pSendOpData->dwMsgId = 0;
  2180. pSendOpData->dwNumBuffers = 0;
  2181. pSendOpData = NULL;
  2182. }
  2183. goto Exit;
  2184. }
  2185. #undef DPF_MODNAME
  2186. #define DPF_MODNAME "DNSendGroupMessage"
  2187. HRESULT DNSendGroupMessage(DIRECTNETOBJECT *const pdnObject,
  2188. CNameTableEntry *const pGroup,
  2189. const DWORD dwMsgId,
  2190. const DPN_BUFFER_DESC *const pdnBufferDesc,
  2191. const DWORD cBufferDesc,
  2192. CRefCountBuffer *const pRefCountBuffer,
  2193. const DWORD dwTimeOut,
  2194. const DWORD dwSendFlags,
  2195. const BOOL fNoLoopBack,
  2196. const BOOL fRequest,
  2197. CAsyncOp *const pParent,
  2198. CAsyncOp **const ppGroupSendParent)
  2199. {
  2200. HRESULT hResultCode;
  2201. CAsyncOp *pAsyncOp;
  2202. CAsyncOp *pGroupSendParent;
  2203. CBilink *pBilink;
  2204. CConnection *pConnection;
  2205. CGroupConnection *pGroupConnection;
  2206. DN_GROUP_SEND_OP *pSendOp;
  2207. DN_GROUP_SEND_OP *pTemp;
  2208. DPNID dpnidLocalPlayer;
  2209. DPFX(DPFPREP, 4,"Parameters: pGroup [0x%p], dwMsgId [0x%lx], pdnBufferDesc [0x%p], pRefCountBuffer [0x%p], dwTimeOut [%ld], dwSendFlags [0x%lx], fNoLoopBack [%ld], fRequest [%ld], pParent [0x%p], ppGroupSendParent [0x%p]",
  2210. pGroup,dwMsgId,pdnBufferDesc,pRefCountBuffer,dwTimeOut,dwSendFlags,fNoLoopBack,fRequest,pParent,ppGroupSendParent);
  2211. DNASSERT(pdnObject != NULL);
  2212. DNASSERT(pGroup != NULL);
  2213. DNASSERT(ppGroupSendParent != NULL);
  2214. pAsyncOp = NULL;
  2215. pGroupSendParent = NULL;
  2216. pConnection = NULL;
  2217. pGroupConnection = NULL;
  2218. if (fNoLoopBack)
  2219. {
  2220. CNameTableEntry *pLocalPlayer;
  2221. pLocalPlayer = NULL;
  2222. if ((hResultCode = pdnObject->NameTable.GetLocalPlayerRef( &pLocalPlayer )) != DPN_OK)
  2223. {
  2224. DPFERR("Could not get local player reference");
  2225. DisplayDNError(0,hResultCode);
  2226. goto Failure;
  2227. }
  2228. dpnidLocalPlayer = pLocalPlayer->GetDPNID();
  2229. pLocalPlayer->Release();
  2230. pLocalPlayer = NULL;
  2231. }
  2232. else
  2233. {
  2234. dpnidLocalPlayer = 0;
  2235. }
  2236. //
  2237. // Create group send target list
  2238. //
  2239. pSendOp = NULL;
  2240. pGroup->Lock();
  2241. pBilink = pGroup->m_bilinkConnections.GetNext();
  2242. while (pBilink != &pGroup->m_bilinkConnections)
  2243. {
  2244. pGroupConnection = CONTAINING_OBJECT(pBilink,CGroupConnection,m_bilink);
  2245. if ((hResultCode = pGroupConnection->GetConnectionRef( &pConnection )) == DPN_OK)
  2246. {
  2247. //
  2248. // We will only use CONNECTED connections (not CONNECTING,DISCONNECTING, or INVALID ones)
  2249. //
  2250. if (pConnection->IsConnected())
  2251. {
  2252. if ((!fNoLoopBack) || (pConnection->GetDPNID() != dpnidLocalPlayer))
  2253. {
  2254. //
  2255. // Save this connection
  2256. //
  2257. pTemp = static_cast<DN_GROUP_SEND_OP*>(MemoryBlockAlloc(pdnObject,sizeof(DN_GROUP_SEND_OP)));
  2258. if (pTemp == NULL)
  2259. {
  2260. pGroup->Unlock();
  2261. DPFERR("Could not create DN_GROUP_SEND_OP");
  2262. hResultCode = DPNERR_OUTOFMEMORY;
  2263. goto Failure;
  2264. }
  2265. pConnection->AddRef();
  2266. pTemp->pConnection = pConnection;
  2267. pTemp->pNext = pSendOp;
  2268. pSendOp = pTemp;
  2269. }
  2270. }
  2271. pConnection->Release();
  2272. pConnection = NULL;
  2273. }
  2274. pBilink = pBilink->GetNext();
  2275. }
  2276. pGroup->Unlock();
  2277. //
  2278. // Create group send parent
  2279. //
  2280. if ((hResultCode = DNCreateSendParent( pdnObject,
  2281. dwMsgId,
  2282. pdnBufferDesc,
  2283. cBufferDesc,
  2284. dwSendFlags,
  2285. &pGroupSendParent)) != DPN_OK)
  2286. {
  2287. DPFERR("Could not create SEND parent");
  2288. DisplayDNError(0,hResultCode);
  2289. goto Failure;
  2290. }
  2291. if (pRefCountBuffer)
  2292. {
  2293. pGroupSendParent->SetRefCountBuffer( pRefCountBuffer );
  2294. }
  2295. pGroupSendParent->SetDPNID( pGroup->GetDPNID() );
  2296. //
  2297. // Make child if parent specified
  2298. //
  2299. if (pParent)
  2300. {
  2301. pParent->Lock();
  2302. if (pParent->IsCancelled())
  2303. {
  2304. pParent->Unlock();
  2305. pGroupSendParent->SetResult( DPNERR_USERCANCEL );
  2306. hResultCode = DPNERR_USERCANCEL;
  2307. goto Failure;
  2308. }
  2309. pGroupSendParent->MakeChild( pParent );
  2310. pParent->Unlock();
  2311. }
  2312. //
  2313. // Preset error code on send parent to DPNSUCCESS_NORECEIVERS
  2314. //
  2315. pGroupSendParent->Lock();
  2316. if (pGroupSendParent->GetResult() == DPNERR_GENERIC)
  2317. {
  2318. pGroupSendParent->SetResult( DPNSUCCESS_NOPLAYERSINGROUP );
  2319. }
  2320. pGroupSendParent->Unlock();
  2321. //
  2322. // Traverse send list and perform sends
  2323. //
  2324. while (pSendOp)
  2325. {
  2326. if (fRequest)
  2327. {
  2328. hResultCode = DNPerformRequest( pdnObject,
  2329. DN_MSG_INTERNAL_REQ_PROCESS_COMPLETION,
  2330. pdnBufferDesc,
  2331. pSendOp->pConnection,
  2332. pGroupSendParent,
  2333. &pAsyncOp);
  2334. }
  2335. else
  2336. {
  2337. hResultCode = DNPerformChildSend( pdnObject,
  2338. pGroupSendParent,
  2339. pSendOp->pConnection,
  2340. dwTimeOut,
  2341. &pAsyncOp,
  2342. FALSE);
  2343. }
  2344. if (pAsyncOp != NULL)
  2345. {
  2346. pAsyncOp->SetDPNID( pSendOp->pConnection->GetDPNID() );
  2347. pAsyncOp->Release();
  2348. pAsyncOp = NULL;
  2349. }
  2350. //
  2351. // Destroy old group send op
  2352. //
  2353. pTemp = pSendOp;
  2354. pSendOp = pSendOp->pNext;
  2355. pTemp->pConnection->Release();
  2356. pTemp->pConnection = NULL;
  2357. MemoryBlockFree(pdnObject,pTemp);
  2358. }
  2359. //
  2360. // Pass back group send parent
  2361. //
  2362. *ppGroupSendParent = pGroupSendParent;
  2363. pGroupSendParent = NULL;
  2364. hResultCode = DPN_OK;
  2365. Exit:
  2366. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  2367. return(hResultCode);
  2368. Failure:
  2369. if (pAsyncOp)
  2370. {
  2371. pAsyncOp->Release();
  2372. pAsyncOp = NULL;
  2373. }
  2374. if (pGroupSendParent)
  2375. {
  2376. pGroupSendParent->Release();
  2377. pGroupSendParent = NULL;
  2378. }
  2379. if (pConnection)
  2380. {
  2381. pConnection->Release();
  2382. pConnection = NULL;
  2383. }
  2384. goto Exit;
  2385. }
  2386. #undef DPF_MODNAME
  2387. #define DPF_MODNAME "DNCreateSendParent"
  2388. HRESULT DNCreateSendParent(DIRECTNETOBJECT *const pdnObject,
  2389. const DWORD dwMsgId,
  2390. const DPN_BUFFER_DESC *const pdnBufferDesc,
  2391. const DWORD cBufferDesc,
  2392. const DWORD dwSendFlags,
  2393. CAsyncOp **const ppParent)
  2394. {
  2395. HRESULT hResultCode;
  2396. CAsyncOp *pAsyncOp;
  2397. DN_SEND_OP_DATA *pSendOpData;
  2398. DWORD dwStart;
  2399. DWORD dw;
  2400. DPFX(DPFPREP, 4,"Parameters: dwMsgId [0x%lx], pdnBufferDesc [0x%p], cBufferDesc [%ld], dwSendFlags [0x%lx], ppParent [0x%p]",
  2401. dwMsgId,pdnBufferDesc,cBufferDesc,dwSendFlags,ppParent);
  2402. DNASSERT(pdnObject != NULL);
  2403. DNASSERT(ppParent != NULL);
  2404. pAsyncOp = NULL;
  2405. pSendOpData = NULL;
  2406. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  2407. {
  2408. DPFERR("Could not create AsyncOp");
  2409. DisplayDNError(0,hResultCode);
  2410. goto Failure;
  2411. }
  2412. pAsyncOp->SetOpType( ASYNC_OP_SEND );
  2413. pAsyncOp->MakeParent();
  2414. pSendOpData = pAsyncOp->GetLocalSendOpData();
  2415. pSendOpData->dwMsgId = dwMsgId;
  2416. if (dwMsgId & DN_MSG_INTERNAL)
  2417. {
  2418. #ifndef DPNBUILD_NOVOICE
  2419. if (dwMsgId == DN_MSG_INTERNAL_VOICE_SEND)
  2420. {
  2421. DNASSERT(pdnBufferDesc != NULL);
  2422. dwStart = 0;
  2423. pAsyncOp->SetOpFlags( dwSendFlags | DN_SENDFLAGS_SET_USER_FLAG | DN_SENDFLAGS_SET_USER_FLAG_TWO | DN_SENDFLAGS_COALESCE );
  2424. }
  2425. else
  2426. #endif // DPNBUILD_NOVOICE
  2427. {
  2428. pSendOpData->BufferDesc[0].pBufferData = reinterpret_cast<BYTE*>( &(pSendOpData->dwMsgId) );
  2429. pSendOpData->BufferDesc[0].dwBufferSize = sizeof( DWORD );
  2430. dwStart = 1;
  2431. pAsyncOp->SetOpFlags( dwSendFlags | DN_SENDFLAGS_SET_USER_FLAG | DN_SENDFLAGS_COALESCE );
  2432. }
  2433. }
  2434. else
  2435. {
  2436. DNASSERT(pdnBufferDesc != NULL);
  2437. dwStart = 0;
  2438. pAsyncOp->SetOpFlags( dwSendFlags );
  2439. }
  2440. for ( dw = 0 ; dw < cBufferDesc ; dw++ )
  2441. {
  2442. pSendOpData->BufferDesc[dwStart+dw].pBufferData = pdnBufferDesc[dw].pBufferData;
  2443. pSendOpData->BufferDesc[dwStart+dw].dwBufferSize = pdnBufferDesc[dw].dwBufferSize;
  2444. }
  2445. pSendOpData->dwNumBuffers = cBufferDesc + dwStart;
  2446. //
  2447. // Completion
  2448. //
  2449. pAsyncOp->SetCompletion( DNCompleteSendAsyncOp );
  2450. pSendOpData = NULL;
  2451. *ppParent = pAsyncOp;
  2452. pAsyncOp = NULL;
  2453. Exit:
  2454. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  2455. return(hResultCode);
  2456. Failure:
  2457. if (pAsyncOp)
  2458. {
  2459. pAsyncOp->Release();
  2460. pAsyncOp = NULL;
  2461. }
  2462. if (pSendOpData)
  2463. {
  2464. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  2465. {
  2466. pSendOpData->BufferDesc[dw].dwBufferSize = 0;
  2467. pSendOpData->BufferDesc[dw].pBufferData = NULL;
  2468. }
  2469. pSendOpData->dwMsgId = 0;
  2470. pSendOpData->dwNumBuffers = 0;
  2471. pSendOpData = NULL;
  2472. }
  2473. goto Exit;
  2474. }
  2475. #undef DPF_MODNAME
  2476. #define DPF_MODNAME "DNPerformChildSend"
  2477. HRESULT DNPerformChildSend(DIRECTNETOBJECT *const pdnObject,
  2478. CAsyncOp *const pParent,
  2479. CConnection *const pConnection,
  2480. const DWORD dwTimeOut,
  2481. CAsyncOp **const ppChild,
  2482. const BOOL fInternal)
  2483. {
  2484. HRESULT hResultCode;
  2485. DWORD dw;
  2486. CAsyncOp *pAsyncOp;
  2487. DN_SEND_OP_DATA *pSendOpData;
  2488. HANDLE hEndPt;
  2489. HANDLE hProtocol;
  2490. CCallbackThread CallbackThread;
  2491. DPFX(DPFPREP, 4,"Parameters: pParent [0x%p], pConnection [0x%p], dwTimeOut [%ld]",
  2492. pParent,pConnection,dwTimeOut);
  2493. DNASSERT(pdnObject != NULL);
  2494. DNASSERT(pParent != NULL);
  2495. DNASSERT(pConnection != NULL);
  2496. pAsyncOp = NULL;
  2497. CallbackThread.Initialize();
  2498. if ((hResultCode = AsyncOpNew(pdnObject,&pAsyncOp)) != DPN_OK)
  2499. {
  2500. DPFERR("Could not create AsyncOp");
  2501. DisplayDNError(0,hResultCode);
  2502. goto Failure;
  2503. }
  2504. pAsyncOp->SetOpType( ASYNC_OP_SEND );
  2505. pAsyncOp->SetCompletion( DNCompleteSendAsyncOp );
  2506. pAsyncOp->SetOpFlags( pParent->GetOpFlags() );
  2507. if (fInternal)
  2508. {
  2509. pAsyncOp->SetInternal();
  2510. }
  2511. pParent->Lock();
  2512. if (pParent->IsCancelled())
  2513. {
  2514. pParent->Unlock();
  2515. pAsyncOp->SetResult( DPNERR_USERCANCEL );
  2516. hResultCode = DPNERR_USERCANCEL;
  2517. goto Failure;
  2518. }
  2519. pAsyncOp->MakeChild( pParent );
  2520. pParent->Unlock();
  2521. //
  2522. // Add to active async op list
  2523. //
  2524. DNEnterCriticalSection(&pdnObject->csActiveList);
  2525. pAsyncOp->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkActiveList);
  2526. DNLeaveCriticalSection(&pdnObject->csActiveList);
  2527. //
  2528. // Get parent's send op data block
  2529. //
  2530. pSendOpData = pParent->GetLocalSendOpData();
  2531. //
  2532. // Save connection and get end point
  2533. //
  2534. if ((hResultCode = pConnection->GetEndPt(&hEndPt,&CallbackThread)) != DPN_OK)
  2535. {
  2536. DPFERR("Could not retrieve EndPt");
  2537. DisplayDNError(0,hResultCode);
  2538. goto Failure;
  2539. }
  2540. pAsyncOp->SetConnection( pConnection );
  2541. //
  2542. // Update outstanding queue info (now guaranteed to be cleaned up by the completion once UseParentOpData is set)
  2543. //
  2544. if (pSendOpData->dwMsgId == DN_MSG_USER_SEND)
  2545. {
  2546. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  2547. {
  2548. pConnection->Lock();
  2549. if (pAsyncOp->GetOpFlags() & DN_SENDFLAGS_HIGH_PRIORITY)
  2550. {
  2551. pConnection->AddToHighQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2552. }
  2553. else if (pAsyncOp->GetOpFlags() & DN_SENDFLAGS_LOW_PRIORITY)
  2554. {
  2555. pConnection->AddToLowQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2556. }
  2557. else
  2558. {
  2559. pConnection->AddToNormalQueue( pSendOpData->BufferDesc[dw].dwBufferSize );
  2560. }
  2561. pConnection->Unlock();
  2562. }
  2563. }
  2564. pAsyncOp->SetUseParentOpData();
  2565. if (hEndPt == NULL)
  2566. {
  2567. //
  2568. // INTERNAL Message
  2569. //
  2570. DPFX(DPFPREP, 5,"INTERNAL Message");
  2571. //
  2572. // AddRef Protocol as SendComplete will release this reference
  2573. //
  2574. DNProtocolAddRef(pdnObject);
  2575. pAsyncOp->AddRef();
  2576. hResultCode = DNWTSendInternal( pdnObject,
  2577. pAsyncOp );
  2578. pConnection->ReleaseEndPt(&CallbackThread);
  2579. #pragma BUGBUG(minara, "Handle out-of-memory case correctly")
  2580. DNASSERT(hResultCode == DPNERR_PENDING);
  2581. }
  2582. else
  2583. {
  2584. //
  2585. // EXTERNAL Message
  2586. //
  2587. DPFX(DPFPREP, 5,"EXTERNAL Message");
  2588. //
  2589. // AddRef Protocol so that it won't go away until this completes
  2590. //
  2591. DNProtocolAddRef(pdnObject);
  2592. pAsyncOp->AddRef();
  2593. hResultCode = DNPSendData( pdnObject->pdnProtocolData,
  2594. hEndPt,
  2595. pSendOpData->dwNumBuffers,
  2596. &pSendOpData->BufferDesc[0],
  2597. dwTimeOut,
  2598. pParent->GetOpFlags(),
  2599. reinterpret_cast<void*>(pAsyncOp),
  2600. &hProtocol);
  2601. pConnection->ReleaseEndPt(&CallbackThread);
  2602. if (hResultCode != DPNERR_PENDING)
  2603. {
  2604. if (hResultCode != DPN_OK)
  2605. {
  2606. DPFERR("SEND failed at Protocol layer");
  2607. DisplayDNError(0,hResultCode);
  2608. }
  2609. pAsyncOp->Release();
  2610. DNProtocolRelease(pdnObject);
  2611. goto Failure;
  2612. }
  2613. pAsyncOp->Lock();
  2614. if (pAsyncOp->IsCancelled() && !pAsyncOp->IsComplete())
  2615. {
  2616. HRESULT hrCancel;
  2617. pAsyncOp->Unlock();
  2618. DPFX(DPFPREP, 7,"Operation marked for cancel");
  2619. if ((hrCancel = DNPCancelCommand(pdnObject->pdnProtocolData,hProtocol)) == DPN_OK)
  2620. {
  2621. hResultCode = DPNERR_USERCANCEL;
  2622. goto Failure;
  2623. }
  2624. DPFERR("Could not cancel operation");
  2625. DisplayDNError(0,hrCancel);
  2626. pAsyncOp->Lock();
  2627. }
  2628. pAsyncOp->SetProtocolHandle( hProtocol );
  2629. pAsyncOp->Unlock();
  2630. }
  2631. //
  2632. // If the caller wants a reference on this operation, give it to them
  2633. //
  2634. if (ppChild != NULL)
  2635. {
  2636. pAsyncOp->AddRef();
  2637. *ppChild = pAsyncOp;
  2638. }
  2639. pAsyncOp->Release();
  2640. pAsyncOp = NULL;
  2641. Exit:
  2642. CallbackThread.Deinitialize();
  2643. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  2644. return(hResultCode);
  2645. Failure:
  2646. if (pAsyncOp)
  2647. {
  2648. DNEnterCriticalSection(&pdnObject->csActiveList);
  2649. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  2650. DNLeaveCriticalSection(&pdnObject->csActiveList);
  2651. pAsyncOp->Release();
  2652. pAsyncOp = NULL;
  2653. }
  2654. goto Exit;
  2655. }
  2656. // DN_ProcessInternalOperation
  2657. //
  2658. // Process an internal operation
  2659. //
  2660. #undef DPF_MODNAME
  2661. #define DPF_MODNAME "DN_ProcessInternalOperation"
  2662. HRESULT DNProcessInternalOperation(DIRECTNETOBJECT *const pdnObject,
  2663. const DWORD dwMsgId,
  2664. void *const pOpBuffer,
  2665. const DWORD dwOpBufferSize,
  2666. CConnection *const pConnection,
  2667. const HANDLE hProtocol,
  2668. CRefCountBuffer *const pRefCountBuffer)
  2669. {
  2670. HRESULT hResultCode;
  2671. CRefCountBuffer *pRCBuffer;
  2672. CWorkerJob *pWorkerJob;
  2673. BOOL fDecRunningOpCount;
  2674. DPFX(DPFPREP, 6,"Parameters: dwMsgId [0x%lx], pOpBuffer [0x%p], dwOpBufferSize [%ld], pConnection [0x%p], hProtocol [0x%p], pRefCountBuffer [0x%p]",
  2675. dwMsgId,pOpBuffer,dwOpBufferSize,pConnection,hProtocol,pRefCountBuffer);
  2676. hResultCode = DPN_OK;
  2677. pRCBuffer = NULL;
  2678. pWorkerJob = NULL;
  2679. fDecRunningOpCount = FALSE;
  2680. //
  2681. // We will track the number of running operations (i.e. threads running through this function).
  2682. // At the start of host migration, we will need to send the new host the latest name table version,
  2683. // so we will need to let running operations finish, before sending in the name table version.
  2684. // If another thread is waiting, we will not perform any operations (other than host migration)
  2685. //
  2686. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  2687. if ((pdnObject->dwFlags & DN_OBJECT_FLAG_HOST_MIGRATING_WAIT) && (dwMsgId != DN_MSG_INTERNAL_HOST_MIGRATE))
  2688. {
  2689. DPFX(DPFPREP,7,"Already waiting for running operations - ignoring this operation");
  2690. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  2691. hResultCode = DPN_OK;
  2692. goto Failure;
  2693. }
  2694. if (dwMsgId != DN_MSG_INTERNAL_HOST_MIGRATE)
  2695. {
  2696. pdnObject->dwRunningOpCount++;
  2697. fDecRunningOpCount = TRUE;
  2698. }
  2699. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  2700. switch(dwMsgId)
  2701. {
  2702. case DN_MSG_INTERNAL_PLAYER_CONNECT_INFO:
  2703. {
  2704. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_PLAYER_CONNECT_INFO");
  2705. if (DNVerifyPlayerConnectInfo(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2706. {
  2707. DPFERR("Invalid message - ignoring");
  2708. hResultCode = DPN_OK;
  2709. goto Failure;
  2710. }
  2711. //
  2712. // Perform validations and send name table to player
  2713. //
  2714. hResultCode = DNHostConnect1(pdnObject,pOpBuffer,dwOpBufferSize,pConnection);
  2715. hResultCode = DPN_OK; // Ignore return code
  2716. break;
  2717. }
  2718. case DN_MSG_INTERNAL_SEND_CONNECT_INFO:
  2719. {
  2720. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_SEND_CONNECT_INFO");
  2721. if (DNVerifyConnectInfo(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2722. {
  2723. DPFERR("Invalid message - ignoring");
  2724. hResultCode = DPN_OK;
  2725. goto Failure;
  2726. }
  2727. //
  2728. // We will pass this stage off to the worker thread since there is a threading
  2729. // issue, and we cannot keep the SP's thread which was passed up to here.
  2730. //
  2731. if ((hResultCode = RefCountBufferNew(pdnObject,dwOpBufferSize,MemoryBlockAlloc,MemoryBlockFree,&pRCBuffer)) != DPN_OK)
  2732. {
  2733. DPFERR("Could not allocate RefCountBuffer");
  2734. DisplayDNError(0,hResultCode);
  2735. DNASSERT(FALSE);
  2736. hResultCode = DPN_OK;
  2737. goto Failure;
  2738. }
  2739. memcpy(pRCBuffer->GetBufferAddress(),pOpBuffer,dwOpBufferSize);
  2740. if ((hResultCode = WorkerJobNew(pdnObject,&pWorkerJob)) != DPN_OK)
  2741. {
  2742. DPFERR("Could not create WorkerJob");
  2743. DisplayDNError(0,hResultCode);
  2744. DNASSERT(FALSE);
  2745. hResultCode = DPN_OK;
  2746. goto Failure;
  2747. }
  2748. pWorkerJob->SetJobType( WORKER_JOB_INSTALL_NAMETABLE );
  2749. pWorkerJob->SetConnection( pConnection );
  2750. pWorkerJob->SetRefCountBuffer( pRCBuffer );
  2751. DNQueueWorkerJob(pdnObject,pWorkerJob);
  2752. pWorkerJob = NULL;
  2753. pRCBuffer->Release();
  2754. pRCBuffer = NULL;
  2755. break;
  2756. }
  2757. case DN_MSG_INTERNAL_ACK_CONNECT_INFO:
  2758. {
  2759. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_ACK_CONNECT_INFO");
  2760. //
  2761. // No verification as there is no payload with this message
  2762. //
  2763. //
  2764. // Process connect info acknowledge by host
  2765. //
  2766. hResultCode = DNHostConnect2(pdnObject,pConnection); // Ignore errors
  2767. break;
  2768. }
  2769. case DN_MSG_INTERNAL_SEND_PLAYER_DNID:
  2770. {
  2771. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_SEND_PLAYER_DNID");
  2772. if (DNVerifySendPlayerDPNID(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2773. {
  2774. DPFERR("Invalid message - ignoring");
  2775. hResultCode = DPN_OK;
  2776. goto Failure;
  2777. }
  2778. //
  2779. // Send this player's DNID to the connecting player to enable name table entry
  2780. //
  2781. hResultCode = DNPlayerConnect1(pdnObject,pOpBuffer,pConnection); // Ignore errors
  2782. break;
  2783. }
  2784. case DN_MSG_INTERNAL_CONNECT_FAILED:
  2785. {
  2786. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_CONNECT_FAILED");
  2787. if (DNVerifyConnectFailed(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2788. {
  2789. DPFERR("Invalid message - ignoring");
  2790. hResultCode = DPN_OK;
  2791. goto Failure;
  2792. }
  2793. //
  2794. // Save reply buffer and clean up
  2795. //
  2796. DNConnectToHostFailed(pdnObject,pOpBuffer,dwOpBufferSize,pConnection);
  2797. hResultCode = DPN_OK;
  2798. break;
  2799. }
  2800. case DN_MSG_INTERNAL_INSTRUCT_CONNECT:
  2801. {
  2802. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_INSTRUCT_CONNECT");
  2803. if (DNVerifyInstructConnect(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2804. {
  2805. DPFERR("Invalid message - ignoring");
  2806. hResultCode = DPN_OK;
  2807. goto Failure;
  2808. }
  2809. hResultCode = DNNTAddOperation( pdnObject,
  2810. DN_MSG_INTERNAL_INSTRUCT_CONNECT,
  2811. pOpBuffer,
  2812. dwOpBufferSize,
  2813. hProtocol,
  2814. pConnection->GetSP());
  2815. break;
  2816. }
  2817. case DN_MSG_INTERNAL_INSTRUCTED_CONNECT_FAILED:
  2818. {
  2819. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_INSTRUCTED_CONNECT_FAILED");
  2820. if (DNVerifyInstructedConnectFailed(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2821. {
  2822. DPFERR("Invalid message - ignoring");
  2823. hResultCode = DPN_OK;
  2824. goto Failure;
  2825. }
  2826. hResultCode = DNHostDropPlayer( pdnObject,
  2827. pConnection->GetDPNID(),
  2828. pOpBuffer); // Ignore errors
  2829. break;
  2830. }
  2831. case DN_MSG_INTERNAL_CONNECT_ATTEMPT_FAILED:
  2832. {
  2833. UNALIGNED DN_INTERNAL_MESSAGE_CONNECT_ATTEMPT_FAILED *pInfo;
  2834. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_CONNECT_ATTEMPT_FAILED");
  2835. if (DNVerifyConnectAttemptFailed(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2836. {
  2837. DPFERR("Invalid message - ignoring");
  2838. hResultCode = DPN_OK;
  2839. goto Failure;
  2840. }
  2841. pInfo = static_cast<DN_INTERNAL_MESSAGE_CONNECT_ATTEMPT_FAILED*>(pOpBuffer);
  2842. DPFX(DPFPREP, 7,"Player [0x%lx] could not connect to us",pInfo->dpnid);
  2843. hResultCode = DNAbortConnect(pdnObject,DPNERR_PLAYERNOTREACHABLE); // Ignore errors
  2844. break;
  2845. }
  2846. case DN_MSG_INTERNAL_NAMETABLE_VERSION:
  2847. {
  2848. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_NAMETABLE_VERSION");
  2849. #ifndef DPNBUILD_NOHOSTMIGRATE
  2850. if (DNVerifyNameTableVersion(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2851. {
  2852. DPFERR("Invalid message - ignoring");
  2853. hResultCode = DPN_OK;
  2854. goto Failure;
  2855. }
  2856. hResultCode = DNNTHostReceiveVersion(pdnObject,pConnection->GetDPNID(),pOpBuffer);
  2857. #else
  2858. DNASSERT(!"Should never get here!");
  2859. #endif // DPNBUILD_NOHOSTMIGRATE
  2860. break;
  2861. }
  2862. case DN_MSG_INTERNAL_RESYNC_VERSION:
  2863. {
  2864. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_RESYNC_VERSION");
  2865. #ifndef DPNBUILD_NOHOSTMIGRATE
  2866. if (DNVerifyResyncVersion(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2867. {
  2868. DPFERR("Invalid message - ignoring");
  2869. hResultCode = DPN_OK;
  2870. goto Failure;
  2871. }
  2872. hResultCode = DNNTPlayerResyncVersion(pdnObject,pOpBuffer);
  2873. #else
  2874. DNASSERT(!"Should never get here!");
  2875. #endif // DPNBUILD_NOHOSTMIGRATE
  2876. break;
  2877. }
  2878. case DN_MSG_INTERNAL_REQ_NAMETABLE_OP:
  2879. {
  2880. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_NAMETABLE_OP");
  2881. #ifndef DPNBUILD_NOHOSTMIGRATE
  2882. if (DNVerifyReqNameTableOp(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2883. {
  2884. DPFERR("Invalid message - ignoring");
  2885. hResultCode = DPN_OK;
  2886. goto Failure;
  2887. }
  2888. hResultCode = DNProcessHostMigration2(pdnObject,pOpBuffer);
  2889. #else
  2890. DNASSERT(!"Should never get here!");
  2891. #endif // DPNBUILD_NOHOSTMIGRATE
  2892. break;
  2893. }
  2894. case DN_MSG_INTERNAL_ACK_NAMETABLE_OP:
  2895. {
  2896. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_ACK_NAMETABLE_OP");
  2897. #ifndef DPNBUILD_NOHOSTMIGRATE
  2898. if (DNVerifyAckNameTableOp(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2899. {
  2900. DPFERR("Invalid message - ignoring");
  2901. hResultCode = DPN_OK;
  2902. goto Failure;
  2903. }
  2904. hResultCode = DNPerformHostMigration3(pdnObject,pOpBuffer);
  2905. #else
  2906. DNASSERT(!"Should never get here!");
  2907. #endif // DPNBUILD_NOHOSTMIGRATE
  2908. break;
  2909. }
  2910. case DN_MSG_INTERNAL_HOST_MIGRATE:
  2911. {
  2912. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_HOST_MIGRATE");
  2913. #ifndef DPNBUILD_NOHOSTMIGRATE
  2914. if (DNVerifyHostMigrate(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2915. {
  2916. DPFERR("Invalid message - ignoring");
  2917. hResultCode = DPN_OK;
  2918. goto Failure;
  2919. }
  2920. hResultCode = DNProcessHostMigration1(pdnObject,pOpBuffer);
  2921. #else
  2922. DNASSERT(!"Should never get here!");
  2923. #endif // DPNBUILD_NOHOSTMIGRATE
  2924. break;
  2925. }
  2926. case DN_MSG_INTERNAL_HOST_MIGRATE_COMPLETE:
  2927. {
  2928. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_HOST_MIGRATE_COMPLETE");
  2929. #ifndef DPNBUILD_NOHOSTMIGRATE
  2930. //
  2931. // No verification as there is no payload with this message
  2932. //
  2933. hResultCode = DNProcessHostMigration3(pdnObject,pConnection->GetDPNID());
  2934. #else
  2935. DNASSERT(!"Should never get here!");
  2936. #endif // DPNBUILD_NOHOSTMIGRATE
  2937. break;
  2938. }
  2939. case DN_MSG_INTERNAL_UPDATE_APPLICATION_DESC:
  2940. {
  2941. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_UPDATE_APPLICATION_DESC");
  2942. if (DNVerifyApplicationDescInfo(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  2943. {
  2944. DPFERR("Invalid message - ignoring");
  2945. hResultCode = DPN_OK;
  2946. goto Failure;
  2947. }
  2948. hResultCode = DNProcessUpdateAppDesc(pdnObject,static_cast<DPN_APPLICATION_DESC_INFO*>(pOpBuffer));
  2949. break;
  2950. }
  2951. case DN_MSG_INTERNAL_ADD_PLAYER:
  2952. {
  2953. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_ADD_PLAYER");
  2954. if (DNVerifyNameTableEntryInfo(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  2955. {
  2956. DPFERR("Invalid message - ignoring");
  2957. hResultCode = DPN_OK;
  2958. goto Failure;
  2959. }
  2960. hResultCode = DNNTAddOperation( pdnObject,
  2961. DN_MSG_INTERNAL_ADD_PLAYER,
  2962. pOpBuffer,
  2963. dwOpBufferSize,
  2964. hProtocol,
  2965. pConnection->GetSP());
  2966. break;
  2967. }
  2968. case DN_MSG_INTERNAL_DESTROY_PLAYER:
  2969. {
  2970. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_DESTROY_PLAYER");
  2971. if (DNVerifyDestroyPlayer(pOpBuffer,dwOpBufferSize) != DPN_OK)
  2972. {
  2973. DPFERR("Invalid message - ignoring");
  2974. hResultCode = DPN_OK;
  2975. goto Failure;
  2976. }
  2977. hResultCode = DNNTAddOperation( pdnObject,
  2978. DN_MSG_INTERNAL_DESTROY_PLAYER,
  2979. pOpBuffer,
  2980. dwOpBufferSize,
  2981. hProtocol,
  2982. pConnection->GetSP());
  2983. break;
  2984. }
  2985. case DN_MSG_INTERNAL_CREATE_GROUP:
  2986. {
  2987. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_CREATE_GROUP");
  2988. if (DNVerifyCreateGroup(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  2989. {
  2990. DPFERR("Invalid message - ignoring");
  2991. hResultCode = DPN_OK;
  2992. goto Failure;
  2993. }
  2994. hResultCode = DNNTAddOperation( pdnObject,
  2995. DN_MSG_INTERNAL_CREATE_GROUP,
  2996. pOpBuffer,
  2997. dwOpBufferSize,
  2998. hProtocol,
  2999. pConnection->GetSP());
  3000. break;
  3001. }
  3002. case DN_MSG_INTERNAL_DESTROY_GROUP:
  3003. {
  3004. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_DESTROY_GROUP");
  3005. if (DNVerifyDestroyGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3006. {
  3007. DPFERR("Invalid message - ignoring");
  3008. hResultCode = DPN_OK;
  3009. goto Failure;
  3010. }
  3011. hResultCode = DNNTAddOperation( pdnObject,
  3012. DN_MSG_INTERNAL_DESTROY_GROUP,
  3013. pOpBuffer,
  3014. dwOpBufferSize,
  3015. hProtocol,
  3016. pConnection->GetSP());
  3017. break;
  3018. }
  3019. case DN_MSG_INTERNAL_ADD_PLAYER_TO_GROUP:
  3020. {
  3021. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_ADD_PLAYER_TO_GROUP");
  3022. if (DNVerifyAddPlayerToGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3023. {
  3024. DPFERR("Invalid message - ignoring");
  3025. hResultCode = DPN_OK;
  3026. goto Failure;
  3027. }
  3028. hResultCode = DNNTAddOperation( pdnObject,
  3029. DN_MSG_INTERNAL_ADD_PLAYER_TO_GROUP,
  3030. pOpBuffer,
  3031. dwOpBufferSize,
  3032. hProtocol,
  3033. pConnection->GetSP());
  3034. break;
  3035. }
  3036. case DN_MSG_INTERNAL_DELETE_PLAYER_FROM_GROUP:
  3037. {
  3038. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_DELETE_PLAYER_FROM_GROUP");
  3039. if (DNVerifyDeletePlayerFromGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3040. {
  3041. DPFERR("Invalid message - ignoring");
  3042. hResultCode = DPN_OK;
  3043. goto Failure;
  3044. }
  3045. hResultCode = DNNTAddOperation( pdnObject,
  3046. DN_MSG_INTERNAL_DELETE_PLAYER_FROM_GROUP,
  3047. pOpBuffer,
  3048. dwOpBufferSize,
  3049. hProtocol,
  3050. pConnection->GetSP());
  3051. break;
  3052. }
  3053. case DN_MSG_INTERNAL_UPDATE_INFO:
  3054. {
  3055. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_UPDATE_INFO");
  3056. if (DNVerifyUpdateInfo(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  3057. {
  3058. DPFERR("Invalid message - ignoring");
  3059. hResultCode = DPN_OK;
  3060. goto Failure;
  3061. }
  3062. hResultCode = DNNTAddOperation( pdnObject,
  3063. DN_MSG_INTERNAL_UPDATE_INFO,
  3064. pOpBuffer,
  3065. dwOpBufferSize,
  3066. hProtocol,
  3067. pConnection->GetSP());
  3068. break;
  3069. }
  3070. case DN_MSG_INTERNAL_REQ_CREATE_GROUP:
  3071. {
  3072. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_CREATE_GROUP");
  3073. if (DNVerifyReqCreateGroup(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  3074. {
  3075. DPFERR("Invalid message - ignoring");
  3076. hResultCode = DPN_OK;
  3077. goto Failure;
  3078. }
  3079. hResultCode = DNHostProcessRequest(pdnObject,DN_MSG_INTERNAL_REQ_CREATE_GROUP,
  3080. pOpBuffer,pConnection->GetDPNID());
  3081. break;
  3082. }
  3083. case DN_MSG_INTERNAL_REQ_DESTROY_GROUP:
  3084. {
  3085. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_DESTROY_GROUP");
  3086. if (DNVerifyReqDestroyGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3087. {
  3088. DPFERR("Invalid message - ignoring");
  3089. hResultCode = DPN_OK;
  3090. goto Failure;
  3091. }
  3092. hResultCode = DNHostProcessRequest(pdnObject,DN_MSG_INTERNAL_REQ_DESTROY_GROUP,
  3093. pOpBuffer,pConnection->GetDPNID());
  3094. break;
  3095. }
  3096. case DN_MSG_INTERNAL_REQ_ADD_PLAYER_TO_GROUP:
  3097. {
  3098. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_ADD_PLAYER_TO_GROUP");
  3099. if (DNVerifyReqAddPlayerToGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3100. {
  3101. DPFERR("Invalid message - ignoring");
  3102. hResultCode = DPN_OK;
  3103. goto Failure;
  3104. }
  3105. hResultCode = DNHostProcessRequest(pdnObject,
  3106. DN_MSG_INTERNAL_REQ_ADD_PLAYER_TO_GROUP,pOpBuffer,pConnection->GetDPNID());
  3107. break;
  3108. }
  3109. case DN_MSG_INTERNAL_REQ_DELETE_PLAYER_FROM_GROUP:
  3110. {
  3111. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_DELETE_PLAYER_FROM_GROUP");
  3112. if (DNVerifyReqDeletePlayerFromGroup(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3113. {
  3114. DPFERR("Invalid message - ignoring");
  3115. hResultCode = DPN_OK;
  3116. goto Failure;
  3117. }
  3118. hResultCode = DNHostProcessRequest(pdnObject,
  3119. DN_MSG_INTERNAL_REQ_DELETE_PLAYER_FROM_GROUP,pOpBuffer,pConnection->GetDPNID());
  3120. break;
  3121. }
  3122. case DN_MSG_INTERNAL_REQ_UPDATE_INFO:
  3123. {
  3124. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_UPDATE_INFO");
  3125. if (DNVerifyReqUpdateInfo(pOpBuffer,dwOpBufferSize,pOpBuffer) != DPN_OK)
  3126. {
  3127. DPFERR("Invalid message - ignoring");
  3128. hResultCode = DPN_OK;
  3129. goto Failure;
  3130. }
  3131. hResultCode = DNHostProcessRequest(pdnObject,DN_MSG_INTERNAL_REQ_UPDATE_INFO,
  3132. pOpBuffer,pConnection->GetDPNID());
  3133. break;
  3134. }
  3135. #ifndef DPNBUILD_NOVOICE
  3136. case DN_MSG_INTERNAL_VOICE_SEND:
  3137. {
  3138. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_VOICE_SEND");
  3139. hResultCode = Voice_Receive( pdnObject, pConnection->GetDPNID(), 0, pOpBuffer, dwOpBufferSize );
  3140. break;
  3141. }
  3142. #endif // DPNBUILD_NOVOICE
  3143. case DN_MSG_INTERNAL_BUFFER_IN_USE:
  3144. {
  3145. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_BUFFER_IN_USE - INVALID !");
  3146. DNASSERT(FALSE);
  3147. break;
  3148. }
  3149. case DN_MSG_INTERNAL_REQUEST_FAILED:
  3150. {
  3151. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQUEST_FAILED - INVALID !");
  3152. if (DNVerifyRequestFailed(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3153. {
  3154. DPFERR("Invalid message - ignoring");
  3155. hResultCode = DPN_OK;
  3156. goto Failure;
  3157. }
  3158. hResultCode = DNProcessFailedRequest(pdnObject,pOpBuffer);
  3159. break;
  3160. }
  3161. case DN_MSG_INTERNAL_TERMINATE_SESSION:
  3162. {
  3163. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_TERMINATE_SESSION");
  3164. if (DNVerifyTerminateSession(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3165. {
  3166. DPFERR("Invalid message - ignoring");
  3167. hResultCode = DPN_OK;
  3168. goto Failure;
  3169. }
  3170. hResultCode = DNProcessTerminateSession(pdnObject,pOpBuffer,dwOpBufferSize);
  3171. break;
  3172. }
  3173. case DN_MSG_INTERNAL_REQ_PROCESS_COMPLETION:
  3174. {
  3175. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_PROCESS_COMPLETION");
  3176. if (DNVerifyReqProcessCompletion(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3177. {
  3178. DPFERR("Invalid message - ignoring");
  3179. hResultCode = DPN_OK;
  3180. goto Failure;
  3181. }
  3182. //
  3183. // Ensure requesting player in NameTable
  3184. //
  3185. hResultCode = DNReceiveCompleteOnProcess(pdnObject,pConnection,pOpBuffer,
  3186. dwOpBufferSize,hProtocol,pRefCountBuffer);
  3187. if (hResultCode != DPNERR_PENDING)
  3188. {
  3189. hResultCode = DPN_OK; // Ignore errors
  3190. }
  3191. break;
  3192. }
  3193. case DN_MSG_INTERNAL_PROCESS_COMPLETION:
  3194. {
  3195. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_PROCESS_COMPLETION");
  3196. if (DNVerifyProcessCompletion(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3197. {
  3198. DPFERR("Invalid message - ignoring");
  3199. hResultCode = DPN_OK;
  3200. goto Failure;
  3201. }
  3202. hResultCode = DNReceiveCompleteOnProcessReply(pdnObject,pOpBuffer,dwOpBufferSize);
  3203. break;
  3204. }
  3205. case DN_MSG_INTERNAL_REQ_INTEGRITY_CHECK:
  3206. {
  3207. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_REQ_INTEGRITY_CHECK");
  3208. if (DNVerifyReqIntegrityCheck(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3209. {
  3210. DPFERR("Invalid message - ignoring");
  3211. hResultCode = DPN_OK;
  3212. goto Failure;
  3213. }
  3214. hResultCode = DNHostProcessRequest(pdnObject,DN_MSG_INTERNAL_REQ_INTEGRITY_CHECK,
  3215. pOpBuffer,pConnection->GetDPNID());
  3216. hResultCode = DPN_OK;
  3217. break;
  3218. }
  3219. case DN_MSG_INTERNAL_INTEGRITY_CHECK:
  3220. {
  3221. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_INTEGRITY_CHECK");
  3222. if (DNVerifyIntegrityCheck(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3223. {
  3224. DPFERR("Invalid message - ignoring");
  3225. hResultCode = DPN_OK;
  3226. goto Failure;
  3227. }
  3228. hResultCode = DNProcessCheckIntegrity(pdnObject,pOpBuffer);
  3229. hResultCode = DPN_OK; // Ignore errors
  3230. break;
  3231. }
  3232. case DN_MSG_INTERNAL_INTEGRITY_CHECK_RESPONSE:
  3233. {
  3234. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL_INTEGRITY_CHECK_RESPONSE");
  3235. if (DNVerifyIntegrityCheckResponse(pOpBuffer,dwOpBufferSize) != DPN_OK)
  3236. {
  3237. DPFERR("Invalid message - ignoring");
  3238. hResultCode = DPN_OK;
  3239. goto Failure;
  3240. }
  3241. hResultCode = DNHostFixIntegrity(pdnObject,pOpBuffer);
  3242. hResultCode = DPN_OK; // Ignore errors
  3243. break;
  3244. }
  3245. default:
  3246. {
  3247. DPFX(DPFPREP, 7,"Received: DN_MSG_INTERNAL (UNKNOWN!)");
  3248. DNASSERT(FALSE);
  3249. hResultCode = DPNERR_UNSUPPORTED;
  3250. break;
  3251. }
  3252. }
  3253. Exit:
  3254. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  3255. if (fDecRunningOpCount)
  3256. {
  3257. pdnObject->dwRunningOpCount--;
  3258. }
  3259. if ((pdnObject->dwRunningOpCount == 0) && (pdnObject->dwFlags & DN_OBJECT_FLAG_HOST_MIGRATING_WAIT))
  3260. {
  3261. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  3262. DNSetEvent(pdnObject->hRunningOpEvent);
  3263. }
  3264. else
  3265. {
  3266. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  3267. }
  3268. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  3269. return(hResultCode);
  3270. Failure:
  3271. if (pRCBuffer)
  3272. {
  3273. pRCBuffer->Release();
  3274. pRCBuffer = NULL;
  3275. }
  3276. if (pWorkerJob)
  3277. {
  3278. pWorkerJob->ReturnSelfToPool();
  3279. pWorkerJob = NULL;
  3280. }
  3281. goto Exit;
  3282. }
  3283. #undef DPF_MODNAME
  3284. #define DPF_MODNAME "DNPerformRequest"
  3285. HRESULT DNPerformRequest(DIRECTNETOBJECT *const pdnObject,
  3286. const DWORD dwMsgId,
  3287. const DPN_BUFFER_DESC *const pBufferDesc,
  3288. CConnection *const pConnection,
  3289. CAsyncOp *const pParent,
  3290. CAsyncOp **const ppRequest)
  3291. {
  3292. DWORD dwFlags;
  3293. HRESULT hResultCode;
  3294. BOOL fInternal;
  3295. BOOL fReleaseLock;
  3296. CAsyncOp *pRequest;
  3297. CAsyncOp *pSend;
  3298. CRefCountBuffer *pRefCountBuffer;
  3299. DN_SEND_OP_DATA *pSendOpData;
  3300. DPNHANDLE handle;
  3301. DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION *pMsg;
  3302. DPFX(DPFPREP, 6,"Parameters: dwMsgId [0x%lx], pBufferDesc [0x%p], pConnection [0x%p], pParent [0x%p], ppRequest [0x%p]",
  3303. dwMsgId,pBufferDesc,pConnection,pParent,ppRequest);
  3304. DNASSERT(pdnObject != NULL);
  3305. DNASSERT(pBufferDesc != NULL);
  3306. pRequest = NULL;
  3307. pSend = NULL;
  3308. pSendOpData = NULL;
  3309. pRefCountBuffer = NULL;
  3310. fReleaseLock = FALSE;
  3311. //
  3312. // Create RefCountBuffer for this operation
  3313. //
  3314. hResultCode = RefCountBufferNew(pdnObject,
  3315. pBufferDesc->dwBufferSize + sizeof(DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION),
  3316. MemoryBlockAlloc,
  3317. MemoryBlockFree,
  3318. &pRefCountBuffer);
  3319. if (hResultCode != DPN_OK)
  3320. {
  3321. DPFERR("Could not create RefCountBuffer");
  3322. DisplayDNError(0,hResultCode);
  3323. DNASSERT(FALSE);
  3324. goto Failure;
  3325. }
  3326. pMsg = reinterpret_cast<DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION*>(pRefCountBuffer->GetBufferAddress());
  3327. memcpy(pMsg + 1,pBufferDesc->pBufferData,pBufferDesc->dwBufferSize);
  3328. //
  3329. // Keep DirectNetObject from vanishing under us !
  3330. //
  3331. if ((hResultCode = DNAddRefLock(pdnObject)) != DPN_OK)
  3332. {
  3333. DPFERR("Aborting request - object is closing");
  3334. hResultCode = DPN_OK;
  3335. goto Failure;
  3336. }
  3337. fReleaseLock = TRUE;
  3338. //
  3339. // Create REQUEST
  3340. //
  3341. if ((hResultCode = AsyncOpNew(pdnObject,&pRequest)) != DPN_OK)
  3342. {
  3343. DPFERR("Could not create AsyncOp");
  3344. DisplayDNError(0,hResultCode);
  3345. DNASSERT(FALSE);
  3346. goto Failure;
  3347. }
  3348. pRequest->SetOpType( ASYNC_OP_REQUEST );
  3349. if (pParent)
  3350. {
  3351. pParent->Lock();
  3352. if (pParent->IsCancelled())
  3353. {
  3354. pParent->Unlock();
  3355. pRequest->SetResult( DPNERR_USERCANCEL );
  3356. hResultCode = DPNERR_USERCANCEL;
  3357. goto Failure;
  3358. }
  3359. pRequest->MakeChild( pParent );
  3360. pParent->Unlock();
  3361. }
  3362. pRequest->MakeParent();
  3363. pRequest->SetRefCountBuffer( pRefCountBuffer );
  3364. //
  3365. // Need a handle for this op (to be sent to the other side who will pass it back)
  3366. //
  3367. if ((hResultCode = pdnObject->HandleTable.Create(pRequest,&handle)) != DPN_OK)
  3368. {
  3369. DPFERR("Could not create handle for this operation");
  3370. DisplayDNError(0,hResultCode);
  3371. goto Failure;
  3372. }
  3373. else
  3374. {
  3375. // Add a reference for the HandleTable
  3376. pRequest->AddRef();
  3377. pRequest->Lock();
  3378. pRequest->SetHandle(handle);
  3379. pRequest->Unlock();
  3380. }
  3381. DNASSERT(pRequest->GetHandle() != 0);
  3382. //
  3383. // Set up SEND op data
  3384. //
  3385. pSendOpData = pRequest->GetLocalSendOpData();
  3386. pSendOpData->dwMsgId = dwMsgId;
  3387. pSendOpData->BufferDesc[0].pBufferData = reinterpret_cast<BYTE*>(&pSendOpData->dwMsgId);
  3388. pSendOpData->BufferDesc[0].dwBufferSize = sizeof(DWORD);
  3389. pSendOpData->BufferDesc[1].pBufferData = pRefCountBuffer->GetBufferAddress();
  3390. pSendOpData->BufferDesc[1].dwBufferSize = pRefCountBuffer->GetBufferSize();
  3391. pSendOpData->dwNumBuffers = 2;
  3392. #pragma TODO(vanceo, "This does not preserve non-sequential & coalesce flags when this is a COMPLETEONPROCESS send request")
  3393. pRequest->SetOpFlags( DN_SENDFLAGS_RELIABLE | DN_SENDFLAGS_SET_USER_FLAG );
  3394. pRequest->SetResult( DPNERR_PLAYERLOST );
  3395. pRequest->SetCompletion( DNCompleteRequest );
  3396. pSendOpData = NULL;
  3397. pMsg->hCompletionOp = pRequest->GetHandle();
  3398. pRefCountBuffer->Release();
  3399. pRefCountBuffer = NULL;
  3400. //
  3401. // User messages sent with COMPLETE_ON_SEND should be allowed to be cancelled
  3402. //
  3403. if (dwMsgId == DN_MSG_INTERNAL_REQ_PROCESS_COMPLETION)
  3404. {
  3405. fInternal = FALSE;
  3406. }
  3407. else
  3408. {
  3409. fInternal = TRUE;
  3410. }
  3411. //
  3412. // We will always hand back the request, even if the send fails
  3413. //
  3414. if (ppRequest)
  3415. {
  3416. pRequest->AddRef();
  3417. *ppRequest = pRequest;
  3418. }
  3419. //
  3420. // Unlock DirectNetObject
  3421. //
  3422. DNDecRefLock(pdnObject);
  3423. fReleaseLock = FALSE;
  3424. //
  3425. // Add request to the request list
  3426. //
  3427. DNEnterCriticalSection(&pdnObject->csActiveList);
  3428. pRequest->m_bilinkActiveList.InsertBefore(&pdnObject->m_bilinkRequestList);
  3429. DNLeaveCriticalSection(&pdnObject->csActiveList);
  3430. //
  3431. // Avoid operations while host migration is taking place. This will get picked up
  3432. // after host migration, when completing outstanding operations
  3433. //
  3434. DNEnterCriticalSection(&pdnObject->csDirectNetObject);
  3435. dwFlags = pdnObject->dwFlags;
  3436. DNLeaveCriticalSection(&pdnObject->csDirectNetObject);
  3437. if (!(dwFlags & DN_OBJECT_FLAG_HOST_MIGRATING) || !fInternal)
  3438. {
  3439. //
  3440. // SEND REQUEST
  3441. //
  3442. if (pConnection)
  3443. {
  3444. hResultCode = DNPerformChildSend( pdnObject,
  3445. pRequest,
  3446. pConnection,
  3447. 0,
  3448. &pSend,
  3449. fInternal );
  3450. if (hResultCode != DPNERR_PENDING)
  3451. {
  3452. DPFERR("Could not perform child SEND");
  3453. DisplayDNError(0,hResultCode);
  3454. DNASSERT(hResultCode != DPN_OK); // it was sent guaranteed, it should not return immediately
  3455. goto Failure;
  3456. }
  3457. //
  3458. // Reset SEND AsyncOp to complete apropriately.
  3459. //
  3460. pSend->SetCompletion( DNCompleteSendRequest );
  3461. pSend->Release();
  3462. pSend = NULL;
  3463. }
  3464. }
  3465. pRequest->Release();
  3466. pRequest = NULL;
  3467. Exit:
  3468. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  3469. return(hResultCode);
  3470. Failure:
  3471. if (fReleaseLock)
  3472. {
  3473. DNDecRefLock(pdnObject);
  3474. fReleaseLock = FALSE;
  3475. }
  3476. if (pRequest)
  3477. {
  3478. pRequest->Release();
  3479. pRequest = NULL;
  3480. }
  3481. if (pSend)
  3482. {
  3483. pSend->Release();
  3484. pSend = NULL;
  3485. }
  3486. if (pRefCountBuffer)
  3487. {
  3488. pRefCountBuffer->Release();
  3489. pRefCountBuffer = NULL;
  3490. }
  3491. if (pSendOpData)
  3492. {
  3493. DWORD dw;
  3494. for ( dw = 0 ; dw < pSendOpData->dwNumBuffers ; dw++ )
  3495. {
  3496. pSendOpData->BufferDesc[dw].dwBufferSize = 0;
  3497. pSendOpData->BufferDesc[dw].pBufferData = NULL;
  3498. }
  3499. pSendOpData->dwMsgId = 0;
  3500. pSendOpData->dwNumBuffers = 0;
  3501. pSendOpData = NULL;
  3502. }
  3503. goto Exit;
  3504. }
  3505. // DNReceiveCompleteOnProcess
  3506. //
  3507. // Receive a CompleteOnProcess message
  3508. // Pass the message up to the application and then return a special completion message
  3509. #undef DPF_MODNAME
  3510. #define DPF_MODNAME "DNReceiveCompleteOnProcess"
  3511. HRESULT DNReceiveCompleteOnProcess(DIRECTNETOBJECT *const pdnObject,
  3512. CConnection *const pConnection,
  3513. void *const pBufferData,
  3514. const DWORD dwBufferSize,
  3515. const HANDLE hProtocol,
  3516. CRefCountBuffer *const pRefCountBuffer)
  3517. {
  3518. HRESULT hResultCode;
  3519. PVOID pvData;
  3520. DWORD dwDataSize;
  3521. UNALIGNED DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION *pReq;
  3522. DPFX(DPFPREP, 4,"Parameters: pConnection [0x%p], pBufferData [0x%p], dwBufferSize [%ld], hProtocol [0x%p], pRefCountBuffer [0x%p]",
  3523. pConnection,pBufferData,dwBufferSize,hProtocol,pRefCountBuffer);
  3524. DNASSERT(pdnObject != NULL);
  3525. DNASSERT(pConnection != NULL);
  3526. //
  3527. // Extract message
  3528. //
  3529. pReq = static_cast<DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION*>(pBufferData);
  3530. pvData = static_cast<void*>(pReq + 1);
  3531. dwDataSize = dwBufferSize - sizeof(DN_INTERNAL_MESSAGE_REQ_PROCESS_COMPLETION);
  3532. //
  3533. // Pass data to application
  3534. //
  3535. hResultCode = DNReceiveUserData(pdnObject,
  3536. pConnection,
  3537. static_cast<BYTE*>(pvData),
  3538. dwDataSize,
  3539. hProtocol,
  3540. pRefCountBuffer,
  3541. pReq->hCompletionOp,
  3542. 0);
  3543. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  3544. return(hResultCode);
  3545. }
  3546. // DNReceiveCompleteOnProcessReply
  3547. //
  3548. // Receive a CompleteOnProcess COMPLETION message
  3549. // Complete the outstanding CompleteOnProcess operation
  3550. #undef DPF_MODNAME
  3551. #define DPF_MODNAME "DNReceiveCompleteOnProcessReply"
  3552. HRESULT DNReceiveCompleteOnProcessReply(DIRECTNETOBJECT *const pdnObject,
  3553. void *const pBufferData,
  3554. const DWORD dwBufferSize)
  3555. {
  3556. HRESULT hResultCode;
  3557. CAsyncOp *pAsyncOp;
  3558. UNALIGNED DN_INTERNAL_MESSAGE_PROCESS_COMPLETION *pMsg;
  3559. DPFX(DPFPREP, 4,"Parameters: pBufferData [0x%p], dwBufferSize [%ld]",pBufferData,dwBufferSize);
  3560. DNASSERT(pBufferData != NULL);
  3561. pAsyncOp = NULL;
  3562. pMsg = static_cast<DN_INTERNAL_MESSAGE_PROCESS_COMPLETION*>(pBufferData);
  3563. //
  3564. // Remove async op from HandleTable and from request list
  3565. //
  3566. DPFX(DPFPREP, 5,"Release completion operation [0x%lx]",pMsg->hCompletionOp);
  3567. if ((hResultCode = pdnObject->HandleTable.Destroy(pMsg->hCompletionOp,(PVOID*)&pAsyncOp)) != DPN_OK)
  3568. {
  3569. DPFERR("Could not find handle in HandleTable");
  3570. DisplayDNError(0,hResultCode);
  3571. DNASSERT(FALSE);
  3572. goto Failure;
  3573. }
  3574. DNASSERT(pAsyncOp->GetOpType() == ASYNC_OP_REQUEST);
  3575. DNEnterCriticalSection(&pdnObject->csActiveList);
  3576. pAsyncOp->m_bilinkActiveList.RemoveFromList();
  3577. DNLeaveCriticalSection(&pdnObject->csActiveList);
  3578. //
  3579. // Mark this operation as completing okay
  3580. //
  3581. pAsyncOp->SetResult( DPN_OK );
  3582. //
  3583. // This release should be the final release of the Request Child Async Op. and should
  3584. // DecRef the Request Parent Async Op.
  3585. //
  3586. pAsyncOp->Release();
  3587. pAsyncOp = NULL;
  3588. hResultCode = DPN_OK;
  3589. Exit:
  3590. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  3591. return(hResultCode);
  3592. Failure:
  3593. if (pAsyncOp)
  3594. {
  3595. pAsyncOp->Release();
  3596. pAsyncOp = NULL;
  3597. }
  3598. goto Exit;
  3599. }
  3600. // DNProcessTerminateSession
  3601. //
  3602. // Process a TERMINATE_SESSION message from the Host player
  3603. #undef DPF_MODNAME
  3604. #define DPF_MODNAME "DNProcessTerminateSession"
  3605. HRESULT DNProcessTerminateSession(DIRECTNETOBJECT *const pdnObject,
  3606. void *const pvBuffer,
  3607. const DWORD dwBufferSize)
  3608. {
  3609. HRESULT hResultCode;
  3610. void *pvTerminateData;
  3611. UNALIGNED DN_INTERNAL_MESSAGE_TERMINATE_SESSION *pMsg;
  3612. DPFX(DPFPREP, 4,"Parameters: pvBuffer [0x%p], dwBufferSize [%ld]",pvBuffer,dwBufferSize);
  3613. DNASSERT(pdnObject != NULL);
  3614. DNASSERT(pvBuffer != NULL);
  3615. pMsg = static_cast<DN_INTERNAL_MESSAGE_TERMINATE_SESSION*>(pvBuffer);
  3616. if (pMsg->dwTerminateDataOffset)
  3617. {
  3618. pvTerminateData = static_cast<void*>(static_cast<BYTE*>(pvBuffer) + pMsg->dwTerminateDataOffset);
  3619. }
  3620. else
  3621. {
  3622. pvTerminateData = NULL;
  3623. }
  3624. //
  3625. // Inform user of termination
  3626. //
  3627. hResultCode = DNUserTerminateSession(pdnObject,DPNERR_HOSTTERMINATEDSESSION,pvTerminateData,pMsg->dwTerminateDataSize);
  3628. // Terminate session
  3629. hResultCode = DNTerminateSession(pdnObject,DPNERR_HOSTTERMINATEDSESSION);
  3630. DPFX(DPFPREP, 4,"Returning: [0x%lx]",hResultCode);
  3631. return(hResultCode);
  3632. }