最近在看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++) {
//每间隔period个槽里都会存放TimeEvent,比如某个TimeEvent的period为100,delta为9,最多
TimeEvent对应的事件只执行一次。
FOR_MAP(_pSlots[_currentSlotIndex % _slotsCount].timers, uint32_t, TimerEvent, j) {
_processTimerEvent(MAP_VAL(j));
}
_currentSlotIndex++;
}
}
void TimersManager::UpdatePeriods(uint32_t period) {
//如果 TimeEvent周期已经存在直接返回
if (MAP_HAS1(_periodsMap, period))
return;
_periodsMap[period] = period;
//将TimeEvent 周期放在period vector中
ADD_VECTOR_END(_periodsVector, period);
//计算所有period的最小公倍数,比如之前period为4,新period为3,总Slot Count 为4 * 3 = 12
uint32_t newSlotsCount = LCM(_periodsVector, 0);
if (newSlotsCount == 0)
newSlotsCount = period;
if (newSlotsCount == _slotsCount)
return;
Slot *pNewSlots = new Slot[newSlotsCount];
if (_slotsCount > 0) {
//将Slot Count增加到30,本质上试讲之前的slot复制5份
for (uint32_t i = 0; i < newSlotsCount; i++) {
pNewSlots[i] = _pSlots[i % _slotsCount];
}
delete[] _pSlots;
}
_pSlots = pNewSlots;
_slotsCount = newSlotsCount;
}
GCD最大公约数,LCM 求最大公倍数
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);
*
* */
如下图所示:第一个TimeEvent的周期为4,所以slot数目为4,也就是图1中的前4个slot。添加第二个TimeEvent的周期为3,UpdatePeriods
方法将前4个slot复制3份得到12个slot,这样TimeEvent1被放到1,5,9个slot中。接下来AddTimer方法会将TimeEvent2添加到slot中:第一
个添加的slot是slot_2,因为这个slot的TimeEvent总数为0,接下来添加的solt分别是slot_5,slot_8,slot_11,如果继续添加下去是(11 + 3)%
3 = 2,但slot_2已经添加过,各槽添加TimeEvent2结束。作者将slot总槽书设计为所有的period最小公倍数,在这里发送作用了。
图1