//-----------------------------------------------------------------------------
// Function:    ProcessUpdateCallBack
//
// Purpose:     Called when PEER_EVENT_PEOPLE_NEAR_ME_CHANGED event is received
//              Reads in event data and displays the type of change
//
// Returns:     VOID
//
VOID CALLBACK ProcessUpdateCallBack(LPVOID lpContext, BOOLEAN fTimer)
{
    PEER_COLLAB_EVENT_DATA * pEventData = NULL;
    PEER_PEOPLE_NEAR_ME * pPersonNearMe = NULL;

    //Unreferenced parameters
    lpContext;
    fTimer;

    while (SUCCEEDED(PeerCollabGetEventData(g_hModelPeerEvent, &pEventData)) && pEventData)
    {   
        // NULL for the PPEER_PEOPLE_NEAR_ME means this event is for the local person
        //
        if (pEventData->peopleNearMeChangedData.pPeopleNearMe == NULL)
        {
            // PEER_CHANGED_ADDED on the local person means they have signed into people near me
            //
            if (pEventData->peopleNearMeChangedData.changeType == PEER_CHANGE_ADDED)
            {
                InitPeoplePickerModel(g_hModelDlgOwner);
            }

            // PEER_CHANGED_ADDED on the local person means they have signed out of people near me
            //
            if (pEventData->peopleNearMeChangedData.changeType == PEER_CHANGE_DELETED)
            {
                PostMessage(g_hModelDlgOwner, WM_CLEARPEOPLE, 0, 0);
            }
        }
        else
        {
            // Copy the person to give to dialog
            //
            if (SUCCEEDED(DuplicatePeerPeopleNearMe(&pPersonNearMe, pEventData->peopleNearMeChangedData.pPeopleNearMe)))
            {
                // The person was added (signed-in), so tell the dialog to add the person
                //
                if (pEventData->peopleNearMeChangedData.changeType == PEER_CHANGE_ADDED)
                {
                    PostMessage(g_hModelDlgOwner, WM_ADDPERSON, 0, (LPARAM) pPersonNearMe);
                }

                // The person was deleted (signed-out), so tell dialog to remove the person
                //
                if (pEventData->peopleNearMeChangedData.changeType == PEER_CHANGE_DELETED)
                {
                    PostMessage(g_hModelDlgOwner, WM_REMOVEPERSON, 0, (LPARAM) pPersonNearMe);
                }
            }
        }
        // Free the used PEER resources
        //
        PeerFreeData(pEventData);
    }
}
//-----------------------------------------------------------------------------
// Function:    EnumPeopleNearMe
//
// Purpose:     Simple wrapper function that calls PeerCollabEnumPeopleNearMe
//              and displays the results.  Setting fPrompt flag prompts user
//              to select an endpoint for further information.
//
// Returns:     HRESULT
//
HRESULT EnumPeopleNearMe()
{
    PEER_PEOPLE_NEAR_ME **   ppPeopleNearMe = NULL;
    PEER_PEOPLE_NEAR_ME *    pPersonNearMe = NULL;
    HRESULT                  hr = S_OK;
    HPEERENUM                hEnum = NULL;
    ULONG                    count = 0;
    ULONG                     index = 0;
 
    // Enumerate the people near me and setup the enumeration
    //
    hr = PeerCollabEnumPeopleNearMe(&hEnum);

    if (SUCCEEDED(hr))
    {
        hr = PeerGetItemCount(hEnum, &count);

        if (SUCCEEDED(hr))
        {
            if (count == 0)
            {
                PeerEndEnumeration(hEnum);
                return hr;
            }
            hr = PeerGetNextItem(hEnum, &count, (PVOID **) &ppPeopleNearMe);
        }

        if (SUCCEEDED(hr))
        {    
            // Add people to dialog
            //
            while (index < count)
            {
                if (SUCCEEDED(DuplicatePeerPeopleNearMe(&pPersonNearMe, ppPeopleNearMe[index])))
                {
                    //Add the person to the Listview
                    //
                    PostMessage(g_hModelDlgOwner, WM_ADDPERSON, 0, (LPARAM) pPersonNearMe);
                }
                index++;
            }
            // Free the used PEER resources
            //
            PeerFreeData(ppPeopleNearMe);
        }

        // End the PEER enumeration
        //
        PeerEndEnumeration(hEnum);     
    }
    return hr;
}
예제 #3
0
//-----------------------------------------------------------------------------
// Function:    SyncPeerNameResolve
// Purpose:     Wrapper function for PeerPnrpResolve that perform the
//              resolution synchronously
// Parameters:
//   pwzPeerName [in]    : Peer Name to resolve
//   pwzCloudName [in]   : Cloud name in which to resolve
//
HRESULT SyncPeerNameResolve(PCWSTR pwzPeerName, PCWSTR pwzCloudName)
{
    ULONG                    i = 0;
    HRESULT                  hr = S_OK;
    PPEER_PNRP_ENDPOINT_INFO pEndpointInfo = NULL;
    ULONG                    cEndpoints = MAX_ENDPOINTS_TO_RESOLVE;

    wprintf(L"Resolving (this may take a few seconds)...\n");
    if (wcslen(pwzCloudName) != 0)
    {
        // Perform a synchronous resolve
        hr = PeerPnrpResolve(pwzPeerName, pwzCloudName, &cEndpoints,
                              &pEndpointInfo);
    }
    else
    {
        // If no cloud name is given, search in all clouds (NULL argument)
        hr = PeerPnrpResolve(pwzPeerName, NULL, &cEndpoints, &pEndpointInfo);
    }

    if (SUCCEEDED(hr))
    {
        // Display results
        if (cEndpoints == 0)
        {
            wprintf(L"No endpoints found for peer name %s\n", pwzPeerName);
        }
        else
        {
            for (i = 0; i < cEndpoints; i++)
            {
                DisplayPNRPEndpoint(&pEndpointInfo[i]);
            }
        }
    }

    if (pEndpointInfo != NULL)
    {
        PeerFreeData(pEndpointInfo);
    }
    wprintf(L"Synchronous resolve complete.\n");
    return hr;
}
예제 #4
0
//-----------------------------------------------------------------------------
// Function:    ShowDNSEncodedNameCommand
// Purpose:     Given a PNRP encoded name show the equivalant DNS encoded name to
//              pass to GetAddrInfo to resolve this name.  The DNS encoded name
//              can be used to lookup a PNRP name from ANY application that 
//              calls GetAddrInfo (which means almost every application that
//              converts a DNS name to an IP address).  Try it from ping.exe or
//              Internet Explorer.
// Parameters:  None
//
// NOTE: The signature of this function must adhere to the MENU_COMMAND typedef
HRESULT ShowDNSEncodedNameCommand()
{
    HRESULT                 hr = S_OK;
    WCHAR                   wzInputBuffer[MAX_PEERNAME_LENGTH];
    PWSTR                   pwzHostName = NULL;

    do
    {
        wprintf(L"Peer Name (e.g. 0.myclassifier): ");
        FLUSH_AND_GET_RESPONSE(hr, wzInputBuffer);

        // Peer Name must be non-null.
        if (SUCCEEDED(hr) && wzInputBuffer[0] == L'\0')
        {
            wprintf(L"Peer Name must be non-empty.\n");
        }
    } while (SUCCEEDED(hr) && wzInputBuffer[0] == L'\0');

    hr = PeerNameToPeerHostName(wzInputBuffer, &pwzHostName);

    if(FAILED(hr))
    {
        goto exit;
    }

    wprintf(L"\n");
    wprintf(L"DNS encoded name is: %s\n", pwzHostName);
    wprintf(L"\n");
    wprintf(L"The DNS encoded name allows PNRP names to be used in ANY application\n");
    wprintf(L"that calls GetAddrInfo to perform name resolution (and that is just about\n");
    wprintf(L"every application).\n");
    wprintf(L"\n");
    wprintf(L"If this name is registered try using the DNS encoded form of the name in\n");
    wprintf(L"ping or Internet Explorer.");
    wprintf(L"\n\n");

exit:
    PeerFreeData(pwzHostName);
    pwzHostName = NULL;
    return hr;
}
예제 #5
0
//-----------------------------------------------------------------------------
// Function:    AsyncPeerNameResolve
// Purpose:     Wrapper function for PeerPnrpResolve that perform the
//              resolution asynchronously
// Parameters:
//   pwzPeerName [in]    : Peer Name to resolve
//   pwzCloudName [in]   : Cloud name in which to resolve
//
HRESULT AsyncPeerNameResolve(PCWSTR pwzPeerName, PCWSTR pwzCloudName)
{
    HRESULT                  hr = S_OK;
    PPEER_PNRP_ENDPOINT_INFO pEndpointInfo = NULL;
    ULONG                    cEndpoints = MAX_ENDPOINTS_TO_RESOLVE;
    HANDLE                   hEvent;
    HANDLE                   hResolve;

    wprintf(L"Resolving (this may take a few seconds)...\n");

    hEvent = CreateEvent(0, FALSE, FALSE, 0);
    if (hEvent == NULL)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
        return hr;
    }
    if (wcslen(pwzCloudName) != 0)
    {
        // Start an asynchronous resolve
        hr = PeerPnrpStartResolve(pwzPeerName, pwzCloudName, cEndpoints,
                                            hEvent, &hResolve);
    }
    else
    {
        // If no cloud name is given, search in all clouds (NULL argument)
        hr = PeerPnrpStartResolve(pwzPeerName, NULL, cEndpoints,
                                            hEvent, &hResolve);
    }

    cEndpoints = 0;
    if (SUCCEEDED(hr))
    {
        // Waits for up to 10 seconds for an endpoint to return
        while (WaitForSingleObject(hEvent, INFINITE) == WAIT_OBJECT_0)
        {
            hr = PeerPnrpGetEndpoint(hResolve, &pEndpointInfo);
            if (hr == PEER_E_NO_MORE)
            {
                // Exit if all endpoints have been retrieved
                break;
            }
            else if (SUCCEEDED(hr))
            {
                // Display results as they are received
                hr = DisplayPNRPEndpoint(pEndpointInfo);
                if (FAILED(hr))
                {
                    wprintf(L"Error in DisplayEndpointInfo\n");
                }
                cEndpoints++;

                if (pEndpointInfo != NULL)
                {
                    PeerFreeData(pEndpointInfo);
                }
            }    
            else
            {
                break;
            }
        }
        if (cEndpoints == 0)
        {
            wprintf(L"No endpoints found for peer name %s\n", pwzPeerName);
        }

        // End Asynchronous Resolve
        hr = PeerPnrpEndResolve(hResolve);
        wprintf(L"Asynchronous resolve complete.\n");
    }

    CloseHandle(hEvent);
    return hr;
}
예제 #6
0
//-----------------------------------------------------------------------------
// Function:    RegisterPeerNameCommand
// Purpose:     Prompts user for registration information, fills out a
//              PEER_PNRP_REGISTRATION_INFO struct, creates a peer name,
//              and calls PeerPnrpRegister to register the peer name
// Parameters:  None
//
// NOTE: The signature of this function must adhere to the MENU_COMMAND typedef
HRESULT RegisterPeerNameCommand()
{
    WCHAR                       wzInputBuffer[256] = {0};
    WCHAR                       wzIdentity[MAX_PEERNAME_LENGTH] = {0};
    WCHAR                       wzClassifier[MAX_CLASSIFIER_LENGTH] = {0};
    HRESULT                     hr = S_OK;
    PWSTR                       pwzPeerName = NULL;
    ULONG                       cAddresses = 0;
    //ppRegAddrs is a pointer to an array of pointers to SOCKADDR structures
    SOCKADDR**                  ppRegAddrs = NULL;

    ZeroMemory(&g_RegInfo, sizeof(PEER_PNRP_REGISTRATION_INFO));

    //Collect Registration Information
    //---------------------------------

    // Addresses
    hr = GetAddress(TRUE, &cAddresses, &ppRegAddrs);
    if (SUCCEEDED(hr))
    {
        // Cloud name
        hr = GetCloudName(cAddresses != 1, celems(g_wzCloudName), g_wzCloudName);
    }

    if (SUCCEEDED(hr))
    {
        // Comment
        hr = GetComment(celems(g_wzComment), g_wzComment);
    }

    if (SUCCEEDED(hr))
    {
        // Payload
        hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData);

        if (cAddresses == 0)
        {
            // If no addresses were specified, the payload must be non-null.
            while (PayloadSize(g_PayloadData) == 0 && SUCCEEDED(hr))
            {
                wprintf(L"Payload must be non-empty if no addresses are specified.\n");
                hr = GetPayload(sizeof(g_PayloadData), (PBYTE)g_PayloadData);
            }
        }
    }

    // Create Peer Name
    //------------------

    // If secure, get identity
    if (SUCCEEDED(hr))
    {
        wprintf(L"Secured peer name [no]: ");
        FLUSH_AND_GET_RESPONSE(hr, wzInputBuffer);

        if (SUCCEEDED(hr) && _wcsicmp(wzInputBuffer, L"yes") == 0)
        {
            hr = GetIdentity(celems(wzIdentity), wzIdentity);
        }
    }
    // Classifier
    if (SUCCEEDED(hr))
    {
        do
        {
            wprintf(L"Classifier: ");
            FLUSH_AND_GET_RESPONSE(hr, wzClassifier);

            // Classifier must be non-null.
            if (SUCCEEDED(hr) && wzClassifier[0] == L'\0')
            {
                wprintf(L"Classifier must be non-empty.\n");
            }
        } while (SUCCEEDED(hr) && wzClassifier[0] == L'\0');
    }

    // Create peer name
    if (SUCCEEDED(hr))
    {
        if (wcslen(wzIdentity) != 0)
        {
            // Create secured peer name
            hr = PeerCreatePeerName(wzIdentity, wzClassifier, &pwzPeerName);
        }
        else
        {
            // Create unsecured peer name
            hr = PeerCreatePeerName(NULL, wzClassifier, &pwzPeerName);
        }
    }

    // Fill out g_RegInfo struct
    // -------------------------
    if (wcslen(g_wzCloudName) == 0)
    {
        g_RegInfo.pwzCloudName = NULL;
    }
    else
    {
        g_RegInfo.pwzCloudName = g_wzCloudName;
    }

    //Copy in the address count and pointer to the array of pointers
    g_RegInfo.cAddresses = cAddresses;
    g_RegInfo.ppAddresses = ppRegAddrs;

    g_RegInfo.pwzComment = g_wzComment;

    if (PayloadSize(g_PayloadData) == 0)
    {
        g_RegInfo.payload.cbData = 0;
        g_RegInfo.payload.pbData = NULL;
    }
    else
    {
        g_RegInfo.payload.cbData = PayloadSize(g_PayloadData);
        g_RegInfo.payload.pbData = (PBYTE) g_PayloadData;
    }

    // Perform registration
    if (SUCCEEDED(hr))
    {
        hr = PeerPnrpRegister(pwzPeerName, &g_RegInfo, &g_hRegistration);
    }

    // Display result
    if (SUCCEEDED(hr))
    {
        wprintf(L"\nSuccessfully registered name: %s\n", pwzPeerName);
    }
    else
    {
        wprintf(L"\nError while registering name.  HRESULT=0x%x\n", hr);
        PrintError(hr);
    }

    if (pwzPeerName != NULL)
    {
        PeerFreeData(pwzPeerName);
    }

    if (ppRegAddrs != NULL)
    {
        free(ppRegAddrs);
    }
    return hr;
}
예제 #7
0
	CFP2PUtil::FreeData(LPCVOID pvData)
	{
		PeerFreeData(pvData);
	}