代码拉取完成,页面将自动刷新
#include "TimeWheelArray.h"
TimeWheelArray::TimeWheelArray(uint32_t slot_num,uint32_t step_ms,const std::string& name )
:m_slot_num(slot_num),
m_step_ms(step_ms),
m_name(name),
m_cur_index(-1),
mp_smaller_unit_array(nullptr),
mp_larger_unit_array(nullptr),
m_slots(m_slot_num)
{
}
TimeWheelArray::~TimeWheelArray()
{
}
void TimeWheelArray::addTimeNode(std::shared_ptr<TimeWheelNode> &timenode)
{
uint64_t smaller_unit_cur_time=0;
if(mp_smaller_unit_array!=nullptr)
{
smaller_unit_cur_time=mp_smaller_unit_array->getTimerRunTime();
}
//diff_time only consider this unit time so shoule delete smaller influence
int diff_time=timenode->getTimestamp()-(getNowTimestamp()-smaller_unit_cur_time);
//if difftime is later than step, so this timenode should in this unit slots
if(diff_time>=m_step_ms)
{
uint32_t index=(m_cur_index+diff_time/m_step_ms)%m_slot_num;
m_slots[index].push_back(timenode);
return;
}
//if diff_time is in cur_slot,should consider smallerunit to make timernode preciser
if(mp_smaller_unit_array!=nullptr)
{
mp_smaller_unit_array->addTimeNode(timenode);
return;
}
m_slots[m_cur_index].push_back(timenode);
}
void TimeWheelArray::deleteTimeNode(std::shared_ptr<TimeWheelNode> &timenode)
{
uint64_t smaller_unit_cur_time=0;
if(mp_smaller_unit_array!=nullptr)
{
smaller_unit_cur_time=mp_smaller_unit_array->getTimerRunTime();
}
int diff_time=timenode->getTimestamp()-(getNowTimestamp()-smaller_unit_cur_time);
if(diff_time>=m_step_ms)
{
uint32_t index=(m_cur_index+diff_time/m_step_ms)%m_slot_num;
for(std::list<std::shared_ptr<TimeWheelNode>>::iterator iter=m_slots[index].begin();iter!=m_slots[index].end();iter++)
{
if(*iter==timenode)
{
m_slots[index].erase(iter);
break;
}
}
return;
}
if(mp_smaller_unit_array!=nullptr)
{
mp_smaller_unit_array->deleteTimeNode(timenode);
return;
}
for(std::list<std::shared_ptr<TimeWheelNode>>::iterator iter=m_slots[m_cur_index].begin();iter!=m_slots[m_cur_index].end();iter++)
{
if (*iter == timenode)
{
m_slots[m_cur_index].erase(iter);
break;
}
}
}
void TimeWheelArray::tick()
{
m_cur_index++;
if(m_cur_index<m_slot_num) return;
//if m_cur_index is at tail of vector,it should move to the head
//and mp_larger_unit_array should move one step
m_cur_index%=m_slot_num;
if(mp_larger_unit_array!=nullptr)
{
mp_larger_unit_array->tick();
//get larger missions and put into this solts
std::list<std::shared_ptr<TimeWheelNode>> templist=mp_larger_unit_array->getDegradationTimeNodes();
for(auto &node:templist)
{
addTimeNode(node);
}
}
}
std::list<std::shared_ptr<TimeWheelNode>> TimeWheelArray::getDegradationTimeNodes()
{
//clear m_slots[m_cur_index] and return the value
return std::move(m_slots[m_cur_index]);
}
uint64_t TimeWheelArray::getTimerRunTime()
{
uint64_t time=m_cur_index*m_step_ms;
if(mp_smaller_unit_array!=nullptr)
{
time+=mp_smaller_unit_array->getTimerRunTime();
}
return time;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。