add skynet.time()

This commit is contained in:
Cloud Wu
2014-06-25 11:15:28 +08:00
parent b1d6f7fd88
commit 36cab8e060
3 changed files with 27 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ Dev version
* socket.listen support put port into address. (address:port)
* Redesign harbor/master/dummy, remove lots of C code and rewite in lua.
* Remove block connect api, queue sending message during connecting now.
* Add skynet.time()
v0.3.2 (2014-6-23)
----------

View File

@@ -247,6 +247,10 @@ function skynet.starttime()
return tonumber(c.command("STARTTIME"))
end
function skynet.time()
return skynet.starttime() + skynet.now()/100
end
function skynet.exit()
skynet.send(".launcher","lua","REMOVE",skynet.self())
c.command("EXIT")

View File

@@ -222,7 +222,7 @@ skynet_timeout(uint32_t handle, int time, int session) {
// centisecond: 1/100 second
static void
_systime(uint32_t *sec, uint32_t *cs) {
systime(uint32_t *sec, uint32_t *cs) {
#if !defined(__APPLE__)
struct timespec ti;
clock_gettime(CLOCK_REALTIME, &ti);
@@ -237,33 +237,44 @@ _systime(uint32_t *sec, uint32_t *cs) {
}
static uint64_t
_gettime() {
gettime() {
uint64_t t;
#if !defined(__APPLE__)
#ifdef CLOCK_MONOTONIC_RAW
#define CLOCK_TIMER CLOCK_MONOTONIC_RAW
#else
#define CLOCK_TIMER CLOCK_MONOTONIC
#endif
struct timespec ti;
clock_gettime(CLOCK_MONOTONIC, &ti);
t = ti.tv_sec * 100;
t += (uint32_t)(ti.tv_nsec / 10000000);
clock_gettime(CLOCK_TIMER, &ti);
t = (uint64_t)ti.tv_sec * 100;
t += ti.tv_nsec / 10000000;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
t = ti.tv_sec * 100;
t += (uint32_t)(tv.tv_usec / 10000);
t = (uint64_t)ti.tv_sec * 100;
t += tv.tv_usec / 10000;
#endif
return t;
}
void
skynet_updatetime(void) {
uint64_t cp = _gettime();
uint64_t cp = gettime();
if(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) {
uint32_t diff = (uint32_t)(cp - TI->current_point);
TI->current_point = cp;
// when cs > 0xffffffff(about 49710), time rewind
uint32_t oc = TI->current;
TI->current += diff;
if (TI->current < oc) {
// when cs > 0xffffffff(about 497 days), time rewind
TI->starttime += 0xffffffff / 100;
}
int i;
for (i=0;i<diff;i++) {
timer_update(TI);
@@ -284,8 +295,8 @@ skynet_gettime(void) {
void
skynet_timer_init(void) {
TI = timer_create_timer();
_systime(&TI->starttime, &TI->current);
uint64_t point = _gettime();
systime(&TI->starttime, &TI->current);
uint64_t point = gettime();
TI->current_point = point;
TI->origin_point = point;
}