⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoreconnect.cs

📁 破解的飞信源代码
💻 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<TickEventArgs> OnTick;

        public event EventHandler TimeUp;

        internal AutoReconnect(int maxTryTime)
        {
            this._maxTryTime = maxTryTime;
            IMPSNetworkChange.Instance.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(this, (IntPtr) 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<TickEventArgs>(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.get_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 minValue;
                int maxValue;
                switch (this._tryCounter)
                {
                    case 0:
                        minValue = 15;
                        maxValue = 0x2d;
                        break;

                    case 1:
                        minValue = 0x2d;
                        maxValue = 0x4b;
                        break;

                    case 2:
                        minValue = 90;
                        maxValue = 150;
                        break;

                    default:
                        minValue = 180;
                        maxValue = 300;
                        break;
                }
                return this._rand.Next(minValue, maxValue);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -