Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lobby Client launch problem (C++)

Started by m_shuhrat Feb 26, 2007 at 2:01 PM 7 replies 850+ views
Original Post
m_shuhrat
m_shuhrat
first I would like to appologise for listing a lot of code here, but I need to figure out what is not working in these 3 functions. I'm modifyin Directplay sample from DX7 (dont ask why DX7, I need this version). but I'm able to launch lobby application, but it is not connecting to host or even hosting a session. How can I debug DX code? please review the code and help me. First enumerate lobbyable applications on system, then try to create address object (I think that is a place of my problems, but cannot see where). then create connection infor and finally launch application with given connection info. application launches fine but is not hosting a session. game message saying unable to join a game.

BOOL FAR PASCAL EnumAddressTypes( REFGUID guidAddressType, VOID* pContext,
								  DWORD dwFlags )
{
	ADDRESSTYPELIST* pAddressTypes = (ADDRESSTYPELIST*)pContext;
	
	if( pAddressTypes->dwCount < 10 )
	{		
		// Save the address type guid in the list
		pAddressTypes->guidAddressTypes[pAddressTypes->dwCount] = guidAddressType;
		pAddressTypes->dwCount++;
	}

	return TRUE;
}



HRESULT CreateAddress( HWND hWnd, GUID* pServiceProviderGUID, VOID** ppAddress, 
					   DWORD* pdwAddressSize )
{
	ADDRESSTYPELIST 		 addressTypeList;
	DPCOMPOUNDADDRESSELEMENT addressElements[11];
	GUID    guidAddressType;	
	VOID*   pAddress = NULL;
	DWORD   dwAddressSize = 0;
	DWORD   i;
	DWORD   dwElementCount;	
	HRESULT hr;	

  char *ipAddress="192.168.1.2";

  
	// Get the list of address types for this service provider
	ZeroMemory( &addressTypeList, sizeof(ADDRESSTYPELIST) );
	hr = pDPLClient->EnumAddressTypes( EnumAddressTypes, *pServiceProviderGUID,
		                             &addressTypeList, 0L );
	if( FAILED(hr) )
		return hr;

	dwElementCount = 0;

	// All DPADDRESS's must have a service provider chunk
	addressElements[dwElementCount].guidDataType = DPAID_ServiceProvider;
	addressElements[dwElementCount].dwDataSize   = sizeof(GUID);
	addressElements[dwElementCount].lpData       = pServiceProviderGUID;
	dwElementCount++;

	_stprintf(str, _T("dwCount 2 is %d"), addressTypeList.dwCount);
	MessageBox(0, str, 0, MB_OK);
	// Loop over the address types
	for( i = 0; i < addressTypeList.dwCount; i++ )
	{		
		guidAddressType = addressTypeList.guidAddressTypes;

		if( IsEqualGUID( guidAddressType, DPAID_INet ) )
		{			
			// Add an IP address chunk			
			addressElements[dwElementCount].guidDataType = DPAID_INet;
			addressElements[dwElementCount].dwDataSize   = lstrlen(ipAddress) + 1;
			addressElements[dwElementCount].lpData       = ipAddress;
			dwElementCount++;			
		}		
	}

	// Bail if no address data is available
	if( dwElementCount == 1 )
		return DPERR_GENERIC;
	
	// See how much room is needed to store this address
	hr = pDPLClient->CreateCompoundAddress( addressElements, dwElementCount,
		                                  NULL, &dwAddressSize );
	if( hr != DPERR_BUFFERTOOSMALL )
		return hr;

	// Allocate space
	pAddress = GlobalAllocPtr( GHND, dwAddressSize );
	if( pAddress == NULL )
		return DPERR_NOMEMORY;

	// Create the address		
	hr = pDPLClient->CreateCompoundAddress( addressElements, dwElementCount,
						                  pAddress, &dwAddressSize );
	if( FAILED(hr) )
	{
		GlobalFreePtr( pAddress );
		return hr;
	}
	
	// Return the address info
	*ppAddress      = pAddress;
	*pdwAddressSize = dwAddressSize;

	return DP_OK;
}

HRESULT RunProgram(){

	HRESULT hr;
	TCHAR str[255];

	CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, IID_IDirectPlay4A, (VOID**)&pDPLobby);
	CoCreateInstance(CLSID_DirectPlayLobby, NULL,CLSCTX_INPROC_SERVER, IID_IDirectPlayLobby3A, (VOID**)&pDPLClient);	 	  
	pDPLClient->EnumLocalApplications(ApplicationEnumerationCallBack, NULL, 0);  

	//This is one way of creating address object
	GUID inet=DPSPGUID_TCPIP;
	CreateAddress( NULL, &inet,&pAddress,&addressSize );

	//This is other way of creating address object
	//DPCOMPOUNDADDRESSELEMENT addEls[2];
	//char *ipAddress="192.168.1.2";

	//addEls[0].guidDataType=DPAID_ServiceProvider;
	//addEls[0].dwDataSize=sizeof(GUID);
	//addEls[0].lpData=(LPVOID)&DPSPGUID_TCPIP;
	//addEls[1].guidDataType=DPAID_INet;
	//addEls[1].dwDataSize=strlen(ipAddress)+1;
	//addEls[1].lpData=ipAddress;

	//pDPLClient->CreateCompoundAddress(addEls, 2, NULL, &addressSize);
	//pDPLClient->CreateCompoundAddress(addEls, 2, &pAddress, &addressSize);
	

	DWORD		   appID;  
	DPLCONNECTION cInfo;
	DPSESSIONDESC2 sessionInfo;
	DPNAME playerName;

	// Fill out session description
	ZeroMemory( &sessionInfo, sizeof(DPSESSIONDESC2) );
	sessionInfo.dwSize  = sizeof(DPSESSIONDESC2);	
	sessionInfo.dwFlags = 0;						  // DPSESSION_xxx flags	
	sessionInfo.guidApplication  = gameGUID; // GUID of the DirectPlay application.
	sessionInfo.dwMaxPlayers     = 0;				  // Maximum # players allowed in session
	sessionInfo.dwCurrentPlayers = 0;				  // Current # players in session (read only)
	sessionInfo.lpszSessionNameA = "Sameple";    // ANSI name of the session
	sessionInfo.lpszPasswordA    = NULL;			  // ANSI password of the session (optional)
	sessionInfo.dwReserved1 = 0;					  // Reserved for future MS use.
	sessionInfo.dwReserved2 = 0;
	sessionInfo.dwUser1     = 0;					  // For use by the application
	sessionInfo.dwUser2     = 0;
	sessionInfo.dwUser3     = 0;
	sessionInfo.dwUser4     = 0;	
	

	// Fill out player name
	ZeroMemory( &playerName, sizeof(DPNAME) );
	playerName.dwSize  = sizeof(DPNAME);
	playerName.dwFlags = 0; 				   // Not used. Must be zero.
	playerName.lpszShortNameA = "Test"; // ANSI short or friendly name
	playerName.lpszLongNameA  = "Test"; // ANSI long or formal name

	
	// Fill out connection description
	ZeroMemory( &cInfo, sizeof(DPLCONNECTION) );
	cInfo.dwSize = sizeof(DPLCONNECTION);
	cInfo.lpSessionDesc = &sessionInfo		   // Pointer to session desc to use on connect
	cInfo.lpPlayerName  = &playerName 		   // Pointer to Player name structure
	cInfo.guidSP        = DPSPGUID_TCPIP; // GUID of the DPlay SP to use
	cInfo.lpAddress     = pAddress;			   // Address for service provider
	cInfo.dwAddressSize = addressSize;		   // Size of address data
	cInfo.dwFlags = DPLCONNECTION_CREATESESSION;
	
	hr=pDPLClient->RunApplication( 0, &appID, &cInfo, NULL );
		  
	if(hr==DPERR_CANTCREATEPROCESS )
	  MessageBoxA(NULL, "Game is not installed", "", MB_OK);
	else if(hr==DPERR_GENERIC )
		MessageBoxA(NULL, "Game is not installed", "", MB_OK);
	else if(hr==DPERR_INVALIDINTERFACE )
		MessageBoxA(NULL, "DPERR_INVALIDINTERFACE ", "", MB_OK);
	else if(hr==DPERR_INVALIDOBJECT )
		MessageBoxA(NULL, "DPERR_INVALIDOBJECT ", "", MB_OK);
	else if(hr==DPERR_INVALIDPARAMS )
		MessageBoxA(NULL, "DPERR_INVALIDPARAMS ", "", MB_OK);
	else if(hr==DPERR_OUTOFMEMORY )
		MessageBoxA(NULL, "DPERR_OUTOFMEMORY ", "", MB_OK);
	else if(hr==DPERR_UNKNOWNAPPLICATION  )
		MessageBoxA(NULL, "DPERR_UNKNOWNAPPLICATION ", "", MB_OK);

	if(pDPLobby)
	  SAFE_RELEASE(pDPLobby);
	if(pDPLClient)
	  SAFE_RELEASE(pDPLClient);	
	return DP_OK;
}


[Edited by - m_shuhrat on February 26, 2007 10:51:34 PM]
m_shuhrat
m_shuhrat
Quote:
Original post by Anonymous Poster
If you had read the FAQ you'd have seen using [ source] and [ /source] around your code would format it and keep the post a LOT shorter. You can click the edit button on your post and fix it, thanks!

P.s. Remove the space after the opening brackets to make it work.


Thanks, for a tip. it is much better. any ideas?
m_shuhrat
m_shuhrat
Please I know it is nearly working. But I need fresh look at it, so with that one can tell where the problem is.
Gagyi
Gagyi
I have used both DirectPlay and winsock and found winsock superior.

DP looks easier with all those lobbies and sessions and automatic player handling, but much less optimal, and surprisingly all those features just make it hard to code and debug.

So people, please use Winsock.

(P.S. I could only make LAN games with DP. Now I have a little online game working with Winsock.)
-----------------------------------"After you finish the first 90% of a project, you have to finish the other 90%." - Michael Abrashstickman.hu <=my game (please tell me your opinion about it)
m_shuhrat
m_shuhrat
Quote:
Original post by Gagyi
I have used both DirectPlay and winsock and found winsock superior.

DP looks easier with all those lobbies and sessions and automatic player handling, but much less optimal, and surprisingly all those features just make it hard to code and debug.

So people, please use Winsock.

(P.S. I could only make LAN games with DP. Now I have a little online game working with Winsock.)


I know, but I'm creating lobby client which launches DP lobbyable game, so not much choice here. It seems it is trying to join existing session when I launch a game with above code, however flag is set to CreateSession. that is what puzzling me.
stanlo
stanlo
Quote:
Original post by m_shuhrat
I know, but I'm creating lobby client which launches DP lobbyable game, so not much choice here. It seems it is trying to join existing session when I launch a game with above code, however flag is set to CreateSession. that is what puzzling me.


This isn't the answer you want to hear, but DP is not in DX9 for a reason and you should not be using it. Bite the bullet and use winsock or another game networking library.
m_shuhrat
m_shuhrat
Quote:
Original post by stanlo
Quote:
Original post by m_shuhrat
I know, but I'm creating lobby client which launches DP lobbyable game, so not much choice here. It seems it is trying to join existing session when I launch a game with above code, however flag is set to CreateSession. that is what puzzling me.


This isn't the answer you want to hear, but DP is not in DX9 for a reason and you should not be using it. Bite the bullet and use winsock or another game networking library.


I understand your anti MS mood, to be fair you are right, but as I said the game I'm trying to launch is lobby launchalbe, and was written to accept DX structures when being launched, so I cannot ask application to accept other network library structures can I?
stanlo
stanlo
I hope I didn't give the impression that I'm anti MS! I think they make a lot of cool things, DP just doesn't happen to be one of them.

I guess I'm not understanding your problem. You're writing a client, but not the actual game? So you don't have the source for it or anything?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.