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); }
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); }
private void RecycleTimerTask(TimerTask task) { if (task != null) { task.OnClear(); idleTimerTaskList.Add(task); } }
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)); }
internal TimerTaskInfo AddTimerTask(TimerTask task) { TimerTaskInfo taskInfo = new TimerTaskInfo(); if (AddTimerTask(task, taskInfo)) { task.OnTaskStart(); return(taskInfo); } else { return(null); } }
internal TimerTask GetIdleTimerTask() { TimerTask task = null; if (idleTimerTaskList.Count == 0) { task = new TimerTask(); } else { task = idleTimerTaskList[0]; idleTimerTaskList.RemoveAt(0); } return(task); }
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); } } } }