Пример #1
0
        //-----------------------------------------------------------------------------
        // Function: JoinGroup
        //
        // Purpose:  Uses the invitation to join a group with a specific identity.
        //           Displays a message if there was an error.
        //
        // Parameters:
        //  pwzIdentity [in] : Path to identity file (not used w/password based groups)
        //  pwzFileName [in] : Path to the group invitation
        //  pwzPassword [in] : Password of group (if applicable)
        //
        // Returns:  HRESULT
        //
        internal HRESULT JoinGroup(string pwzIdentity, string pwzFileName, string pwzPassword, bool pwdAuth)
        {
            HRESULT hr;
            string  wzInvitation = null;

            g_dwAuth = pwdAuth ? PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_PASSWORD_AUTHENTICATION : PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_GMC_AUTHENTICATION;
            try
            {
                using var fs = File.OpenRead(pwzFileName);
                var bytes = new byte[MAX_INVITATION * 2];
                fs.Read(bytes, 0, bytes.Length);
                wzInvitation = Encoding.Unicode.GetString(bytes);
            }
            catch
            {
                hr = HRESULT.E_FAIL;
                DisplayHrError("Error opening group invitation file", hr);
                return(hr);
            }

            if (g_dwAuth == PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_GMC_AUTHENTICATION)
            {
                // NULL parameter indicates that the cloud name will be selected automatically
                hr = PeerGroupJoin(pwzIdentity, wzInvitation, default, out g_hGroup);
Пример #2
0
        //-----------------------------------------------------------------------------
        // Function: CreateGroup
        //
        // Purpose:  Creates a new group with the friendly name.
        //
        // Parameters:
        //      pwzName     [in] : Friendly name of group
        //      pwzIdentity [in] : Path to identity file of group creator
        //      pwzPassword [in] : Password of group (if applicable)
        //
        // Returns:  HRESULT
        //
        internal HRESULT CreateGroup(string pwzName, string pwzIdentity, string pwzPassword, bool pwdAuth, bool globalScope)
        {
            HRESULT hr = HRESULT.S_OK;

            // Retrieve user's selection on authentication method
            if (pwdAuth)
            {
                g_dwAuth = PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_PASSWORD_AUTHENTICATION;
            }
            else
            {
                g_dwAuth    = PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_GMC_AUTHENTICATION;
                pwzPassword = null;
            }

            g_fGlobalScope = globalScope;

            if (hr.Succeeded)
            {
                if (string.IsNullOrEmpty(pwzName))
                {
                    hr = HRESULT.E_INVALIDARG;
                    DisplayHrError("Please enter a group name.", hr);
                }
            }

            if (hr.Succeeded)
            {
                CleanupGroup();

                PEER_GROUP_PROPERTIES props = new PEER_GROUP_PROPERTIES
                {
                    dwSize                  = (uint)Marshal.SizeOf <PEER_GROUP_PROPERTIES>(),
                    pwzClassifier           = "SampleChatGroup",
                    pwzFriendlyName         = pwzName,
                    pwzCreatorPeerName      = pwzIdentity,
                    dwAuthenticationSchemes = g_dwAuth
                };
                if (g_dwAuth == PEER_GROUP_AUTHENTICATION_SCHEME.PEER_GROUP_PASSWORD_AUTHENTICATION)
                {
                    props.groupPasswordRole = PEER_GROUP_ROLE_ADMIN;
                    props.pwzGroupPassword  = pwzPassword;
                }

                if (g_fGlobalScope)
                {
                    props.pwzCloud = null;
                    hr             = PeerGroupCreate(props, out g_hGroup);
                }
                else
                {
                    hr = GetLocalCloudName(out var pwzCloudName);
                    if (hr.Succeeded)
                    {
                        props.pwzCloud = pwzCloudName;
                        hr             = PeerGroupCreate(props, out g_hGroup);
                    }
                }

                if (hr.Failed)
                {
                    if (hr == HRESULT.PEER_E_PASSWORD_DOES_NOT_MEET_POLICY)
                    {
                        DisplayHrError("Password does not meet local policy.", hr);
                    }
                    else
                    {
                        DisplayHrError("Failed to create a new group.", hr);
                    }
                }
            }

            if (hr.Succeeded)
            {
                hr = PrepareToChat();
            }

            return(hr);
        }