/// <summary>
        ///删除定时任务
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        internal bool RemoveTimerTask(TimerTaskInfo taskInfo)
        {
            if (taskInfo == null || taskInfo.WheelIndex < 0 || taskInfo.WheelSlotIndex < 0 || taskInfo.TaskListIndex < 0)
            {
                return(false);
            }
            if (!m_TaskInfoDic.ContainsKey(taskInfo.Index))
            {
                return(false);
            }

            if (taskInfo.WheelIndex < 0 || taskInfo.WheelIndex >= m_WheelArr.Length || m_WheelArr[taskInfo.WheelIndex] == null)
            {
                return(false);
            }

            m_TaskInfoDic.Remove(taskInfo.Index);

            int wheelIndex     = taskInfo.WheelIndex;
            int wheelSlotIndex = taskInfo.WheelSlotIndex;
            int taskListIndex  = taskInfo.TaskListIndex;

            taskInfo.OnClear();

            return(m_WheelArr[wheelIndex].RemoveTimerTask(wheelSlotIndex, taskListIndex));
        }
        /// <summary>
        /// 添加定时任务
        /// </summary>
        /// <param name="task"></param>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        private bool AddTimerTask(TimerTask task, TimerTaskInfo taskInfo)
        {
            if (!task.IsValidTask())
            {
                return(false);
            }

            for (int i = 0; i < m_WheelArr.Length; i++)
            {
                int slotIndex     = -1;
                int taskListIndex = -1;
                if (m_WheelArr[i].AddTimerTask(task, ref slotIndex, ref taskListIndex))
                {
                    taskInfo.WheelSlotIndex = slotIndex;
                    taskInfo.TaskListIndex  = taskListIndex;
                    taskInfo.WheelIndex     = i;
                    break;
                }
            }
            if (taskInfo.WheelIndex < 0 || taskInfo.WheelSlotIndex < 0 || taskInfo.TaskListIndex < 0)
            {
                return(false);
            }
            m_TaskIndex++;
            task.Index     = m_TaskIndex;
            taskInfo.Index = m_TaskIndex;
            m_TaskInfoDic.Add(taskInfo.Index, taskInfo);
            return(true);
        }
Example #3
0
 /// <summary>
 /// 删除指定的定时任务
 /// </summary>
 /// <param name="taskInfo"></param>
 /// <returns></returns>
 public bool RemoveTimer(TimerTaskInfo taskInfo)
 {
     if (m_HTimerWheel != null)
     {
         return(m_HTimerWheel.RemoveTimerTask(taskInfo));
     }
     return(false);
 }
        /// <summary>
        /// 添加定时任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        internal TimerTaskInfo AddTimerTask(TimerTask task)
        {
            TimerTaskInfo taskInfo = new TimerTaskInfo();

            if (AddTimerTask(task, taskInfo))
            {
                task.OnTaskStart();
                return(taskInfo);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 指定
        /// </summary>
        /// <param name="index"></param>
        /// <param name="taskList"></param>
        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 (!m_TaskInfoDic.TryGetValue(task.Index, out taskInfo))
                {
                    continue;
                }
                if (task.RemainingWheelInMS == 0)
                {
                    task.OnTrigger();
                }

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