예제 #1
0
        IPeerNeighbor GetNeighbor()
        {
            IPeerNeighbor neighbor = (IPeerNeighbor)getNeighborCallback(OperationContext.Current.GetCallbackChannel <IPeerProxy>());

            if (neighbor == null || neighbor.State == PeerNeighborState.Closed)
            {
                if (DiagnosticUtility.ShouldTraceWarning)
                {
                    TraceUtility.TraceEvent(TraceEventType.Warning, TraceCode.PeerNeighborNotFound,
                                            SR.GetString(SR.TraceCodePeerNeighborNotFound),
                                            new PeerNodeTraceRecord(config.NodeId),
                                            OperationContext.Current.IncomingMessage);
                }
                return(null);
            }

            if (DiagnosticUtility.ShouldTraceVerbose)
            {
                PeerNeighborState state = neighbor.State;

                PeerNodeAddress listenAddr    = null;
                IPAddress       connectIPAddr = null;

                if (state >= PeerNeighborState.Opened && state <= PeerNeighborState.Connected)
                {
                    listenAddr    = config.GetListenAddress(true);
                    connectIPAddr = config.ListenIPAddress;
                }

                PeerNeighborTraceRecord record = new PeerNeighborTraceRecord(neighbor.NodeId,
                                                                             this.config.NodeId, listenAddr, connectIPAddr, neighbor.GetHashCode(),
                                                                             neighbor.IsInitiator, state.ToString(), null, null,
                                                                             OperationContext.Current.IncomingMessage.Headers.Action);

                TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.PeerNeighborMessageReceived, SR.GetString(SR.TraceCodePeerNeighborMessageReceived), record, this, null);
            }

            return(neighbor);
        }
 private void OnOpen(TimeSpan timeout, bool waitForOnline)
 {
     bool aborted = false;
     EventHandler handler = (source, args) => this.connectCompletedEvent.Set();
     EventHandler handler2 = delegate (object source, EventArgs args) {
         aborted = true;
         this.connectCompletedEvent.Set();
     };
     this.openException = null;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     try
     {
         this.NeighborConnected += handler;
         this.Aborted += handler2;
         this.OpenCore(timeout);
         if (waitForOnline && !TimeoutHelper.WaitOne(this.connectCompletedEvent, helper.RemainingTime()))
         {
             throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
         }
         if (aborted)
         {
             throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationObjectAbortedException(System.ServiceModel.SR.GetString("PeerNodeAborted")));
         }
         if (this.isOpen)
         {
             if (this.openException != null)
             {
                 throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.openException);
             }
             string lclMeshId = null;
             PeerNodeConfig config = null;
             lock (this.ThisLock)
             {
                 lclMeshId = this.meshId;
                 config = this.config;
             }
             this.RegisterAddress(lclMeshId, config.GetListenAddress(false), helper.RemainingTime());
         }
     }
     catch (Exception exception)
     {
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         this.CloseCore(TimeSpan.FromTicks(0L), false);
         throw;
     }
     finally
     {
         this.NeighborConnected -= handler;
         this.Aborted -= handler2;
     }
 }
        // Get some addresses and make sure they are not in my neighborlist
        ReadOnlyCollection <PeerNodeAddress> ResolveNewAddresses(TimeSpan timeLeft, bool retryResolve)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeLeft);
            Dictionary <string, PeerNodeAddress> alreadySeen = new Dictionary <string, PeerNodeAddress>();
            List <PeerNodeAddress> reply = new List <PeerNodeAddress>();

            // Is this address me
            PeerNodeAddress lclNodeAddress = config.GetListenAddress(true);

            alreadySeen.Add(lclNodeAddress.ServicePath, lclNodeAddress);

            // Maximum of 2 resolves to get new addresses - if the resolver doesn't return us good addresses in 2 goes (8 randomly returned addresses)
            // it is probably messing with us
            int nresolves = (retryResolve) ? 2 : 1;

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord("Resolving");
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                        record, this, null);
            }

            for (int i = 0; i < nresolves && reply.Count < config.MaxResolveAddresses && isOpen && timeoutHelper.RemainingTime() > TimeSpan.Zero; i++)
            {
                ReadOnlyCollection <PeerNodeAddress> addresses;
                try
                {
                    addresses = config.Resolver.Resolve(config.MeshId, config.MaxResolveAddresses, timeoutHelper.RemainingTime());
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord("Resolve exception " + e.Message);
                        TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                record, this, null);
                    }

                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.ResolverException), e));
                }

                if (addresses != null)
                {
                    foreach (PeerNodeAddress address in addresses)
                    {
                        if (!alreadySeen.ContainsKey(address.ServicePath))
                        {
                            alreadySeen.Add(address.ServicePath, address);
                            if (((IPeerMaintainer)this).FindDuplicateNeighbor(address) == null)
                            {
                                reply.Add(address);
                            }
                        }
                    }
                }
            }
            return(new ReadOnlyCollection <PeerNodeAddress>(reply));
        }