Source code of Windows XP (NT5)
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.

1774 lines
45 KiB

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