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.

1103 lines
30 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: dplay4obj.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // _dxj_DirectPlay2Obj.cpp : Implementation of C_dxj_DirectPlay2Object
  11. // DHF begin - entire file
  12. #include "stdafx.h"
  13. #include "Direct.h"
  14. #include "dms.h"
  15. #include "dPlay.h"
  16. #include "DPAddressObj.h"
  17. #include "DPLConnectionObj.h"
  18. #include "dPlay4Obj.h"
  19. #include "DPEnumPlayersObj.h"
  20. #include "DPEnumSessionsObj.h"
  21. #include "DPEnumConnectionsObj.h"
  22. #include "dpMsgObj.h"
  23. #include "dpSessDataObj.h"
  24. typedef HRESULT (__stdcall *DIRECTPLAYCREATE)( LPGUID lpGUID, LPDIRECTPLAY *lplpDP, IUnknown *pUnk);
  25. extern DIRECTPLAYCREATE pDirectPlayCreate;
  26. typedef HRESULT (__stdcall *DIRECTPLAYENUMERATE)( LPDPENUMDPCALLBACK, LPVOID );
  27. extern DIRECTPLAYENUMERATE pDirectPlayEnumerate;
  28. extern HRESULT FillRealSessionDesc(DPSESSIONDESC2 *dpSessionDesc,DPSessionDesc2 *sessionDesc);
  29. extern void FillCoverSessionDesc(DPSessionDesc2 *sessionDesc,DPSESSIONDESC2 *dpSessionDesc);
  30. extern HRESULT BSTRtoGUID(LPGUID,BSTR);
  31. extern HRESULT BSTRtoPPGUID(LPGUID*,BSTR);
  32. extern BSTR GUIDtoBSTR(LPGUID);
  33. extern void *g_dxj_DirectPlay4;
  34. C_dxj_DirectPlay4Object::C_dxj_DirectPlay4Object()
  35. {
  36. m__dxj_DirectPlay4 = NULL;
  37. parent = NULL;
  38. nextobj = g_dxj_DirectPlay4;
  39. g_dxj_DirectPlay4 = this;
  40. creationid = ++g_creationcount;
  41. }
  42. DESTRUCTOR(_dxj_DirectPlay4, {})
  43. GETSET_OBJECT(_dxj_DirectPlay4);
  44. //
  45. /*** I_dxj_DirectPlay4 methods ***/
  46. //
  47. PASS_THROUGH_CAST_2_R(_dxj_DirectPlay4, addPlayerToGroup, AddPlayerToGroup, Dpid,(DPID), Dpid,(DPID));
  48. PASS_THROUGH_R(_dxj_DirectPlay4, close, Close);
  49. //PASS_THROUGH_CAST_1_R(_dxj_DirectPlay4, initialize, Initialize, DxGuid*, (LPGUID));
  50. PASS_THROUGH_CAST_2_R(_dxj_DirectPlay4, deletePlayerFromGroup, DeletePlayerFromGroup, Dpid, (DPID), Dpid, (DPID));
  51. PASS_THROUGH_CAST_1_R(_dxj_DirectPlay4, destroyPlayer, DestroyPlayer, Dpid,(DPID));
  52. PASS_THROUGH_CAST_1_R(_dxj_DirectPlay4, destroyGroup, DestroyGroup, Dpid,(DPID));
  53. PASS_THROUGH_CAST_2_R(_dxj_DirectPlay4, getMessageCount, GetMessageCount, Dpid,(DPID), long *,(DWORD*));
  54. //PASS_THROUGH_CAST_3_R(_dxj_DirectPlay4, getPlayerCaps, GetPlayerCaps, Dpid, (DPID), DPCaps*, (DPCAPS*), long, (DWORD));
  55. //PASS_THROUGH_CAST_2_R(_dxj_DirectPlay4, startSession, StartSession, long, (DWORD), Dpid, (DPID));
  56. //PASS_THROUGH_CAST_3_R(_dxj_DirectPlay4, getPlayerCaps, GetPlayerCaps, Dpid, (DPID), DPCaps*, (DPCAPS*), long, (DWORD));
  57. //PASS_THROUGH_CAST_2_R(_dxj_DirectPlay4, getCaps, GetCaps, DPCaps*, (LPDPCAPS), long, (DWORD));
  58. STDMETHODIMP C_dxj_DirectPlay4Object::getCaps(DPCaps *c,long flags){
  59. if (!c) return E_INVALIDARG;
  60. c->lSize=sizeof(DPCAPS);
  61. HRESULT hr = m__dxj_DirectPlay4->GetCaps((DPCAPS*)c,(DWORD)flags);
  62. return hr;
  63. }
  64. STDMETHODIMP C_dxj_DirectPlay4Object::startSession(Dpid id){
  65. HRESULT hr = m__dxj_DirectPlay4->StartSession(0,(DPID)id);
  66. return hr;
  67. }
  68. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerCaps(Dpid id, DPCaps *caps, long flags)
  69. {
  70. ((DPCAPS*)caps)->dwSize=sizeof(DPCAPS);
  71. HRESULT hr = m__dxj_DirectPlay4->GetPlayerCaps((DPID)id,(DPCAPS*)caps,(DWORD)flags);
  72. return hr;
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. STDMETHODIMP C_dxj_DirectPlay4Object::createGroup(
  76. /* [in] */ BSTR friendlyName,
  77. /* [in] */ BSTR formalName,
  78. long flags,
  79. /* [retval][out] */ Dpid *v1)
  80. {
  81. if ( m__dxj_DirectPlay4 == NULL ) return E_FAIL;
  82. DPNAME dpName;
  83. ZeroMemory(&dpName,sizeof(DPNAME));
  84. dpName.dwSize = sizeof(dpName);
  85. if ((!friendlyName) ||(!formalName)) return E_INVALIDARG;
  86. if (0==_wcsicmp(friendlyName,formalName)) return E_INVALIDARG;
  87. if ( friendlyName[0]!=0 )
  88. {
  89. __try { dpName.lpszShortName = (LPWSTR)alloca(sizeof(WCHAR)*(wcslen(friendlyName)+1)); } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  90. wcscpy(dpName.lpszShortName, friendlyName);
  91. }
  92. if ( formalName[0]!=0 )
  93. {
  94. __try { dpName.lpszLongName = (LPWSTR)alloca(sizeof(WCHAR)*(wcslen(formalName)+1)); } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  95. wcscpy(dpName.lpszLongName, formalName);
  96. }
  97. HRESULT hr = m__dxj_DirectPlay4->CreateGroup((DWORD*)v1, &dpName, NULL, 0, (DWORD)flags);
  98. return hr;
  99. }
  100. /////////////////////////////////////////////////////////////////////////////
  101. STDMETHODIMP C_dxj_DirectPlay4Object::createPlayer(
  102. /* [in] */ BSTR friendlyName,
  103. /* [in] */ BSTR formalName,
  104. /* [in] */ long receiveEvent,
  105. /* [in] */ long flags,
  106. /* [retval][out] */ Dpid __RPC_FAR *v1)
  107. {
  108. if ( m__dxj_DirectPlay4 == NULL ) return E_FAIL;
  109. DPNAME dpName;
  110. ZeroMemory(&dpName,sizeof(DPNAME));
  111. dpName.dwSize = sizeof(DPNAME);
  112. if ((!friendlyName) ||(!formalName)) return E_INVALIDARG;
  113. if (0==_wcsicmp(friendlyName,formalName)) return E_INVALIDARG;
  114. if ( friendlyName[0]!=0 )
  115. {
  116. dpName.lpszShortName = (LPWSTR)friendlyName;
  117. //dpName.lpszShortName = (LPWSTR)alloca(sizeof(WCHAR)*(wcslen(friendlyName)+1));
  118. //wcscpy(dpName.lpszShortName, friendlyName);
  119. }
  120. if ( formalName[0]!=0 )
  121. {
  122. //dpName.lpszLongName = (LPWSTR)alloca(sizeof(WCHAR)*(wcslen(formalName)+1));
  123. //wcscpy(dpName.lpszLongName, formalName);
  124. dpName.lpszLongName = (LPWSTR)formalName;
  125. }
  126. HRESULT hr = m__dxj_DirectPlay4->CreatePlayer((DWORD*)v1, &dpName, (LPVOID)receiveEvent, NULL, 0, (DWORD)flags);
  127. //if (dpName.lpszShortName) free(dpName.lpszShortName);
  128. //if (dpName.lpszLongName) free(dpName.lpszLongName);
  129. return hr;
  130. }
  131. //////////////////////////////////////////////////////////////////////////
  132. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumGroupPlayers(
  133. Dpid groupPID,
  134. BSTR strGuid,
  135. long flags,
  136. I_dxj_DPEnumPlayers2 **retVal){
  137. HRESULT hr=C_dxj_DPEnumPlayersObject::create(m__dxj_DirectPlay4,DPENUMGROUPPLAYERS,groupPID,strGuid,flags,retVal);
  138. return hr;
  139. }
  140. //////////////////////////////////////////////////////////////////////////
  141. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumGroups(
  142. BSTR strGuid,
  143. long flags,
  144. I_dxj_DPEnumPlayers2 **retVal){
  145. HRESULT hr=C_dxj_DPEnumPlayersObject::create(m__dxj_DirectPlay4,DPENUMGROUPS,0,strGuid,flags,retVal);
  146. return hr;
  147. }
  148. //////////////////////////////////////////////////////////////////////////
  149. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumGroupsInGroup(
  150. Dpid groupPID,
  151. BSTR strGuid,
  152. long flags,
  153. I_dxj_DPEnumPlayers2 **retVal){
  154. HRESULT hr=C_dxj_DPEnumPlayersObject::create(m__dxj_DirectPlay4,DPENUMGROUPSINGROUP,groupPID,strGuid,flags,retVal);
  155. return hr;
  156. }
  157. //////////////////////////////////////////////////////////////////////////
  158. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumPlayers(
  159. BSTR strGuid,
  160. long flags,
  161. I_dxj_DPEnumPlayers2 **retVal){
  162. HRESULT hr=C_dxj_DPEnumPlayersObject::create(m__dxj_DirectPlay4,DPENUMPLAYERS,0,strGuid,flags,retVal);
  163. return hr;
  164. }
  165. //////////////////////////////////////////////////////////////////////////
  166. // USE void because we can accept null in VB
  167. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumSessions(
  168. I_dxj_DirectPlaySessionData *sessionDesc,
  169. long timeout,
  170. long flags,
  171. I_dxj_DPEnumSessions2 **retVal)
  172. {
  173. HRESULT hr=C_dxj_DPEnumSessionsObject::create(m__dxj_DirectPlay4,sessionDesc,timeout,flags,retVal);
  174. return hr;
  175. }
  176. //////////////////////////////////////////////////////////////////////////
  177. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupData( Dpid id,long flags, BSTR *ret)
  178. {
  179. DWORD size;
  180. HRESULT hr;
  181. void *pData=NULL;
  182. //ASSERT ( m__dxj_DirectPlay4 != NULL )
  183. if (!ret) return E_INVALIDARG;
  184. hr= m__dxj_DirectPlay4->GetGroupData((DWORD)id, (void*)NULL, (LPDWORD)&size, (DWORD)flags);
  185. if (size==0) {
  186. *ret=NULL;
  187. return S_OK;
  188. }
  189. //we only want data we can cast to a string
  190. if ((size % 2)!=0) return E_INVALIDARG;
  191. pData=malloc(size+2);
  192. if (!pData) return E_OUTOFMEMORY;
  193. //null terminate.
  194. ((char*)pData)[size]='\0';
  195. ((char*)pData)[size+1]='\0';
  196. hr= m__dxj_DirectPlay4->GetGroupData((DWORD)id, (void*)pData, (LPDWORD)&size, (DWORD)flags);
  197. if FAILED(hr) {
  198. if (pData) free(pData);
  199. return hr;
  200. }
  201. *ret=SysAllocString((WCHAR*)pData);
  202. if (pData) free(pData);
  203. return hr;
  204. }
  205. //////////////////////////////////////////////////////////////////////////
  206. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupShortName(Dpid id, BSTR *friendlyN)
  207. {
  208. DWORD dwDataSize;
  209. if ( m__dxj_DirectPlay4 == NULL )
  210. return E_FAIL;
  211. if(!friendlyN) return E_INVALIDARG;
  212. *friendlyN=NULL;
  213. HRESULT hr=m__dxj_DirectPlay4->GetGroupName(id, (char*)NULL, &dwDataSize);
  214. if (dwDataSize<sizeof(DPNAME)) return E_INVALIDARG;
  215. // Supply the stack based buffers needed by DIRECTX
  216. LPDPNAME dpName = NULL;
  217. __try { dpName = (LPDPNAME)alloca(dwDataSize); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  218. ZeroMemory(dpName,dwDataSize);
  219. dpName->dwSize=sizeof(DPNAME);
  220. hr = m__dxj_DirectPlay4->GetGroupName((DPID)id, dpName, &dwDataSize);
  221. if FAILED(hr) return hr;
  222. *friendlyN = SysAllocString(dpName->lpszShortName);
  223. return hr;
  224. }
  225. /////////////////////////////////////////////////////////////////////////////
  226. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupLongName(Dpid id, BSTR *formalN)
  227. {
  228. DWORD dwDataSize=0;
  229. if ( m__dxj_DirectPlay4 == NULL )
  230. return E_FAIL;
  231. if(!formalN) return E_INVALIDARG;
  232. *formalN=NULL;
  233. HRESULT hr=m__dxj_DirectPlay4->GetGroupName(id, (char*)NULL, &dwDataSize);
  234. if (dwDataSize<sizeof(DPNAME)) return E_INVALIDARG;
  235. // Supply the stack based buffers needed by DIRECTX
  236. LPDPNAME dpName = NULL;
  237. __try { dpName = (LPDPNAME)alloca(dwDataSize); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  238. ZeroMemory(dpName,dwDataSize);
  239. dpName->dwSize=sizeof(DPNAME);
  240. hr = m__dxj_DirectPlay4->GetGroupName(id, dpName, &dwDataSize);
  241. if FAILED(hr) return hr;
  242. *formalN = SysAllocString(dpName->lpszLongName);
  243. return hr;
  244. }
  245. //////////////////////////////////////////////////////////////////////////
  246. // Gets a DirectPlay abstract address using a player id
  247. //////////////////////////////////////////////////////////////////////////
  248. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerAddress(
  249. /* [in] */ Dpid id,
  250. /* [out] */ I_dxj_DPAddress **ret)
  251. {
  252. if ( m__dxj_DirectPlay4 == NULL ) return E_FAIL;
  253. void *pAddress;
  254. DWORD size=0;
  255. HRESULT hr;
  256. hr= m__dxj_DirectPlay4->GetPlayerAddress((DPID)id, NULL,&size);
  257. if (size==0) return E_FAIL;
  258. pAddress=malloc(size);
  259. if (pAddress==NULL) return E_OUTOFMEMORY;
  260. hr= m__dxj_DirectPlay4->GetPlayerAddress((DPID)id,pAddress,&size);
  261. if FAILED(hr){
  262. free(pAddress);
  263. return hr;
  264. }
  265. INTERNAL_CREATE_STRUCT(_dxj_DPAddress,ret);
  266. if (*ret==NULL) return E_OUTOFMEMORY;
  267. hr=(*ret)->setAddress((long)PtrToLong(pAddress),size); //bugbug SUNDOWN
  268. free(pAddress);
  269. return hr;
  270. }
  271. //////////////////////////////////////////////////////////////////////////
  272. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerData(
  273. /* [in] */ Dpid id,
  274. long flags,
  275. /* [out] */ BSTR *ret)
  276. {
  277. DWORD size;
  278. HRESULT hr;
  279. void *pData=NULL;
  280. //ASSERT ( m__dxj_DirectPlay4 != NULL )
  281. hr=m__dxj_DirectPlay4->GetPlayerData((DWORD)id, (void*)NULL, (LPDWORD)&size, (DWORD)flags);
  282. if (size==0) {
  283. *ret=NULL;
  284. return S_OK;
  285. }
  286. //we only want data we can cast to a string
  287. if ((size % 2)!=0) return E_INVALIDARG;
  288. pData=malloc(size+sizeof(WCHAR));
  289. if (!pData) return E_OUTOFMEMORY;
  290. ZeroMemory(pData,size+sizeof(WCHAR));
  291. hr= m__dxj_DirectPlay4->GetPlayerData((DWORD)id, (void*)pData, (LPDWORD)&size, (DWORD)flags);
  292. if FAILED(hr) {
  293. if (pData) free(pData);
  294. }
  295. *ret=SysAllocString((WCHAR*)pData);
  296. if (pData) free(pData);
  297. return hr;
  298. }
  299. //////////////////////////////////////////////////////////////////////////
  300. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerFriendlyName(Dpid id, BSTR *friendlyN)
  301. {
  302. if ( m__dxj_DirectPlay4 == NULL )
  303. return E_FAIL;
  304. DWORD dwDataSize;
  305. HRESULT hr;
  306. hr=m__dxj_DirectPlay4->GetPlayerName((DPID)id, (char*)NULL, &dwDataSize);
  307. // Supply the stack based buffers needed by DIRECTX
  308. LPDPNAME dpName = NULL;
  309. __try { dpName = (LPDPNAME)alloca(dwDataSize); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  310. ZeroMemory(dpName,dwDataSize);
  311. dpName->dwSize=sizeof(DPNAME);
  312. hr = m__dxj_DirectPlay4->GetPlayerName((DPID)id, dpName, &dwDataSize);
  313. if FAILED(hr) return hr;
  314. *friendlyN = SysAllocString(dpName->lpszShortName);
  315. return hr;
  316. }
  317. /////////////////////////////////////////////////////////////////////////////
  318. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerFormalName(Dpid id, BSTR *formalN)
  319. {
  320. if ( m__dxj_DirectPlay4 == NULL )
  321. return E_FAIL;
  322. DWORD dwDataSize;
  323. HRESULT hr;
  324. hr=m__dxj_DirectPlay4->GetPlayerName((DPID)id, (char*)NULL, &dwDataSize);
  325. //// Supply the stack based buffers needed by DIRECTX
  326. LPDPNAME dpName = NULL;
  327. __try { dpName = (LPDPNAME)alloca(dwDataSize); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  328. ZeroMemory(dpName,dwDataSize);
  329. dpName->dwSize=sizeof(DPNAME);
  330. hr=m__dxj_DirectPlay4->GetPlayerName((DPID)id, dpName, &dwDataSize); // get ANSI
  331. if FAILED(hr) return hr;
  332. *formalN = SysAllocString(dpName->lpszLongName);
  333. return hr;
  334. }
  335. //////////////////////////////////////////////////////////////////////////
  336. // Gets the current session description
  337. //////////////////////////////////////////////////////////////////////////
  338. STDMETHODIMP C_dxj_DirectPlay4Object::getSessionDesc(
  339. /* [out] */ I_dxj_DirectPlaySessionData __RPC_FAR **sessionDesc)
  340. {
  341. if ( m__dxj_DirectPlay4 == NULL )
  342. return E_FAIL;
  343. if ( sessionDesc == NULL )
  344. return E_FAIL;
  345. DWORD dataSize = 0;
  346. HRESULT hr = m__dxj_DirectPlay4->GetSessionDesc(NULL, &dataSize);
  347. LPVOID data = NULL;
  348. __try { data = (LPDPNAME)alloca(dataSize); /* Now convert to ANSI */ } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  349. hr = m__dxj_DirectPlay4->GetSessionDesc((LPVOID)data, &dataSize);
  350. if(hr != DP_OK) {
  351. return hr;
  352. }
  353. LPDPSESSIONDESC2 dpSessionDesc = (LPDPSESSIONDESC2)data;
  354. hr=C_dxj_DirectPlaySessionDataObject::create(dpSessionDesc,sessionDesc);
  355. return hr;
  356. }
  357. //////////////////////////////////////////////////////////////////////////
  358. // Establish a gaming session instance - create or join a game session
  359. //////////////////////////////////////////////////////////////////////////
  360. STDMETHODIMP C_dxj_DirectPlay4Object::open(I_dxj_DirectPlaySessionData *sessionDesc, long flags)
  361. {
  362. if ( m__dxj_DirectPlay4 == NULL )
  363. return E_FAIL;
  364. if ( sessionDesc == NULL )
  365. return E_INVALIDARG;
  366. DPSESSIONDESC2 dpSessionDesc;
  367. //CONSIDER - validating sessiondesc object
  368. // to return friendly error
  369. sessionDesc->AddRef();
  370. sessionDesc->getData(&dpSessionDesc);
  371. HRESULT hr = m__dxj_DirectPlay4->Open(&dpSessionDesc, flags);
  372. //SysFreeString((BSTR)dpSessionDesc.lpszPassword);
  373. //SysFreeString((BSTR)dpSessionDesc.lpszSessionName);
  374. sessionDesc->Release();
  375. if FAILED(hr) return hr;
  376. //FillCoverSessionDesc(sessionDesc,&dpSessionDesc);
  377. return hr;
  378. }
  379. //////////////////////////////////////////////////////////////////////////
  380. STDMETHODIMP C_dxj_DirectPlay4Object::receive(
  381. /* [in, out] */ Dpid *fromPlayerId,
  382. /* [in, out] */ Dpid *toPlayerId,
  383. /* [in] */ long flags,
  384. I_dxj_DirectPlayMessage **msg )
  385. {
  386. HRESULT hr;
  387. DWORD dwSize=0;
  388. void *pData=NULL;
  389. //#pragma message ("check with DPLAY folks if this is necessary")
  390. //DONE: The loop is not neccesary..
  391. //aaronO indicated the message order is consistent
  392. //a call to recieve to get the size can always be folowed by a call to get
  393. //the message - ANDREWKE
  394. //
  395. //BOOL fCont=TRUE;
  396. //while (fCont){
  397. hr = m__dxj_DirectPlay4->Receive((DPID*)fromPlayerId, (DPID*)toPlayerId, (DWORD)flags, NULL, &dwSize);
  398. //fix for manbug24192
  399. if (hr == DPERR_NOMESSAGES ) {
  400. *msg=NULL;
  401. return S_OK;
  402. }
  403. if (hr!=DPERR_BUFFERTOOSMALL) return hr;
  404. hr=C_dxj_DirectPlayMessageObject::create((DWORD)*fromPlayerId,dwSize,&pData,msg);
  405. if FAILED(hr) return hr;
  406. hr = m__dxj_DirectPlay4->Receive((DPID*)fromPlayerId, (DPID*)toPlayerId, (DWORD)flags,pData, &dwSize);
  407. //should never hit this
  408. // if ( hr == DPERR_BUFFERTOOSMALL){
  409. // fCont=TRUE;
  410. // (*msg)->Release();
  411. //
  412. // }
  413. // else{
  414. // fCont=FALSE;
  415. // }
  416. //} end while
  417. if FAILED(hr) {
  418. if (*msg) (*msg)->Release();
  419. *msg=NULL;
  420. }
  421. return hr;
  422. }
  423. /////////////////////////////////////////////////////////////////////////////
  424. STDMETHODIMP C_dxj_DirectPlay4Object::receiveSize(
  425. /* [in,out] */ Dpid *fromPlayerId,
  426. /* [in,out] */ Dpid *toPlayerId,
  427. /* [in] */ long flags,
  428. /* [retval][out] */ int *dataSize)
  429. {
  430. if ( m__dxj_DirectPlay4 == NULL )
  431. return E_FAIL;
  432. unsigned long *id1 = 0, *id2 = 0;
  433. HRESULT hr = m__dxj_DirectPlay4->Receive((DPID*)fromPlayerId, (DPID*)toPlayerId,
  434. (DWORD)flags, (void*)NULL, (LPDWORD)dataSize);
  435. if ( hr == DPERR_BUFFERTOOSMALL || hr == DPERR_NOMESSAGES )
  436. hr = S_OK;
  437. return hr;
  438. }
  439. /////////////////////////////////////////////////////////////////////////////
  440. STDMETHODIMP C_dxj_DirectPlay4Object::send(
  441. /* [in] */ Dpid fromPlayerId,
  442. /* [in] */ Dpid toPlayerId,
  443. /* [in] */ long flags,
  444. /* [in] */ I_dxj_DirectPlayMessage *msg)
  445. {
  446. HRESULT hr;
  447. void *pdata=NULL;
  448. DWORD dataSize=0;
  449. if (!msg) return E_INVALIDARG;
  450. __try {
  451. msg->getPointer((long*)&pdata);
  452. msg->getMessageSize((long*)&dataSize);
  453. hr= m__dxj_DirectPlay4->Send((DPID)fromPlayerId, (DPID)toPlayerId, (DWORD)flags,
  454. (void*)pdata, (DWORD) dataSize);
  455. }
  456. __except (1,1)
  457. {
  458. return E_INVALIDARG;
  459. }
  460. return hr;
  461. }
  462. //////////////////////////////////////////////////////////////////////////
  463. STDMETHODIMP C_dxj_DirectPlay4Object::setGroupData(
  464. /* [in] */ long id,
  465. /* [in] */ BSTR data,
  466. /* [in] */ long flags)
  467. {
  468. HRESULT hr;
  469. DWORD datasize=0;
  470. void *pdata=NULL;
  471. if (data){
  472. pdata=data;
  473. datasize= ((DWORD*)data)[-1];
  474. }
  475. __try {
  476. hr = m__dxj_DirectPlay4->SetGroupData((DPID)id,(void*)pdata,
  477. (DWORD)datasize, (DWORD)flags);
  478. }
  479. __except(1,1){
  480. return E_INVALIDARG;
  481. }
  482. return hr;
  483. }
  484. /////////////////////////////////////////////////////////////////////////////
  485. STDMETHODIMP C_dxj_DirectPlay4Object::setGroupName(
  486. /* [in] */ Dpid id,
  487. /* [in] */ BSTR friendlyName,
  488. /* [in] */ BSTR formalName,
  489. /* [in] */ long flags)
  490. {
  491. //ASSERT( m__dxj_DirectPlay4 != NULL )
  492. DPNAME dpName;
  493. ZeroMemory(&dpName,sizeof(DPNAME));
  494. dpName.dwSize = sizeof(DPNAME);
  495. dpName.lpszShortName=NULL;
  496. dpName.lpszLongName=NULL;
  497. if ( friendlyName )
  498. {
  499. dpName.lpszShortName = friendlyName;
  500. }
  501. if ( formalName )
  502. {
  503. dpName.lpszLongName = formalName;
  504. }
  505. HRESULT hr = m__dxj_DirectPlay4->SetGroupName((DPID)id, &dpName, flags);
  506. return hr;
  507. }
  508. //////////////////////////////////////////////////////////////////////////
  509. STDMETHODIMP C_dxj_DirectPlay4Object::setPlayerData(
  510. /* [in] */ long id,
  511. /* [in] */ BSTR data,
  512. /* [in] */ long flags)
  513. {
  514. HRESULT hr;
  515. DWORD datasize=0;
  516. void *pdata=NULL;
  517. if (data){
  518. if (data[0]!=0x00) {
  519. pdata=data;
  520. datasize= ((DWORD*)data)[-1];
  521. }
  522. }
  523. __try {
  524. hr = m__dxj_DirectPlay4->SetPlayerData((DPID)id,(void*)pdata,
  525. (DWORD)datasize, (DWORD)flags);
  526. }
  527. __except(1,1){
  528. return E_INVALIDARG;
  529. }
  530. return hr;
  531. }
  532. /////////////////////////////////////////////////////////////////////////////
  533. STDMETHODIMP C_dxj_DirectPlay4Object::setPlayerName(
  534. /* [in] */ Dpid id,
  535. /* [in] */ BSTR friendlyName,
  536. /* [in] */ BSTR formalName,
  537. /* [in] */ long flags)
  538. {
  539. if ( m__dxj_DirectPlay4 == NULL )
  540. return E_FAIL;
  541. DWORD l=0;
  542. DPNAME dpName;
  543. ZeroMemory(&dpName,sizeof(DPNAME)) ;
  544. dpName.dwSize=sizeof(DPNAME);
  545. dpName.lpszShortName = friendlyName;
  546. dpName.lpszLongName = formalName;
  547. HRESULT hr = m__dxj_DirectPlay4->SetPlayerName((DPID) id, &dpName, (DWORD)flags);
  548. return hr;
  549. }
  550. //////////////////////////////////////////////////////////////////////////
  551. // Sets the current session description
  552. //////////////////////////////////////////////////////////////////////////
  553. STDMETHODIMP C_dxj_DirectPlay4Object::setSessionDesc(
  554. /* [in] */ I_dxj_DirectPlaySessionData __RPC_FAR *sessionDesc
  555. )
  556. {
  557. if ( m__dxj_DirectPlay4 == NULL )
  558. return E_FAIL;
  559. if (!sessionDesc) return E_INVALIDARG;
  560. DPSESSIONDESC2 dpSessionDesc;
  561. sessionDesc->AddRef();
  562. sessionDesc->getData(&dpSessionDesc);
  563. HRESULT hr = m__dxj_DirectPlay4->SetSessionDesc(&dpSessionDesc, 0);
  564. sessionDesc->Release();
  565. return hr;
  566. }
  567. STDMETHODIMP C_dxj_DirectPlay4Object::setGroupConnectionSettings(// long flags,
  568. Dpid idGroup, I_dxj_DPLConnection *connect)
  569. {
  570. DPLCONNECTION *con;
  571. connect->getConnectionStruct((long*)&con);
  572. HRESULT hr = m__dxj_DirectPlay4->SetGroupConnectionSettings((DWORD)0,(DPID) idGroup,con);
  573. return hr;
  574. }
  575. STDMETHODIMP C_dxj_DirectPlay4Object::sendChatMessage(
  576. Dpid fromPlayerId, Dpid toPlayerId, long flags, BSTR message)
  577. {
  578. DPCHAT dpChat;
  579. dpChat.dwSize=sizeof(DPCHAT);
  580. dpChat.dwFlags=(DWORD)0;
  581. dpChat.lpszMessage=message;
  582. HRESULT hr = m__dxj_DirectPlay4->SendChatMessage((DPID)fromPlayerId,(DPID)toPlayerId,(DWORD)flags,&dpChat);
  583. return hr;
  584. }
  585. STDMETHODIMP C_dxj_DirectPlay4Object::secureOpen(I_dxj_DirectPlaySessionData *desc,
  586. long flags, DPSecurityDesc *security, DPCredentials *credentials){
  587. DPSESSIONDESC2 dpSessionDesc;
  588. DPSECURITYDESC dpSecurity;
  589. DPCREDENTIALS dpCredentials;
  590. LPCDPSECURITYDESC lpSecurity=NULL;
  591. LPCDPCREDENTIALS lpCredentials=NULL;
  592. DWORD l=0,i,j;
  593. if (desc==NULL) return E_INVALIDARG;
  594. //FillRealSessionDesc(&dpSessionDesc,desc);
  595. ZeroMemory((void*)&dpSecurity,sizeof(DPSECURITYDESC));
  596. ZeroMemory((void*)&dpCredentials,sizeof(DPCREDENTIALS));
  597. if (security){
  598. //if all members are NULL then replace with null pointer
  599. j=0;
  600. for (i=0;i<sizeof(DPSecurityDesc);i++){
  601. if (((char*)security)[i]==0) j++;
  602. }
  603. if (j!=sizeof(DPSecurityDesc)){
  604. dpSecurity.dwSize=sizeof(DPSECURITYDESC);
  605. l=0;l=wcslen(security->strSSPIProvider);
  606. if (l){
  607. dpSecurity.lpszSSPIProvider = SysAllocString(security->strSSPIProvider);
  608. }
  609. l=0;l=wcslen(security->strCAPIProvider);
  610. if (l){
  611. dpSecurity.lpszCAPIProvider = SysAllocString(security->strCAPIProvider);
  612. }
  613. lpSecurity=&dpSecurity;
  614. }
  615. }
  616. if (credentials){
  617. //if all members are NULL then replace with null pointer
  618. j=0;
  619. for (i=0;i<sizeof(DPCredentials);i++){
  620. if (((char*)credentials)[i]==0) j++;
  621. }
  622. if (j!=sizeof(DPCredentials)){
  623. dpCredentials.dwSize=sizeof(DPCREDENTIALS);
  624. l=0;l=wcslen(credentials->strUsername);
  625. if (l){
  626. //dpCredentials.lpszUsername = (LPWSTR)alloca(sizeof(WCHAR)*(l+1));
  627. //wcscpy(dpCredentials.lpszUsername , credentials->username);
  628. dpCredentials.lpszUsername=SysAllocString(credentials->strUsername);
  629. }
  630. l=0;l=wcslen(credentials->strPassword);
  631. if (l){
  632. //dpCredentials.lpszPassword = (LPWSTR)alloca(sizeof(WCHAR)*(l+1));
  633. //wcscpy(dpCredentials.lpszPassword , credentials->password);
  634. dpCredentials.lpszPassword = SysAllocString(credentials->strPassword);
  635. }
  636. l=0;l=wcslen(credentials->strDomain);
  637. if (l){
  638. //dpCredentials.lpszDomain = (LPWSTR)alloca(sizeof(WCHAR)*(l+1));
  639. //wcscpy(dpCredentials.lpszDomain , credentials->domain);
  640. dpCredentials.lpszDomain = SysAllocString(credentials->strDomain);
  641. }
  642. lpSecurity=&dpSecurity;
  643. }
  644. }
  645. desc->AddRef();
  646. desc->getData(&dpSessionDesc);
  647. HRESULT hr = m__dxj_DirectPlay4->SecureOpen(&dpSessionDesc,(DWORD)flags,
  648. lpSecurity, lpCredentials);
  649. desc->Release();
  650. if (dpCredentials.lpszDomain) SysFreeString((BSTR)dpCredentials.lpszDomain);
  651. if (dpCredentials.lpszPassword) SysFreeString((BSTR)dpCredentials.lpszPassword);
  652. if (dpCredentials.lpszUsername) SysFreeString((BSTR)dpCredentials.lpszUsername);
  653. if (dpSecurity.lpszSSPIProvider)SysFreeString((BSTR)dpSecurity.lpszSSPIProvider);
  654. if (dpSecurity.lpszCAPIProvider)SysFreeString((BSTR)dpSecurity.lpszCAPIProvider);
  655. return hr;
  656. }
  657. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerFlags(Dpid id, long *ret){
  658. HRESULT hr = m__dxj_DirectPlay4->GetPlayerFlags((DPID)id,(DWORD*)ret);
  659. return hr;
  660. }
  661. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupFlags(Dpid id, long *ret){
  662. HRESULT hr = m__dxj_DirectPlay4->GetGroupFlags((DPID)id,(DWORD*)ret);
  663. return hr;
  664. }
  665. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupParent(Dpid id, long *ret){
  666. HRESULT hr = m__dxj_DirectPlay4->GetGroupParent((DPID)id,(DPID*)ret);
  667. return hr;
  668. }
  669. STDMETHODIMP C_dxj_DirectPlay4Object::deleteGroupFromGroup(Dpid id, Dpid id2){
  670. HRESULT hr = m__dxj_DirectPlay4->DeleteGroupFromGroup((DPID)id,(DPID)id2);
  671. return hr;
  672. }
  673. STDMETHODIMP C_dxj_DirectPlay4Object::addGroupToGroup(Dpid id, Dpid id2){
  674. HRESULT hr = m__dxj_DirectPlay4->AddGroupToGroup((DPID)id,(DPID)id2);
  675. return hr;
  676. }
  677. STDMETHODIMP C_dxj_DirectPlay4Object::getPlayerAccountId(Dpid id, BSTR *ret){
  678. LPDPACCOUNTDESC pdesc;
  679. HRESULT hr;
  680. DWORD size=0;
  681. hr=m__dxj_DirectPlay4->GetPlayerAccount((DPID)id,0,NULL,&size);
  682. if (size==0) return E_FAIL;
  683. pdesc=(LPDPACCOUNTDESC)malloc(size);
  684. if (!pdesc) return E_OUTOFMEMORY;
  685. hr=m__dxj_DirectPlay4->GetPlayerAccount((DPID)id,0,(void*)pdesc,&size);
  686. if FAILED(hr) {
  687. free(pdesc);
  688. return hr;
  689. }
  690. *ret=SysAllocString(pdesc->lpszAccountID);
  691. return S_OK;
  692. }
  693. STDMETHODIMP C_dxj_DirectPlay4Object::initializeConnection(I_dxj_DPAddress *con//,long flags
  694. )
  695. {
  696. DWORD size;
  697. void *pData;
  698. if (!con) return E_INVALIDARG;
  699. con->getAddress((long*)&pData,(long*)&size);
  700. HRESULT hr=m__dxj_DirectPlay4->InitializeConnection(pData,(DWORD)0);
  701. return hr;
  702. }
  703. STDMETHODIMP C_dxj_DirectPlay4Object::createGroupInGroup(Dpid id,BSTR longName,BSTR shortName,long flags, Dpid *retval){
  704. DPNAME dpName;
  705. DWORD l1=0;
  706. DWORD l2=0;
  707. ZeroMemory(&dpName,sizeof(DPNAME));
  708. dpName.dwSize=sizeof(DPNAME);
  709. if (shortName){
  710. l1=wcslen(shortName);
  711. if (l1>0){
  712. __try { dpName.lpszShortName = (LPWSTR)alloca(sizeof(WCHAR)*(l1+1)); } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  713. wcscpy(dpName.lpszShortName, shortName);
  714. }
  715. }
  716. if (longName){
  717. l2=0;l2=wcslen(longName);
  718. if (l2>0){
  719. __try { dpName.lpszLongName = (LPWSTR)alloca(sizeof(WCHAR)*(l2+1)); } __except(EXCEPTION_EXECUTE_HANDLER) { return E_FAIL; }
  720. wcscpy(dpName.lpszLongName, longName);
  721. }
  722. }
  723. DPID ret;
  724. HRESULT hr;
  725. if ((l1==0)&&(l2==0)){
  726. hr=m__dxj_DirectPlay4->CreateGroupInGroup((DPID)id,&ret,NULL,NULL,0,(DWORD)flags);
  727. }
  728. else {
  729. hr=m__dxj_DirectPlay4->CreateGroupInGroup((DPID)id,&ret,&dpName,NULL,0,(DWORD)flags);
  730. }
  731. *retval=(Dpid)ret;
  732. return hr;
  733. }
  734. //////////////////////////////////////////////////////////////////////////
  735. STDMETHODIMP C_dxj_DirectPlay4Object::getDPEnumConnections(
  736. BSTR strGuid,
  737. long flags,
  738. I_dxj_DPEnumConnections **retVal){
  739. HRESULT hr=C_dxj_DPEnumConnectionsObject::create(m__dxj_DirectPlay4,strGuid,flags,retVal);
  740. return hr;
  741. }
  742. STDMETHODIMP C_dxj_DirectPlay4Object::getMessageQueue(
  743. /* [in] */ long from,
  744. /* [in] */ long to,
  745. long flags,
  746. long *nMessages,
  747. long *nBytes)
  748. {
  749. return m__dxj_DirectPlay4->GetMessageQueue((DPID)from,(DPID)to,(DWORD)flags,(DWORD*)nMessages,(DWORD*)nBytes);
  750. }
  751. STDMETHODIMP C_dxj_DirectPlay4Object::getGroupOwner(
  752. /* [in] */ long groupId,
  753. /* [retval][out] */ long __RPC_FAR *ret)
  754. {
  755. return m__dxj_DirectPlay4->GetGroupOwner((DWORD)groupId,(DWORD*)ret);
  756. }
  757. STDMETHODIMP C_dxj_DirectPlay4Object::cancelPriority(
  758. long minPriority,
  759. long maxPriority
  760. // long flags
  761. )
  762. {
  763. return m__dxj_DirectPlay4->CancelPriority((DWORD)minPriority,(DWORD)maxPriority,(DWORD) 0);
  764. }
  765. STDMETHODIMP C_dxj_DirectPlay4Object::cancelMessage(
  766. /* [in] */ long msgid
  767. ///* [in] */ long flags
  768. )
  769. {
  770. return m__dxj_DirectPlay4->CancelMessage((DWORD)msgid,(DWORD)0);
  771. }
  772. STDMETHODIMP C_dxj_DirectPlay4Object::setGroupOwner(
  773. /* [in] */ long groupId,
  774. /* [in] */ long ownerId)
  775. {
  776. return m__dxj_DirectPlay4->SetGroupOwner((DWORD)groupId,(DWORD)ownerId);
  777. }
  778. STDMETHODIMP C_dxj_DirectPlay4Object::sendEx(
  779. /* [in] */ long fromPlayerId,
  780. /* [in] */ long toPlayerId,
  781. /* [in] */ long flags,
  782. /* [in] */ I_dxj_DirectPlayMessage *msg,
  783. /* [in] */ long priority,
  784. /* [in] */ long timeout,
  785. /* [in] */ long context,
  786. /* [retval][out] */ long *messageid)
  787. {
  788. HRESULT hr;
  789. void *pdata=NULL;
  790. DWORD dataSize=0;
  791. if (!msg) return E_INVALIDARG;
  792. __try {
  793. msg->getPointer((long*)&pdata);
  794. msg->getMessageSize((long*)&dataSize);
  795. hr= m__dxj_DirectPlay4->SendEx((DPID)fromPlayerId, (DPID)toPlayerId, (DWORD)flags,
  796. (void*)pdata, (DWORD) dataSize,
  797. (DWORD) priority,
  798. (DWORD) timeout,
  799. (void*) context,
  800. (DWORD_PTR*) messageid); //bugbug SUNDOWN
  801. }
  802. __except (1,1)
  803. {
  804. return E_INVALIDARG;
  805. }
  806. return hr;
  807. }
  808. STDMETHODIMP C_dxj_DirectPlay4Object::createSessionData( I_dxj_DirectPlaySessionData __RPC_FAR *__RPC_FAR *sessionDesc)
  809. {
  810. HRESULT hr;
  811. hr=C_dxj_DirectPlaySessionDataObject::create((DPSESSIONDESC2*)NULL,sessionDesc);
  812. return hr;
  813. }
  814. STDMETHODIMP C_dxj_DirectPlay4Object::createMessage( I_dxj_DirectPlayMessage __RPC_FAR *__RPC_FAR *ret)
  815. {
  816. HRESULT hr;
  817. hr= C_dxj_DirectPlayMessageObject::create(1,0,NULL,ret);
  818. return hr;
  819. }