示例#1
0
//-----------------------------------------------------------------------------
// Name: 
// Desc: 
//-----------------------------------------------------------------------------
static BOOL nm_InitConnection(const char* pcszServerIP)
{
	HRESULT					hRet;
	LPDIRECTPLAYLOBBY3A		pDPLobby = NULL;


	// Create a ANSI DirectPlay interface
	hRet = CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
							IID_IDirectPlay4A, (VOID**)&g_pDPlay);
	if (FAILED(hRet))
		return FALSE;

	// Get ANSI DirectPlayLobby interface
	hRet = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER,
							IID_IDirectPlayLobby3A, (VOID**)&pDPLobby);
	if (FAILED(hRet))
		return FALSE;

	DPCOMPOUNDADDRESSELEMENT addressElements[3];
	CHAR strIPAddressString[MAX_BUFFER];
	VOID* pAddress = NULL;
	DWORD dwAddressSize = 0;
	DWORD dwElementCount = 0;

	// TCP/IP needs a service provider, an IP address

	// Service provider
	addressElements[dwElementCount].guidDataType = DPAID_ServiceProvider;
	addressElements[dwElementCount].dwDataSize = sizeof(GUID);
	addressElements[dwElementCount].lpData = (VOID*)&DPSPGUID_TCPIP;
	dwElementCount++;

	// IP address string
	if (pcszServerIP != NULL) {
		lstrcpy(strIPAddressString, pcszServerIP);
	} else {
		lstrcpy(strIPAddressString, "");
	}
	addressElements[dwElementCount].guidDataType = DPAID_INet;
	addressElements[dwElementCount].dwDataSize = lstrlen(strIPAddressString) + 1;
	addressElements[dwElementCount].lpData = strIPAddressString;
	dwElementCount++;

	// See how much room is needed to store this address
	hRet = pDPLobby->CreateCompoundAddress(addressElements, dwElementCount, NULL,
												&dwAddressSize);
	if (hRet != DPERR_BUFFERTOOSMALL) {
		SAFE_RELEASE(pDPLobby);
		return FALSE;
	}

	// Allocate space
	pAddress = GlobalAllocPtr(GHND, dwAddressSize);
	if (pAddress == NULL) {
		SAFE_RELEASE(pDPLobby);
		return FALSE;
	}

	// Create the address
	hRet = pDPLobby->CreateCompoundAddress(addressElements, dwElementCount, pAddress,
												&dwAddressSize);
	if (FAILED(hRet)) {
		SAFE_RELEASE(pDPLobby);
		GlobalFreePtr(pAddress);
		return FALSE;
	}

	// initialize connection
	hRet = g_pDPlay->InitializeConnection(pAddress, 0);
	if (FAILED(hRet)) {
		SAFE_RELEASE(pDPLobby);
		GlobalFreePtr(pAddress);
		return FALSE;
	}
	return TRUE;
}
示例#2
0
bool bld_browse_sessions(const char *ip_address)
{
        DWORD lpConnectionSize;
        char lpConnection[128];
        HRESULT hr;
        LPDIRECTPLAYLOBBYA lpdplobby;
        GUID dp_provider;

        lpConnectionSize = sizeof(lpConnection);

        hr = bld_create_thread();
        if (FAILED(hr))
                goto cleanup;

        if (gbl_dp_interface == NULL) {
                if (ip_address)
                        dp_provider = GUID_NULL;
                else
                        dp_provider = DPSPGUID_IPX;

                hr = bld_create_dp_interface(&dp_provider, &gbl_dp_interface);
                bld_assert_result(hr);
                if (FAILED(hr))
                        goto cleanup;
        }

        if (ip_address)
        {
                if (gbl_player_info.dp_lobby == NULL)
                {
                        // creating lobby object
                        hr = DirectPlayLobbyCreateA(
                                NULL, &lpdplobby, NULL, NULL, 0
                        );
                        if (FAILED(hr))
                                goto cleanup;

                        // get new interface of lobby
	                    hr = lpdplobby->QueryInterface(
                                IID_IDirectPlayLobby3A,
                                (LPVOID *)&gbl_player_info.dp_lobby
                        );

                        // release old interface since we have new one
                        lpdplobby->Release();
                        if (FAILED(hr))
                                goto cleanup;
                }

                hr = gbl_player_info.dp_lobby->CreateAddress(
                        DPSPGUID_TCPIP, DPAID_INet, ip_address,
                        strlen(ip_address), &lpConnection, &lpConnectionSize
                );
                if (FAILED(hr))
                        goto cleanup;

                hr = gbl_dp_interface->InitializeConnection(&lpConnection, 0);
                bld_assert_result(hr);
                if (FAILED(hr))
                        goto cleanup;
        }

        hr = bld_enum_sessions(gbl_dp_interface);
        if (FAILED(hr))
                goto cleanup;

        return true;

cleanup:
        if (gbl_player_info.dp_lobby)
                gbl_player_info.dp_lobby->Release();

        gbl_player_info.dp_lobby = NULL;

        bld_destroy_dp_interface(gbl_dp_interface);

        gbl_dp_interface = NULL;

        return false;
}