OpenMetaverse.NetworkManager.DisconnectTimer_Elapsed C# (CSharp) Method

DisconnectTimer_Elapsed() private method

private DisconnectTimer_Elapsed ( object obj ) : void
obj object
return void
        private void DisconnectTimer_Elapsed(object obj)
        {
            if (!connected || CurrentSim == null)
            {
                if (DisconnectTimer != null) DisconnectTimer.Dispose();
                connected = false;
            }
            else if (CurrentSim.DisconnectCandidate)
            {
                // The currently occupied simulator hasn't sent us any traffic in a while, shutdown
                Logger.Log("Network timeout for the current simulator (" +
                    CurrentSim.ToString() + "), logging out", Helpers.LogLevel.Warning, Client);

                if (DisconnectTimer != null) DisconnectTimer.Dispose();
                connected = false;

                // Shutdown the network layer
                Shutdown(DisconnectType.NetworkTimeout);
            }
            else
            {
                #region Check for timed out simulators

                // Figure out which sims need to be disconnected, then fire
                // all of the events to avoid calling DisconnectSim() inside
                // the Simulators lock
                List<Simulator> disconnectedSims = null;

                // Check all of the connected sims for disconnects
                lock (Simulators)
                {
                    for (int i = 0; i < Simulators.Count; i++)
                    {
                        if (Simulators[i].DisconnectCandidate)
                        {
                            // Avoid initializing a new List<> every time the timer
                            // fires with this piece of code
                            if (disconnectedSims == null)
                                disconnectedSims = new List<Simulator>();

                            disconnectedSims.Add(Simulators[i]);
                        }
                        else
                        {
                            Simulators[i].DisconnectCandidate = true;
                        }
                    }
                }

                // Actually disconnect each sim we detected as disconnected
                if (disconnectedSims != null)
                {
                    for (int i = 0; i < disconnectedSims.Count; i++)
                    {
                        if (disconnectedSims[i] != null)
                        {
                            // This sim hasn't received any network traffic since the 
                            // timer last elapsed, consider it disconnected
                            Logger.Log("Network timeout for simulator " + disconnectedSims[i].ToString() +
                                ", disconnecting", Helpers.LogLevel.Warning, Client);

                            DisconnectSim(disconnectedSims[i], true);
                        }
                    }
                }

                #endregion Check for timed out simulators
            }
        }