Use uint64_t for uptime in millis, not int64_t

This commit is contained in:
Sergey Lyubka 2022-04-07 13:50:25 +01:00
parent 2f77855ec1
commit a1ec179229
11 changed files with 45 additions and 46 deletions

View File

@ -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

View File

@ -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

View File

@ -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
};

View File

@ -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,

View File

@ -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
};

View File

@ -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)));
}

View File

@ -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();

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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)