www.pudn.com > Fetion.rar > AutoReconnect.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Client.Core; 
    using Imps.Utils; 
    using System; 
    using System.Net.NetworkInformation; 
    using System.Runtime.CompilerServices; 
 
    public class AutoReconnect 
    { 
        private volatile int _maxTryTime = 3; 
        private volatile int _nextInterval; 
        private Random _rand = new Random(Environment.TickCount); 
        private volatile bool _timerStarted; 
        public volatile int _tryCounter; 
 
        public event EventHandler OnTick; 
 
        public event EventHandler TimeUp; 
 
        internal AutoReconnect(int maxTryTime) 
        { 
            this._maxTryTime = maxTryTime; 
            IMPSNetworkChange.Instance.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(this.Instance_NetworkAvailabilityChanged); 
            IMPSNetworkChange.Instance.NetworkRestored += new EventHandler(this.Instance_NetworkRestored); 
        } 
 
        private void AdjustInterval(object sender, EventArgs e) 
        { 
            if (--this._nextInterval >= 0) 
            { 
                if (this._nextInterval != 0) 
                { 
                    if (this.OnTick != null) 
                    { 
                        FuncDispatcher.InvokeEventHandlerInUiThread(null, this.OnTick, new TickEventArgs(this._nextInterval)); 
                    } 
                } 
                else if (this.TimeUp != null) 
                { 
                    FuncDispatcher.InvokeEventHandlerInUiThread(null, this.TimeUp, EventArgs.Empty); 
                } 
            } 
            else 
            { 
                this.StopTimer(); 
            } 
        } 
 
        public void ConnectNow() 
        { 
            this._nextInterval = 1; 
        } 
 
        private bool HasAvaildAbleNetwork() 
        { 
            return NetworkInterface.GetIsNetworkAvailable(); 
        } 
 
        private void Instance_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) 
        { 
            if (e.IsAvailable) 
            { 
                this.Instance_NetworkRestored(this, EventArgs.Empty); 
            } 
        } 
 
        private void Instance_NetworkRestored(object sender, EventArgs e) 
        { 
            this.StopTimer(true); 
            FuncDispatcher.InvokeEventHandlerInUiThread(this, this.TimeUp, EventArgs.Empty); 
        } 
 
        public void StartTimer() 
        { 
            if (!this._timerStarted && (this._tryCounter < this._maxTryTime)) 
            { 
                this._timerStarted = true; 
                this._nextInterval = this.NextInterval; 
                this._tryCounter++; 
                GlobalTimer.Register(new EventHandler(this.AdjustInterval), 10); 
            } 
        } 
 
        public void StopTimer() 
        { 
            this.StopTimer(false); 
        } 
 
        public void StopTimer(bool reset) 
        { 
            GlobalTimer.Unregister(new EventHandler(this.AdjustInterval)); 
            this._timerStarted = false; 
            if (reset) 
            { 
                this._tryCounter = 0; 
            } 
        } 
 
        public int NextInterval 
        { 
            get 
            { 
                int num; 
                int num2; 
                switch (this._tryCounter) 
                { 
                    case 0: 
                        num = 15; 
                        num2 = 0x2d; 
                        break; 
 
                    case 1: 
                        num = 0x2d; 
                        num2 = 0x4b; 
                        break; 
 
                    case 2: 
                        num = 90; 
                        num2 = 150; 
                        break; 
 
                    default: 
                        num = 180; 
                        num2 = 300; 
                        break; 
                } 
                return this._rand.Next(num, num2); 
            } 
        } 
    } 
}