예제 #1
0
        internal bool AddTimerTask(TimerTask task, ref int slotIndex, ref int taskListIndex)
        {
            if (task.remainingWheelInMS >= slotSize * tickInMS)
            {
                slotIndex     = -1;
                taskListIndex = -1;
                return(false);
            }

            int targetSlot = task.remainingWheelInMS / tickInMS;

            if (targetSlot == 0)
            {
                targetSlot = 1;
                task.remainingWheelInMS = 0;
            }
            else
            {
                task.remainingWheelInMS = task.remainingWheelInMS % tickInMS;
            }

            slotIndex = currentSlotIndex + targetSlot;
            slotIndex = slotIndex % slotSize;
            if (slotArr[slotIndex] == null)
            {
                slotArr[slotIndex] = new List <TimerTask>();
            }
            taskListIndex = slotArr[slotIndex].Count;
            slotArr[slotIndex].Add(task);

            return(true);
        }
예제 #2
0
        private bool AddTimerTask(TimerTask task, TimerTaskInfo taskInfo)
        {
            if (!task.IsValidTask())
            {
                return(false);
            }

            for (int i = 0; i < wheelArr.Length; i++)
            {
                if (wheelArr[i].AddTimerTask(task, ref taskInfo.wheelSlotIndex, ref taskInfo.taskListIndex))
                {
                    taskInfo.wheelIndex = i;
                    break;
                }
            }
            if (taskInfo.wheelIndex < 0 || taskInfo.wheelSlotIndex < 0 || taskInfo.taskListIndex < 0)
            {
                return(false);
            }
            taskIndex++;
            task.index     = taskIndex;
            taskInfo.index = taskIndex;
            taskInfoDic.Add(taskInfo.index, taskInfo);
            return(true);
        }
예제 #3
0
 private void RecycleTimerTask(TimerTask task)
 {
     if (task != null)
     {
         task.OnClear();
         idleTimerTaskList.Add(task);
     }
 }
예제 #4
0
        public TimerTaskInfo AddTimerTask(float intervalInSec,
                                          float totalInSec,
                                          VoidDelegateWithObj startCallback,
                                          VoidDelegateWithObj intervalCallback,
                                          VoidDelegateWithObj endCallback,
                                          object callbackData)
        {
            TimerTask task = hTimerWheel.GetIdleTimerTask();

            task.OnReused(intervalInSec, totalInSec, startCallback, intervalCallback, endCallback, callbackData);
            return(hTimerWheel.AddTimerTask(task));
        }
예제 #5
0
        internal TimerTaskInfo AddTimerTask(TimerTask task)
        {
            TimerTaskInfo taskInfo = new TimerTaskInfo();

            if (AddTimerTask(task, taskInfo))
            {
                task.OnTaskStart();
                return(taskInfo);
            }
            else
            {
                return(null);
            }
        }
예제 #6
0
        internal TimerTask GetIdleTimerTask()
        {
            TimerTask task = null;

            if (idleTimerTaskList.Count == 0)
            {
                task = new TimerTask();
            }
            else
            {
                task = idleTimerTaskList[0];
                idleTimerTaskList.RemoveAt(0);
            }
            return(task);
        }
예제 #7
0
        private void OnTimerWheelTrigger(int index, List <TimerTask> taskList)
        {
            for (int i = 0; i < taskList.Count; i++)
            {
                TimerTask task = taskList[i];
                if (task == null)
                {
                    continue;
                }
                TimerTaskInfo taskInfo = null;
                if (!taskInfoDic.TryGetValue(task.index, out taskInfo))
                {
                    continue;
                }
                if (task.remainingWheelInMS == 0)
                {
                    task.OnTrigger();
                }

                if (taskInfo.IsClear())
                {
                    RecycleTimerTask(task);
                }
                else
                {
                    taskInfoDic.Remove(task.index);
                    taskInfo.OnClear();
                    if (task.IsValidTask())
                    {
                        AddTimerTask(task, taskInfo);
                    }
                    else
                    {
                        RecycleTimerTask(task);
                    }
                }
            }
        }