timersmanager 解析

Posted 王纲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 解析的主要内容,如果未能解决你的问题,请参考以下文章

无法解析片段中的 findViewById [重复]

无法解析片段中的 ViewModelProvider 构造?

Relay.js 没有正确解析组合片段

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

从 XML 声明片段获取 XML 编码:部分内容解析不支持 XmlDeclaration

NodeJs GraphQL 片段解析器