// Maintainer is expected to validate and accept the contents of referrals
        // and to determine how many referrals it will accept from the array.
        // Neighbor reference is passed in case the Maintainer decided to reject a referral
        // based on invalid content and close the neighbor.
        public bool AddReferrals(IList <Referral> referrals, IPeerNeighbor neighbor)
        {
            Fx.Assert(null != config.Resolver, "");

            bool valid             = true;
            bool canShareReferrals = false;

            try
            {
                canShareReferrals = config.Resolver.CanShareReferrals;
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(SR.GetString(SR.ResolverException), e);
            }
            if (referrals != null && canShareReferrals)
            {
                foreach (Referral referral in referrals)
                {
                    // If any referral is invalid then the connection is bad so don't accept any referals from this neighbor.
                    if (referral == null ||
                        referral.NodeId == PeerTransportConstants.InvalidNodeId ||
                        !PeerValidateHelper.ValidNodeAddress(referral.Address) ||
                        !PeerValidateHelper.ValidReferralNodeAddress(referral.Address))
                    {
                        valid = false;
                        break;
                    }
                }
                if (valid)
                {
                    lock (ThisLock)
                    {
                        foreach (Referral referral in referrals)
                        {
                            EndpointAddress key = referral.Address.EndpointAddress;
                            if (referralCache.Count <= this.config.MaxReferralCacheSize && !referralCache.ContainsKey(key))
                            {
                                referralCache.Add(key, referral);
                            }
                        }
                    }

                    // Invoke any handler that is interested in Referrals being added.
                    ReferralsAddedHandler handler = ReferralsAdded;
                    if (handler != null)
                    {
                        ReferralsAdded(referrals, neighbor);
                    }
                }
            }
            return(valid);
        }
Ejemplo n.º 2
0
        public bool AddReferrals(IList <Referral> referrals, IPeerNeighbor neighbor)
        {
            bool flag = true;
            bool canShareReferrals = false;

            try
            {
                canShareReferrals = this.config.Resolver.CanShareReferrals;
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(System.ServiceModel.SR.GetString("ResolverException"), exception);
            }
            if ((referrals != null) && canShareReferrals)
            {
                foreach (Referral referral in referrals)
                {
                    if (((referral == null) || (referral.NodeId == 0L)) || (!PeerValidateHelper.ValidNodeAddress(referral.Address) || !PeerValidateHelper.ValidReferralNodeAddress(referral.Address)))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    lock (this.ThisLock)
                    {
                        foreach (Referral referral2 in referrals)
                        {
                            EndpointAddress endpointAddress = referral2.Address.EndpointAddress;
                            if ((this.referralCache.Count <= this.config.MaxReferralCacheSize) && !this.referralCache.ContainsKey(endpointAddress))
                            {
                                this.referralCache.Add(endpointAddress, referral2);
                            }
                        }
                    }
                    if (this.ReferralsAdded != null)
                    {
                        this.ReferralsAdded(referrals, neighbor);
                    }
                }
            }
            return(flag);
        }