mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-18 10:59:45 +00:00
Add mg_now() to sntp.c
This commit is contained in:
1 parent
6357b30712
commit
22de1dc0f2
6 files changed
+60
-114
No files matched your search
+27
-12
@@ -7,6 +7,12 @@
|
||||
#define SNTP_TIME_OFFSET 2208988800U // (1970 - 1900) in seconds
|
||||
#define SNTP_MAX_FRAC 4294967295.0 // 2 ** 32 - 1
|
||||
|
||||
static uint64_t s_boot_timestamp = 0; // Updated by SNTP
|
||||
|
||||
uint64_t mg_now(void) {
|
||||
return mg_millis() + s_boot_timestamp;
|
||||
}
|
||||
|
||||
static int64_t gettimestamp(const uint32_t *data) {
|
||||
uint32_t sec = mg_ntohl(data[0]), frac = mg_ntohl(data[1]);
|
||||
if (sec) sec -= SNTP_TIME_OFFSET;
|
||||
@@ -29,8 +35,9 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t receive_time = gettimestamp((uint32_t *) &buf[32]);
|
||||
int64_t transmit_time = gettimestamp((uint32_t *) &buf[40]);
|
||||
int64_t now = (int64_t) mg_millis();
|
||||
int64_t travel_time = (now - origin_time) - (transmit_time - receive_time);
|
||||
epoch_milliseconds = transmit_time + travel_time / 2;
|
||||
int64_t latency = (now - origin_time) - (transmit_time - receive_time);
|
||||
epoch_milliseconds = transmit_time + latency / 2;
|
||||
s_boot_timestamp = (uint64_t) (epoch_milliseconds - now);
|
||||
} else {
|
||||
MG_ERROR(("unexpected version: %d", version));
|
||||
}
|
||||
@@ -38,17 +45,22 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
}
|
||||
|
||||
static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_READ) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
MG_DEBUG(("%lu got time: %lld ms from epoch", c->id, milliseconds));
|
||||
mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds);
|
||||
MG_VERBOSE(("%u.%u", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000)));
|
||||
}
|
||||
mg_iobuf_del(&c->recv, 0, c->recv.len); // Free receive buffer
|
||||
uint64_t *expiration_time = (uint64_t *) c->data;
|
||||
if (ev == MG_EV_OPEN) {
|
||||
*expiration_time = mg_millis() + 3000; // Store expiration time in 3s
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
mg_sntp_request(c);
|
||||
} else if (ev == MG_EV_READ) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
s_boot_timestamp = (uint64_t) milliseconds - mg_millis();
|
||||
mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds);
|
||||
MG_DEBUG(("%lu got time: %lld ms from epoch", c->id, milliseconds));
|
||||
}
|
||||
// mg_iobuf_del(&c->recv, 0, c->recv.len); // Free receive buffer
|
||||
c->is_closing = 1;
|
||||
} else if (ev == MG_EV_POLL) {
|
||||
if (mg_millis() > *expiration_time) c->is_closing = 1;
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
}
|
||||
(void) ev_data;
|
||||
@@ -73,6 +85,9 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fnd) {
|
||||
struct mg_connection *c = NULL;
|
||||
if (url == NULL) url = "udp://time.google.com:123";
|
||||
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) c->pfn = sntp_cb;
|
||||
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) {
|
||||
c->pfn = sntp_cb;
|
||||
sntp_cb(c, MG_EV_OPEN, (void *) url);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
Reference in new issue
Block a user