Exemplo n.º 1
0
        public void Reset(int seconds, string format = null)
        {
            this.seconds = seconds;
            if (format != null)
            {
                this.format = format;
            }

            FormatCountdown();

            var timeManagerModule = ModuleManager.Instance.GetGameModule(TimerManageModule.Name) as TimerManageModule;

            if (seconds == 0)
            {
                if (OnFinish != null)
                {
                    OnFinish();
                }
            }
            else
            {
                var timerManager = timeManagerModule.GeneralTimerMgr;
                timerManager.CacheTimerNode(timerNode);
                timerNode = timerManager.AddTimer(1000, 1000, OnTick, true);
            }
        }
Exemplo n.º 2
0
        public Countdown(int seconds, string format = null, Action <string> onOutputAction = null, Action onFinishAction = null)
        {
            this.seconds  = seconds;
            this.OnOutput = onOutputAction;
            this.OnFinish = onFinishAction;
            if (format != null)
            {
                this.format = format;
            }

            FormatCountdown();

            var timerManagerModule = ModuleManager.Instance.GetGameModule(TimerManageModule.Name) as TimerManageModule;

            if (seconds == 0)
            {
                if (OnFinish != null)
                {
                    OnFinish();
                }
            }
            else
            {
                var timerManager = timerManagerModule.GeneralTimerMgr;
                timerManager.CacheTimerNode(timerNode);
                timerNode = timerManager.AddTimer(1000, 1000, OnTick, true);
            }
        }
Exemplo n.º 3
0
 public virtual void CacheTimerNode(TimerNode timerNode)
 {
     if (timerNode != null)
     {
         timerNode.RemoveTimer();
         mTimerNodeCache.Push(timerNode);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 添加定时器到相应的时间轮的插槽上
        /// </summary>
        protected virtual TimerNode AddTimerNode(uint millisecond, TimerNode timerNode)
        {
            uint interval = millisecond / Granularity;     // 计算毫秒数跨越了多少个单位时间刻度

            if (interval != 0 && millisecond % Granularity == 0)
            {
                --interval;
            }

            uint threshold1 = 256;                         // 1 << 8           第一个时间轮代表的最大刻度数
            uint threshold2 = 16384;                       // 1 << (8 + 6)     第二个时间轮代表的最大刻度数
            uint threshold3 = 1048576;                     // 1 << (8 + 2 * 6) 第三个时间轮代表的最大刻度数
            uint threshold4 = 67108864;                    // 1 << (8 + 3 * 6) 第四个时间轮代表的最大刻度数

            if (interval < threshold1)
            {
                //跨度小于第一个时间轮的跨度,加入到第一个时间轮中,并通过取模获取在时间轮中的哪个插槽上,以下类似
                uint index = (interval + mTimeWheels[0].CurrentSpokeIndex) & 255; //使用位运算对2^8 = 256 进行取模
                mTimeWheels[0].AddTimerNode((int)index, timerNode);
            }
            else if (interval < threshold2)
            {
                uint index = ((interval - threshold1 + mTimeWheels[1].CurrentSpokeIndex * threshold1) >> 8) & 63;
                mTimeWheels[1].AddTimerNode((int)index, timerNode);
            }
            else if (interval < threshold3)
            {
                //uint index = ((interval - threshold2 + mTimeWheels[2].CurrentSpokeIndex * threshold2) >> (8 + 6)) & 63;
                uint index = ((interval - threshold2 + mTimeWheels[2].CurrentSpokeIndex * threshold2) >> 14) & 63;
                mTimeWheels[2].AddTimerNode((int)index, timerNode);
            }
            else if (interval < threshold4)
            {
                //uint index = ((interval - threshold3 + mTimeWheels[3].CurrentSpokeIndex * threshold3) >> (8 + 2 * 6)) & 63;
                uint index = ((interval - threshold3 + mTimeWheels[3].CurrentSpokeIndex * threshold3) >> 20) & 63;
                mTimeWheels[3].AddTimerNode((int)index, timerNode);
            }
            else
            {
                //uint index = ((interval - threshold4 + mTimeWheels[4].CurrentSpokeIndex * threshold4) >> (8 + 3 * 6)) & 63;
                uint index = ((interval - threshold4 + mTimeWheels[4].CurrentSpokeIndex * threshold4) >> 26) & 63;
                mTimeWheels[4].AddTimerNode((int)index, timerNode);
            }

            ++mTotalTimerNum;
            if (mAutoPauseMode)
            {
                Restart();
            }
            return(timerNode);
        }
Exemplo n.º 5
0
        public virtual void DeleteTimer(TimerNode timerNode)
        {
            if (timerNode.TimerManagerHost != this)
            {
                throw new Exception("timerNode.timerManagerHost != this,cannot delete the timerNode");
            }
            timerNode.TimerNodeHost.List.Remove(timerNode.TimerNodeHost);
            --mTotalTimerNum;

            if (mAutoPauseMode && mTotalTimerNum <= 0)
            {
                Pause();
            }
        }
Exemplo n.º 6
0
        public void AddTimerNode(int spokeIndex, TimerNode timerNode)
        {
            LinkedList <TimerNode> value;

            if (!TryGetSpoke(spokeIndex, out value))
            {
                value = new LinkedList <TimerNode>();
                Insert(spokeIndex, value, true);
            }

            LinkedListNode <TimerNode> timerNodeHost = new LinkedListNode <TimerNode>(timerNode);

            timerNode.TimerNodeHost = timerNodeHost;
            value.AddLast(timerNodeHost);
        }
Exemplo n.º 7
0
        public virtual TimerNode AddTimer(uint startTime, int intervalTime, Action callback, bool continueInvoke = false)
        {
            TimerNode timerNode = null;

            if (mTimerNodeCache.Count == 0)
            {
                timerNode = new TimerNode(mCheckTime + startTime, intervalTime, callback, continueInvoke, this);
            }
            else
            {
                timerNode = mTimerNodeCache.Pop();
                timerNode.RemoveTimer();
                timerNode.DeadTime         = mCheckTime + startTime;
                timerNode.Interval         = intervalTime;
                timerNode.Callback         = callback;
                timerNode.ContinueInvoke   = continueInvoke;
                timerNode.TimerManagerHost = this;
            }

            return(AddTimerNode(startTime, timerNode));
        }
Exemplo n.º 8
0
 /// <summary>
 /// 定时器超时后,用于继续添加需要间隔执行的TimerNode
 /// </summary>
 protected virtual void AddIntervalTimer(uint startTime, TimerNode timerNode)
 {
     timerNode.DeadTime = mCheckTime + startTime;
     AddTimerNode(startTime, timerNode);
 }