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.

927 lines
30 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: DPLConset.cpp
  6. * Content: DirectPlay Lobby Connection Settings Utility Functions
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 06/13/00 rmt Created
  12. * 07/07/00 rmt Bug #38755 - No way to specify player name in connection settings
  13. * 07/08/2000 rmt Bug #38725 - Need to provide method to detect if app was lobby launched
  14. * rmt Bug #38757 - Callback messages for connections may return AFTER WaitForConnection returns
  15. * rmt Bug #38755 - No way to specify player name in Connection Settings
  16. * rmt Bug #38758 - DPLOBBY8.H has incorrect comments
  17. * rmt Bug #38783 - pvUserApplicationContext is only partially implemented
  18. * rmt Added DPLHANDLE_ALLCONNECTIONS and dwFlags (reserved field to couple of funcs).
  19. * 07/12/2000 rmt Removed improper assert
  20. * 02/06/2001 rodtoll WINBUG #293871: DPLOBBY8: [IA64] Lobby launching a 64-bit
  21. * app from 64-bit lobby launcher crashes with unaligned memory error.
  22. *@@END_MSINTERNAL
  23. *
  24. ***************************************************************************/
  25. #include "dnlobbyi.h"
  26. #undef DPF_MODNAME
  27. #define DPF_MODNAME "CConnectionSettings::CConnectionSettings"
  28. CConnectionSettings::CConnectionSettings(): m_dwSignature(DPLSIGNATURE_LOBBYCONSET), m_fManaged(FALSE), m_pdplConnectionSettings(NULL), m_fCritSecInited(FALSE)
  29. {
  30. }
  31. CConnectionSettings::~CConnectionSettings()
  32. {
  33. if( !m_fManaged && m_pdplConnectionSettings )
  34. {
  35. FreeConnectionSettings( m_pdplConnectionSettings );
  36. m_pdplConnectionSettings = NULL;
  37. }
  38. if (m_fCritSecInited)
  39. {
  40. DNDeleteCriticalSection( &m_csLock );
  41. }
  42. m_dwSignature = DPLSIGNATURE_LOBBYCONSET_FREE;
  43. }
  44. #undef DPF_MODNAME
  45. #define DPF_MODNAME "CConnectionSettings::FreeConnectionSettings"
  46. // CConnectionSettings::FreeConnectionSettings
  47. //
  48. // This function frees the memory associated with the specified connection
  49. void CConnectionSettings::FreeConnectionSettings( DPL_CONNECTION_SETTINGS *pConnectionSettings )
  50. {
  51. if( pConnectionSettings )
  52. {
  53. if( pConnectionSettings->pwszPlayerName )
  54. {
  55. delete [] pConnectionSettings->pwszPlayerName;
  56. pConnectionSettings->pwszPlayerName = NULL;
  57. }
  58. if( pConnectionSettings->dpnAppDesc.pwszSessionName )
  59. {
  60. delete [] pConnectionSettings->dpnAppDesc.pwszSessionName;
  61. pConnectionSettings->dpnAppDesc.pwszSessionName = NULL;
  62. }
  63. if( pConnectionSettings->dpnAppDesc.pwszPassword )
  64. {
  65. delete [] pConnectionSettings->dpnAppDesc.pwszPassword;
  66. pConnectionSettings->dpnAppDesc.pwszPassword = NULL;
  67. }
  68. if( pConnectionSettings->dpnAppDesc.pvReservedData )
  69. {
  70. delete [] pConnectionSettings->dpnAppDesc.pvReservedData;
  71. pConnectionSettings->dpnAppDesc.pvReservedData = NULL;
  72. }
  73. if( pConnectionSettings->dpnAppDesc.pvApplicationReservedData )
  74. {
  75. delete [] pConnectionSettings->dpnAppDesc.pvApplicationReservedData;
  76. pConnectionSettings->dpnAppDesc.pvApplicationReservedData = NULL;
  77. }
  78. if( pConnectionSettings->pdp8HostAddress )
  79. {
  80. IDirectPlay8Address_Release( pConnectionSettings->pdp8HostAddress );
  81. pConnectionSettings->pdp8HostAddress = NULL;
  82. }
  83. if( pConnectionSettings->ppdp8DeviceAddresses )
  84. {
  85. for( DWORD dwIndex = 0; dwIndex < pConnectionSettings->cNumDeviceAddresses; dwIndex++ )
  86. {
  87. IDirectPlay8Address_Release( pConnectionSettings->ppdp8DeviceAddresses[dwIndex] );
  88. }
  89. delete [] pConnectionSettings->ppdp8DeviceAddresses;
  90. pConnectionSettings->ppdp8DeviceAddresses = NULL;
  91. }
  92. delete pConnectionSettings;
  93. }
  94. }
  95. #undef DPF_MODNAME
  96. #define DPF_MODNAME "CConnectionSettings::Initialize"
  97. // Initialize (DPL_CONNECTION_SETTINGS version)
  98. //
  99. // This function tells this class to take the specified connection settings and
  100. // work with it.
  101. //
  102. HRESULT CConnectionSettings::Initialize( DPL_CONNECTION_SETTINGS * pdplSettings )
  103. {
  104. if (!DNInitializeCriticalSection( &m_csLock ) )
  105. {
  106. DPFX(DPFPREP, 0, "Failed to create critical section");
  107. return DPNERR_OUTOFMEMORY;
  108. }
  109. m_fCritSecInited = TRUE;
  110. m_pdplConnectionSettings = pdplSettings;
  111. m_fManaged = FALSE;
  112. return DPN_OK;
  113. }
  114. #undef DPF_MODNAME
  115. #define DPF_MODNAME "CConnectionSettings::Initialize"
  116. // Initialize (Wire Version)
  117. //
  118. // THis function initializes this object to contain a connection settings structure
  119. // that mirrors the values of the wire message.
  120. HRESULT CConnectionSettings::Initialize( UNALIGNED DPL_INTERNAL_CONNECTION_SETTINGS *pdplSettingsMsg, UNALIGNED BYTE * pbBufferStart )
  121. {
  122. DNASSERT( pdplSettingsMsg );
  123. HRESULT hr = DPN_OK;
  124. DPL_CONNECTION_SETTINGS *pdplConnectionSettings = NULL;
  125. UNALIGNED BYTE *pBasePointer = pbBufferStart;
  126. PDIRECTPLAY8ADDRESS pdp8Address = NULL;
  127. WCHAR *wszTmpAlignedBuffer = NULL;
  128. DWORD dwTmpOffset = 0;
  129. DWORD dwTmpLength = 0;
  130. UNALIGNED DWORD *pdwOffsets = NULL;
  131. UNALIGNED DWORD *pdwLengths = NULL;
  132. if (!DNInitializeCriticalSection( &m_csLock ) )
  133. {
  134. DPFX(DPFPREP, 0, "Failed to create critical section");
  135. return DPNERR_OUTOFMEMORY;
  136. }
  137. m_fCritSecInited = TRUE;
  138. pdplConnectionSettings = new DPL_CONNECTION_SETTINGS;
  139. if( !pdplConnectionSettings )
  140. {
  141. hr = DPNERR_OUTOFMEMORY;
  142. goto INITIALIZE_FAILED;
  143. }
  144. // Zero out the memory
  145. ZeroMemory( pdplConnectionSettings, sizeof( DPL_CONNECTION_SETTINGS ) );
  146. pdplConnectionSettings->dwSize = sizeof( DPL_CONNECTION_SETTINGS );
  147. pdplConnectionSettings->dwFlags = pdplSettingsMsg->dwFlags;
  148. //
  149. // PLAYER NAME COPY
  150. //
  151. if( pdplSettingsMsg->dwPlayerNameLength )
  152. {
  153. pdplConnectionSettings->pwszPlayerName = new WCHAR[pdplSettingsMsg->dwPlayerNameLength >> 1];
  154. if( !pdplConnectionSettings->pwszPlayerName )
  155. {
  156. hr = DPNERR_OUTOFMEMORY;
  157. goto INITIALIZE_FAILED;
  158. }
  159. memcpy( pdplConnectionSettings->pwszPlayerName, pBasePointer + pdplSettingsMsg->dwPlayerNameOffset,
  160. pdplSettingsMsg->dwPlayerNameLength );
  161. }
  162. //
  163. // HOST ADDRESS COPY
  164. //
  165. if( pdplSettingsMsg->dwHostAddressLength )
  166. {
  167. // We need to create a buffer for string that we know is aligned. - Ick -
  168. wszTmpAlignedBuffer = new WCHAR[pdplSettingsMsg->dwHostAddressLength >> 1];
  169. if( !wszTmpAlignedBuffer )
  170. {
  171. hr = DPNERR_OUTOFMEMORY;
  172. goto INITIALIZE_FAILED;
  173. }
  174. // Copy the potentially unaligned data to the aligned data string.
  175. memcpy( wszTmpAlignedBuffer, pBasePointer + pdplSettingsMsg->dwHostAddressOffset,pdplSettingsMsg->dwHostAddressLength );
  176. hr = COM_CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay8Address, (void **) &pdp8Address, FALSE );
  177. if( FAILED( hr ) )
  178. {
  179. DPFX(DPFPREP, 0, "Error creating address hr=0x%x", hr );
  180. goto INITIALIZE_FAILED;
  181. }
  182. // Convert the host address (if there is one)
  183. hr = IDirectPlay8Address_BuildFromURLW( pdp8Address, wszTmpAlignedBuffer );
  184. if( FAILED( hr ) )
  185. {
  186. DPFX(DPFPREP, 0, "Error building URL from address hr=0x%x", hr );
  187. goto INITIALIZE_FAILED;
  188. }
  189. pdplConnectionSettings->pdp8HostAddress = pdp8Address;
  190. pdp8Address = NULL;
  191. if( wszTmpAlignedBuffer )
  192. {
  193. delete [] wszTmpAlignedBuffer;
  194. wszTmpAlignedBuffer = NULL;
  195. }
  196. }
  197. if( pdplSettingsMsg->dwNumDeviceAddresses )
  198. {
  199. pdplConnectionSettings->cNumDeviceAddresses = pdplSettingsMsg->dwNumDeviceAddresses;
  200. //
  201. // DEVICE ADDRESS COPY
  202. //
  203. pdplConnectionSettings->ppdp8DeviceAddresses = new PDIRECTPLAY8ADDRESS[pdplSettingsMsg->dwNumDeviceAddresses];
  204. if( !pdplConnectionSettings->ppdp8DeviceAddresses )
  205. {
  206. hr = DPNERR_OUTOFMEMORY;
  207. goto INITIALIZE_FAILED;
  208. }
  209. // Give us an unaligned dword pointer to the device addresses offset
  210. pdwOffsets = (UNALIGNED DWORD *) (pBasePointer + pdplSettingsMsg->dwDeviceAddressOffset);
  211. pdwLengths = (UNALIGNED DWORD *) (pBasePointer + pdplSettingsMsg->dwDeviceAddressLengthOffset);
  212. for( DWORD dwIndex = 0; dwIndex < pdplSettingsMsg->dwNumDeviceAddresses; dwIndex++ )
  213. {
  214. dwTmpOffset = pdwOffsets[dwIndex];
  215. dwTmpLength = pdwLengths[dwIndex];
  216. // We need to create a buffer for string that we know is aligned. - Ick -
  217. wszTmpAlignedBuffer = new WCHAR[dwTmpLength >> 1];
  218. if( !wszTmpAlignedBuffer )
  219. {
  220. hr = DPNERR_OUTOFMEMORY;
  221. goto INITIALIZE_FAILED;
  222. }
  223. memcpy( wszTmpAlignedBuffer, pBasePointer + dwTmpOffset, dwTmpLength );
  224. hr = COM_CoCreateInstance( CLSID_DirectPlay8Address, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay8Address, (void **) &pdp8Address, FALSE );
  225. if( FAILED( hr ) )
  226. {
  227. DPFX(DPFPREP, 0, "Error creating address hr=0x%x", hr );
  228. return hr;
  229. }
  230. // Convert the host address (if there is one)
  231. hr = IDirectPlay8Address_BuildFromURLW( pdp8Address, wszTmpAlignedBuffer );
  232. if( FAILED( hr ) )
  233. {
  234. DPFX(DPFPREP, 0, "Error building URL from address hr=0x%x", hr );
  235. DNASSERT( FALSE );
  236. return hr;
  237. }
  238. pdplConnectionSettings->ppdp8DeviceAddresses[dwIndex] = pdp8Address;
  239. pdp8Address = NULL;
  240. if( wszTmpAlignedBuffer )
  241. {
  242. delete [] wszTmpAlignedBuffer;
  243. wszTmpAlignedBuffer = NULL;
  244. }
  245. }
  246. }
  247. else
  248. {
  249. pdplConnectionSettings->ppdp8DeviceAddresses = NULL;
  250. }
  251. //
  252. // APPLICATION DESCRIPTION COPY
  253. //
  254. pdplConnectionSettings->dpnAppDesc.dwSize = sizeof( DPN_APPLICATION_DESC );
  255. pdplConnectionSettings->dpnAppDesc.dwFlags = pdplSettingsMsg->dpnApplicationDesc.dwFlags;
  256. pdplConnectionSettings->dpnAppDesc.guidInstance = pdplSettingsMsg->dpnApplicationDesc.guidInstance;
  257. pdplConnectionSettings->dpnAppDesc.guidApplication = pdplSettingsMsg->dpnApplicationDesc.guidApplication;
  258. pdplConnectionSettings->dpnAppDesc.dwMaxPlayers = pdplSettingsMsg->dpnApplicationDesc.dwMaxPlayers;
  259. pdplConnectionSettings->dpnAppDesc.dwCurrentPlayers = pdplSettingsMsg->dpnApplicationDesc.dwCurrentPlayers;
  260. if( pdplSettingsMsg->dpnApplicationDesc.dwSessionNameSize )
  261. {
  262. pdplConnectionSettings->dpnAppDesc.pwszSessionName = new WCHAR[pdplSettingsMsg->dpnApplicationDesc.dwSessionNameSize >> 1];
  263. if( !pdplConnectionSettings->dpnAppDesc.pwszSessionName )
  264. {
  265. hr = DPNERR_OUTOFMEMORY;
  266. goto INITIALIZE_FAILED;
  267. }
  268. memcpy( pdplConnectionSettings->dpnAppDesc.pwszSessionName,
  269. pBasePointer + pdplSettingsMsg->dpnApplicationDesc.dwSessionNameOffset,
  270. pdplSettingsMsg->dpnApplicationDesc.dwSessionNameSize );
  271. }
  272. if( pdplSettingsMsg->dpnApplicationDesc.dwPasswordSize )
  273. {
  274. pdplConnectionSettings->dpnAppDesc.pwszPassword = new WCHAR[pdplSettingsMsg->dpnApplicationDesc.dwPasswordSize >> 1];
  275. if( !pdplConnectionSettings->dpnAppDesc.pwszPassword )
  276. {
  277. hr = DPNERR_OUTOFMEMORY;
  278. goto INITIALIZE_FAILED;
  279. }
  280. memcpy( pdplConnectionSettings->dpnAppDesc.pwszPassword,
  281. pBasePointer + pdplSettingsMsg->dpnApplicationDesc.dwPasswordOffset,
  282. pdplSettingsMsg->dpnApplicationDesc.dwPasswordSize );
  283. }
  284. if( pdplSettingsMsg->dpnApplicationDesc.dwReservedDataSize )
  285. {
  286. pdplConnectionSettings->dpnAppDesc.pvReservedData = new BYTE[pdplSettingsMsg->dpnApplicationDesc.dwReservedDataSize];
  287. if( !pdplConnectionSettings->dpnAppDesc.pvReservedData )
  288. {
  289. hr = DPNERR_OUTOFMEMORY;
  290. goto INITIALIZE_FAILED;
  291. }
  292. memcpy( pdplConnectionSettings->dpnAppDesc.pvReservedData,
  293. pBasePointer + pdplSettingsMsg->dpnApplicationDesc.dwReservedDataOffset,
  294. pdplSettingsMsg->dpnApplicationDesc.dwReservedDataSize );
  295. pdplConnectionSettings->dpnAppDesc.dwReservedDataSize = pdplSettingsMsg->dpnApplicationDesc.dwReservedDataSize;
  296. }
  297. if( pdplSettingsMsg->dpnApplicationDesc.dwApplicationReservedDataSize )
  298. {
  299. pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData = new BYTE[pdplSettingsMsg->dpnApplicationDesc.dwApplicationReservedDataSize];
  300. if( !pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData )
  301. {
  302. hr = DPNERR_OUTOFMEMORY;
  303. goto INITIALIZE_FAILED;
  304. }
  305. memcpy( pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData,
  306. pBasePointer + pdplSettingsMsg->dpnApplicationDesc.dwApplicationReservedDataOffset,
  307. pdplSettingsMsg->dpnApplicationDesc.dwApplicationReservedDataSize );
  308. pdplConnectionSettings->dpnAppDesc.dwApplicationReservedDataSize = pdplSettingsMsg->dpnApplicationDesc.dwApplicationReservedDataSize;
  309. }
  310. // Free the old structure if one exists.
  311. if( m_fManaged )
  312. {
  313. m_fManaged = FALSE;
  314. }
  315. else if( m_pdplConnectionSettings )
  316. {
  317. FreeConnectionSettings( m_pdplConnectionSettings );
  318. }
  319. m_pdplConnectionSettings = pdplConnectionSettings;
  320. if( wszTmpAlignedBuffer )
  321. delete [] wszTmpAlignedBuffer;
  322. return DPN_OK;
  323. INITIALIZE_FAILED:
  324. FreeConnectionSettings( pdplConnectionSettings );
  325. if( wszTmpAlignedBuffer )
  326. delete [] wszTmpAlignedBuffer;
  327. if( pdp8Address )
  328. IDirectPlay8Address_Release( pdp8Address );
  329. return hr;
  330. }
  331. #undef DPF_MODNAME
  332. #define DPF_MODNAME "CConnectionSettings::InitializeAndCopy"
  333. // InitializeAndCopy
  334. //
  335. // This function initializes this class to contain a copy of the specified
  336. // connection settings structure.
  337. //
  338. HRESULT CConnectionSettings::InitializeAndCopy( const DPL_CONNECTION_SETTINGS * const pdplSettings )
  339. {
  340. DNASSERT( pdplSettings );
  341. HRESULT hr = DPN_OK;
  342. DPL_CONNECTION_SETTINGS *pdplConnectionSettings = NULL;
  343. if (!DNInitializeCriticalSection( &m_csLock ) )
  344. {
  345. DPFX(DPFPREP, 0, "Failed to create critical section");
  346. return DPNERR_OUTOFMEMORY;
  347. }
  348. m_fCritSecInited = TRUE;
  349. pdplConnectionSettings = new DPL_CONNECTION_SETTINGS;
  350. if( !pdplConnectionSettings )
  351. {
  352. hr = DPNERR_OUTOFMEMORY;
  353. goto INITIALIZE_FAILED;
  354. }
  355. // Copy over. This is a little dangerous as we copy pointer values. Pointers
  356. // should be set in our local structure to NULL so that proper cleanup can occur
  357. // on error. (Otherwise we'll free other structure's memory!!)
  358. memcpy( pdplConnectionSettings, pdplSettings, sizeof( DPL_CONNECTION_SETTINGS ) );
  359. // Reset pointers as mentioned above.
  360. pdplConnectionSettings->pdp8HostAddress = NULL;
  361. pdplConnectionSettings->ppdp8DeviceAddresses = NULL;
  362. pdplConnectionSettings->pwszPlayerName = NULL;
  363. pdplConnectionSettings->dpnAppDesc.pwszSessionName = NULL;
  364. pdplConnectionSettings->dpnAppDesc.pwszPassword = NULL;
  365. pdplConnectionSettings->dpnAppDesc.pvReservedData = NULL;
  366. pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData = NULL;
  367. if( pdplSettings->pdp8HostAddress )
  368. {
  369. hr = IDirectPlay8Address_Duplicate( pdplSettings->pdp8HostAddress, &pdplConnectionSettings->pdp8HostAddress );
  370. if( FAILED( hr ) )
  371. {
  372. DPFX(DPFPREP, 0, "Error duplicating host address hr [0x%x]", hr );
  373. goto INITIALIZE_FAILED;
  374. }
  375. }
  376. if( pdplSettings->ppdp8DeviceAddresses )
  377. {
  378. pdplConnectionSettings->ppdp8DeviceAddresses = new PDIRECTPLAY8ADDRESS[pdplSettings->cNumDeviceAddresses];
  379. if( !pdplConnectionSettings->ppdp8DeviceAddresses )
  380. {
  381. hr = DPNERR_OUTOFMEMORY;
  382. DPFX(DPFPREP, 0, "Failed allocating memory" );
  383. goto INITIALIZE_FAILED;
  384. }
  385. for( DWORD dwIndex = 0; dwIndex < pdplSettings->cNumDeviceAddresses; dwIndex++ )
  386. {
  387. hr = IDirectPlay8Address_Duplicate( pdplSettings->ppdp8DeviceAddresses[dwIndex], &pdplConnectionSettings->ppdp8DeviceAddresses[dwIndex] );
  388. if( FAILED( hr ) )
  389. {
  390. DPFX(DPFPREP, 0, "Error duplicating host address hr [0x%x]", hr );
  391. goto INITIALIZE_FAILED;
  392. }
  393. }
  394. }
  395. if( pdplSettings->pwszPlayerName )
  396. {
  397. pdplConnectionSettings->pwszPlayerName = new WCHAR[wcslen(pdplSettings->pwszPlayerName)+1];
  398. if( !pdplConnectionSettings->pwszPlayerName )
  399. {
  400. DPFX(DPFPREP, 0, "Failed allocating memory" );
  401. hr = DPNERR_OUTOFMEMORY;
  402. goto INITIALIZE_FAILED;
  403. }
  404. wcscpy( pdplConnectionSettings->pwszPlayerName, pdplSettings->pwszPlayerName );
  405. }
  406. if( pdplSettings->dpnAppDesc.pwszSessionName )
  407. {
  408. pdplConnectionSettings->dpnAppDesc.pwszSessionName = new WCHAR[wcslen(pdplSettings->dpnAppDesc.pwszSessionName)+1];
  409. if( !pdplConnectionSettings->dpnAppDesc.pwszSessionName )
  410. {
  411. DPFX(DPFPREP, 0, "Failed allocating memory" );
  412. hr = DPNERR_OUTOFMEMORY;
  413. goto INITIALIZE_FAILED;
  414. }
  415. wcscpy( pdplConnectionSettings->dpnAppDesc.pwszSessionName, pdplSettings->dpnAppDesc.pwszSessionName );
  416. }
  417. if( pdplSettings->dpnAppDesc.pwszPassword )
  418. {
  419. pdplConnectionSettings->dpnAppDesc.pwszPassword = new WCHAR[wcslen(pdplSettings->dpnAppDesc.pwszPassword)+1];
  420. if( !pdplConnectionSettings->dpnAppDesc.pwszPassword )
  421. {
  422. DPFX(DPFPREP, 0, "Failed allocating memory" );
  423. hr = DPNERR_OUTOFMEMORY;
  424. goto INITIALIZE_FAILED;
  425. }
  426. wcscpy( pdplConnectionSettings->dpnAppDesc.pwszPassword, pdplSettings->dpnAppDesc.pwszPassword );
  427. }
  428. if( pdplSettings->dpnAppDesc.pvReservedData )
  429. {
  430. pdplConnectionSettings->dpnAppDesc.pvReservedData = new BYTE[pdplSettings->dpnAppDesc.dwReservedDataSize];
  431. if( !pdplConnectionSettings->dpnAppDesc.pvReservedData )
  432. {
  433. DPFX(DPFPREP, 0, "Failed allocating memory" );
  434. hr = DPNERR_OUTOFMEMORY;
  435. goto INITIALIZE_FAILED;
  436. }
  437. memcpy( pdplConnectionSettings->dpnAppDesc.pvReservedData,
  438. pdplSettings->dpnAppDesc.pvReservedData,
  439. pdplSettings->dpnAppDesc.dwReservedDataSize );
  440. }
  441. if( pdplSettings->dpnAppDesc.pvApplicationReservedData )
  442. {
  443. pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData = new BYTE[pdplSettings->dpnAppDesc.dwApplicationReservedDataSize];
  444. if( !pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData )
  445. {
  446. DPFX(DPFPREP, 0, "Failed allocating memory" );
  447. hr = DPNERR_OUTOFMEMORY;
  448. goto INITIALIZE_FAILED;
  449. }
  450. memcpy( pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData,
  451. pdplSettings->dpnAppDesc.pvApplicationReservedData,
  452. pdplSettings->dpnAppDesc.dwApplicationReservedDataSize );
  453. }
  454. // Free the old structure if one exists.
  455. if( m_fManaged )
  456. {
  457. m_fManaged = FALSE;
  458. }
  459. else if( m_pdplConnectionSettings )
  460. {
  461. FreeConnectionSettings( m_pdplConnectionSettings );
  462. }
  463. m_pdplConnectionSettings = pdplConnectionSettings;
  464. return DPN_OK;
  465. INITIALIZE_FAILED:
  466. FreeConnectionSettings( pdplConnectionSettings );
  467. return hr;
  468. }
  469. #undef DPF_MODNAME
  470. #define DPF_MODNAME "CConnectionSettings::BuildWireStruct"
  471. // BuildWireStruct
  472. //
  473. // This function fills the packed buffer with the wire representation of the
  474. // connection settings structure.
  475. HRESULT CConnectionSettings::BuildWireStruct( CPackedBuffer *const pPackedBuffer )
  476. {
  477. HRESULT hr = DPN_OK;
  478. DPL_INTERNAL_CONNECTION_SETTINGS *pdplConnectSettings = NULL;
  479. WCHAR *wszTmpAddress = NULL;
  480. DWORD dwTmpStringSize = 0;
  481. UNALIGNED DWORD *pdwTmpOffsets = NULL;
  482. UNALIGNED DWORD *pdwTmpLengths = NULL;
  483. pdplConnectSettings = (DPL_INTERNAL_CONNECTION_SETTINGS *) pPackedBuffer->GetHeadAddress();
  484. hr = pPackedBuffer->AddToFront( NULL, sizeof( DPL_INTERNAL_CONNECTION_SETTINGS ) );
  485. if( hr == DPN_OK )
  486. {
  487. ZeroMemory( pdplConnectSettings, sizeof( DPL_INTERNAL_CONNECTION_SETTINGS ) );
  488. //
  489. // COPY CORE FIXED VALUES
  490. //
  491. pdplConnectSettings->dwFlags = m_pdplConnectionSettings->dwFlags;
  492. pdplConnectSettings->dwNumDeviceAddresses = m_pdplConnectionSettings->cNumDeviceAddresses;
  493. //
  494. // COPY APPDESC FIXED VALUES
  495. //
  496. pdplConnectSettings->dpnApplicationDesc.dwSize = sizeof( DPN_APPLICATION_DESC_INFO );
  497. pdplConnectSettings->dpnApplicationDesc.dwFlags = m_pdplConnectionSettings->dpnAppDesc.dwFlags;
  498. pdplConnectSettings->dpnApplicationDesc.dwMaxPlayers = m_pdplConnectionSettings->dpnAppDesc.dwMaxPlayers;
  499. pdplConnectSettings->dpnApplicationDesc.dwCurrentPlayers = m_pdplConnectionSettings->dpnAppDesc.dwCurrentPlayers;
  500. pdplConnectSettings->dpnApplicationDesc.guidInstance = m_pdplConnectionSettings->dpnAppDesc.guidInstance;
  501. pdplConnectSettings->dpnApplicationDesc.guidApplication = m_pdplConnectionSettings->dpnAppDesc.guidApplication;
  502. }
  503. //
  504. // COPY VARIABLE CORE VALUES
  505. //
  506. if( m_pdplConnectionSettings->pwszPlayerName )
  507. {
  508. hr = pPackedBuffer->AddWCHARStringToBack( m_pdplConnectionSettings->pwszPlayerName );
  509. if( hr == DPN_OK && pdplConnectSettings )
  510. {
  511. pdplConnectSettings->dwPlayerNameOffset = pPackedBuffer->GetTailOffset();
  512. pdplConnectSettings->dwPlayerNameLength =
  513. (wcslen( m_pdplConnectionSettings->pwszPlayerName )+1) * sizeof( WCHAR );
  514. }
  515. }
  516. if( m_pdplConnectionSettings->pdp8HostAddress )
  517. {
  518. hr = IDirectPlay8Address_GetURLW( m_pdplConnectionSettings->pdp8HostAddress, NULL, &dwTmpStringSize );
  519. if( hr != DPNERR_BUFFERTOOSMALL )
  520. {
  521. DPFX(DPFPREP, 0, "Failed converting address hr [0x%x]", hr );
  522. goto BUILDWIRESTRUCT_FAILURE;
  523. }
  524. wszTmpAddress = new WCHAR[dwTmpStringSize];
  525. if( !wszTmpAddress )
  526. {
  527. hr = DPNERR_OUTOFMEMORY;
  528. DPFX(DPFPREP, 0, "Failed allocating memory" );
  529. goto BUILDWIRESTRUCT_FAILURE;
  530. }
  531. hr = IDirectPlay8Address_GetURLW( m_pdplConnectionSettings->pdp8HostAddress, wszTmpAddress, &dwTmpStringSize );
  532. if( FAILED( hr ) )
  533. {
  534. DPFX(DPFPREP, 0, "Failed converting address hr [0x%x]", hr );
  535. goto BUILDWIRESTRUCT_FAILURE;
  536. }
  537. hr = pPackedBuffer->AddWCHARStringToBack( wszTmpAddress );
  538. if( hr == DPN_OK && pdplConnectSettings )
  539. {
  540. pdplConnectSettings->dwHostAddressOffset = pPackedBuffer->GetTailOffset();
  541. pdplConnectSettings->dwHostAddressLength =
  542. (wcslen( wszTmpAddress )+1) * sizeof( WCHAR );
  543. }
  544. delete [] wszTmpAddress;
  545. wszTmpAddress = NULL;
  546. }
  547. hr = pPackedBuffer->AddToBack( NULL, sizeof( DWORD ) * m_pdplConnectionSettings->cNumDeviceAddresses );
  548. if( hr == DPN_OK && pdplConnectSettings )
  549. {
  550. pdwTmpOffsets = (DWORD *) pPackedBuffer->GetTailAddress();
  551. pdplConnectSettings->dwDeviceAddressOffset = pPackedBuffer->GetTailOffset();
  552. }
  553. hr = pPackedBuffer->AddToBack( NULL, sizeof( DWORD ) * m_pdplConnectionSettings->cNumDeviceAddresses );
  554. if( hr == DPN_OK && pdplConnectSettings )
  555. {
  556. pdwTmpLengths = (DWORD *) pPackedBuffer->GetTailAddress();
  557. pdplConnectSettings->dwDeviceAddressLengthOffset = pPackedBuffer->GetTailOffset();
  558. }
  559. DWORD dwIndex;
  560. for( dwIndex = 0; dwIndex < m_pdplConnectionSettings->cNumDeviceAddresses; dwIndex++ )
  561. {
  562. dwTmpStringSize = 0;
  563. hr = IDirectPlay8Address_GetURLW(
  564. m_pdplConnectionSettings->ppdp8DeviceAddresses[dwIndex],
  565. NULL, &dwTmpStringSize );
  566. if( hr != DPNERR_BUFFERTOOSMALL )
  567. {
  568. DPFX(DPFPREP, 0, "Failed converting address hr [0x%x]", hr );
  569. goto BUILDWIRESTRUCT_FAILURE;
  570. }
  571. wszTmpAddress = new WCHAR[dwTmpStringSize];
  572. if( !wszTmpAddress )
  573. {
  574. hr = DPNERR_OUTOFMEMORY;
  575. DPFX(DPFPREP, 0, "Failed allocating memory" );
  576. goto BUILDWIRESTRUCT_FAILURE;
  577. }
  578. hr = IDirectPlay8Address_GetURLW(
  579. m_pdplConnectionSettings->ppdp8DeviceAddresses[dwIndex],
  580. wszTmpAddress, &dwTmpStringSize );
  581. if( FAILED( hr ) )
  582. {
  583. DPFX(DPFPREP, 0, "Failed converting address hr [0x%x]", hr );
  584. goto BUILDWIRESTRUCT_FAILURE;
  585. }
  586. hr = pPackedBuffer->AddWCHARStringToBack( wszTmpAddress );
  587. if( hr == DPN_OK && pdplConnectSettings && pdwTmpLengths )
  588. {
  589. pdwTmpOffsets[dwIndex] = pPackedBuffer->GetTailOffset();
  590. pdwTmpLengths[dwIndex] = (wcslen( wszTmpAddress )+1) * sizeof( WCHAR );
  591. }
  592. delete [] wszTmpAddress;
  593. wszTmpAddress = NULL;
  594. }
  595. //
  596. // COPY APP DESC VARIABLE MEMBERS
  597. //
  598. if( m_pdplConnectionSettings->dpnAppDesc.pwszPassword )
  599. {
  600. hr = pPackedBuffer->AddWCHARStringToBack( m_pdplConnectionSettings->dpnAppDesc.pwszPassword );
  601. if( hr == DPN_OK && pdplConnectSettings )
  602. {
  603. pdplConnectSettings->dpnApplicationDesc.dwPasswordOffset = pPackedBuffer->GetTailOffset();
  604. pdplConnectSettings->dpnApplicationDesc.dwPasswordSize =
  605. (wcslen( m_pdplConnectionSettings->dpnAppDesc.pwszPassword )+1) * sizeof( WCHAR );
  606. }
  607. }
  608. if( m_pdplConnectionSettings->dpnAppDesc.pwszSessionName)
  609. {
  610. hr = pPackedBuffer->AddWCHARStringToBack( m_pdplConnectionSettings->dpnAppDesc.pwszSessionName );
  611. if( hr == DPN_OK && pdplConnectSettings )
  612. {
  613. pdplConnectSettings->dpnApplicationDesc.dwSessionNameOffset = pPackedBuffer->GetTailOffset();
  614. pdplConnectSettings->dpnApplicationDesc.dwSessionNameSize =
  615. (wcslen( m_pdplConnectionSettings->dpnAppDesc.pwszSessionName )+1) * sizeof( WCHAR );
  616. }
  617. }
  618. if( m_pdplConnectionSettings->dpnAppDesc.pvReservedData )
  619. {
  620. hr = pPackedBuffer->AddToBack( m_pdplConnectionSettings->dpnAppDesc.pvReservedData,
  621. m_pdplConnectionSettings->dpnAppDesc.dwReservedDataSize );
  622. if( hr == DPN_OK && pdplConnectSettings )
  623. {
  624. pdplConnectSettings->dpnApplicationDesc.dwReservedDataOffset = pPackedBuffer->GetTailOffset();
  625. pdplConnectSettings->dpnApplicationDesc.dwReservedDataSize = m_pdplConnectionSettings->dpnAppDesc.dwReservedDataSize;
  626. }
  627. }
  628. if( m_pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData)
  629. {
  630. hr = pPackedBuffer->AddToBack( m_pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData,
  631. m_pdplConnectionSettings->dpnAppDesc.dwApplicationReservedDataSize);
  632. if( hr == DPN_OK && pdplConnectSettings )
  633. {
  634. pdplConnectSettings->dpnApplicationDesc.dwApplicationReservedDataOffset = pPackedBuffer->GetTailOffset();
  635. pdplConnectSettings->dpnApplicationDesc.dwApplicationReservedDataSize = m_pdplConnectionSettings->dpnAppDesc.dwApplicationReservedDataSize;
  636. }
  637. }
  638. BUILDWIRESTRUCT_FAILURE:
  639. if( wszTmpAddress )
  640. delete [] wszTmpAddress;
  641. return hr;
  642. }
  643. #undef DPF_MODNAME
  644. #define DPF_MODNAME "CConnectionSettings::SetEqual"
  645. // SetEqual
  646. //
  647. // This function provides a deep copy of the specified class into this object
  648. HRESULT CConnectionSettings::SetEqual( CConnectionSettings * pdplSettings )
  649. {
  650. PDPL_CONNECTION_SETTINGS pConnectSettings = pdplSettings->GetConnectionSettings();
  651. if( pConnectSettings == NULL )
  652. {
  653. DPFX(DPFPREP, 0, "Error getting settings -- no settings available!" );
  654. return DPNERR_DOESNOTEXIST;
  655. }
  656. return Initialize( pConnectSettings );
  657. }
  658. #undef DPF_MODNAME
  659. #define DPF_MODNAME "CConnectionSettings::CopyToBuffer( BYTE *pbBuffer, DWORD *pdwBufferSize )"
  660. HRESULT CConnectionSettings::CopyToBuffer( BYTE *pbBuffer, DWORD *pdwBufferSize )
  661. {
  662. if( m_pdplConnectionSettings == NULL )
  663. {
  664. *pdwBufferSize = 0;
  665. return DPNERR_DOESNOTEXIST;
  666. }
  667. CPackedBuffer packBuff;
  668. HRESULT hr = DPN_OK;
  669. DPL_CONNECTION_SETTINGS *pConnectionSettings = NULL;
  670. packBuff.Initialize( pbBuffer, *pdwBufferSize, TRUE );
  671. pConnectionSettings = (DPL_CONNECTION_SETTINGS *) packBuff.GetHeadAddress();
  672. hr = packBuff.AddToFront( m_pdplConnectionSettings, sizeof( DPL_CONNECTION_SETTINGS ), TRUE );
  673. if( FAILED( hr ) )
  674. {
  675. pConnectionSettings = NULL;
  676. }
  677. // Add app desc's session name if there is one
  678. if( m_pdplConnectionSettings->dpnAppDesc.pwszSessionName != NULL )
  679. {
  680. hr = packBuff.AddWCHARStringToBack( m_pdplConnectionSettings->dpnAppDesc.pwszSessionName, TRUE );
  681. if( pConnectionSettings )
  682. pConnectionSettings->dpnAppDesc.pwszSessionName = (WCHAR *) packBuff.GetTailAddress();
  683. }
  684. // Copy player name
  685. if( m_pdplConnectionSettings->pwszPlayerName != NULL )
  686. {
  687. hr = packBuff.AddWCHARStringToBack( m_pdplConnectionSettings->pwszPlayerName, TRUE );
  688. if( pConnectionSettings )
  689. pConnectionSettings->pwszPlayerName = (WCHAR *) packBuff.GetTailAddress();
  690. }
  691. // Copy password
  692. if( m_pdplConnectionSettings->dpnAppDesc.pwszPassword )
  693. {
  694. hr = packBuff.AddWCHARStringToBack( m_pdplConnectionSettings->dpnAppDesc.pwszPassword, TRUE );
  695. if( pConnectionSettings )
  696. pConnectionSettings->dpnAppDesc.pwszPassword = (WCHAR *) packBuff.GetTailAddress();
  697. }
  698. if( m_pdplConnectionSettings->dpnAppDesc.dwReservedDataSize )
  699. {
  700. hr = packBuff.AddToBack( m_pdplConnectionSettings->dpnAppDesc.pvReservedData, m_pdplConnectionSettings->dpnAppDesc.dwReservedDataSize, TRUE );
  701. if( pConnectionSettings )
  702. pConnectionSettings->dpnAppDesc.pvReservedData = (WCHAR *) packBuff.GetTailAddress();
  703. }
  704. if( m_pdplConnectionSettings->dpnAppDesc.dwApplicationReservedDataSize )
  705. {
  706. hr = packBuff.AddToBack( m_pdplConnectionSettings->dpnAppDesc.pvApplicationReservedData, m_pdplConnectionSettings->dpnAppDesc.dwApplicationReservedDataSize, TRUE );
  707. if( pConnectionSettings )
  708. pConnectionSettings->dpnAppDesc.pvApplicationReservedData = (WCHAR *) packBuff.GetTailAddress();
  709. }
  710. hr = packBuff.AddToBack( m_pdplConnectionSettings->ppdp8DeviceAddresses, sizeof( IDirectPlay8Address * )*m_pdplConnectionSettings->cNumDeviceAddresses, TRUE );
  711. if( pConnectionSettings )
  712. pConnectionSettings->ppdp8DeviceAddresses = (IDirectPlay8Address **) packBuff.GetTailAddress();
  713. if( pConnectionSettings )
  714. {
  715. if( m_pdplConnectionSettings->pdp8HostAddress != NULL )
  716. {
  717. hr = IDirectPlay8Address_Duplicate( m_pdplConnectionSettings->pdp8HostAddress, &pConnectionSettings->pdp8HostAddress );
  718. if( FAILED( hr ) )
  719. {
  720. DPFX(DPFPREP, 0, "Error duplicating host address hr [0x%x]", hr );
  721. goto INITIALIZE_COMPLETE;
  722. }
  723. }
  724. for( DWORD dwIndex = 0; dwIndex < m_pdplConnectionSettings->cNumDeviceAddresses; dwIndex++ )
  725. {
  726. hr = IDirectPlay8Address_Duplicate( m_pdplConnectionSettings->ppdp8DeviceAddresses[dwIndex], &pConnectionSettings->ppdp8DeviceAddresses[dwIndex] );
  727. if( FAILED( hr ) )
  728. {
  729. DPFX(DPFPREP, 0, "Error duplicating device address hr [0x%x]", hr );
  730. goto INITIALIZE_COMPLETE;
  731. }
  732. }
  733. }
  734. INITIALIZE_COMPLETE:
  735. *pdwBufferSize = packBuff.GetSizeRequired();
  736. return hr;
  737. }