From a1ec179229717da3ac7f98b6a9e7cab9efb1be93 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 7 Apr 2022 13:50:25 +0100 Subject: [PATCH] Use uint64_t for uptime in millis, not int64_t --- docs/README.md | 15 +++++++-------- mongoose.c | 24 ++++++++++++------------ mongoose.h | 14 +++++++------- src/dns.c | 6 +++--- src/event.h | 4 ++-- src/sntp.c | 2 +- src/sock.c | 2 +- src/timer.c | 8 ++++---- src/timer.h | 8 ++++---- src/util.c | 6 +++--- src/util.h | 2 +- 11 files changed, 45 insertions(+), 46 deletions(-) diff --git a/docs/README.md b/docs/README.md index 07a27230..529e3713 100644 --- a/docs/README.md +++ b/docs/README.md @@ -134,7 +134,7 @@ to an event handler: enum { MG_EV_ERROR, // Error char *error_message MG_EV_OPEN, // Connection created NULL - MG_EV_POLL, // mg_mgr_poll iteration int64_t *milliseconds + MG_EV_POLL, // mg_mgr_poll iteration uint64_t *milliseconds MG_EV_RESOLVE, // Host name is resolved NULL MG_EV_CONNECT, // Connection established NULL MG_EV_ACCEPT, // Connection accepted NULL @@ -149,7 +149,7 @@ 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 int64_t *milliseconds + MG_EV_SNTP_TIME, // SNTP time received uint64_t *milliseconds MG_EV_USER, // Starting ID for user events }; ``` @@ -1839,8 +1839,8 @@ mg_tls_init(c, &opts); ```c struct mg_timer { - int64_t period_ms; // Timer period in milliseconds - int64_t expire; // Expiration timestamp in milliseconds + uint64_t period_ms; // Timer period in milliseconds + uint64_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 @@ -1856,7 +1856,7 @@ as the `mg_mgr_poll()` timeout argument in the main event loop. ### mg\_timer\_init() ```c -void mg_timer_init(struct mg_timer *t, int64_t period_ms, unsigned flags, +void mg_timer_init(struct mg_timer *t, uint64_t period_ms, unsigned flags, void (*fn)(void *), void *fn_data); ``` @@ -1904,7 +1904,7 @@ mg_timer_free(&timer); ### mg\_timer\_poll() ```c -void mg_timer_poll(int64_t uptime_ms); +void mg_timer_poll(uint64_t uptime_ms); ``` Traverse list of timers and call them if current timestamp `uptime_ms` is @@ -1921,8 +1921,7 @@ Return value: None Usage example: ```c -int64_t now = mg_millis(); -mg_timer_poll(now); +mg_timer_poll(mg_millis()); ``` ## Time diff --git a/mongoose.c b/mongoose.c index f947205b..c8c9b8ab 100644 --- a/mongoose.c +++ b/mongoose.c @@ -120,7 +120,7 @@ int mg_base64_decode(const char *src, int n, char *dst) { struct dns_data { struct dns_data *next; struct mg_connection *c; - int64_t expire; + uint64_t expire; uint16_t txnid; }; @@ -250,7 +250,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) { - int64_t now = *(int64_t *) ev_data; + uint64_t now = *(uint64_t *) ev_data; for (d = s_reqs; d != NULL; d = tmp) { tmp = d->next; // MG_DEBUG ("%lu %lu dns poll", d->expire, now)); @@ -352,7 +352,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() + (int64_t) ms; + d->expire = mg_millis() + (uint64_t) ms; d->c = c; c->is_resolving = 1; MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len, @@ -3086,7 +3086,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { if (ev == MG_EV_READ) { int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len); if (milliseconds > 0) { - mg_call(c, MG_EV_SNTP_TIME, &milliseconds); + mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds); MG_DEBUG(("%u.%u", (unsigned) (milliseconds / 1000), (unsigned) (milliseconds % 1000))); } @@ -3655,7 +3655,7 @@ static void connect_conn(struct mg_connection *c) { void mg_mgr_poll(struct mg_mgr *mgr, int ms) { struct mg_connection *c, *tmp; - int64_t now; + uint64_t now; mg_iotest(mgr, ms); now = mg_millis(); @@ -4179,7 +4179,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) { struct mg_timer *g_timers; -void mg_timer_init(struct mg_timer *t, int64_t ms, unsigned flags, +void mg_timer_init(struct mg_timer *t, uint64_t ms, unsigned flags, void (*fn)(void *), void *arg) { struct mg_timer tmp = {ms, 0UL, flags, fn, arg, g_timers}; *t = tmp; @@ -4193,11 +4193,11 @@ void mg_timer_free(struct mg_timer *t) { if (*head) *head = t->next; } -void mg_timer_poll(int64_t now_ms) { +void mg_timer_poll(uint64_t now_ms) { // If time goes back (wrapped around), reset timers struct mg_timer *t, *tmp; - static int64_t oldnow; // Timestamp in a previous invocation - if (oldnow > now_ms) { // If it is wrapped, reset timers + static uint64_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; @@ -4781,7 +4781,7 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { #if MG_ENABLE_CUSTOM_MILLIS #else -int64_t mg_millis(void) { +uint64_t mg_millis(void) { #if MG_ARCH == MG_ARCH_WIN32 return GetTickCount(); #elif MG_ARCH == MG_ARCH_ESP32 @@ -4798,7 +4798,7 @@ int64_t 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 (int64_t) (uptime_nanos / 1000000); + return (uint64_t) (uptime_nanos / 1000000); #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts; #ifdef _POSIX_MONOTONIC_CLOCK @@ -4810,7 +4810,7 @@ int64_t mg_millis(void) { #else clock_gettime(CLOCK_REALTIME, &ts); #endif - return ((int64_t) ts.tv_sec * 1000 + (int64_t) ts.tv_nsec / 1000000); + return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000); #else return time(NULL) * 1000; #endif diff --git a/mongoose.h b/mongoose.h index b036a211..d79c26c7 100644 --- a/mongoose.h +++ b/mongoose.h @@ -673,8 +673,8 @@ void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *param); struct mg_timer { - int64_t period_ms; // Timer period in milliseconds - int64_t expire; // Expiration timestamp in milliseconds + uint64_t period_ms; // Timer period in milliseconds + uint64_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 @@ -685,10 +685,10 @@ struct mg_timer { extern struct mg_timer *g_timers; // Global list of timers -void mg_timer_init(struct mg_timer *, int64_t, unsigned, void (*)(void *), +void mg_timer_init(struct mg_timer *, uint64_t, unsigned, void (*)(void *), void *); void mg_timer_free(struct mg_timer *); -void mg_timer_poll(int64_t current_time_ms); +void mg_timer_poll(uint64_t current_time_ms); @@ -741,7 +741,7 @@ void mg_random(void *buf, size_t len); uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len); -int64_t mg_millis(void); +uint64_t mg_millis(void); #define mg_htons(x) mg_ntohs(x) #define mg_htonl(x) mg_ntohl(x) @@ -852,7 +852,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 int64_t *milliseconds + MG_EV_POLL, // mg_mgr_poll iteration uint64_t *milliseconds MG_EV_RESOLVE, // Host name is resolved NULL MG_EV_CONNECT, // Connection established NULL MG_EV_ACCEPT, // Connection accepted NULL @@ -867,7 +867,7 @@ 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 int64_t *milliseconds + MG_EV_SNTP_TIME, // SNTP time received uint64_t *milliseconds MG_EV_USER, // Starting ID for user events }; diff --git a/src/dns.c b/src/dns.c index e7fcfae7..4e5a05a1 100644 --- a/src/dns.c +++ b/src/dns.c @@ -8,7 +8,7 @@ struct dns_data { struct dns_data *next; struct mg_connection *c; - int64_t expire; + uint64_t expire; uint16_t txnid; }; @@ -138,7 +138,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) { - int64_t now = *(int64_t *) ev_data; + uint64_t now = *(uint64_t *) ev_data; for (d = s_reqs; d != NULL; d = tmp) { tmp = d->next; // MG_DEBUG ("%lu %lu dns poll", d->expire, now)); @@ -240,7 +240,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() + (int64_t) ms; + d->expire = mg_millis() + (uint64_t) ms; d->c = c; c->is_resolving = 1; MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len, diff --git a/src/event.h b/src/event.h index 8578ab0b..19644e26 100644 --- a/src/event.h +++ b/src/event.h @@ -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 int64_t *milliseconds + MG_EV_POLL, // mg_mgr_poll iteration uint64_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 int64_t *milliseconds + MG_EV_SNTP_TIME, // SNTP time received uint64_t *milliseconds MG_EV_USER, // Starting ID for user events }; diff --git a/src/sntp.c b/src/sntp.c index 57454c26..a528234e 100644 --- a/src/sntp.c +++ b/src/sntp.c @@ -32,7 +32,7 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { if (ev == MG_EV_READ) { int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len); if (milliseconds > 0) { - mg_call(c, MG_EV_SNTP_TIME, &milliseconds); + mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds); MG_DEBUG(("%u.%u", (unsigned) (milliseconds / 1000), (unsigned) (milliseconds % 1000))); } diff --git a/src/sock.c b/src/sock.c index 49cdf339..a416535e 100644 --- a/src/sock.c +++ b/src/sock.c @@ -532,7 +532,7 @@ static void connect_conn(struct mg_connection *c) { void mg_mgr_poll(struct mg_mgr *mgr, int ms) { struct mg_connection *c, *tmp; - int64_t now; + uint64_t now; mg_iotest(mgr, ms); now = mg_millis(); diff --git a/src/timer.c b/src/timer.c index 8c1effb4..387f583b 100644 --- a/src/timer.c +++ b/src/timer.c @@ -6,7 +6,7 @@ struct mg_timer *g_timers; -void mg_timer_init(struct mg_timer *t, int64_t ms, unsigned flags, +void mg_timer_init(struct mg_timer *t, uint64_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(int64_t now_ms) { +void mg_timer_poll(uint64_t now_ms) { // If time goes back (wrapped around), reset timers struct mg_timer *t, *tmp; - static int64_t oldnow; // Timestamp in a previous invocation - if (oldnow > now_ms) { // If it is wrapped, reset timers + static uint64_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; diff --git a/src/timer.h b/src/timer.h index 62e4a5b6..18c4803e 100644 --- a/src/timer.h +++ b/src/timer.h @@ -3,8 +3,8 @@ #include "arch.h" struct mg_timer { - int64_t period_ms; // Timer period in milliseconds - int64_t expire; // Expiration timestamp in milliseconds + uint64_t period_ms; // Timer period in milliseconds + uint64_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 @@ -15,7 +15,7 @@ struct mg_timer { extern struct mg_timer *g_timers; // Global list of timers -void mg_timer_init(struct mg_timer *, int64_t, unsigned, void (*)(void *), +void mg_timer_init(struct mg_timer *, uint64_t, unsigned, void (*)(void *), void *); void mg_timer_free(struct mg_timer *); -void mg_timer_poll(int64_t current_time_ms); +void mg_timer_poll(uint64_t current_time_ms); diff --git a/src/util.c b/src/util.c index 88417068..abaaabef 100644 --- a/src/util.c +++ b/src/util.c @@ -80,7 +80,7 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { #if MG_ENABLE_CUSTOM_MILLIS #else -int64_t mg_millis(void) { +uint64_t mg_millis(void) { #if MG_ARCH == MG_ARCH_WIN32 return GetTickCount(); #elif MG_ARCH == MG_ARCH_ESP32 @@ -97,7 +97,7 @@ int64_t 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 (int64_t) (uptime_nanos / 1000000); + return (uint64_t) (uptime_nanos / 1000000); #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts; #ifdef _POSIX_MONOTONIC_CLOCK @@ -109,7 +109,7 @@ int64_t mg_millis(void) { #else clock_gettime(CLOCK_REALTIME, &ts); #endif - return ((int64_t) ts.tv_sec * 1000 + (int64_t) ts.tv_nsec / 1000000); + return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000); #else return time(NULL) * 1000; #endif diff --git a/src/util.h b/src/util.h index bffb7d03..b05e842d 100644 --- a/src/util.h +++ b/src/util.h @@ -8,7 +8,7 @@ void mg_random(void *buf, size_t len); uint16_t mg_ntohs(uint16_t net); uint32_t mg_ntohl(uint32_t net); uint32_t mg_crc32(uint32_t crc, const char *buf, size_t len); -int64_t mg_millis(void); +uint64_t mg_millis(void); #define mg_htons(x) mg_ntohs(x) #define mg_htonl(x) mg_ntohl(x)