mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-15 10:29:47 +00:00
Use int64_t for timers and mg_millis()
This commit is contained in:
1 parent
fb0a9bc7e3
commit
ae6767b1d2
15 files changed
+123
-118
No files matched your search
+6
-4
@@ -89,13 +89,15 @@ typedef int socklen_t;
|
||||
|
||||
// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
|
||||
static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
|
||||
(void) tm;
|
||||
return gmtime(t);
|
||||
struct tm *x = gmtime(t);
|
||||
*tm = *x;
|
||||
return tm;
|
||||
}
|
||||
|
||||
static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
|
||||
(void) tm;
|
||||
return localtime(t);
|
||||
struct tm *x = localtime(t);
|
||||
*tm = *x;
|
||||
return tm;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,7 +7,7 @@
|
||||
struct dns_data {
|
||||
struct dns_data *next;
|
||||
struct mg_connection *c;
|
||||
unsigned long expire;
|
||||
int64_t expire;
|
||||
uint16_t txnid;
|
||||
};
|
||||
|
||||
@@ -132,7 +132,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
|
||||
void *fn_data) {
|
||||
struct dns_data *d, *tmp;
|
||||
if (ev == MG_EV_POLL) {
|
||||
unsigned long now = *(unsigned long *) ev_data;
|
||||
int64_t now = *(int64_t *) ev_data;
|
||||
for (d = s_reqs; d != NULL; d = tmp) {
|
||||
tmp = d->next;
|
||||
// LOG(LL_DEBUG, ("%lu %lu dns poll", d->expire, now));
|
||||
@@ -246,7 +246,7 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
||||
d->txnid = s_reqs ? (uint16_t) (s_reqs->txnid + 1) : 1;
|
||||
d->next = s_reqs;
|
||||
s_reqs = d;
|
||||
d->expire = mg_millis() + (unsigned long) ms;
|
||||
d->expire = mg_millis() + (int64_t) ms;
|
||||
d->c = c;
|
||||
c->is_resolving = 1;
|
||||
LOG(LL_VERBOSE_DEBUG,
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ void mg_error(struct mg_connection *c, const char *fmt, ...);
|
||||
enum {
|
||||
MG_EV_ERROR, // Error char *error_message
|
||||
MG_EV_OPEN, // Connection created NULL
|
||||
MG_EV_POLL, // mg_mgr_poll iteration unsigned long *millis
|
||||
MG_EV_POLL, // mg_mgr_poll iteration int64_t *milliseconds
|
||||
MG_EV_RESOLVE, // Host name is resolved NULL
|
||||
MG_EV_CONNECT, // Connection established NULL
|
||||
MG_EV_ACCEPT, // Connection accepted NULL
|
||||
@@ -24,6 +24,6 @@ enum {
|
||||
MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message *
|
||||
MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message *
|
||||
MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code
|
||||
MG_EV_SNTP_TIME, // SNTP time received struct timeval *
|
||||
MG_EV_SNTP_TIME, // SNTP time received int64_t *milliseconds
|
||||
MG_EV_USER, // Starting ID for user events
|
||||
};
|
||||
+14
-12
@@ -4,13 +4,14 @@
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#define SNTP_INTERVAL_SEC (3600)
|
||||
#define SNTP_INTERVAL_SEC 3600
|
||||
#define SNTP_TIME_OFFSET 2208988800UL
|
||||
|
||||
static unsigned long s_sntmp_next;
|
||||
|
||||
int mg_sntp_parse(const unsigned char *buf, size_t len, struct timeval *tv) {
|
||||
int mode = len > 0 ? buf[0] & 7 : 0, res = -1;
|
||||
int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
int64_t res = -1;
|
||||
int mode = len > 0 ? buf[0] & 7 : 0;
|
||||
if (len < 48) {
|
||||
LOG(LL_ERROR, ("%s", "corrupt packet"));
|
||||
} else if ((buf[0] & 0x38) >> 3 != 4) {
|
||||
@@ -21,21 +22,22 @@ int mg_sntp_parse(const unsigned char *buf, size_t len, struct timeval *tv) {
|
||||
LOG(LL_ERROR, ("%s", "server sent a kiss of death"));
|
||||
} else {
|
||||
uint32_t *data = (uint32_t *) &buf[40];
|
||||
tv->tv_sec = (time_t) (mg_ntohl(data[0]) - SNTP_TIME_OFFSET);
|
||||
tv->tv_usec = (suseconds_t) mg_ntohl(data[1]);
|
||||
s_sntmp_next = (unsigned long) (tv->tv_sec + SNTP_INTERVAL_SEC);
|
||||
res = 0;
|
||||
unsigned long seconds = mg_ntohl(data[0]) - SNTP_TIME_OFFSET;
|
||||
unsigned long useconds = mg_ntohl(data[1]);
|
||||
// LOG(LL_DEBUG, ("%lu %lu %lu", time(0), seconds, useconds));
|
||||
res = ((int64_t) seconds) * 1000 + (int64_t) ((useconds / 1000) % 1000);
|
||||
s_sntmp_next = seconds + SNTP_INTERVAL_SEC;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
|
||||
if (ev == MG_EV_READ) {
|
||||
struct timeval tv = {0, 0};
|
||||
if (mg_sntp_parse(c->recv.buf, c->recv.len, &tv) == 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &tv);
|
||||
LOG(LL_DEBUG, ("%u.%u, next at %lu", (unsigned) tv.tv_sec,
|
||||
(unsigned) tv.tv_usec, s_sntmp_next));
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
if (milliseconds > 0) {
|
||||
mg_call(c, MG_EV_SNTP_TIME, &milliseconds);
|
||||
LOG(LL_DEBUG, ("%u.%u, next at %lu", (unsigned) (milliseconds / 1000),
|
||||
(unsigned) (milliseconds % 1000), s_sntmp_next));
|
||||
}
|
||||
c->recv.len = 0; // Clear receive buffer
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
|
||||
+1
-1
@@ -5,4 +5,4 @@
|
||||
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data);
|
||||
void mg_sntp_send(struct mg_connection *c, unsigned long utc);
|
||||
int mg_sntp_parse(const unsigned char *buf, size_t len, struct timeval *tv);
|
||||
int64_t mg_sntp_parse(const unsigned char *buf, size_t len);
|
||||
+1
-1
@@ -572,7 +572,7 @@ static void connect_conn(struct mg_connection *c) {
|
||||
|
||||
void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
|
||||
struct mg_connection *c, *tmp;
|
||||
unsigned long now;
|
||||
int64_t now;
|
||||
|
||||
mg_iotest(mgr, ms);
|
||||
now = mg_millis();
|
||||
|
||||
+6
-7
@@ -6,7 +6,7 @@
|
||||
|
||||
struct mg_timer *g_timers;
|
||||
|
||||
void mg_timer_init(struct mg_timer *t, unsigned long ms, unsigned flags,
|
||||
void mg_timer_init(struct mg_timer *t, int64_t ms, unsigned flags,
|
||||
void (*fn)(void *), void *arg) {
|
||||
struct mg_timer tmp = {ms, 0UL, flags, fn, arg, g_timers};
|
||||
*t = tmp;
|
||||
@@ -20,11 +20,11 @@ void mg_timer_free(struct mg_timer *t) {
|
||||
if (*head) *head = t->next;
|
||||
}
|
||||
|
||||
void mg_timer_poll(unsigned long now_ms) {
|
||||
void mg_timer_poll(int64_t now_ms) {
|
||||
// If time goes back (wrapped around), reset timers
|
||||
struct mg_timer *t, *tmp;
|
||||
static unsigned long oldnow; // Timestamp in a previous invocation
|
||||
if (oldnow > now_ms) { // If it is wrapped, reset timers
|
||||
static int64_t oldnow; // Timestamp in a previous invocation
|
||||
if (oldnow > now_ms) { // If it is wrapped, reset timers
|
||||
for (t = g_timers; t != NULL; t = t->next) t->expire = 0;
|
||||
}
|
||||
oldnow = now_ms;
|
||||
@@ -36,9 +36,8 @@ void mg_timer_poll(unsigned long now_ms) {
|
||||
t->fn(t->arg);
|
||||
// Try to tick timers with the given period as accurate as possible,
|
||||
// even if this polling function is called with some random period.
|
||||
t->expire = now_ms - t->expire > (unsigned long) t->period_ms
|
||||
? now_ms + t->period_ms
|
||||
: t->expire + t->period_ms;
|
||||
t->expire = now_ms - t->expire > t->period_ms ? now_ms + t->period_ms
|
||||
: t->expire + t->period_ms;
|
||||
if (!(t->flags & MG_TIMER_REPEAT)) mg_timer_free(t);
|
||||
}
|
||||
}
|
||||
+7
-5
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "arch.h"
|
||||
|
||||
struct mg_timer {
|
||||
unsigned long period_ms; // Timer period in milliseconds
|
||||
unsigned long expire; // Expiration timestamp in milliseconds
|
||||
int64_t period_ms; // Timer period in milliseconds
|
||||
int64_t expire; // Expiration timestamp in milliseconds
|
||||
unsigned flags; // Possible flags values below
|
||||
#define MG_TIMER_REPEAT 1 // Call function periodically, otherwise run once
|
||||
#define MG_TIMER_RUN_NOW 2 // Call immediately when timer is set
|
||||
@@ -13,7 +15,7 @@ struct mg_timer {
|
||||
|
||||
extern struct mg_timer *g_timers; // Global list of timers
|
||||
|
||||
void mg_timer_init(struct mg_timer *, unsigned long ms, unsigned,
|
||||
void (*fn)(void *), void *);
|
||||
void mg_timer_init(struct mg_timer *, int64_t, unsigned, void (*)(void *),
|
||||
void *);
|
||||
void mg_timer_free(struct mg_timer *);
|
||||
void mg_timer_poll(unsigned long current_time_ms);
|
||||
void mg_timer_poll(int64_t current_time_ms);
|
||||
+3
-4
@@ -299,7 +299,7 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) {
|
||||
return allowed == '+';
|
||||
}
|
||||
|
||||
unsigned long mg_millis(void) {
|
||||
int64_t mg_millis(void) {
|
||||
#if MG_ARCH == MG_ARCH_WIN32
|
||||
return GetTickCount();
|
||||
#elif MG_ARCH == MG_ARCH_ESP32
|
||||
@@ -316,7 +316,7 @@ unsigned long mg_millis(void) {
|
||||
mach_timebase_info(&timebase);
|
||||
double ticks_to_nanos = (double) timebase.numer / timebase.denom;
|
||||
uint64_t uptime_nanos = (uint64_t) (ticks_to_nanos * ticks);
|
||||
return (unsigned long) (uptime_nanos / 1000000);
|
||||
return (int64_t) (uptime_nanos / 1000000);
|
||||
#else
|
||||
struct timespec ts;
|
||||
#ifdef _POSIX_MONOTONIC_CLOCK
|
||||
@@ -328,7 +328,6 @@ unsigned long mg_millis(void) {
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
#endif
|
||||
return (unsigned long) ((uint64_t) ts.tv_sec * 1000 +
|
||||
(uint64_t) ts.tv_nsec / 1000000);
|
||||
return ((int64_t) ts.tv_sec * 1000 + (int64_t) ts.tv_nsec / 1000000);
|
||||
#endif
|
||||
}
|
||||
+2
-2
@@ -19,9 +19,9 @@ void mg_unhex(const char *buf, size_t len, unsigned char *to);
|
||||
unsigned long mg_unhexn(const char *s, size_t len);
|
||||
int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
|
||||
int mg_vasprintf(char **buf, size_t size, const char *fmt, va_list ap);
|
||||
int64_t mg_to64(struct mg_str str);
|
||||
int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip);
|
||||
unsigned long mg_millis(void);
|
||||
int64_t mg_to64(struct mg_str str);
|
||||
int64_t mg_millis(void);
|
||||
|
||||
#define mg_htons(x) mg_ntohs(x)
|
||||
#define mg_htonl(x) mg_ntohl(x)
|
||||
|
||||
Reference in new issue
Block a user