sendwindow.cs

来自「破解的飞信源代码」· CS 代码 · 共 71 行

CS
71
字号
namespace Imps.Client.CommLayer
{
    using System;
    using System.Collections.Generic;
    using System.Threading;

    internal sealed class SendWindow
    {
        private readonly int _maxMessageCount;
        private readonly int _period;
        private readonly LinkedList<DateTime> _sendTimeList;

        public SendWindow(int period, int maxMessageCount)
        {
            this._period = period;
            this._maxMessageCount = maxMessageCount;
            this._sendTimeList = new LinkedList<DateTime>();
        }

        public bool CanSendNow()
        {
            return this.CanSendNow(false);
        }

        private bool CanSendNow(bool needWait)
        {
            DateTime now = DateTime.Now;
            lock (this._sendTimeList)
            {
                this._sendTimeList.AddLast(now);
            }
            if (this._sendTimeList.get_Count() > this._maxMessageCount)
            {
                try
                {
                    TimeSpan span;
                    lock (this._sendTimeList)
                    {
                        span = (TimeSpan) (now - this._sendTimeList.get_First().get_Value());
                    }
                    if (span.TotalMilliseconds < this._period)
                    {
                        if (needWait)
                        {
                            Thread.Sleep((int) (this._period - ((int) span.TotalMilliseconds)));
                        }
                        return false;
                    }
                }
                catch
                {
                }
                finally
                {
                    lock (this._sendTimeList)
                    {
                        this._sendTimeList.RemoveFirst();
                    }
                }
            }
            return true;
        }

        public void SendRateControl()
        {
            this.CanSendNow(true);
        }
    }
}

⌨️ 快捷键说明

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