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.

1978 lines
53 KiB

  1. #include "stdafx.h"
  2. #include "Direct.h"
  3. #include "dms.h"
  4. #include "dplayaddressobj.h"
  5. #include "DPlayPeerObj.h"
  6. extern void *g_dxj_DirectPlayPeer;
  7. extern void *g_dxj_DirectPlayAddress;
  8. extern BSTR GUIDtoBSTR(LPGUID);
  9. extern HRESULT DPLAYBSTRtoGUID(LPGUID,BSTR);
  10. extern BOOL IsEmptyString(BSTR szString);
  11. #define SAFE_DELETE(p) { if(p) { delete (p); p=NULL; } }
  12. #define SAFE_DELETE_ARRAY(p) { if(p) { delete [] (p); p=NULL; } }
  13. #define SAFE_RELEASE(p) { __try { if(p) { int i = 0; i = (p)->Release(); DPF1(1,"--DirectPlayPeer SafeRelease (RefCount = %d)\n",i); if (!i) { (p)=NULL;}} } __except(EXCEPTION_EXECUTE_HANDLER) { (p) = NULL;} }
  14. HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext,
  15. DWORD dwMessageId,
  16. PVOID pMsgBuffer );
  17. DWORD WINAPI ClosePeerThreadProc(void* lpParam);
  18. DWORD WINAPI ReleasePeerThreadProc(void* lpParam);
  19. ///////////////////////////////////////////////////////////////////
  20. // InternalAddRef
  21. ///////////////////////////////////////////////////////////////////
  22. DWORD C_dxj_DirectPlayPeerObject::InternalAddRef(){
  23. DWORD i;
  24. i=CComObjectRoot::InternalAddRef();
  25. DPF1(1,"------ DXVB: DirectPlayPeer8 AddRef %d \n",i);
  26. return i;
  27. }
  28. ///////////////////////////////////////////////////////////////////
  29. // InternalRelease
  30. ///////////////////////////////////////////////////////////////////
  31. DWORD C_dxj_DirectPlayPeerObject::InternalRelease(){
  32. DWORD i;
  33. i=CComObjectRoot::InternalRelease();
  34. DPF1(1,"------ DXVB: DirectPlayPeer8 Release %d \n",i);
  35. return i;
  36. }
  37. ///////////////////////////////////////////////////////////////////
  38. // C_dxj_DirectPlayPeerObject
  39. ///////////////////////////////////////////////////////////////////
  40. C_dxj_DirectPlayPeerObject::C_dxj_DirectPlayPeerObject(){
  41. DPF(1,"------ DXVB: Constructor Creation DirectPlayPeer8 Object\n ");
  42. m__dxj_DirectPlayPeer = NULL;
  43. m_SPInfo = NULL;
  44. m_dwSPCount = 0;
  45. m_ClientsGroups = NULL;
  46. m_GroupMembers = NULL;
  47. m_dwGroupID = 0;
  48. m_dwClientCount = 0;
  49. m_dwGroupMemberCount = 0;
  50. m_fInit = FALSE;
  51. m_fHandleEvents = FALSE;
  52. m_pEventStream=NULL;
  53. m_dwMsgCount = 0;
  54. }
  55. ///////////////////////////////////////////////////////////////////
  56. // ~C_dxj_DirectPlayPeerObject
  57. ///////////////////////////////////////////////////////////////////
  58. C_dxj_DirectPlayPeerObject::~C_dxj_DirectPlayPeerObject()
  59. {
  60. DPF(1,"------ DXVB: Entering ~C_dxj_DirectPlayPeerObject destructor \n");
  61. //We still have messages to process get rid of them
  62. m_fHandleEvents = FALSE;
  63. FlushBuffer(0);
  64. SAFE_RELEASE(m_pEventStream);
  65. SAFE_DELETE_ARRAY(m_SPInfo);
  66. SAFE_DELETE_ARRAY(m_ClientsGroups);
  67. SAFE_DELETE_ARRAY(m_GroupMembers);
  68. HANDLE hThread = NULL;
  69. DWORD dwThread = 0;
  70. // We are quitting anyway, so we don't really care what is going on in this thread..
  71. hThread = CreateThread(NULL, 0, ReleasePeerThreadProc, this->m__dxj_DirectPlayPeer, 0, &dwThread);
  72. }
  73. HRESULT C_dxj_DirectPlayPeerObject::InternalGetObject(IUnknown **pUnk){
  74. *pUnk=(IUnknown*)m__dxj_DirectPlayPeer;
  75. return S_OK;
  76. }
  77. HRESULT C_dxj_DirectPlayPeerObject::InternalSetObject(IUnknown *pUnk){
  78. m__dxj_DirectPlayPeer=(IDirectPlay8Peer*)pUnk;
  79. return S_OK;
  80. }
  81. HRESULT C_dxj_DirectPlayPeerObject::CancelAsyncOperation(long lAsyncHandle, long lFlags)
  82. {
  83. HRESULT hr;
  84. __try {
  85. DPF(1,"-----Entering (DplayPeer) CancelAsyncOp call...\n");
  86. if (FAILED( hr= m__dxj_DirectPlayPeer->CancelAsyncOperation((DPNHANDLE) lAsyncHandle, (DWORD) lFlags) ) )
  87. return hr;
  88. }
  89. __except(EXCEPTION_EXECUTE_HANDLER)
  90. {
  91. return E_FAIL;
  92. }
  93. return S_OK;
  94. }
  95. HRESULT C_dxj_DirectPlayPeerObject::GetApplicationDesc(long lFlags, DPN_APPLICATION_DESC_CDESC *ret)
  96. {
  97. HRESULT hr;
  98. DWORD dwSize = 0;
  99. DPN_APPLICATION_DESC *desc = NULL;
  100. __try {
  101. DPF(1,"-----Entering (DplayPeer) GetAppDesc call...\n");
  102. //First get the size
  103. hr = m__dxj_DirectPlayPeer->GetApplicationDesc(NULL, &dwSize, (DWORD) lFlags);
  104. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  105. return hr;
  106. desc = (DPN_APPLICATION_DESC*) new BYTE[dwSize];
  107. if (!desc)
  108. return E_OUTOFMEMORY;
  109. ZeroMemory(desc, dwSize);
  110. desc->dwSize = sizeof(DPN_APPLICATION_DESC);
  111. if (FAILED( hr= m__dxj_DirectPlayPeer->GetApplicationDesc(desc, &dwSize, (DWORD) lFlags) ) )
  112. return hr;
  113. // Now return the vals
  114. ret->lSize = dwSize;
  115. ret->lFlags = desc->dwFlags;
  116. ret->guidInstance = GUIDtoBSTR(&desc->guidInstance);
  117. ret->guidApplication = GUIDtoBSTR(&desc->guidApplication);
  118. ret->lMaxPlayers = desc->dwMaxPlayers;
  119. ret->lCurrentPlayers = desc->dwCurrentPlayers;
  120. ret->SessionName = SysAllocString(desc->pwszSessionName);
  121. ret->Password = SysAllocString(desc->pwszPassword);
  122. }
  123. __except(EXCEPTION_EXECUTE_HANDLER)
  124. {
  125. return E_FAIL;
  126. }
  127. return S_OK;
  128. }
  129. HRESULT C_dxj_DirectPlayPeerObject::SetApplicationDesc(DPN_APPLICATION_DESC_CDESC *AppDesc, long lFlags)
  130. {
  131. HRESULT hr;
  132. DPN_APPLICATION_DESC *desc = NULL;
  133. GUID guidApp;
  134. GUID guidInst;
  135. WCHAR wszSessionName[MAX_PATH];
  136. WCHAR wszPassword[MAX_PATH];
  137. __try {
  138. DPF(1,"-----Entering (DplayPeer) SetAppDesc call...\n");
  139. desc = (DPN_APPLICATION_DESC*) new BYTE[AppDesc->lSize];
  140. if (!desc)
  141. return E_OUTOFMEMORY;
  142. ZeroMemory(desc, AppDesc->lSize);
  143. // Set up our Desc
  144. desc->dwSize = sizeof(DPN_APPLICATION_DESC);
  145. if (!IsEmptyString(AppDesc->SessionName))
  146. {
  147. wcscpy(wszSessionName,AppDesc->SessionName);
  148. desc->pwszSessionName = wszSessionName;
  149. }
  150. if (!IsEmptyString(AppDesc->Password))
  151. {
  152. wcscpy(wszPassword,AppDesc->Password);
  153. desc->pwszPassword = wszPassword;
  154. }
  155. desc->dwFlags = AppDesc->lFlags;
  156. desc->dwMaxPlayers = AppDesc->lMaxPlayers;
  157. desc->dwCurrentPlayers = AppDesc->lCurrentPlayers;
  158. if (FAILED(hr = DPLAYBSTRtoGUID(&guidApp, AppDesc->guidApplication) ) )
  159. return hr;
  160. desc->guidApplication = guidApp;
  161. if (FAILED(hr = DPLAYBSTRtoGUID(&guidInst, AppDesc->guidInstance) ) )
  162. return hr;
  163. desc->guidInstance = guidInst;
  164. if (FAILED( hr= m__dxj_DirectPlayPeer->SetApplicationDesc(desc, (DWORD) lFlags) ) )
  165. return hr;
  166. SAFE_DELETE_ARRAY(desc);
  167. }
  168. __except(EXCEPTION_EXECUTE_HANDLER)
  169. {
  170. return E_FAIL;
  171. }
  172. return S_OK;
  173. }
  174. HRESULT C_dxj_DirectPlayPeerObject::Close(long lFlags)
  175. {
  176. HRESULT hr;
  177. BOOL bGotMsg = FALSE;
  178. BOOL bWait = FALSE;
  179. DWORD dwObj = 0;
  180. int i=0;
  181. MSG msg;
  182. HANDLE hThread = NULL;
  183. __try {
  184. DPF(1,"-----Entering (DplayPeer) Close call...\n");
  185. FlushBuffer(0);
  186. DWORD dwThread = 0;
  187. hThread = CreateThread(NULL, 0, ClosePeerThreadProc, this->m__dxj_DirectPlayPeer, 0, &dwThread);
  188. msg.message = WM_NULL;
  189. while ((WM_QUIT != msg.message) && (!bWait))
  190. {
  191. bGotMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  192. i++;
  193. if ((!bGotMsg) || (i>10))
  194. {
  195. dwObj = WaitForSingleObject(hThread, 10);
  196. bWait = (dwObj == WAIT_OBJECT_0);
  197. i = 0;
  198. }
  199. if (bGotMsg)
  200. {
  201. TranslateMessage( &msg );
  202. DispatchMessage( &msg );
  203. }
  204. bGotMsg = FALSE;
  205. }
  206. }
  207. __except(EXCEPTION_EXECUTE_HANDLER)
  208. {
  209. return E_FAIL;
  210. }
  211. CloseHandle(hThread);
  212. DPF(1,"-----Leaving (DplayPeer) Close call...\n");
  213. return S_OK;
  214. }
  215. HRESULT C_dxj_DirectPlayPeerObject::GetCaps(long lFlags, DPNCAPS_CDESC *ret)
  216. {
  217. HRESULT hr;
  218. __try {
  219. DPF(1,"-----Entering (DplayPeer) GetCaps call...\n");
  220. ret->lSize = sizeof(DPN_CAPS);
  221. if (FAILED (hr=m__dxj_DirectPlayPeer->GetCaps( (DPN_CAPS*) ret, (DWORD) lFlags) ) )
  222. return hr;
  223. }
  224. __except(EXCEPTION_EXECUTE_HANDLER)
  225. {
  226. return E_FAIL;
  227. }
  228. return S_OK;
  229. }
  230. HRESULT C_dxj_DirectPlayPeerObject::SetCaps(DPNCAPS_CDESC *Caps, long lFlags)
  231. {
  232. HRESULT hr;
  233. __try {
  234. DPF(1,"-----Entering (DplayPeer) SetCaps call...\n");
  235. if (FAILED( hr = m__dxj_DirectPlayPeer->SetCaps((DPN_CAPS*)Caps, (DWORD)lFlags)))
  236. return hr;
  237. }
  238. __except(EXCEPTION_EXECUTE_HANDLER)
  239. {
  240. return E_FAIL;
  241. }
  242. return S_OK;
  243. }
  244. HRESULT C_dxj_DirectPlayPeerObject::DestroyGroup(long idGroup,long lFlags, long *hAsyncHandle)
  245. {
  246. HRESULT hr;
  247. DPNHANDLE *dpAsync = NULL;
  248. __try {
  249. if (!(lFlags & DPNSEND_SYNC))
  250. {
  251. dpAsync = new DPNHANDLE;
  252. if (!dpAsync)
  253. return E_OUTOFMEMORY;
  254. }
  255. DPF(1,"-----Entering (DplayPeer) DestroyGroup call...\n");
  256. hr= m__dxj_DirectPlayPeer->DestroyGroup((DPNID) idGroup, NULL, dpAsync, (DWORD) lFlags);
  257. if (dpAsync)
  258. {
  259. *hAsyncHandle = (long)*dpAsync;
  260. SAFE_DELETE(dpAsync);
  261. }
  262. if ((hr != DPNERR_PENDING) && FAILED(hr))
  263. return hr;
  264. }
  265. __except(EXCEPTION_EXECUTE_HANDLER)
  266. {
  267. return E_FAIL;
  268. }
  269. return S_OK;
  270. }
  271. HRESULT C_dxj_DirectPlayPeerObject::RemovePlayerFromGroup(long idGroup, long idClient,long lFlags, long *hAsyncHandle)
  272. {
  273. HRESULT hr;
  274. DPNHANDLE *dpAsync = NULL;
  275. __try {
  276. if (!(lFlags & DPNSEND_SYNC))
  277. {
  278. dpAsync = new DPNHANDLE;
  279. if (!dpAsync)
  280. return E_OUTOFMEMORY;
  281. }
  282. DPF(1,"-----Entering (DplayPeer) RemovePlayerFromGroup call...\n");
  283. hr = m__dxj_DirectPlayPeer->RemovePlayerFromGroup( (DPNID) idGroup,
  284. (DPNID) idClient, NULL, dpAsync, (DWORD) lFlags);
  285. if (dpAsync)
  286. {
  287. *hAsyncHandle = (long)*dpAsync;
  288. SAFE_DELETE(dpAsync);
  289. }
  290. if ((hr != DPNERR_PENDING) && FAILED(hr))
  291. return hr;
  292. }
  293. __except(EXCEPTION_EXECUTE_HANDLER)
  294. {
  295. return E_FAIL;
  296. }
  297. return S_OK;
  298. }
  299. HRESULT C_dxj_DirectPlayPeerObject::DestroyPeer(long idClient, long lFlags, void *UserData, long UserDataSize)
  300. {
  301. HRESULT hr;
  302. __try {
  303. DPF(1,"-----Entering (DplayPeer) DestroyPeer call...\n");
  304. if (FAILED( hr= m__dxj_DirectPlayPeer->DestroyPeer((DPNID) idClient, UserData, UserDataSize, (DWORD) lFlags) ))
  305. return hr;
  306. }
  307. __except(EXCEPTION_EXECUTE_HANDLER)
  308. {
  309. return E_FAIL;
  310. }
  311. return S_OK;
  312. }
  313. HRESULT C_dxj_DirectPlayPeerObject::Connect(DPN_APPLICATION_DESC_CDESC *AppDesc,I_dxj_DirectPlayAddress *Address,I_dxj_DirectPlayAddress *DeviceInfo, long lFlags, void *UserData, long UserDataSize, long *hAsyncHandle)
  314. {
  315. HRESULT hr;
  316. DPN_APPLICATION_DESC desc;
  317. WCHAR wszSessionName[MAX_PATH];
  318. WCHAR wszPassword[MAX_PATH];
  319. DPNHANDLE *dpAsync = NULL;
  320. __try {
  321. if (!(lFlags & DPNSEND_SYNC))
  322. {
  323. dpAsync = new DPNHANDLE;
  324. if (!dpAsync)
  325. return E_OUTOFMEMORY;
  326. }
  327. DPF(1,"-----Entering (DplayPeer) Connect call...\n");
  328. if (!IsEmptyString(AppDesc->SessionName)) wcscpy(wszSessionName,AppDesc->SessionName);
  329. if (!IsEmptyString(AppDesc->Password)) wcscpy(wszPassword,AppDesc->Password);
  330. ZeroMemory(&desc, sizeof(desc));
  331. // Set up our Desc
  332. desc.dwSize = sizeof(DPN_APPLICATION_DESC);
  333. desc.dwFlags = AppDesc->lFlags;
  334. desc.dwMaxPlayers = AppDesc->lMaxPlayers;
  335. desc.dwCurrentPlayers = AppDesc->lCurrentPlayers;
  336. if (!IsEmptyString(AppDesc->SessionName))
  337. desc.pwszSessionName = wszSessionName;
  338. if (!IsEmptyString(AppDesc->Password))
  339. desc.pwszPassword = wszPassword;
  340. if (AppDesc->guidApplication)
  341. {
  342. if (FAILED(hr = DPLAYBSTRtoGUID(&desc.guidApplication, AppDesc->guidApplication) ) )
  343. return hr;
  344. }
  345. if (AppDesc->guidInstance)
  346. {
  347. if (FAILED(hr = DPLAYBSTRtoGUID(&desc.guidInstance , AppDesc->guidInstance) ) )
  348. return hr;
  349. }
  350. // Get our host and device address
  351. IDirectPlay8Address *lpAddress = NULL;
  352. IDirectPlay8Address *lpDevice = NULL;
  353. if(Address)
  354. {
  355. Address->InternalGetObject((IUnknown **)(&lpAddress));
  356. }
  357. if(DeviceInfo)
  358. {
  359. DeviceInfo->InternalGetObject((IUnknown **)(&lpDevice));
  360. }
  361. // Time to connect
  362. hr = m__dxj_DirectPlayPeer->Connect(&desc, lpAddress, lpDevice, NULL, NULL, UserData, (DWORD)UserDataSize, NULL, NULL, dpAsync, (DWORD) lFlags);
  363. if (dpAsync)
  364. {
  365. *hAsyncHandle = (long)*dpAsync;
  366. SAFE_DELETE(dpAsync);
  367. }
  368. if ((hr != DPNERR_PENDING) && FAILED(hr))
  369. return hr;
  370. }
  371. __except(EXCEPTION_EXECUTE_HANDLER)
  372. {
  373. return E_FAIL;
  374. }
  375. return S_OK;
  376. }
  377. HRESULT C_dxj_DirectPlayPeerObject::GetCountPlayersAndGroups(long lFlags, long *ret)
  378. {
  379. HRESULT hr;
  380. __try {
  381. DPF(1,"-----Entering (DplayPeer) GetCountPlayersAndGroups call...\n");
  382. // On the GetCount call we will always get the latest info
  383. if (FAILED ( hr = GetClientsAndGroups(lFlags) ) )
  384. return hr;
  385. *ret = m_dwClientCount;
  386. }
  387. __except(EXCEPTION_EXECUTE_HANDLER)
  388. {
  389. return E_FAIL;
  390. }
  391. return S_OK;
  392. }
  393. HRESULT C_dxj_DirectPlayPeerObject::GetPlayerOrGroup(long lIndex, long *ret)
  394. {
  395. __try {
  396. DPF(1,"-----Entering (DplayPeer) GetPlayerOrGroup call...\n");
  397. if (!m_ClientsGroups)
  398. return E_INVALIDARG;
  399. if ((lIndex < 1 ) || ((DWORD)lIndex > m_dwClientCount))
  400. return E_INVALIDARG;
  401. // Fill out our structure
  402. *ret = m_ClientsGroups[lIndex - 1];
  403. }
  404. __except(EXCEPTION_EXECUTE_HANDLER)
  405. {
  406. return E_FAIL;
  407. }
  408. return S_OK;
  409. }
  410. HRESULT C_dxj_DirectPlayPeerObject::GetCountGroupMembers(long dpid,long lFlags, long *ret)
  411. {
  412. HRESULT hr;
  413. __try {
  414. DPF(1,"-----Entering (DplayPeer) GetCountGroupMembers call...\n");
  415. // On the GetCount call we will always get the latest info
  416. if (FAILED ( hr = GetGroupMembers(lFlags, (DPNID) dpid) ) )
  417. return hr;
  418. *ret = m_dwGroupMemberCount;
  419. }
  420. __except(EXCEPTION_EXECUTE_HANDLER)
  421. {
  422. return E_FAIL;
  423. }
  424. return S_OK;
  425. }
  426. HRESULT C_dxj_DirectPlayPeerObject::GetGroupMember(long lIndex,long dpid, long *ret)
  427. {
  428. HRESULT hr;
  429. __try {
  430. DPF(1,"-----Entering (DplayPeer) GetGroupMember call...\n");
  431. if ((!m_GroupMembers) || ((DPNID)dpid != m_dwGroupID) )
  432. if (FAILED (hr = GetGroupMembers(0, (DPNID) dpid) ) )
  433. return hr;
  434. if ((lIndex < 1 ) || ((DWORD)lIndex > m_dwGroupMemberCount))
  435. return E_INVALIDARG;
  436. if (!m_GroupMembers)
  437. return E_INVALIDARG;
  438. // Fill out our structure
  439. *ret = m_GroupMembers[lIndex - 1];
  440. }
  441. __except(EXCEPTION_EXECUTE_HANDLER)
  442. {
  443. return E_FAIL;
  444. }
  445. return S_OK;
  446. }
  447. HRESULT C_dxj_DirectPlayPeerObject::EnumHosts(DPN_APPLICATION_DESC_CDESC *ApplicationDesc,I_dxj_DirectPlayAddress *AddrHost,I_dxj_DirectPlayAddress *DeviceInfo,long lRetryCount, long lRetryInterval, long lTimeOut,long lFlags, void *UserData, long UserDataSize, long *lAsync)
  448. {
  449. HRESULT hr;
  450. DPN_APPLICATION_DESC desc;
  451. WCHAR wszSessionName[MAX_PATH];
  452. WCHAR wszPassword[MAX_PATH];
  453. DPNHANDLE *dpAsync = NULL;
  454. __try {
  455. if (!(lFlags & DPNSEND_SYNC))
  456. {
  457. dpAsync = new DPNHANDLE;
  458. if (!dpAsync)
  459. return E_OUTOFMEMORY;
  460. }
  461. DPF(1,"-----Entering (DplayPeer) EnumHosts call...\n");
  462. if (!IsEmptyString(ApplicationDesc->SessionName)) wcscpy(wszSessionName,ApplicationDesc->SessionName);
  463. if (!IsEmptyString(ApplicationDesc->Password)) wcscpy(wszPassword,ApplicationDesc->Password);
  464. ZeroMemory(&desc, sizeof(desc));
  465. // Set up our Desc
  466. desc.dwSize = sizeof(DPN_APPLICATION_DESC);
  467. desc.dwFlags = ApplicationDesc->lFlags;
  468. desc.dwMaxPlayers = ApplicationDesc->lMaxPlayers;
  469. desc.dwCurrentPlayers = ApplicationDesc->lCurrentPlayers;
  470. if (!IsEmptyString(ApplicationDesc->SessionName))
  471. desc.pwszSessionName = wszSessionName;
  472. if (!IsEmptyString(ApplicationDesc->Password))
  473. desc.pwszPassword = wszPassword;
  474. if (ApplicationDesc->guidApplication)
  475. {
  476. if (FAILED(hr = DPLAYBSTRtoGUID(&desc.guidApplication, ApplicationDesc->guidApplication) ) )
  477. return hr;
  478. }
  479. if (ApplicationDesc->guidInstance)
  480. {
  481. if (FAILED(hr = DPLAYBSTRtoGUID(&desc.guidInstance, ApplicationDesc->guidInstance) ) )
  482. return hr;
  483. }
  484. // Get our host and device address
  485. DO_GETOBJECT_NOTNULL( IDirectPlay8Address*, lpHost, AddrHost);
  486. DO_GETOBJECT_NOTNULL( IDirectPlay8Address*, lpDevice, DeviceInfo);
  487. hr = m__dxj_DirectPlayPeer->EnumHosts(&desc, lpHost, lpDevice, UserData, UserDataSize, (DWORD) lRetryCount, (DWORD) lRetryInterval, (DWORD) lTimeOut, NULL, dpAsync, (DWORD) lFlags);
  488. // This should return E_PENDING
  489. if (dpAsync)
  490. {
  491. *lAsync = (long)*dpAsync;
  492. SAFE_DELETE(dpAsync);
  493. }
  494. if( hr != E_PENDING && FAILED(hr) )
  495. return hr;
  496. }
  497. __except(EXCEPTION_EXECUTE_HANDLER)
  498. {
  499. return E_FAIL;
  500. }
  501. return S_OK;
  502. }
  503. HRESULT C_dxj_DirectPlayPeerObject::ReturnBuffer(long lBufferHandle)
  504. {
  505. HRESULT hr;
  506. __try {
  507. //We should actually never get here since this is hidden, but just in case
  508. DPF(1,"-----Entering (DplayPeer) ReturnBuffer call...\n");
  509. if (FAILED(hr = m__dxj_DirectPlayPeer->ReturnBuffer( (DPNHANDLE) lBufferHandle, 0 ) ) )
  510. return hr;
  511. }
  512. __except(EXCEPTION_EXECUTE_HANDLER)
  513. {
  514. return E_FAIL;
  515. }
  516. return S_OK;
  517. }
  518. HRESULT C_dxj_DirectPlayPeerObject::Host(DPN_APPLICATION_DESC_CDESC *AppDesc,I_dxj_DirectPlayAddress *Address, long lFlags)
  519. {
  520. HRESULT hr;
  521. DPN_APPLICATION_DESC desc;
  522. GUID guidApp;
  523. GUID guidInst;
  524. WCHAR wszSessionName[MAX_PATH];
  525. WCHAR wszPassword[MAX_PATH];
  526. __try {
  527. DPF(1,"-----Entering (DplayPeer) Host call...\n");
  528. if (!IsEmptyString(AppDesc->SessionName)) wcscpy(wszSessionName,AppDesc->SessionName);
  529. if (!IsEmptyString(AppDesc->Password)) wcscpy(wszPassword,AppDesc->Password);
  530. DO_GETOBJECT_NOTNULL( IDirectPlay8Address*, lpAddress, Address);
  531. ZeroMemory(&desc, sizeof(DPN_APPLICATION_DESC) );
  532. desc.dwSize = sizeof(DPN_APPLICATION_DESC);
  533. desc.dwFlags = AppDesc->lFlags;
  534. desc.dwMaxPlayers = AppDesc->lMaxPlayers;
  535. desc.dwCurrentPlayers = AppDesc->lCurrentPlayers;
  536. if (!IsEmptyString(AppDesc->SessionName))
  537. desc.pwszSessionName = wszSessionName;
  538. if (!IsEmptyString(AppDesc->Password))
  539. desc.pwszPassword = wszPassword;
  540. if (AppDesc->guidApplication)
  541. {
  542. if (FAILED(hr = DPLAYBSTRtoGUID(&guidApp, AppDesc->guidApplication) ) )
  543. return hr;
  544. desc.guidApplication = guidApp;
  545. }
  546. if (AppDesc->guidInstance)
  547. {
  548. if (FAILED(hr = DPLAYBSTRtoGUID(&guidInst, AppDesc->guidInstance) ) )
  549. return hr;
  550. desc.guidInstance = guidInst;
  551. }
  552. if (FAILED (hr = m__dxj_DirectPlayPeer->Host(&desc, &lpAddress, 1, NULL, NULL, NULL, (DWORD) lFlags ) ) )
  553. return hr;
  554. }
  555. __except(EXCEPTION_EXECUTE_HANDLER)
  556. {
  557. return E_FAIL;
  558. }
  559. return S_OK;
  560. }
  561. HRESULT C_dxj_DirectPlayPeerObject::SendTo(long idSend, SAFEARRAY **Buffer, long lTimeOut,long lFlags, long *hAsyncHandle)
  562. {
  563. HRESULT hr;
  564. DPN_BUFFER_DESC lpBuf;
  565. DWORD dwBufSize = ((SAFEARRAY*)*Buffer)->rgsabound[0].cElements;
  566. DPNHANDLE *dpAsync = NULL;
  567. __try {
  568. DPF(1,"-----Entering (DplayPeer) SendTo call...\n");
  569. if (!(lFlags & DPNSEND_SYNC))
  570. {
  571. dpAsync = new DPNHANDLE;
  572. if (!dpAsync)
  573. return E_OUTOFMEMORY;
  574. }
  575. lpBuf.dwBufferSize = dwBufSize;
  576. lpBuf.pBufferData = (BYTE*)((SAFEARRAY*)*Buffer)->pvData;
  577. hr = m__dxj_DirectPlayPeer->SendTo((DPNID) idSend, &lpBuf, 1, (DWORD) lTimeOut, NULL, dpAsync, (DWORD) lFlags);
  578. if (dpAsync)
  579. {
  580. *hAsyncHandle = (long)*dpAsync;
  581. SAFE_DELETE(dpAsync);
  582. }
  583. if ((hr != DPNERR_PENDING) && FAILED(hr))
  584. return hr;
  585. }
  586. __except(EXCEPTION_EXECUTE_HANDLER)
  587. {
  588. return E_FAIL;
  589. }
  590. return S_OK;
  591. }
  592. HRESULT C_dxj_DirectPlayPeerObject::CreateGroup(DPN_GROUP_INFO_CDESC *GroupInfo, long lFlags, long *hAsyncHandle)
  593. {
  594. HRESULT hr;
  595. DPN_GROUP_INFO dpnGroup;
  596. WCHAR wszName[MAX_PATH];
  597. DPNHANDLE *dpAsync = NULL;
  598. __try {
  599. if (!(lFlags & DPNSEND_SYNC))
  600. {
  601. dpAsync = new DPNHANDLE;
  602. if (!dpAsync)
  603. return E_OUTOFMEMORY;
  604. }
  605. DPF(1,"-----Entering (DplayPeer) CreateGroup call...\n");
  606. if (!IsEmptyString(GroupInfo->Name)) wcscpy(wszName,GroupInfo->Name);
  607. ZeroMemory(&dpnGroup, sizeof(DPN_GROUP_INFO) );
  608. dpnGroup.dwSize = sizeof(DPN_GROUP_INFO);
  609. dpnGroup.dwInfoFlags = GroupInfo->lInfoFlags;
  610. dpnGroup.dwGroupFlags = GroupInfo->lGroupFlags;
  611. dpnGroup.pwszName = wszName;
  612. hr = m__dxj_DirectPlayPeer->CreateGroup(&dpnGroup, NULL, NULL, dpAsync, (DWORD) lFlags);
  613. if (dpAsync)
  614. {
  615. *hAsyncHandle = (long)*dpAsync;
  616. SAFE_DELETE(dpAsync);
  617. }
  618. if ((hr != DPNERR_PENDING) && FAILED(hr))
  619. return hr;
  620. }
  621. __except(EXCEPTION_EXECUTE_HANDLER)
  622. {
  623. return E_FAIL;
  624. }
  625. return S_OK;
  626. }
  627. HRESULT C_dxj_DirectPlayPeerObject::AddPlayerToGroup(long idGroup, long idClient,long lFlags, long *hAsyncHandle)
  628. {
  629. HRESULT hr;
  630. DPNHANDLE *dpAsync = NULL;
  631. __try {
  632. if (!(lFlags & DPNSEND_SYNC))
  633. {
  634. dpAsync = new DPNHANDLE;
  635. if (!dpAsync)
  636. return E_OUTOFMEMORY;
  637. }
  638. DPF(1,"-----Entering (DplayPeer) AddPlayerToGroup call...\n");
  639. hr = m__dxj_DirectPlayPeer->AddPlayerToGroup((DPNID) idGroup, (DPNID) idClient, NULL, dpAsync, (DWORD) lFlags);
  640. if (dpAsync)
  641. {
  642. *hAsyncHandle = (long)*dpAsync;
  643. SAFE_DELETE(dpAsync);
  644. }
  645. if ((hr != DPNERR_PENDING) && FAILED(hr))
  646. return hr;
  647. }
  648. __except(EXCEPTION_EXECUTE_HANDLER)
  649. {
  650. return E_FAIL;
  651. }
  652. return S_OK;
  653. }
  654. HRESULT C_dxj_DirectPlayPeerObject::GetSendQueueInfo(long idPlayer, long *lNumMsgs, long *lNumBytes, long lFlags)
  655. {
  656. HRESULT hr;
  657. __try {
  658. DPF(1,"-----Entering (DplayPeer) GetSendQueueInfo call...\n");
  659. if (FAILED (hr = m__dxj_DirectPlayPeer->GetSendQueueInfo((DPNID) idPlayer, (DWORD*)lNumMsgs, (DWORD*)lNumBytes, (DWORD) lFlags) ) )
  660. return hr;
  661. }
  662. __except(EXCEPTION_EXECUTE_HANDLER)
  663. {
  664. return E_FAIL;
  665. }
  666. return S_OK;
  667. }
  668. HRESULT C_dxj_DirectPlayPeerObject::SetGroupInfo(long idGroup, DPN_GROUP_INFO_CDESC *PlayerInfo, long lFlags, long *hAsyncHandle)
  669. {
  670. HRESULT hr;
  671. DPN_GROUP_INFO dpInfo;
  672. DPNHANDLE *dpAsync = NULL;
  673. __try {
  674. if (!(lFlags & DPNSEND_SYNC))
  675. {
  676. dpAsync = new DPNHANDLE;
  677. if (!dpAsync)
  678. return E_OUTOFMEMORY;
  679. }
  680. DPF(1,"-----Entering (DplayPeer) SetGroupInfo call...\n");
  681. ZeroMemory(&dpInfo, sizeof(DPN_GROUP_INFO) );
  682. dpInfo.dwSize = sizeof(DPN_GROUP_INFO);
  683. dpInfo.dwInfoFlags = PlayerInfo->lInfoFlags;
  684. dpInfo.pwszName = PlayerInfo->Name;
  685. dpInfo.dwGroupFlags = PlayerInfo->lGroupFlags;
  686. hr = m__dxj_DirectPlayPeer->SetGroupInfo((DPNID) idGroup, &dpInfo, NULL, dpAsync, (DWORD) lFlags);
  687. if (dpAsync)
  688. {
  689. *hAsyncHandle = (long)*dpAsync;
  690. SAFE_DELETE(dpAsync);
  691. }
  692. if ((hr != DPNERR_PENDING) && FAILED(hr))
  693. return hr;
  694. }
  695. __except(EXCEPTION_EXECUTE_HANDLER)
  696. {
  697. return E_FAIL;
  698. }
  699. return S_OK;
  700. }
  701. HRESULT C_dxj_DirectPlayPeerObject::GetGroupInfo(long idGroup,long lFlags, DPN_GROUP_INFO_CDESC *layerInfo)
  702. {
  703. HRESULT hr;
  704. DPN_GROUP_INFO *PlayerInfo = NULL;
  705. DWORD dwInfoSize = 0;
  706. __try {
  707. DPF(1,"-----Entering (DplayPeer) GetGroupInfo call...\n");
  708. hr = m__dxj_DirectPlayPeer->GetGroupInfo( (DPNID) idGroup, NULL, &dwInfoSize, (DWORD) lFlags );
  709. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  710. return hr;
  711. PlayerInfo = (DPN_GROUP_INFO*) new BYTE[ dwInfoSize ];
  712. if (!PlayerInfo)
  713. return E_OUTOFMEMORY;
  714. ZeroMemory( PlayerInfo, dwInfoSize );
  715. PlayerInfo->dwSize = sizeof(DPN_GROUP_INFO);
  716. hr = m__dxj_DirectPlayPeer->GetGroupInfo( (DPNID) idGroup, PlayerInfo, &dwInfoSize, (DWORD)lFlags );
  717. if( FAILED(hr) )
  718. return hr;
  719. layerInfo->lSize = sizeof(DPN_GROUP_INFO_CDESC);
  720. layerInfo->lInfoFlags = PlayerInfo->dwInfoFlags;
  721. layerInfo->Name = SysAllocString(PlayerInfo->pwszName);
  722. layerInfo->lGroupFlags = PlayerInfo->dwGroupFlags;
  723. // We no longer need the playerinfo we got.. get rid of it..
  724. SAFE_DELETE_ARRAY(PlayerInfo);
  725. }
  726. __except(EXCEPTION_EXECUTE_HANDLER)
  727. {
  728. return E_FAIL;
  729. }
  730. return S_OK;
  731. }
  732. HRESULT C_dxj_DirectPlayPeerObject::SetPeerInfo(DPN_PLAYER_INFO_CDESC *PlayerInfo,long lFlags, long *hAsyncHandle)
  733. {
  734. HRESULT hr;
  735. DPN_PLAYER_INFO dpInfo;
  736. DPNHANDLE *dpAsync = NULL;
  737. __try {
  738. if (!(lFlags & DPNSEND_SYNC))
  739. {
  740. dpAsync = new DPNHANDLE;
  741. if (!dpAsync)
  742. return E_OUTOFMEMORY;
  743. }
  744. DPF(1,"-----Entering (DplayPeer) SetPeerInfo call...\n");
  745. ZeroMemory(&dpInfo, sizeof(DPN_PLAYER_INFO) );
  746. dpInfo.dwSize = sizeof(DPN_PLAYER_INFO);
  747. dpInfo.dwInfoFlags = PlayerInfo->lInfoFlags;
  748. dpInfo.pwszName = PlayerInfo->Name;
  749. dpInfo.dwPlayerFlags = PlayerInfo->lPlayerFlags;
  750. hr = m__dxj_DirectPlayPeer->SetPeerInfo(&dpInfo, NULL, dpAsync, (DWORD) lFlags);
  751. if (dpAsync)
  752. {
  753. *hAsyncHandle = (long)*dpAsync;
  754. SAFE_DELETE(dpAsync);
  755. }
  756. if ((hr != DPNERR_PENDING) && FAILED(hr))
  757. return hr;
  758. }
  759. __except(EXCEPTION_EXECUTE_HANDLER)
  760. {
  761. return E_FAIL;
  762. }
  763. return S_OK;
  764. }
  765. HRESULT C_dxj_DirectPlayPeerObject::GetPeerInfo(long idPeer,long lFlags, DPN_PLAYER_INFO_CDESC *layerInfo)
  766. {
  767. HRESULT hr;
  768. DWORD dwSize = 0;
  769. DPN_PLAYER_INFO *PlayerInfo = NULL;
  770. DPF(1,"-----Entering (DplayPeer) GetPeerInfo call...\n");
  771. __try
  772. {
  773. hr = m__dxj_DirectPlayPeer->GetPeerInfo( (DPNID) idPeer, NULL, &dwSize, (DWORD) lFlags );
  774. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  775. return hr;
  776. PlayerInfo = (DPN_PLAYER_INFO*) new BYTE[ dwSize ];
  777. if (!PlayerInfo)
  778. return E_OUTOFMEMORY;
  779. ZeroMemory( PlayerInfo, dwSize );
  780. PlayerInfo->dwSize = sizeof(DPN_PLAYER_INFO);
  781. hr = m__dxj_DirectPlayPeer->GetPeerInfo( (DPNID) idPeer, PlayerInfo, &dwSize, (DWORD) lFlags );
  782. if( FAILED(hr) )
  783. return hr;
  784. layerInfo->lSize = sizeof(DPN_PLAYER_INFO_CDESC);
  785. layerInfo->lInfoFlags = PlayerInfo->dwInfoFlags;
  786. layerInfo->Name = SysAllocString(PlayerInfo->pwszName);
  787. layerInfo->lPlayerFlags = PlayerInfo->dwPlayerFlags;
  788. // We no longer need the playerinfo we got.. get rid of it..
  789. SAFE_DELETE_ARRAY(PlayerInfo);
  790. return S_OK;
  791. }
  792. __except(EXCEPTION_EXECUTE_HANDLER)
  793. {
  794. DPF(1,"***** GetPeerInfo ERROR...\n");
  795. return E_FAIL;
  796. }
  797. }
  798. HRESULT C_dxj_DirectPlayPeerObject::GetCountServiceProviders(long lFlags, long *ret)
  799. {
  800. HRESULT hr;
  801. __try {
  802. DPF(1,"-----Entering (DplayPeer) GetCountSP call...\n");
  803. if (!m_SPInfo)
  804. if (FAILED (hr=GetSP(lFlags) ) )
  805. return hr;
  806. *ret = (long)m_dwSPCount;
  807. }
  808. __except(EXCEPTION_EXECUTE_HANDLER)
  809. {
  810. return E_FAIL;
  811. }
  812. return S_OK;
  813. }
  814. HRESULT C_dxj_DirectPlayPeerObject::GetServiceProvider(long lIndex, DPN_SERVICE_PROVIDER_INFO_CDESC *ret)
  815. {
  816. HRESULT hr;
  817. GUID *guidSP = NULL;
  818. __try {
  819. DPF(1,"-----Entering (DplayPeer) GetSP call...\n");
  820. if (!m_SPInfo)
  821. if (FAILED (hr=GetSP(0) ) )
  822. return hr;
  823. if ((lIndex < 1 ) || ((DWORD)lIndex > m_dwSPCount))
  824. return E_INVALIDARG;
  825. // Fill out our structure
  826. ret->lFlags = (long) m_SPInfo[lIndex-1].dwFlags;
  827. ret->Name = SysAllocString(m_SPInfo[lIndex-1].pwszName);
  828. guidSP = new GUID;
  829. if (!guidSP)
  830. return E_OUTOFMEMORY;
  831. memcpy(guidSP,&m_SPInfo[lIndex-1].guid,sizeof(GUID));
  832. ret->Guid = GUIDtoBSTR(guidSP);
  833. }
  834. __except(EXCEPTION_EXECUTE_HANDLER)
  835. {
  836. return E_FAIL;
  837. }
  838. return S_OK;
  839. }
  840. HRESULT C_dxj_DirectPlayPeerObject::GetSP(long lFlags)
  841. {
  842. // Enumerate all DirectPlay sevice providers
  843. HRESULT hr;
  844. DWORD dwSize=0;
  845. DWORD dwItems=0;
  846. __try {
  847. SAFE_DELETE_ARRAY(m_SPInfo);
  848. hr = m__dxj_DirectPlayPeer->EnumServiceProviders( NULL, NULL, m_SPInfo, &dwSize,
  849. &dwItems, (DWORD) lFlags );
  850. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  851. return hr;
  852. m_SPInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
  853. if (!m_SPInfo)
  854. return E_OUTOFMEMORY;
  855. ZeroMemory(m_SPInfo, dwSize);
  856. if( FAILED( hr = m__dxj_DirectPlayPeer->EnumServiceProviders( NULL, NULL, m_SPInfo, &dwSize,
  857. &dwItems, (DWORD) lFlags ) ) )
  858. return hr;
  859. m_dwSPCount = dwItems;
  860. }
  861. __except(EXCEPTION_EXECUTE_HANDLER)
  862. {
  863. return E_FAIL;
  864. }
  865. return S_OK;
  866. }
  867. HRESULT C_dxj_DirectPlayPeerObject::GetClientsAndGroups(long lFlags)
  868. {
  869. // Enumerate all DirectPlay clients and groups
  870. HRESULT hr;
  871. DWORD dwSize=0;
  872. __try {
  873. SAFE_DELETE_ARRAY(m_ClientsGroups);
  874. hr = m__dxj_DirectPlayPeer->EnumPlayersAndGroups(NULL, &dwSize, (DWORD) lFlags);
  875. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  876. return hr;
  877. m_ClientsGroups = (DPNID*) new BYTE[dwSize * sizeof(DPNID)];
  878. if (!m_ClientsGroups)
  879. return E_OUTOFMEMORY;
  880. if( FAILED( hr = m__dxj_DirectPlayPeer->EnumPlayersAndGroups(m_ClientsGroups, &dwSize, (DWORD) lFlags) ) )
  881. return hr;
  882. m_dwClientCount = dwSize;// sizeof(DPNID);
  883. }
  884. __except(EXCEPTION_EXECUTE_HANDLER)
  885. {
  886. return E_FAIL;
  887. }
  888. return S_OK;
  889. }
  890. HRESULT C_dxj_DirectPlayPeerObject::GetGroupMembers(long lFlags, DPNID dpGroupID)
  891. {
  892. // Enumerate all DirectPlay group members for this group
  893. HRESULT hr;
  894. DWORD dwSize=0;
  895. __try {
  896. SAFE_DELETE_ARRAY(m_GroupMembers);
  897. hr = m__dxj_DirectPlayPeer->EnumGroupMembers (dpGroupID, NULL, &dwSize, (DWORD) lFlags);
  898. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  899. return hr;
  900. m_GroupMembers = (DPNID*) new BYTE[dwSize * sizeof(DPNID)];
  901. if (!m_GroupMembers)
  902. return E_OUTOFMEMORY;
  903. if( FAILED( hr = m__dxj_DirectPlayPeer->EnumGroupMembers (dpGroupID, m_GroupMembers, &dwSize, (DWORD) lFlags) ) )
  904. return hr;
  905. m_dwGroupMemberCount = dwSize;
  906. m_dwGroupID = dpGroupID;
  907. }
  908. __except(EXCEPTION_EXECUTE_HANDLER)
  909. {
  910. return E_FAIL;
  911. }
  912. return S_OK;
  913. }
  914. HRESULT C_dxj_DirectPlayPeerObject::RegisterLobby(long dpnHandle, I_dxj_DirectPlayLobbiedApplication *LobbyApp, long lFlags)
  915. {
  916. HRESULT hr;
  917. __try {
  918. DPF(1,"-----Entering (DplayPeer) RegisterLobby call...\n");
  919. DO_GETOBJECT_NOTNULL( IDirectPlay8LobbiedApplication*, lpLobby, LobbyApp);
  920. if (FAILED( hr = m__dxj_DirectPlayPeer->RegisterLobby((DPNHANDLE) dpnHandle, lpLobby,(DWORD) lFlags) ) )
  921. return hr;
  922. }
  923. __except(EXCEPTION_EXECUTE_HANDLER)
  924. {
  925. return E_FAIL;
  926. }
  927. return S_OK;
  928. }
  929. HRESULT C_dxj_DirectPlayPeerObject::GetConnectionInfo(long idPlayer, long lFlags, DPN_CONNECTION_INFO_CDESC *pdpConnectionInfo)
  930. {
  931. HRESULT hr;
  932. __try {
  933. DPF(1,"-----Entering (DplayPeer) GetConnectionInfo call...\n");
  934. pdpConnectionInfo->lSize = sizeof(DPN_CONNECTION_INFO);
  935. if (FAILED( hr = m__dxj_DirectPlayPeer->GetConnectionInfo((DPNID) idPlayer, (DPN_CONNECTION_INFO*)pdpConnectionInfo, lFlags) ) )
  936. return hr;
  937. }
  938. __except(EXCEPTION_EXECUTE_HANDLER)
  939. {
  940. return E_FAIL;
  941. }
  942. return S_OK;
  943. }
  944. HRESULT C_dxj_DirectPlayPeerObject::GetPeerAddress(long idPlayer,long lFlags, I_dxj_DirectPlayAddress **pAddress)
  945. {
  946. IDirectPlay8Address *lpAdd = NULL;
  947. HRESULT hr;
  948. __try {
  949. DPF(1,"-----Entering (DplayPeer) GetPeerAddress call...\n");
  950. if (FAILED (hr = m__dxj_DirectPlayPeer->GetPeerAddress( (DPNID) idPlayer, &lpAdd, (DWORD) lFlags) ) )
  951. return hr;
  952. INTERNAL_CREATE_ADDRESS(_dxj_DirectPlayAddress,lpAdd, pAddress);
  953. }
  954. __except(EXCEPTION_EXECUTE_HANDLER)
  955. {
  956. return E_FAIL;
  957. }
  958. return S_OK;
  959. }
  960. HRESULT C_dxj_DirectPlayPeerObject::GetLocalHostAddress(long lFlags, I_dxj_DirectPlayAddress **pAddress)
  961. {
  962. IDirectPlay8Address *lpAdd = NULL;
  963. HRESULT hr;
  964. DWORD dwItems = 0;
  965. __try {
  966. DPF(1,"-----Entering (DplayPeer) GetLocalHostAddress call...\n");
  967. hr = m__dxj_DirectPlayPeer->GetLocalHostAddresses( &lpAdd,&dwItems, (DWORD) lFlags);
  968. if (FAILED (hr) && hr != DPNERR_BUFFERTOOSMALL)
  969. return hr;
  970. if (dwItems>1)
  971. return E_INVALIDARG;
  972. if ( FAILED (hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,CLSCTX_INPROC_SERVER,
  973. IID_IDirectPlay8Address, (LPVOID*) &lpAdd ) ) )
  974. return hr;
  975. hr = m__dxj_DirectPlayPeer->GetLocalHostAddresses( &lpAdd,&dwItems, (DWORD) lFlags);
  976. if (FAILED (hr))
  977. return hr;
  978. INTERNAL_CREATE_ADDRESS(_dxj_DirectPlayAddress,lpAdd, pAddress);
  979. }
  980. __except(EXCEPTION_EXECUTE_HANDLER)
  981. {
  982. return E_FAIL;
  983. }
  984. return S_OK;
  985. }
  986. HRESULT C_dxj_DirectPlayPeerObject::SetSPCaps(BSTR guidSP, DPN_SP_CAPS_CDESC *spCaps, long lFlags)
  987. {
  988. HRESULT hr;
  989. GUID guidServiceProvider;
  990. __try {
  991. DPF(1,"-----Entering (DplayPeer) SetSPCaps call...\n");
  992. if (FAILED(hr = DPLAYBSTRtoGUID(&guidServiceProvider, guidSP) ) )
  993. return hr;
  994. spCaps->lSize = sizeof(DPN_SP_CAPS);
  995. //
  996. // MiNara: Added 0 for dwFlags parameter
  997. //
  998. if (FAILED(hr = m__dxj_DirectPlayPeer->SetSPCaps(&guidServiceProvider,(DPN_SP_CAPS*)spCaps,(DWORD) lFlags) ) )
  999. return hr;
  1000. }
  1001. __except(EXCEPTION_EXECUTE_HANDLER)
  1002. {
  1003. return E_FAIL;
  1004. }
  1005. return S_OK;
  1006. }
  1007. HRESULT C_dxj_DirectPlayPeerObject::GetSPCaps(BSTR guidSP, long lFlags, DPN_SP_CAPS_CDESC *spCaps)
  1008. {
  1009. HRESULT hr;
  1010. GUID guidServiceProvider;
  1011. __try {
  1012. DPF(1,"-----Entering (DplayPeer) GetSPCaps call...\n");
  1013. spCaps->lSize = sizeof(DPN_SP_CAPS);
  1014. if (FAILED(hr = DPLAYBSTRtoGUID(&guidServiceProvider, guidSP) ) )
  1015. return hr;
  1016. if (FAILED(hr = m__dxj_DirectPlayPeer->GetSPCaps(&guidServiceProvider,(DPN_SP_CAPS*)spCaps, (DWORD) lFlags) ) )
  1017. return hr;
  1018. }
  1019. __except(EXCEPTION_EXECUTE_HANDLER)
  1020. {
  1021. return E_FAIL;
  1022. }
  1023. return S_OK;
  1024. }
  1025. HRESULT C_dxj_DirectPlayPeerObject::TerminateSession(long lFlags, void *UserData, long UserDataSize)
  1026. {
  1027. HRESULT hr;
  1028. __try {
  1029. DPF(1,"-----Entering (DplayPeer) TerminateSession call...\n");
  1030. if (FAILED (hr = m__dxj_DirectPlayPeer->TerminateSession(UserData,UserDataSize,(DWORD) lFlags)))
  1031. return hr;
  1032. }
  1033. __except(EXCEPTION_EXECUTE_HANDLER)
  1034. {
  1035. return E_FAIL;
  1036. }
  1037. return S_OK;
  1038. }
  1039. HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext,
  1040. DWORD dwMessageId,
  1041. PVOID pMsgBuffer )
  1042. {
  1043. HRESULT hr = S_OK;
  1044. LPUNKNOWN lpUnk = NULL;
  1045. BOOL fCallCoUninit = FALSE;
  1046. VARIANT_BOOL fRejectMsg = VARIANT_FALSE;
  1047. // User context for the message handler is a pointer to our class module.
  1048. C_dxj_DirectPlayPeerObject *lpPeer = (C_dxj_DirectPlayPeerObject*)pvUserContext;
  1049. if (!lpPeer)
  1050. return S_OK; //Object must be gone
  1051. DPF2(1,"-----Entering (DplayPeer) MessageHandler call... (Current msg count=%d) MSGID = %d\n", lpPeer->m_dwMsgCount, dwMessageId );
  1052. //Increment the msg count
  1053. InterlockedIncrement(&lpPeer->m_dwMsgCount);
  1054. if (!lpPeer->m_fHandleEvents)
  1055. {
  1056. DPF(1,"-----Leaving (DplayPeer) MessageHandler call (*Not Handling Events*)...\n");
  1057. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1058. return S_OK;
  1059. }
  1060. if (!lpPeer->m_pEventStream)
  1061. {
  1062. DPF(1,"-----Leaving (DplayPeer) MessageHandler call (Stream Not Present)...\n");
  1063. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1064. return S_OK;
  1065. }
  1066. // First we need to set our stream seek back to the beginning
  1067. // We will do this every time we enter this function since we don't know if
  1068. // we are on the same thread or not...
  1069. I_dxj_DirectPlayEvent *lpEvent = NULL;
  1070. __try {
  1071. LARGE_INTEGER l;
  1072. l.QuadPart = 0;
  1073. lpPeer->m_pEventStream->Seek(l, STREAM_SEEK_SET, NULL);
  1074. hr = CoUnmarshalInterface(lpPeer->m_pEventStream, IID_I_dxj_DirectPlayEvent, (void**)&lpEvent);
  1075. if (hr == CO_E_NOTINITIALIZED) // Call CoInit so we can unmarshal
  1076. {
  1077. DPF1(1,"-----Calling CoInitEx... HR = %d\n", hr);
  1078. CoInitializeEx(NULL,COINIT_MULTITHREADED);
  1079. hr = CoUnmarshalInterface(lpPeer->m_pEventStream, IID_I_dxj_DirectPlayEvent, (void**)&lpEvent);
  1080. fCallCoUninit = TRUE;
  1081. }
  1082. if (!lpEvent)
  1083. {
  1084. DPF1(1,"-----Leaving (DplayPeer) MessageHandler call (No event interface)... HR = %d\n", hr);
  1085. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1086. return hr;
  1087. }
  1088. }
  1089. __except(EXCEPTION_EXECUTE_HANDLER)
  1090. {
  1091. lpPeer->m_fHandleEvents = FALSE;
  1092. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1093. DPF(1,"-----Leaving (DplayPeer) MessageHandler call (Stream Gone)...\n");
  1094. return S_OK;
  1095. }
  1096. switch( dwMessageId )
  1097. {
  1098. //Receive
  1099. case DPN_MSGID_RECEIVE:
  1100. {
  1101. DPF(1,"-----DirectPlayPeer8 Callback Receive\n");
  1102. DPNMSG_RECEIVE *pMsgReceive = (DPNMSG_RECEIVE*)pMsgBuffer;
  1103. DPNMSG_RECEIVE_CDESC m_dpReceive;
  1104. SAFEARRAY *lpData = NULL;
  1105. SAFEARRAYBOUND rgsabound[1];
  1106. BYTE *lpTemp = NULL;
  1107. ZeroMemory(&m_dpReceive, sizeof(DPNMSG_RECEIVE_CDESC));
  1108. m_dpReceive.idSender = pMsgReceive->dpnidSender;
  1109. // Let's load our SafeArray
  1110. if (pMsgReceive->dwReceiveDataSize)
  1111. {
  1112. rgsabound[0].lLbound = 0;
  1113. rgsabound[0].cElements = pMsgReceive->dwReceiveDataSize;
  1114. lpData = SafeArrayCreate(VT_UI1, 1, rgsabound);
  1115. lpTemp = (BYTE*)lpData->pvData;
  1116. lpData->pvData = pMsgReceive->pReceiveData;
  1117. m_dpReceive.lDataSize = pMsgReceive->dwReceiveDataSize;
  1118. m_dpReceive.ReceivedData = lpData;
  1119. }
  1120. lpEvent->Receive(&m_dpReceive, &fRejectMsg);
  1121. if (lpData) //Get rid of the safearray
  1122. {
  1123. lpData->pvData = lpTemp;
  1124. SafeArrayDestroy(lpData);
  1125. }
  1126. break;
  1127. }
  1128. //Send complete
  1129. case DPN_MSGID_SEND_COMPLETE:
  1130. {
  1131. DPF(1,"-----DirectPlayPeer8 Callback SendComplete\n");
  1132. DPNMSG_SEND_COMPLETE *msg = (DPNMSG_SEND_COMPLETE*)pMsgBuffer;
  1133. DPNMSG_SEND_COMPLETE_CDESC m_dpSend;
  1134. ZeroMemory(&m_dpSend, sizeof(DPNMSG_SEND_COMPLETE_CDESC));
  1135. m_dpSend.AsyncOpHandle = (long)msg->hAsyncOp;
  1136. m_dpSend.hResultCode = (long)msg->hResultCode;
  1137. m_dpSend.lSendTime = (long)msg->dwSendTime;
  1138. lpEvent->SendComplete(&m_dpSend, &fRejectMsg);
  1139. break;
  1140. }
  1141. //Async Op complete
  1142. case DPN_MSGID_ASYNC_OP_COMPLETE:
  1143. {
  1144. DPF(1,"-----DirectPlayPeer8 Callback AsyncOpComplete\n");
  1145. DPNMSG_ASYNC_OP_COMPLETE *msg = (DPNMSG_ASYNC_OP_COMPLETE*)pMsgBuffer;
  1146. DPNMSG_ASYNC_OP_COMPLETE_CDESC m_dpAsynOp;
  1147. ZeroMemory(&m_dpAsynOp, sizeof(DPNMSG_ASYNC_OP_COMPLETE_CDESC));
  1148. m_dpAsynOp.AsyncOpHandle = (long) msg->hAsyncOp;
  1149. m_dpAsynOp.hResultCode = (long) msg->hResultCode;
  1150. lpEvent->AsyncOpComplete(&m_dpAsynOp, &fRejectMsg);
  1151. break;
  1152. }
  1153. // Add/Remove players from groups
  1154. case DPN_MSGID_ADD_PLAYER_TO_GROUP:
  1155. case DPN_MSGID_REMOVE_PLAYER_FROM_GROUP:
  1156. {
  1157. DPF(1,"-----DirectPlayPeer8 Callback Add/Remove Group\n");
  1158. DPNMSG_ADD_PLAYER_TO_GROUP *msg = (DPNMSG_ADD_PLAYER_TO_GROUP*)pMsgBuffer;
  1159. DPNID m_dpnidAddRemoveGroupID = 0;
  1160. DPNID m_dpnidAddRemovePlayerID = 0;
  1161. m_dpnidAddRemoveGroupID = msg->dpnidGroup;
  1162. m_dpnidAddRemovePlayerID = msg->dpnidPlayer;
  1163. lpEvent->AddRemovePlayerGroup(dwMessageId, m_dpnidAddRemovePlayerID, m_dpnidAddRemoveGroupID, &fRejectMsg);
  1164. break;
  1165. }
  1166. // App Desc
  1167. case DPN_MSGID_APPLICATION_DESC:
  1168. {
  1169. DPF(1,"-----DirectPlayPeer8 Callback App desc\n");
  1170. lpEvent->AppDesc(&fRejectMsg);
  1171. break;
  1172. }
  1173. // Indicate Connect
  1174. case DPN_MSGID_INDICATE_CONNECT:
  1175. {
  1176. DPF(1,"-----DirectPlayPeer8 Callback Indicate Connect\n");
  1177. DPNMSG_INDICATE_CONNECT *msg = (DPNMSG_INDICATE_CONNECT*)pMsgBuffer;
  1178. DPNMSG_INDICATE_CONNECT_CDESC m_dpIndConnect;
  1179. WCHAR* wszAddress = NULL;
  1180. WCHAR* wszDevice = NULL;
  1181. DWORD dwNumChars = 0;
  1182. ZeroMemory(&m_dpIndConnect, sizeof(DPNMSG_INDICATE_CONNECT_CDESC));
  1183. __try {
  1184. if (msg->pAddressPlayer)
  1185. {
  1186. hr = msg->pAddressPlayer->GetURLW(NULL, &dwNumChars);
  1187. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1188. {
  1189. DPF1(1,"-----Failed... hr = %d\n",hr);
  1190. }
  1191. else
  1192. {
  1193. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1194. if (FAILED (hr = msg->pAddressPlayer->GetURLW(wszAddress,&dwNumChars) ) )
  1195. {
  1196. DPF1(1,"-----Failed... hr = %d\n",hr);
  1197. }
  1198. else
  1199. {
  1200. m_dpIndConnect.AddressPlayerUrl = SysAllocString(wszAddress);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. __except(EXCEPTION_EXECUTE_HANDLER)
  1206. {
  1207. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1208. DPF(1,"-----Exception (Indicate Connect - Part1)...\n");
  1209. }
  1210. __try {
  1211. dwNumChars = 0;
  1212. hr = msg->pAddressDevice->GetURLW(NULL, &dwNumChars);
  1213. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1214. {
  1215. DPF1(1,"-----Failed... hr = %d\n",hr);
  1216. }
  1217. else
  1218. {
  1219. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1220. if (FAILED (hr = msg->pAddressDevice->GetURLW(wszDevice,&dwNumChars) ) )
  1221. {
  1222. DPF1(1,"-----Failed... hr = %d\n",hr);
  1223. }
  1224. else
  1225. {
  1226. m_dpIndConnect.AddressDeviceUrl = SysAllocString(wszDevice);
  1227. }
  1228. }
  1229. }
  1230. __except(EXCEPTION_EXECUTE_HANDLER)
  1231. {
  1232. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1233. DPF(1,"-----Exception (Indicate Connect - Part2)...\n");
  1234. }
  1235. lpEvent->IndicateConnect(&m_dpIndConnect, &fRejectMsg);
  1236. // Get rid of these addresses
  1237. if (m_dpIndConnect.AddressPlayerUrl)
  1238. SysFreeString(m_dpIndConnect.AddressPlayerUrl);
  1239. if (m_dpIndConnect.AddressDeviceUrl)
  1240. SysFreeString(m_dpIndConnect.AddressDeviceUrl);
  1241. break;
  1242. }
  1243. // Connect complete
  1244. case DPN_MSGID_CONNECT_COMPLETE:
  1245. {
  1246. DPF(1,"-----DirectPlayPeer8 Callback ConnectComplete\n");
  1247. DPNMSG_CONNECT_COMPLETE *msg = (DPNMSG_CONNECT_COMPLETE*)pMsgBuffer;
  1248. DPNMSG_CONNECT_COMPLETE_CDESC m_dpConnectComp;
  1249. SAFEARRAY *lpData = NULL;
  1250. SAFEARRAYBOUND rgsabound[1];
  1251. BYTE *lpTemp = NULL;
  1252. ZeroMemory(&m_dpConnectComp, sizeof(DPNMSG_CONNECT_COMPLETE_CDESC));
  1253. m_dpConnectComp.hResultCode = (long) msg->hResultCode;
  1254. m_dpConnectComp.AsyncOpHandle =(long) msg->hAsyncOp;
  1255. // Let's load our SafeArray
  1256. if (msg->dwApplicationReplyDataSize)
  1257. {
  1258. rgsabound[0].lLbound = 0;
  1259. rgsabound[0].cElements = msg->dwApplicationReplyDataSize;
  1260. lpData = SafeArrayCreate(VT_UI1, 1, rgsabound);
  1261. lpTemp = (BYTE*)lpData->pvData;
  1262. lpData->pvData = msg->pvApplicationReplyData;
  1263. m_dpConnectComp.ReplyData = lpData;
  1264. }
  1265. lpEvent->ConnectComplete(&m_dpConnectComp, &fRejectMsg);
  1266. if (lpData) //Get rid of the safearray
  1267. {
  1268. lpData->pvData = lpTemp;
  1269. SafeArrayDestroy(lpData);
  1270. }
  1271. break;
  1272. }
  1273. // Host migrated
  1274. case DPN_MSGID_HOST_MIGRATE:
  1275. {
  1276. DPF(1,"-----DirectPlayPeer8 Callback HostMigrate\n");
  1277. DPNMSG_HOST_MIGRATE *msg = (DPNMSG_HOST_MIGRATE*)pMsgBuffer;
  1278. DPNID m_dpnidNewHostID = 0;
  1279. m_dpnidNewHostID = msg->dpnidNewHost;
  1280. lpEvent->HostMigrate(m_dpnidNewHostID, &fRejectMsg);
  1281. break;
  1282. }
  1283. // Terminate Session
  1284. case DPN_MSGID_TERMINATE_SESSION:
  1285. {
  1286. DPF(1,"-----DirectPlayPeer8 Callback TerminateSession\n");
  1287. DPNMSG_TERMINATE_SESSION *msg = (DPNMSG_TERMINATE_SESSION*)pMsgBuffer;
  1288. DPNMSG_TERMINATE_SESSION_CDESC m_dpTerm;
  1289. SAFEARRAY *lpData = NULL;
  1290. SAFEARRAYBOUND rgsabound[1];
  1291. BYTE *lpTemp = NULL;
  1292. ZeroMemory(&m_dpTerm, sizeof(DPNMSG_TERMINATE_SESSION_CDESC));
  1293. m_dpTerm.hResultCode = msg->hResultCode;
  1294. // Let's load our SafeArray
  1295. if (msg->dwTerminateDataSize)
  1296. {
  1297. rgsabound[0].lLbound = 0;
  1298. rgsabound[0].cElements = msg->dwTerminateDataSize;
  1299. lpData = SafeArrayCreate(VT_UI1, 1, rgsabound);
  1300. lpTemp = (BYTE*)lpData->pvData;
  1301. lpData->pvData = msg->pvTerminateData;
  1302. m_dpTerm.TerminateData = lpData;
  1303. }
  1304. lpEvent->TerminateSession(&m_dpTerm,&fRejectMsg);
  1305. if (lpData) //Get rid of the safearray
  1306. {
  1307. lpData->pvData = lpTemp;
  1308. SafeArrayDestroy(lpData);
  1309. }
  1310. break;
  1311. }
  1312. // Enum Host query
  1313. case DPN_MSGID_ENUM_HOSTS_QUERY:
  1314. {
  1315. DPF(1,"-----DirectPlayPeer8 Callback EnumHostQuery\n");
  1316. DPNMSG_ENUM_HOSTS_QUERY *msg = (DPNMSG_ENUM_HOSTS_QUERY*)pMsgBuffer;
  1317. DPNMSG_ENUM_HOSTS_QUERY_CDESC m_dpEnumHostQuery;
  1318. WCHAR* wszAddress = NULL;
  1319. WCHAR* wszDevice = NULL;
  1320. DWORD dwNumChars = 0;
  1321. ZeroMemory(&m_dpEnumHostQuery, sizeof(DPNMSG_ENUM_HOSTS_QUERY_CDESC));
  1322. __try {
  1323. hr = msg->pAddressSender->GetURLW(NULL, &dwNumChars);
  1324. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1325. {
  1326. DPF1(1,"-----Failed... hr = %d\n",hr);
  1327. }
  1328. else
  1329. {
  1330. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1331. if (FAILED (hr = msg->pAddressSender->GetURLW(wszAddress,&dwNumChars) ) )
  1332. {
  1333. DPF1(1,"-----Failed... hr = %d\n",hr);
  1334. }
  1335. else
  1336. {
  1337. m_dpEnumHostQuery.AddressSenderUrl = SysAllocString(wszAddress);
  1338. }
  1339. }
  1340. }
  1341. __except(EXCEPTION_EXECUTE_HANDLER)
  1342. {
  1343. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1344. DPF(1,"-----Exception (EnumQuery Connect - Part1)...\n");
  1345. }
  1346. __try {
  1347. dwNumChars = 0;
  1348. hr = msg->pAddressDevice->GetURLW(NULL, &dwNumChars);
  1349. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1350. {
  1351. DPF1(1,"-----Failed... hr = %d\n",hr);
  1352. }
  1353. else
  1354. {
  1355. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1356. if (FAILED (hr = msg->pAddressDevice->GetURLW(wszDevice,&dwNumChars) ) )
  1357. {
  1358. DPF1(1,"-----Failed... hr = %d\n",hr);
  1359. }
  1360. else
  1361. {
  1362. m_dpEnumHostQuery.AddressDeviceUrl = SysAllocString(wszDevice);
  1363. }
  1364. }
  1365. }
  1366. __except(EXCEPTION_EXECUTE_HANDLER)
  1367. {
  1368. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1369. DPF(1,"-----Exception (EnumQuery Connect - Part2)...\n");
  1370. }
  1371. lpEvent->EnumHostsQuery(&m_dpEnumHostQuery, &fRejectMsg);
  1372. // Get rid of these addresses
  1373. if (m_dpEnumHostQuery.AddressSenderUrl)
  1374. SysFreeString(m_dpEnumHostQuery.AddressSenderUrl);
  1375. if (m_dpEnumHostQuery.AddressDeviceUrl)
  1376. SysFreeString(m_dpEnumHostQuery.AddressDeviceUrl);
  1377. break;
  1378. }
  1379. // Create Player
  1380. case DPN_MSGID_CREATE_PLAYER:
  1381. {
  1382. DPF(1,"-----DirectPlayPeer8 Callback CreatePlayer\n");
  1383. DPNMSG_CREATE_PLAYER *msg = (DPNMSG_CREATE_PLAYER*)pMsgBuffer;
  1384. DPNID m_dpnidPlayerID = 0;
  1385. m_dpnidPlayerID = msg->dpnidPlayer;
  1386. lpEvent->CreatePlayer(m_dpnidPlayerID, &fRejectMsg);
  1387. break;
  1388. }
  1389. // Destroy Player
  1390. case DPN_MSGID_DESTROY_PLAYER:
  1391. {
  1392. DPF(1,"-----DirectPlayPeer8 Callback DestroyPlayer\n");
  1393. DPNMSG_DESTROY_PLAYER *msg = (DPNMSG_DESTROY_PLAYER*)pMsgBuffer;
  1394. DPNID m_dpnidPlayerID = 0;
  1395. DWORD m_dwReason = 0;
  1396. m_dpnidPlayerID = msg->dpnidPlayer;
  1397. m_dwReason = msg->dwReason;
  1398. lpEvent->DestroyPlayer(m_dpnidPlayerID, m_dwReason, &fRejectMsg);
  1399. break;
  1400. }
  1401. // Create Group
  1402. case DPN_MSGID_CREATE_GROUP:
  1403. {
  1404. DPF(1,"-----DirectPlayPeer8 Callback CreateGroup\n");
  1405. DPNMSG_CREATE_GROUP *msg = (DPNMSG_CREATE_GROUP*)pMsgBuffer;
  1406. DPNID m_dpnidPlayerID = 0;
  1407. DPNID m_dpnidOwnerID = 0;
  1408. m_dpnidPlayerID = msg->dpnidGroup;
  1409. m_dpnidOwnerID = msg->dpnidOwner;
  1410. lpEvent->CreateGroup(m_dpnidPlayerID, m_dpnidOwnerID, &fRejectMsg);
  1411. break;
  1412. }
  1413. //Destroy Group
  1414. case DPN_MSGID_DESTROY_GROUP:
  1415. {
  1416. DPF(1,"-----DirectPlayPeer8 Callback DestroyGroup\n");
  1417. DPNMSG_DESTROY_GROUP *msg = (DPNMSG_DESTROY_GROUP*)pMsgBuffer;
  1418. DPNID m_dpnidPlayerID = 0;
  1419. DWORD m_dwReason = 0;
  1420. m_dpnidPlayerID = msg->dpnidGroup;
  1421. m_dwReason = msg->dwReason;
  1422. lpEvent->DestroyGroup(m_dpnidPlayerID, m_dwReason, &fRejectMsg);
  1423. break;
  1424. }
  1425. // Info
  1426. case DPN_MSGID_PEER_INFO:
  1427. case DPN_MSGID_CLIENT_INFO:
  1428. case DPN_MSGID_SERVER_INFO:
  1429. case DPN_MSGID_GROUP_INFO:
  1430. {
  1431. DPF(1,"-----DirectPlayPeer8 Callback Info\n");
  1432. DPNMSG_PEER_INFO *msg = (DPNMSG_PEER_INFO*)pMsgBuffer;
  1433. DPNID m_dpnidInfoID = 0;
  1434. m_dpnidInfoID = msg->dpnidPeer;
  1435. lpEvent->InfoNotify(dwMessageId, m_dpnidInfoID, &fRejectMsg);
  1436. break;
  1437. }
  1438. // EnumHostRes
  1439. case DPN_MSGID_ENUM_HOSTS_RESPONSE:
  1440. {
  1441. DPF(1,"-----DirectPlayPeer8 Callback EnumHostResponse\n");
  1442. DPNMSG_ENUM_HOSTS_RESPONSE *msg = (DPNMSG_ENUM_HOSTS_RESPONSE*)pMsgBuffer;
  1443. DPNMSG_ENUM_HOSTS_RESPONSE_CDESC m_dpEnumHostRes;
  1444. DWORD dwNumChars = 0;
  1445. WCHAR* wszAddress = NULL;
  1446. WCHAR* wszDevice = NULL;
  1447. ZeroMemory(&m_dpEnumHostRes, sizeof(DPNMSG_ENUM_HOSTS_RESPONSE_CDESC));
  1448. m_dpEnumHostRes.ApplicationDescription.lSize = (long)msg->pApplicationDescription->dwSize;
  1449. m_dpEnumHostRes.ApplicationDescription.lFlags = msg->pApplicationDescription->dwFlags;
  1450. m_dpEnumHostRes.ApplicationDescription.guidInstance = GUIDtoBSTR((GUID*)&msg->pApplicationDescription->guidInstance);
  1451. m_dpEnumHostRes.ApplicationDescription.guidApplication = GUIDtoBSTR((GUID*)&msg->pApplicationDescription->guidApplication);
  1452. m_dpEnumHostRes.ApplicationDescription.lMaxPlayers = (long)msg->pApplicationDescription->dwMaxPlayers;
  1453. m_dpEnumHostRes.ApplicationDescription.lCurrentPlayers = (long)msg->pApplicationDescription->dwCurrentPlayers;
  1454. m_dpEnumHostRes.ApplicationDescription.SessionName = SysAllocString(msg->pApplicationDescription->pwszSessionName);
  1455. m_dpEnumHostRes.ApplicationDescription.Password = SysAllocString(msg->pApplicationDescription->pwszPassword);
  1456. m_dpEnumHostRes.lRoundTripLatencyMS = (long) msg->dwRoundTripLatencyMS;
  1457. __try {
  1458. if (msg->pAddressSender)
  1459. {
  1460. DPF(1,"-----About to get AdressSender...\n");
  1461. hr = msg->pAddressSender->GetURLW(NULL, &dwNumChars);
  1462. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1463. {
  1464. DPF1(1,"-----Failed... hr = %d\n",hr);
  1465. }
  1466. else
  1467. {
  1468. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1469. DPF1(1,"--- About to call GetURLW, NumChars= %d\n",dwNumChars);
  1470. if (FAILED (hr = msg->pAddressSender->GetURLW(wszAddress,&dwNumChars) ) )
  1471. {
  1472. DPF1(1,"-----Failed... hr = %d\n",hr);
  1473. }
  1474. else
  1475. {
  1476. m_dpEnumHostRes.AddressSenderUrl = SysAllocString(wszAddress);
  1477. }
  1478. }
  1479. }
  1480. }
  1481. __except(EXCEPTION_EXECUTE_HANDLER)
  1482. {
  1483. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1484. DPF(1,"-----Exception (EnumRes Connect - Part1)...\n");
  1485. }
  1486. __try {
  1487. dwNumChars = 0;
  1488. if (msg->pAddressDevice)
  1489. {
  1490. DPF(1,"-----About to get AdressDevice...\n");
  1491. hr = msg->pAddressDevice->GetURLW(NULL, &dwNumChars);
  1492. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  1493. {
  1494. DPF1(1,"-----Failed... hr = %d\n",hr);
  1495. }
  1496. else {
  1497. DPF1(1,"--- About to call GetURLW, NumChars= %d\n",dwNumChars);
  1498. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  1499. if (FAILED (hr = msg->pAddressDevice->GetURLW(wszDevice,&dwNumChars) ) )
  1500. {
  1501. DPF1(1,"-----Failed... hr = %d\n",hr);
  1502. }
  1503. else
  1504. {
  1505. m_dpEnumHostRes.AddressDeviceUrl = SysAllocString(wszDevice);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. __except(EXCEPTION_EXECUTE_HANDLER)
  1511. {
  1512. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1513. DPF(1,"-----Exception (EnumRes Connect - Part1)...\n");
  1514. }
  1515. lpEvent->EnumHostsResponse(&m_dpEnumHostRes, &fRejectMsg);
  1516. if (m_dpEnumHostRes.AddressSenderUrl)
  1517. SysFreeString(m_dpEnumHostRes.AddressSenderUrl);
  1518. if (m_dpEnumHostRes.AddressDeviceUrl)
  1519. SysFreeString(m_dpEnumHostRes.AddressDeviceUrl);
  1520. break;
  1521. }
  1522. // Indicate Connect
  1523. case DPN_MSGID_INDICATED_CONNECT_ABORTED:
  1524. {
  1525. DPF(1,"-----DirectPlayServer8 Callback Indicated Connect Abort\n");
  1526. lpEvent->IndicatedConnectAborted(&fRejectMsg);
  1527. break;
  1528. }
  1529. }
  1530. if (fCallCoUninit)
  1531. CoUninitialize();
  1532. InterlockedDecrement(&lpPeer->m_dwMsgCount);
  1533. DPF(1,"-----Leaving (DplayPeer) MessageHandler call...\n");
  1534. if (fRejectMsg != VARIANT_FALSE)
  1535. return E_FAIL;
  1536. return S_OK;
  1537. }
  1538. HRESULT C_dxj_DirectPlayPeerObject::RegisterMessageHandler(I_dxj_DirectPlayEvent *event)
  1539. {
  1540. HRESULT hr=S_OK;
  1541. LPSTREAM pStm=NULL;
  1542. if (!event) return E_INVALIDARG;
  1543. if (!m_fHandleEvents)
  1544. {
  1545. DPF(1,"-----Entering (DplayPeer) RegisterMessageHandler call...\n");
  1546. SAFE_RELEASE(m_pEventStream);
  1547. // Create a global stream. The stream needs to be global so we can
  1548. // marshal once, and unmarshal as many times as necessary
  1549. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStm);
  1550. if FAILED(hr) return hr;
  1551. // Now we can marshal our IUnknown interface. We use MSHLFLAGS_TABLEWEAK
  1552. // so we can unmarshal any number of times
  1553. hr = CoMarshalInterface(pStm, IID_I_dxj_DirectPlayEvent, event, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
  1554. if FAILED(hr) return hr;
  1555. // Now we need to set the seek location of the stream to the beginning
  1556. LARGE_INTEGER l;
  1557. l.QuadPart = 0;
  1558. pStm->Seek(l, STREAM_SEEK_SET, NULL);
  1559. m_pEventStream=pStm;
  1560. if (!m_fInit)
  1561. {
  1562. if (FAILED ( hr = m__dxj_DirectPlayPeer->Initialize( this, DirectPlayMessageHandler, 0 ) ) )
  1563. return hr;
  1564. m_fInit = TRUE;
  1565. }
  1566. m_fHandleEvents = TRUE;
  1567. }
  1568. else
  1569. return DPNERR_ALREADYINITIALIZED;
  1570. return hr;
  1571. }
  1572. HRESULT C_dxj_DirectPlayPeerObject::UnRegisterMessageHandler()
  1573. {
  1574. DPF(1,"-----Entering (DplayPeer) UnregisterMessageHandler call...\n");
  1575. m_fHandleEvents = FALSE;
  1576. //Clear out the messages currently waiting
  1577. FlushBuffer(0);
  1578. return S_OK;
  1579. }
  1580. HRESULT C_dxj_DirectPlayPeerObject::FlushBuffer(LONG dwNumMessagesLeft)
  1581. {
  1582. DWORD dwTime = GetTickCount();
  1583. DPF(1,"-----Entering (DplayPeer) FlushBuffer call...\n");
  1584. //Clear out the messages currently waiting
  1585. BOOL bGotMsg = FALSE;
  1586. BOOL bWait = FALSE;
  1587. int i=0;
  1588. MSG msg;
  1589. HRESULT hr = S_OK;
  1590. msg.message = WM_NULL;
  1591. while ((WM_QUIT != msg.message) && (m_dwMsgCount > dwNumMessagesLeft))
  1592. {
  1593. bGotMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  1594. i++;
  1595. if (GetTickCount() - dwTime > 5000)
  1596. {
  1597. // Don't let FlushBuffer wait more than 5 seconds
  1598. DPF1(1,"-----Leaving (DplayPeer) FlushBuffer call (All messages *not* flushed - %d remained)...\n", m_dwMsgCount);
  1599. return S_OK;
  1600. }
  1601. if (bGotMsg)
  1602. {
  1603. TranslateMessage( &msg );
  1604. DispatchMessage( &msg );
  1605. }
  1606. bGotMsg = FALSE;
  1607. Sleep(0);
  1608. }
  1609. DPF(1,"-----Leaving (DplayPeer) FlushBuffer call (All messages flushed)...\n");
  1610. return S_OK;
  1611. }
  1612. DWORD WINAPI ClosePeerThreadProc(void* lpParam)
  1613. {
  1614. // User context for the message handler is a pointer to our class module.
  1615. IDirectPlay8Peer *lpPeer = (IDirectPlay8Peer*)lpParam;
  1616. DPF(1,"-----Entering (DplayPeer) ClosePeerThreadProc call...\n");
  1617. lpPeer->Close(0);
  1618. DPF(1,"-----Leaving (DplayPeer) ClosePeerThreadProc call ...\n");
  1619. return 0;
  1620. }
  1621. DWORD WINAPI ReleasePeerThreadProc(void* lpParam)
  1622. {
  1623. // User context for the message handler is a pointer to our class module.
  1624. IDirectPlay8Peer *lpPeer = (IDirectPlay8Peer*)lpParam;
  1625. DPF(1,"-----Entering (DplayPeer) ReleasePeerThreadProc call...\n");
  1626. SAFE_RELEASE(lpPeer);
  1627. DPF(1,"-----Leaving (DplayPeer) ReleasePeerThreadProc call ...\n");
  1628. CloseHandle(GetCurrentThread());
  1629. return 0;
  1630. }