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.

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