mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
bugfix: ntp校验时把时间往后调整
This commit is contained in:
@@ -26,8 +26,6 @@ typedef void (*timer_execute_func)(void *ud,void *arg);
|
|||||||
#define TIME_NEAR_MASK (TIME_NEAR-1)
|
#define TIME_NEAR_MASK (TIME_NEAR-1)
|
||||||
#define TIME_LEVEL_MASK (TIME_LEVEL-1)
|
#define TIME_LEVEL_MASK (TIME_LEVEL-1)
|
||||||
|
|
||||||
#define CENTISECOND_MASK 0x1ffffff
|
|
||||||
|
|
||||||
struct timer_event {
|
struct timer_event {
|
||||||
uint32_t handle;
|
uint32_t handle;
|
||||||
int session;
|
int session;
|
||||||
@@ -50,6 +48,8 @@ struct timer {
|
|||||||
int time;
|
int time;
|
||||||
uint32_t current;
|
uint32_t current;
|
||||||
uint32_t starttime;
|
uint32_t starttime;
|
||||||
|
uint64_t current_point;
|
||||||
|
uint64_t origin_point;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct timer * TI = NULL;
|
static struct timer * TI = NULL;
|
||||||
@@ -236,26 +236,34 @@ _systime(uint32_t *sec, uint32_t *cs) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t
|
static uint64_t
|
||||||
_gettime() {
|
_gettime() {
|
||||||
uint32_t sec, cs;
|
uint64_t t;
|
||||||
_systime(&sec, &cs);
|
#if !defined(__APPLE__)
|
||||||
// if sec < TI->starttime, need to update TI->starttime?
|
struct timespec ti;
|
||||||
// if(sec < TI->starttime) {
|
clock_gettime(CLOCK_MONOTONIC, &ti);
|
||||||
// skynet_error(NULL, "time diff error, change from %u -> %u", TI->starttime, sec);
|
t = ti.tv_sec * 100;
|
||||||
// TI->starttime = sec;
|
t += (uint32_t)(ti.tv_nsec / 10000000);
|
||||||
// TI->current = cs;
|
#else
|
||||||
// }
|
struct timeval tv;
|
||||||
uint32_t diff = (sec - TI->starttime) & CENTISECOND_MASK;
|
gettimeofday(&tv, NULL);
|
||||||
return diff * 100 + cs;
|
t = ti.tv_sec * 100;
|
||||||
|
t += (uint32_t)(tv.tv_usec / 10000);
|
||||||
|
#endif
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_updatetime(void) {
|
skynet_updatetime(void) {
|
||||||
uint32_t ct = _gettime();
|
uint64_t cp = _gettime();
|
||||||
if (ct != TI->current) {
|
if(cp < TI->current_point) {
|
||||||
uint32_t diff = ct>=TI->current?ct-TI->current:(uint32_t)(CENTISECOND_MASK+1)*100-TI->current+ct;
|
skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point);
|
||||||
TI->current = ct;
|
} else if (cp != TI->current_point) {
|
||||||
|
uint32_t diff = (uint32_t)(cp - TI->current_point);
|
||||||
|
TI->current_point = cp;
|
||||||
|
|
||||||
|
// when cs > 0xffffffff(about 49710), time rewind
|
||||||
|
TI->current += diff;
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<diff;i++) {
|
for (i=0;i<diff;i++) {
|
||||||
timer_update(TI);
|
timer_update(TI);
|
||||||
@@ -277,5 +285,8 @@ void
|
|||||||
skynet_timer_init(void) {
|
skynet_timer_init(void) {
|
||||||
TI = timer_create_timer();
|
TI = timer_create_timer();
|
||||||
_systime(&TI->starttime, &TI->current);
|
_systime(&TI->starttime, &TI->current);
|
||||||
|
uint64_t point = _gettime();
|
||||||
|
TI->current_point = point;
|
||||||
|
TI->origin_point = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,4 +2,21 @@ local skynet = require "skynet"
|
|||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
print(skynet.starttime())
|
print(skynet.starttime())
|
||||||
print(skynet.now())
|
print(skynet.now())
|
||||||
|
|
||||||
|
skynet.timeout(1, function()
|
||||||
|
print("in 1", skynet.now())
|
||||||
|
end)
|
||||||
|
skynet.timeout(2, function()
|
||||||
|
print("in 2", skynet.now())
|
||||||
|
end)
|
||||||
|
skynet.timeout(3, function()
|
||||||
|
print("in 3", skynet.now())
|
||||||
|
end)
|
||||||
|
|
||||||
|
skynet.timeout(4, function()
|
||||||
|
print("in 4", skynet.now())
|
||||||
|
end)
|
||||||
|
skynet.timeout(100, function()
|
||||||
|
print("in 100", skynet.now())
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user