fork download
  1.  
  2. #include "directplay.h"
  3. #include "glText.h"
  4. #include "chatmsglist.h"
  5.  
  6. extern CglText glText;
  7.  
  8. extern CChatMsgList ChatMsgList;
  9.  
  10.  
  11.  
  12. //-----------------------------------------------------------------------------
  13. // Name: DirectPlayMessageHandler
  14. // Desc: Handler for DirectPlay messages.
  15. //-----------------------------------------------------------------------------
  16. HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId,
  17. PVOID pMsgBuffer)
  18. {
  19.  
  20. switch( dwMessageId )
  21. {
  22. // case DPN_MSGID_RECEIVE:
  23. // {
  24.  
  25. // *****************************
  26. // *** Create New Player ***
  27. // *****************************
  28. case DPN_MSGID_CREATE_PLAYER:
  29. {
  30. // static float diff = 40.0;
  31.  
  32. // Remote player to be inserted
  33. // Player* rPlayer;
  34. // rPlayer = new Player;
  35. // rPlayer->LoadTrisAndTextures("models\\bong\\tris.md2", "models\\bong\\zigzag.pcx");
  36. // rPlayer->SetPosition(0.0, diff, 0.0);
  37.  
  38. // insertPlayer( &g_startPtr, rPlayer);
  39.  
  40. // diff = diff + 40.0;
  41.  
  42. break;
  43. }
  44.  
  45.  
  46. // **************************************
  47. // *** Reeive Message From Player ***
  48. // **************************************
  49. case DPN_MSGID_RECEIVE:
  50. {
  51. PDPNMSG_RECEIVE pMsg;
  52. TCHAR strBuffer[256];
  53.  
  54.  
  55. pMsg = (PDPNMSG_RECEIVE) pMsgBuffer;
  56. Position *pRemotePlayerPosition;
  57.  
  58.  
  59. pRemotePlayerPosition = (Position*)pMsg->pReceiveData;
  60.  
  61. // if chat msg is not empty, then print msg
  62. if ( strcmp(pRemotePlayerPosition->strChatMsg, "") != 0)
  63. {
  64.  
  65. ChatMsgList.insertChatMsg( pRemotePlayerPosition->strPlayerName,
  66. pRemotePlayerPosition->strChatMsg );
  67. }
  68.  
  69.  
  70. remotePlayerList.updatePosition( pRemotePlayerPosition );
  71.  
  72. break;
  73. }
  74. // }
  75. }
  76.  
  77.  
  78. return(DPN_OK);
  79.  
  80.  
  81.  
  82. // return S_OK;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. // Default Constructor
  94. CDirectPlay::CDirectPlay()
  95. {
  96.  
  97.  
  98.  
  99. g_pDP = NULL;
  100. g_pDeviceAddress = NULL;
  101. g_pHostAddress = NULL;
  102.  
  103. g_bHosting = FALSE;
  104. g_bConnected = FALSE;
  105.  
  106. g_dwLocalPort = CONNECT_PORT;
  107. g_dwRemotePort = CONNECT_PORT;
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. //-----------------------------------------------------------------------------
  116. // Name: InitDirectPlay()
  117. // Desc: Initialize DirectPlay
  118. //-----------------------------------------------------------------------------
  119. HRESULT CDirectPlay::InitDirectPlay()
  120. {
  121. HRESULT hr = S_OK;
  122.  
  123. InitCommonControls();
  124.  
  125.  
  126. // Init COM so we can use CoCreateInstance
  127. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  128.  
  129. // Create the IDirectPlay8Peer Object
  130. if( FAILED( hr = CoCreateInstance( CLSID_DirectPlay8Peer, NULL,
  131. CLSCTX_INPROC_SERVER,
  132. IID_IDirectPlay8Peer,
  133. (LPVOID*) &g_pDP ) ) )
  134. {
  135. // OutputError( TEXT("Failed Creating the IDirectPlay8Peer Object"), hr );
  136. return hr;
  137. }
  138.  
  139. // Init DirectPlay
  140. if( FAILED( hr = g_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) ) )
  141. {
  142. // OutputError( TEXT("Failed Initializing DirectPlay"), hr );
  143. return hr;
  144. }
  145.  
  146. return S_OK;
  147. }
  148.  
  149.  
  150.  
  151.  
  152. //-----------------------------------------------------------------------------
  153. // Name: CreateDeviceAddress()
  154. // Desc: Creates a device address
  155. //-----------------------------------------------------------------------------
  156. HRESULT CDirectPlay::CreateDeviceAddress( bool bIsHost )
  157. {
  158. HRESULT hr = S_OK;
  159.  
  160. // Start clean
  161. // SAFE_RELEASE( g_pDeviceAddress );
  162.  
  163. // Create our IDirectPlay8Address Device Address
  164. if( FAILED( hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,
  165. CLSCTX_INPROC_SERVER,
  166. IID_IDirectPlay8Address,
  167. (LPVOID*) &g_pDeviceAddress ) ) )
  168. {
  169. OutputError( TEXT("Failed Creating the IDirectPlay8Address Object"), hr );
  170. return hr;
  171. }
  172.  
  173. // Set the SP for our Device Address
  174. if( FAILED( hr = g_pDeviceAddress->SetSP(&CLSID_DP8SP_TCPIP ) ) )
  175. {
  176. OutputError( TEXT("Failed Setting the Service Provider"), hr );
  177. return hr;
  178. }
  179.  
  180. // Set the port number on which to host
  181. if( bIsHost && g_dwLocalPort > 0 )
  182. {
  183. if( FAILED( hr = g_pDeviceAddress->AddComponent( DPNA_KEY_PORT,
  184. &g_dwLocalPort,
  185. sizeof(DWORD),
  186. DPNA_DATATYPE_DWORD ) ) )
  187. {
  188. OutputError( TEXT("Failed setting the local port"), hr );
  189. return hr;
  190. }
  191. }
  192.  
  193. return hr;
  194. }
  195.  
  196.  
  197.  
  198. //-----------------------------------------------------------------------------
  199. // Name: CleanupDirectPlay()
  200. // Desc: Cleanup DirectPlay
  201. //-----------------------------------------------------------------------------
  202. void CDirectPlay::CleanupDirectPlay()
  203. {
  204. // Cleanup DirectPlay
  205. // if( g_pDP)
  206. // g_pDP->Close(0);
  207.  
  208. // SAFE_RELEASE(g_pDeviceAddress);
  209. // SAFE_RELEASE(g_pHostAddress);
  210. // SAFE_RELEASE(g_pDP);
  211.  
  212. g_pDP->Close(0);
  213.  
  214. g_pDP->Release();
  215. g_pDP = NULL;
  216.  
  217. // Cleanup COM
  218. CoUninitialize();
  219.  
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. void CDirectPlay::SendPosition(Position localPlayerPosition)
  227. {
  228.  
  229.  
  230.  
  231. // *****************************************************
  232. // *** Send Local Player Position to all Players ***
  233. // *****************************************************
  234. // becomes their remote player position
  235. DPN_BUFFER_DESC dpnBuffer;
  236. WCHAR wszData[256];
  237. TCHAR strBuffer[256] = "testing";
  238.  
  239. // convert string to WCHAR
  240. // DXUtil_ConvertGenericStringToWideCch( wszData, strBuffer, 256 );
  241.  
  242. // dpnBuffer.pBufferData = (BYTE*) wszData;
  243. // dpnBuffer.dwBufferSize = 2 * (wcslen(wszData) + 1);
  244.  
  245. dpnBuffer.pBufferData = (BYTE*) &localPlayerPosition;
  246. dpnBuffer.dwBufferSize = sizeof(localPlayerPosition);
  247.  
  248. HRESULT hr;
  249.  
  250. hr = g_pDP->SendTo( DPNID_ALL_PLAYERS_GROUP, // dpnid
  251. &dpnBuffer, // pBufferDesc
  252. 1, // cBufferDesc
  253. 0, // dwTimeOut
  254. NULL, // pvAsyncContext
  255. NULL, // pvAsyncHandle
  256. DPNSEND_SYNC |
  257. DPNSEND_NOLOOPBACK );
  258.  
  259. }
  260.  
  261.  
  262.  
  263. //-----------------------------------------------------------------------------
  264. // Name: HostSession()
  265. // Desc: Host a DirectPlay session
  266. //-----------------------------------------------------------------------------
  267. HRESULT CDirectPlay::HostSession()
  268. {
  269. HRESULT hr = S_OK;
  270. DPN_APPLICATION_DESC dpAppDesc;
  271.  
  272. // Now set up the Application Description
  273. ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
  274. dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  275. dpAppDesc.guidApplication = guidApp;
  276. dpAppDesc.dwFlags = DPNSESSION_NODPNSVR | DPNSESSION_MIGRATE_HOST;
  277.  
  278. // We are now ready to host the app
  279. if( FAILED( hr = g_pDP->Host( &dpAppDesc, // AppDesc
  280. &g_pDeviceAddress, 1, // Device Address
  281. NULL, NULL, // Reserved
  282. NULL, // Player Context
  283. 0 ) ) ) // dwFlags
  284. {
  285. OutputError( TEXT("Failed to host a new session"), hr );
  286. return hr;
  287. }
  288. else
  289. {
  290. // SendMessage( GetDlgItem( g_hDlg, IDC_STATUS ), WM_SETTEXT, 0,
  291. // (LPARAM) TEXT("Hosting a session.") );
  292. // SendMessage( GetDlgItem( g_hDlg, IDC_HOST ), WM_SETTEXT, 0,
  293. // (LPARAM) TEXT("St&op Hosting") );
  294. g_bHosting = true;
  295. }
  296.  
  297. return S_OK;
  298. }
  299.  
  300.  
  301.  
  302.  
  303. HRESULT CDirectPlay::Host()
  304. {
  305.  
  306. HRESULT hr = S_OK;
  307.  
  308. // Init the DirectPlay system
  309. if( FAILED( hr = InitDirectPlay() ) )
  310. {
  311. // OutputError( TEXT("Failed to initialize DirectPlay.")
  312. // TEXT("\n\nThe sample will now exit.") );
  313. // goto LCleanup;
  314.  
  315. // Cleanup DirectPlay
  316. // CleanupDirectPlay();
  317.  
  318. return 0;
  319. }
  320.  
  321. // *****************************************
  322. // *** Create Device Address (local) ***
  323. // *****************************************
  324.  
  325. CreateDeviceAddress(true); // pass true if hosting
  326.  
  327.  
  328. // Everything OK so far, go ahead and start hosting
  329.  
  330. hr = HostSession();
  331.  
  332. g_bHosting = true;
  333.  
  334. return hr;
  335.  
  336.  
  337. }
  338.  
  339.  
  340.  
  341. HRESULT CDirectPlay::ConnectToHost( char* szHostAddress )
  342. {
  343. HRESULT hr = S_OK;
  344.  
  345. // Init the DirectPlay system
  346. if( FAILED( hr = InitDirectPlay() ) )
  347. {
  348. // OutputError( TEXT("Failed to initialize DirectPlay.")
  349. // TEXT("\n\nThe sample will now exit.") );
  350. // goto LCleanup;
  351.  
  352. // Cleanup DirectPlay
  353. // CleanupDirectPlay();
  354.  
  355. return 0;
  356. }
  357.  
  358. // *****************************************
  359. // *** Create Device Address (local) ***
  360. // *****************************************
  361.  
  362. CreateDeviceAddress(false);
  363.  
  364.  
  365. // ****************************************
  366. // *** Create Host Address (remote) ***
  367. // ****************************************
  368.  
  369.  
  370. // TCHAR strHost[128] = "127.0.0.1";
  371. WCHAR strBuffer[128] = {0};
  372.  
  373. hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,
  374. CLSCTX_INPROC_SERVER,
  375. IID_IDirectPlay8Address,
  376. (LPVOID*) &g_pHostAddress );
  377.  
  378. hr = g_pHostAddress->SetSP( &CLSID_DP8SP_TCPIP );
  379.  
  380. // Convert the generic host string to UNICODE.
  381. DXUtil_ConvertGenericStringToWideCch( strBuffer, szHostAddress, 20 );
  382.  
  383.  
  384. // g_pHostIPAddress->BuildAddress( (const WCHAR* const)"127.0.0.1", 4200);
  385.  
  386. hr = g_pHostAddress->AddComponent( DPNA_KEY_HOSTNAME, strBuffer,
  387. 2*((UINT)wcslen(strBuffer) + 1), /*bytes*/
  388. DPNA_DATATYPE_STRING );
  389.  
  390. // hr = g_pHostAddress->AddComponent( DPNA_KEY_HOSTNAME, strBuffer,
  391. // sizeof(strBuffer), /*bytes*/
  392. // DPNA_DATATYPE_STRING );
  393.  
  394.  
  395.  
  396. hr = g_pHostAddress->AddComponent( DPNA_KEY_PORT,
  397. &g_dwRemotePort,
  398. sizeof(DWORD),
  399. DPNA_DATATYPE_DWORD );
  400.  
  401.  
  402. // *************************************
  403. // *** Connect to remote session ***
  404. // *************************************
  405.  
  406. DPN_APPLICATION_DESC dpAppDesc;
  407.  
  408.  
  409.  
  410. // Now set up the Application Description
  411. ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
  412. dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  413. dpAppDesc.guidApplication = guidApp;
  414.  
  415.  
  416.  
  417. hr = g_pDP->Connect(&dpAppDesc, // pdnAppDesc
  418. g_pHostAddress, // pHostAddr
  419. g_pDeviceAddress, // pDeviceInfo
  420. NULL, // pdnSecurity
  421. NULL, // pdnCredentials
  422. NULL, 0, // pvUserConnectData/Size
  423. NULL, // pvPlayerContext
  424. NULL, // pvAsyncContext
  425. NULL, // pvAsyncHandle
  426. DPNCONNECT_SYNC); // dwFlags
  427.  
  428. if( SUCCEEDED(hr) )
  429. {
  430. g_bConnected = true;
  431.  
  432. OutputError( TEXT("Connected.")
  433. TEXT("\n\nWoohoo.") );
  434. }
  435.  
  436.  
  437.  
  438.  
  439.  
  440. }
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447. //-----------------------------------------------------------------------------
  448. // Name: OutputError
  449. // Desc: Output the error and error code using a message box
  450. //-----------------------------------------------------------------------------
  451. void CDirectPlay::OutputError( TCHAR* str, HRESULT hr )
  452. {
  453. TCHAR strBuf[256] = {0};
  454.  
  455. if( hr )
  456. _stprintf( strBuf, TEXT("%s: 0x%X"), str, hr );
  457. else
  458. _stprintf( strBuf, TEXT("%s"), str );
  459.  
  460. MessageBox( NULL, strBuf, TEXT("DirectPlay Sample"), MB_OK );
  461. }
  462.  
  463.  
  464.  
  465.  
  466. bool CDirectPlay::isHosting()
  467. {
  468. return g_bHosting;
  469. }
  470.  
  471.  
  472. bool CDirectPlay::isConnected()
  473. {
  474. return g_bConnected;
  475. }
  476.  
  477.  
  478.  
  479. void CDirectPlay::SetRemotePlayerPosition( Position rPosition )
  480. {
  481. *m_pRemotePlayerPosition = rPosition;
  482. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty