From cacf0bcfcb303cf054eaf72cd88cc04c313f7b6d Mon Sep 17 00:00:00 2001 From: root Date: Mon, 8 Apr 2019 07:31:13 -0400 Subject: [PATCH] fix clock goes backward, simpilify timer, hopefully it works --- common.cpp | 40 +++++++++++++++++++++++----------------- common.h | 2 +- delay_manager.cpp | 9 ++------- fec_manager.cpp | 10 +--------- fec_manager.h | 4 ++-- 5 files changed, 29 insertions(+), 36 deletions(-) diff --git a/common.cpp b/common.cpp index 718e631..a81fce8 100644 --- a/common.cpp +++ b/common.cpp @@ -495,25 +495,31 @@ u64_t get_current_time_us() return (uint64_t(tmp_time.tv_sec))*1000llu*1000llu+ (uint64_t(tmp_time.tv_nsec))/1000llu; }*/ -u64_t get_current_time()//ms -{ - //timespec tmp_time; - //clock_gettime(CLOCK_MONOTONIC, &tmp_time); - //return ((u64_t)tmp_time.tv_sec)*1000llu+((u64_t)tmp_time.tv_nsec)/(1000*1000llu); - return (u64_t)(ev_time()*1000); -} - -u64_t get_current_time_rough()//ms -{ - return (u64_t)(ev_now(ev_default_loop(0))*1000); -} - u64_t get_current_time_us() { - //timespec tmp_time; - //clock_gettime(CLOCK_MONOTONIC, &tmp_time); - //return (uint64_t(tmp_time.tv_sec))*1000llu*1000llu+ (uint64_t(tmp_time.tv_nsec))/1000llu; - return (u64_t)(ev_time()*1000*1000); + static u64_t value_fix=0; + static u64_t largest_value=0; + + u64_t raw_value=(u64_t)(ev_time()*1000*1000); + + u64_t fixed_value=raw_value+value_fix; + + if(fixed_value< largest_value) + { + value_fix+= largest_value- fixed_value; + } + else + { + largest_value=fixed_value; + } + + //printf("<%lld,%lld,%lld>\n",raw_value,value_fix,raw_value + value_fix); + return raw_value + value_fix; //new fixed value +} + +u64_t get_current_time() +{ + return get_current_time_us()/1000lu; } u64_t pack_u64(u32_t a,u32_t b) diff --git a/common.h b/common.h index 02f3b6e..3be975f 100644 --- a/common.h +++ b/common.h @@ -386,7 +386,7 @@ struct fd_info_t }; u64_t get_current_time(); -u64_t get_current_time_rough(); +//u64_t get_current_time_rough(); u64_t get_current_time_us(); u64_t pack_u64(u32_t a,u32_t b); diff --git a/delay_manager.cpp b/delay_manager.cpp index 1de8cc9..88eea49 100644 --- a/delay_manager.cpp +++ b/delay_manager.cpp @@ -116,14 +116,9 @@ int delay_manager_t::check() } 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); - + const double m=1000*1000; ev_timer_stop(loop, &timer); - ev_timer_set(&timer, delay_mp.begin()->first /1000000.0 - ev_now(loop),0 ); //we should use ev_now here. + ev_timer_set(&timer, delay_mp.begin()->first/m -get_current_time_us()/m,0 ); ev_timer_start(loop, &timer); } else diff --git a/fec_manager.cpp b/fec_manager.cpp index e96855c..935a4b2 100644 --- a/fec_manager.cpp +++ b/fec_manager.cpp @@ -184,19 +184,11 @@ int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/) { if(counter==0) { - my_itimerspec its; - memset(&its.it_interval,0,sizeof(its.it_interval)); first_packet_time=get_current_time_us(); - my_time_t tmp_time=fec_par.timeout+first_packet_time; - its.it_value.tv_sec=tmp_time/1000000llu; - its.it_value.tv_nsec=(tmp_time%1000000llu)*1000llu; - //timerfd_settime(timer_fd,TFD_TIMER_ABSTIME,&its,0); ev_timer_stop(loop, &timer); - ev_timer_set(&timer, tmp_time/1000000.0 - ev_now(loop) ,0 ); //we should use ev_now here. + ev_timer_set(&timer, fec_par.timeout/1000000.0 ,0 ); ev_timer_start(loop, &timer); - - //ev_timer_set(loop,) } if(fec_par.mode==0)//for type 0 use blob { diff --git a/fec_manager.h b/fec_manager.h index aa94741..12b32bc 100644 --- a/fec_manager.h +++ b/fec_manager.h @@ -239,7 +239,7 @@ struct anti_replay_t } replay_buffer[index]=seq; assert(mp.find(seq)==mp.end()); - mp[seq].my_time=get_current_time_rough(); + mp[seq].my_time=get_current_time(); mp[seq].index=index; index++; if(index==int(anti_replay_buff_size)) index=0; @@ -248,7 +248,7 @@ struct anti_replay_t { if(mp.find(seq)==mp.end()) return 1; - if(get_current_time_rough()-mp[seq].my_time>anti_replay_timeout) + if(get_current_time()-mp[seq].my_time>anti_replay_timeout) { replay_buffer[mp[seq].index]=u64_t(i64_t(-1)); mp.erase(seq);