Beispiel #1
0
        private TimerInstance AddTask(TimerTask task, TimerInstance handler)
        {
            int wheelIndex = -1;
            int slotIndex  = -1;

            for (int i = 0; i < m_Wheels.Length; ++i)
            {
                TimerWheel wheel = m_Wheels[i];
                if (wheel.TotalTickInMS >= task.TriggerLeftInMS)
                {
                    slotIndex  = wheel.AddTask(task);
                    wheelIndex = i;
                    break;
                }
            }
            if (handler != null)
            {
                handler.WheelIndex     = wheelIndex;
                handler.WheelSlotIndex = slotIndex;
            }
            else
            {
                handler = new TimerInstance()
                {
                    Index          = task.Index,
                    WheelIndex     = wheelIndex,
                    WheelSlotIndex = slotIndex,
                };
                m_Handlers.Add(task.Index, handler);
            }

            return(handler);
        }
Beispiel #2
0
        public TimerInstance AddTimer(
            float intervalInSec,
            float totalInSec,
            TimerEventHandler intervalCallback,
            TimerEventHandler endCallback,
            object userdata)
        {
            int index = m_IndexCreator.NextID;

            TimerTask task = m_TaskPool.Get();

            task.SetData(index, intervalInSec, totalInSec, intervalCallback, endCallback, userdata);

            return(AddTask(task, null));
        }
Beispiel #3
0
        public bool RemoveTimer(TimerInstance handler)
        {
            if (handler != null && m_Handlers.ContainsKey(handler.Index))
            {
                m_Handlers.Remove(handler.Index);
                if (handler.IsValid())
                {
                    TimerTask task = m_Wheels[handler.WheelIndex].RemoveTask(handler);
                    handler.Clear();
                    if (task != null)
                    {
                        m_TaskPool.Release(task);
                        return(true);
                    }
                }
            }

            return(false);
        }