public void RefreshConnection()
        {
            // Are we open and is any maintenance activity occuring
            if (isOpen)
            {
                bool continueMaintenance = false;
                if (!isRunningMaintenance)
                {
                    lock (ThisLock)
                    {
                        if (!isRunningMaintenance)
                        {
                            isRunningMaintenance = true;
                            continueMaintenance  = true;
                        }
                    }
                }
                if (continueMaintenance)
                {
                    try
                    {
                        TimeoutHelper timeoutHelper = new TimeoutHelper(config.MaintainerTimeout);
                        maintainerTimer.Cancel();                   // No maintainer until after connect has succeeded

                        using (IConnectAlgorithms connectAlgorithm = (IConnectAlgorithms) new TConnectAlgorithms())
                        {
                            // Always go to the resolver for RefreshConnection
                            ReadOnlyCollection <PeerNodeAddress> addresses = ResolveNewAddresses(timeoutHelper.RemainingTime(), true);
                            connectAlgorithm.Initialize(this, config, neighborManager.ConnectedNeighborCount + 1, new Dictionary <EndpointAddress, Referral>());
                            if (addresses.Count > 0)
                            {
                                if (isOpen)
                                {
                                    connectAlgorithm.UpdateEndpointsCollection(addresses);
                                    connectAlgorithm.Connect(timeoutHelper.RemainingTime());
                                }
                            }
                        }
                    }
                    finally
                    {
                        ResetMaintenance();
                    }
                }
            }
        }
Exemple #2
0
 public void RefreshConnection()
 {
     if (this.isOpen)
     {
         bool flag = false;
         if (!this.isRunningMaintenance)
         {
             lock (this.ThisLock)
             {
                 if (!this.isRunningMaintenance)
                 {
                     this.isRunningMaintenance = true;
                     flag = true;
                 }
             }
         }
         if (flag)
         {
             try
             {
                 TimeoutHelper helper = new TimeoutHelper(this.config.MaintainerTimeout);
                 this.maintainerTimer.Cancel();
                 using (IConnectAlgorithms algorithms = (default(TConnectAlgorithms) == null) ? ((IConnectAlgorithms)Activator.CreateInstance <TConnectAlgorithms>()) : ((IConnectAlgorithms)default(TConnectAlgorithms)))
                 {
                     ReadOnlyCollection <PeerNodeAddress> src = this.ResolveNewAddresses(helper.RemainingTime(), true);
                     algorithms.Initialize(this, this.config, this.neighborManager.ConnectedNeighborCount + 1, new Dictionary <EndpointAddress, Referral>());
                     if ((src.Count > 0) && this.isOpen)
                     {
                         algorithms.UpdateEndpointsCollection(src);
                         algorithms.Connect(helper.RemainingTime());
                     }
                 }
             }
             finally
             {
                 this.ResetMaintenance();
             }
         }
     }
 }
Exemple #3
0
 private void MaintainConnections(object dummy)
 {
     if (this.isOpen)
     {
         bool flag = false;
         if (!this.isRunningMaintenance)
         {
             lock (this.ThisLock)
             {
                 if (!this.isRunningMaintenance)
                 {
                     this.isRunningMaintenance = true;
                     flag = true;
                 }
             }
         }
         if (flag)
         {
             if (DiagnosticUtility.ShouldTraceInformation)
             {
                 PeerMaintainerTraceRecord extendedData = new PeerMaintainerTraceRecord(System.ServiceModel.SR.GetString("PeerMaintainerStarting", new object[] { this.config.MeshId }));
                 TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), extendedData, this, null);
             }
             TimeoutHelper helper = new TimeoutHelper(this.config.MaintainerTimeout);
             try
             {
                 this.maintainerTimer.Cancel();
                 int connectedNeighborCount = this.neighborManager.ConnectedNeighborCount;
                 if (connectedNeighborCount != this.config.IdealNeighbors)
                 {
                     using (IConnectAlgorithms algorithms = (default(TConnectAlgorithms) == null) ? ((IConnectAlgorithms)Activator.CreateInstance <TConnectAlgorithms>()) : ((IConnectAlgorithms)default(TConnectAlgorithms)))
                     {
                         algorithms.Initialize(this, this.config, this.config.IdealNeighbors, this.referralCache);
                         if (connectedNeighborCount > this.config.IdealNeighbors)
                         {
                             if (DiagnosticUtility.ShouldTraceInformation)
                             {
                                 PeerMaintainerTraceRecord record2 = new PeerMaintainerTraceRecord(System.ServiceModel.SR.GetString("PeerMaintainerPruneMode", new object[] { this.config.MeshId }));
                                 TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), record2, this, null);
                             }
                             algorithms.PruneConnections();
                         }
                         if (this.neighborManager.ConnectedNeighborCount < this.config.IdealNeighbors)
                         {
                             if (this.referralCache.Count == 0)
                             {
                                 ReadOnlyCollection <PeerNodeAddress> src = this.ResolveNewAddresses(helper.RemainingTime(), true);
                                 algorithms.UpdateEndpointsCollection(src);
                             }
                             if (DiagnosticUtility.ShouldTraceInformation)
                             {
                                 PeerMaintainerTraceRecord record3 = new PeerMaintainerTraceRecord(System.ServiceModel.SR.GetString("PeerMaintainerConnectMode", new object[] { this.config.MeshId }));
                                 TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), record3, this, null);
                             }
                             algorithms.Connect(helper.RemainingTime());
                         }
                     }
                 }
             }
             catch (Exception exception)
             {
                 if (Fx.IsFatal(exception))
                 {
                     throw;
                 }
                 DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information);
             }
             finally
             {
                 if (DiagnosticUtility.ShouldTraceInformation)
                 {
                     PeerMaintainerTraceRecord record4 = new PeerMaintainerTraceRecord("Maintainer cycle finish");
                     TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), record4, this, null);
                 }
             }
             this.ResetMaintenance();
         }
     }
 }
Exemple #4
0
 private void InitialConnection(object dummy)
 {
     if (this.isOpen)
     {
         bool flag = false;
         if (!this.isRunningMaintenance)
         {
             lock (this.ThisLock)
             {
                 if (!this.isRunningMaintenance)
                 {
                     this.isRunningMaintenance = true;
                     flag = true;
                 }
             }
         }
         if (flag)
         {
             if (DiagnosticUtility.ShouldTraceInformation)
             {
                 PeerMaintainerTraceRecord extendedData = new PeerMaintainerTraceRecord(System.ServiceModel.SR.GetString("PeerMaintainerInitialConnect", new object[] { this.config.MeshId }));
                 TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), extendedData, this, null);
             }
             TimeoutHelper helper = new TimeoutHelper(this.config.MaintainerTimeout);
             Exception     e      = null;
             try
             {
                 this.maintainerTimer.Cancel();
                 using (IConnectAlgorithms algorithms = (default(TConnectAlgorithms) == null) ? ((IConnectAlgorithms)Activator.CreateInstance <TConnectAlgorithms>()) : ((IConnectAlgorithms)default(TConnectAlgorithms)))
                 {
                     algorithms.Initialize(this, this.config, this.config.MinNeighbors, this.referralCache);
                     if (this.referralCache.Count == 0)
                     {
                         ReadOnlyCollection <PeerNodeAddress> src = this.ResolveNewAddresses(helper.RemainingTime(), false);
                         algorithms.UpdateEndpointsCollection(src);
                     }
                     if (this.isOpen)
                     {
                         algorithms.Connect(helper.RemainingTime());
                     }
                 }
             }
             catch (Exception exception2)
             {
                 if (Fx.IsFatal(exception2))
                 {
                     throw;
                 }
                 DiagnosticUtility.ExceptionUtility.TraceHandledException(exception2, TraceEventType.Information);
                 e = exception2;
             }
             if (this.isOpen)
             {
                 try
                 {
                     lock (this.ThisLock)
                     {
                         if (this.isOpen)
                         {
                             if (this.neighborManager.ConnectedNeighborCount < 1)
                             {
                                 this.maintainerTimer.Set(this.config.MaintainerRetryInterval);
                             }
                             else
                             {
                                 this.maintainerTimer.Set(this.config.MaintainerInterval);
                             }
                         }
                     }
                 }
                 catch (Exception exception3)
                 {
                     if (Fx.IsFatal(exception3))
                     {
                         throw;
                     }
                     DiagnosticUtility.ExceptionUtility.TraceHandledException(exception3, TraceEventType.Information);
                     if (e == null)
                     {
                         e = exception3;
                     }
                 }
             }
             lock (this.ThisLock)
             {
                 this.isRunningMaintenance = false;
             }
             if (this.connectCallback != null)
             {
                 this.connectCallback(e);
             }
         }
     }
 }
        // This activity maintains the connected nodes
        void MaintainConnections(object dummy)
        {
            // Are we open and is any maintenance activity occuring
            if (isOpen)
            {
                bool continueMaintenance = false;
                if (!isRunningMaintenance)
                {
                    lock (ThisLock)
                    {
                        if (!isRunningMaintenance)
                        {
                            isRunningMaintenance = true;
                            continueMaintenance  = true;
                        }
                    }
                }
                if (continueMaintenance)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord(SR.GetString(SR.PeerMaintainerStarting, this.config.MeshId));
                        TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                record, this, null);
                    }

                    TimeoutHelper timeoutHelper = new TimeoutHelper(config.MaintainerTimeout);
                    try
                    {
                        maintainerTimer.Cancel();                               // No reconnect until after connect has succeeded

                        int currentlyConnected = neighborManager.ConnectedNeighborCount;
                        if (currentlyConnected != config.IdealNeighbors)        // Already at ideal no work to do
                        {
                            using (IConnectAlgorithms connectAlgorithm = (IConnectAlgorithms) new TConnectAlgorithms())
                            {
                                connectAlgorithm.Initialize(this, config, config.IdealNeighbors, referralCache);
                                if (currentlyConnected > config.IdealNeighbors)
                                {
                                    if (DiagnosticUtility.ShouldTraceInformation)
                                    {
                                        PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord(SR.GetString(SR.PeerMaintainerPruneMode, this.config.MeshId));
                                        TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                                record, this, null);
                                    }
                                    connectAlgorithm.PruneConnections();
                                }

                                // During Prune some other neighbor may have gone away which leaves us below Ideal
                                // So try to reconnect
                                currentlyConnected = neighborManager.ConnectedNeighborCount;
                                if (currentlyConnected < config.IdealNeighbors)
                                {
                                    if (referralCache.Count == 0)
                                    {
                                        ReadOnlyCollection <PeerNodeAddress> addresses = ResolveNewAddresses(timeoutHelper.RemainingTime(), true);
                                        connectAlgorithm.UpdateEndpointsCollection(addresses);
                                    }
                                    if (DiagnosticUtility.ShouldTraceInformation)
                                    {
                                        PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord(SR.GetString(SR.PeerMaintainerConnectMode, this.config.MeshId));
                                        TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                                record, this, null);
                                    }
                                    connectAlgorithm.Connect(timeoutHelper.RemainingTime());
                                }
                            }
                        }
                    }
#pragma warning suppress 56500 // covered by FxCOP
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        // We ---- all non Fatal exceptions because this is a worker thread, with no user code waiting
                    }
                    finally
                    {
                        if (DiagnosticUtility.ShouldTraceInformation)
                        {
                            PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord("Maintainer cycle finish");
                            TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                    record, this, null);
                        }
                    }
                    ResetMaintenance();
                }
            }
        }
        void InitialConnection(object dummy)
        {
            // Are we open and is any maintenance activity occuring
            if (isOpen)
            {
                bool continueMaintenance = false;
                if (!isRunningMaintenance)
                {
                    lock (ThisLock)
                    {
                        if (!isRunningMaintenance)
                        {
                            isRunningMaintenance = true;
                            continueMaintenance  = true;
                        }
                    }
                }
                if (continueMaintenance)
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        PeerMaintainerTraceRecord record = new PeerMaintainerTraceRecord(SR.GetString(SR.PeerMaintainerInitialConnect, this.config.MeshId));
                        TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.PeerMaintainerActivity, SR.GetString(SR.TraceCodePeerMaintainerActivity),
                                                record, this, null);
                    }

                    TimeoutHelper timeoutHelper = new TimeoutHelper(config.MaintainerTimeout);
                    Exception     exception     = null;
                    // The connection algorithm may be pluggable if we provide an api or metadata to enable it.
                    // I am sure that research would be interested in doing such a thing.
                    try
                    {
                        maintainerTimer.Cancel();                   // No reconnect until after connect has succeeded

                        using (IConnectAlgorithms connectAlgorithm = (IConnectAlgorithms) new TConnectAlgorithms())
                        {
                            connectAlgorithm.Initialize(this, config, config.MinNeighbors, referralCache);
                            if (referralCache.Count == 0)
                            {
                                ReadOnlyCollection <PeerNodeAddress> addresses = ResolveNewAddresses(timeoutHelper.RemainingTime(), false);
                                connectAlgorithm.UpdateEndpointsCollection(addresses);
                            }
                            if (isOpen)
                            {
                                connectAlgorithm.Connect(timeoutHelper.RemainingTime());
                            }
                        }
                    }
#pragma warning suppress 56500 // covered by FxCOP
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                        exception = e;                              // Exeption is saved and transferred
                    }
                    if (isOpen)
                    {
                        try
                        {
                            lock (ThisLock)
                            {
                                if (isOpen)
                                {
                                    // No reconnect until after connect has succeeded
                                    if (neighborManager.ConnectedNeighborCount < 1)
                                    {
                                        maintainerTimer.Set(config.MaintainerRetryInterval);
                                    }
                                    else
                                    {
                                        maintainerTimer.Set(config.MaintainerInterval);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }
                            DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                            if (exception == null)
                            {
                                exception = e;                                   // Exeption is saved and transferred via callback
                            }
                        }
                    }
                    lock (ThisLock)
                    {
                        isRunningMaintenance = false;
                    }
                    if (connectCallback != null)
                    {
                        connectCallback(exception);
                    }
                }
            }
        }