首页 > 代码库 > timersmanager 解析
timersmanager 解析
最近在看crtmp源代码,看到timersmanager 模块时感觉很难理解,花了不少时间反复思考该模块
的逻辑,现在思考的结果记录下来,方便以后查阅。
构造函数中将处理时间方法传进来,将_lastTime赋值为当前时间,将当前slot Index设置为0,Slot指针
赋为空,slot数目赋为0。slot可以理解为槽。
TimersManager::TimersManager(ProcessTimerEvent processTimerEvent) { _processTimerEvent = processTimerEvent; _lastTime = time(NULL); _currentSlotIndex = 0; _pSlots = NULL; _slotsCount = 0;}
析构时释放掉slot,无需多解释。
TimersManager::~TimersManager() { if (_pSlots != NULL) delete[] _pSlots;}
移除处理事件方法,将id为eventTimerId的事件处理方法从所有的slot中移除掉
void TimersManager::RemoveTimer(uint32_t eventTimerId) { for (uint32_t i = 0; i < _slotsCount; i++) { if (MAP_HAS1(_pSlots[i].timers, eventTimerId)) { _pSlots[i].timers.erase(eventTimerId); } }}
void TimersManager::AddTimer(TimerEvent& timerEvent) { UpdatePeriods(timerEvent.period); uint32_t min = 999999999; uint32_t startIndex = 0; for (uint32_t i = 0; i < _slotsCount; i++) { if (min > _pSlots[i].timers.size()) { startIndex = i; min = _pSlots[i].timers.size(); } } while (!MAP_HAS1(_pSlots[startIndex % _slotsCount].timers, timerEvent.id)) { _pSlots[startIndex % _slotsCount].timers[timerEvent.id] = timerEvent; startIndex += timerEvent.period; }}void TimersManager::TimeElapsed(uint64_t currentTime) { int64_t delta = currentTime - _lastTime; _lastTime = currentTime; if (delta <= 0 || _slotsCount == 0) return; for (int32_t i = 0; i < delta; i++) { FOR_MAP(_pSlots[_currentSlotIndex % _slotsCount].timers, uint32_t, TimerEvent, j) { _processTimerEvent(MAP_VAL(j)); } _currentSlotIndex++; }}void TimersManager::UpdatePeriods(uint32_t period) { if (MAP_HAS1(_periodsMap, period)) return; _periodsMap[period] = period; ADD_VECTOR_END(_periodsVector, period); uint32_t newSlotsCount = LCM(_periodsVector, 0); if (newSlotsCount == 0) newSlotsCount = period; if (newSlotsCount == _slotsCount) return; Slot *pNewSlots = new Slot[newSlotsCount]; if (_slotsCount > 0) { for (uint32_t i = 0; i < newSlotsCount; i++) { pNewSlots[i] = _pSlots[i % _slotsCount]; } delete[] _pSlots; } _pSlots = pNewSlots; _slotsCount = newSlotsCount;}uint32_t TimersManager::GCD(uint32_t a, uint32_t b) { while (b != 0) { uint32_t t = b; b = a % b; a = t; } return a;}uint32_t TimersManager::LCM(uint32_t a, uint32_t b) { if (a == 0 || b == 0) return 0; uint32_t result = a * b / GCD(a, b); FINEST("a: %u; b: %u; r: %u", a, b, result); return result;}uint32_t TimersManager::GCD(vector<uint32_t> numbers, uint32_t startIndex) { if (numbers.size() <= 1) return 0; if (numbers.size() <= startIndex) return 0; if (numbers.size() - startIndex > 2) { return GCD(numbers[startIndex], GCD(numbers, startIndex + 1)); } else { return GCD(numbers[startIndex], numbers[startIndex + 1]); }}uint32_t TimersManager::LCM(vector<uint32_t> numbers, uint32_t startIndex) { if (numbers.size() <= 1) return 0; if (numbers.size() <= startIndex) return 0; if (numbers.size() - startIndex > 2) { return LCM(numbers[startIndex], LCM(numbers, startIndex + 1)); } else { return LCM(numbers[startIndex], numbers[startIndex + 1]); }}/* * * TimersManager tm(NULL); TimerEvent t1 = {2, 1, NULL}; TimerEvent t2 = {3, 2, NULL}; TimerEvent t3 = {3, 3, NULL}; TimerEvent t4 = {4, 4, NULL}; TimerEvent t5 = {3, 5, NULL}; TimerEvent t6 = {2, 6, NULL}; TimerEvent t7 = {4, 7, NULL}; tm.AddTimer(t1); tm.AddTimer(t2); tm.AddTimer(t3); tm.AddTimer(t4); tm.AddTimer(t5); tm.AddTimer(t6); tm.AddTimer(t7); * * */
timersmanager 解析
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。