wangyu-UDPspeeder/delay_manager.cpp
2017-09-25 10:40:01 -05:00

112 lines
2.0 KiB
C++

/*
* delay_manager.cpp
*
* Created on: Sep 15, 2017
* Author: root
*/
#include "delay_manager.h"
#include "log.h"
#include "packet.h"
int delay_data_t::handle()
{
return my_send(dest,data,len)>=0;
}
delay_manager_t::delay_manager_t()
{
capacity=0;
if ((timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK)) < 0)
{
mylog(log_fatal,"timer_fd create error");
myexit(1);
}
itimerspec zero_its;
memset(&zero_its, 0, sizeof(zero_its));
timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &zero_its, 0);
}
delay_manager_t::~delay_manager_t()
{
//TODO ,we currently dont need to deconstruct it
}
/*
int delay_manager_t::get_timer_fd()
{
return delay_timer_fd;
}*/
int delay_manager_t::add(my_time_t delay,delay_data_t &delay_data)
{
if(capacity!=0&&int(delay_mp.size()) >=capacity)
{
mylog(log_warn,"max pending packet reached,ignored\n");
return -1;
}
if(delay==0)
{
int ret=delay_data.handle();
if (ret != 0) {
mylog(log_debug, "handle() return %d\n", ret);
}
return 0;
}
delay_data_t tmp=delay_data;
tmp.data=(char *)malloc(delay_data.len);
memcpy(tmp.data,delay_data.data,delay_data.len);
my_time_t tmp_time=get_current_time_us();
tmp_time+=delay;
delay_mp.insert(make_pair(tmp_time,tmp));
return 0;
}
int delay_manager_t::check()
{
if(!delay_mp.empty())
{
my_time_t current_time;
multimap<my_time_t,delay_data_t>::iterator it;
while(1)
{
int ret=0;
it=delay_mp.begin();
if(it==delay_mp.end()) break;
current_time=get_current_time_us();
if(it->first <= current_time)
{
ret=it->second.handle();
if (ret != 0) {
mylog(log_debug, "handle() return %d\n", ret);
}
free(it->second.data);
delay_mp.erase(it);
}
else
{
break;
}
}
if(!delay_mp.empty())
{
itimerspec its;
memset(&its.it_interval,0,sizeof(its.it_interval));
its.it_value.tv_sec=delay_mp.begin()->first/1000000llu;
its.it_value.tv_nsec=(delay_mp.begin()->first%1000000llu)*1000llu;
timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0);
}
}
return 0;
}