mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
add skynet.time()
This commit is contained in:
@@ -7,6 +7,7 @@ Dev version
|
|||||||
* socket.listen support put port into address. (address:port)
|
* socket.listen support put port into address. (address:port)
|
||||||
* Redesign harbor/master/dummy, remove lots of C code and rewite in lua.
|
* Redesign harbor/master/dummy, remove lots of C code and rewite in lua.
|
||||||
* Remove block connect api, queue sending message during connecting now.
|
* Remove block connect api, queue sending message during connecting now.
|
||||||
|
* Add skynet.time()
|
||||||
|
|
||||||
v0.3.2 (2014-6-23)
|
v0.3.2 (2014-6-23)
|
||||||
----------
|
----------
|
||||||
|
|||||||
@@ -247,6 +247,10 @@ function skynet.starttime()
|
|||||||
return tonumber(c.command("STARTTIME"))
|
return tonumber(c.command("STARTTIME"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function skynet.time()
|
||||||
|
return skynet.starttime() + skynet.now()/100
|
||||||
|
end
|
||||||
|
|
||||||
function skynet.exit()
|
function skynet.exit()
|
||||||
skynet.send(".launcher","lua","REMOVE",skynet.self())
|
skynet.send(".launcher","lua","REMOVE",skynet.self())
|
||||||
c.command("EXIT")
|
c.command("EXIT")
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ skynet_timeout(uint32_t handle, int time, int session) {
|
|||||||
|
|
||||||
// centisecond: 1/100 second
|
// centisecond: 1/100 second
|
||||||
static void
|
static void
|
||||||
_systime(uint32_t *sec, uint32_t *cs) {
|
systime(uint32_t *sec, uint32_t *cs) {
|
||||||
#if !defined(__APPLE__)
|
#if !defined(__APPLE__)
|
||||||
struct timespec ti;
|
struct timespec ti;
|
||||||
clock_gettime(CLOCK_REALTIME, &ti);
|
clock_gettime(CLOCK_REALTIME, &ti);
|
||||||
@@ -237,33 +237,44 @@ _systime(uint32_t *sec, uint32_t *cs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t
|
static uint64_t
|
||||||
_gettime() {
|
gettime() {
|
||||||
uint64_t t;
|
uint64_t t;
|
||||||
#if !defined(__APPLE__)
|
#if !defined(__APPLE__)
|
||||||
|
|
||||||
|
#ifdef CLOCK_MONOTONIC_RAW
|
||||||
|
#define CLOCK_TIMER CLOCK_MONOTONIC_RAW
|
||||||
|
#else
|
||||||
|
#define CLOCK_TIMER CLOCK_MONOTONIC
|
||||||
|
#endif
|
||||||
|
|
||||||
struct timespec ti;
|
struct timespec ti;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ti);
|
clock_gettime(CLOCK_TIMER, &ti);
|
||||||
t = ti.tv_sec * 100;
|
t = (uint64_t)ti.tv_sec * 100;
|
||||||
t += (uint32_t)(ti.tv_nsec / 10000000);
|
t += ti.tv_nsec / 10000000;
|
||||||
#else
|
#else
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
t = ti.tv_sec * 100;
|
t = (uint64_t)ti.tv_sec * 100;
|
||||||
t += (uint32_t)(tv.tv_usec / 10000);
|
t += tv.tv_usec / 10000;
|
||||||
#endif
|
#endif
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_updatetime(void) {
|
skynet_updatetime(void) {
|
||||||
uint64_t cp = _gettime();
|
uint64_t cp = gettime();
|
||||||
if(cp < TI->current_point) {
|
if(cp < TI->current_point) {
|
||||||
skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point);
|
skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point);
|
||||||
} else if (cp != TI->current_point) {
|
} else if (cp != TI->current_point) {
|
||||||
uint32_t diff = (uint32_t)(cp - TI->current_point);
|
uint32_t diff = (uint32_t)(cp - TI->current_point);
|
||||||
TI->current_point = cp;
|
TI->current_point = cp;
|
||||||
|
|
||||||
// when cs > 0xffffffff(about 49710), time rewind
|
uint32_t oc = TI->current;
|
||||||
TI->current += diff;
|
TI->current += diff;
|
||||||
|
if (TI->current < oc) {
|
||||||
|
// when cs > 0xffffffff(about 497 days), time rewind
|
||||||
|
TI->starttime += 0xffffffff / 100;
|
||||||
|
}
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<diff;i++) {
|
for (i=0;i<diff;i++) {
|
||||||
timer_update(TI);
|
timer_update(TI);
|
||||||
@@ -284,8 +295,8 @@ skynet_gettime(void) {
|
|||||||
void
|
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();
|
uint64_t point = gettime();
|
||||||
TI->current_point = point;
|
TI->current_point = point;
|
||||||
TI->origin_point = point;
|
TI->origin_point = point;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user