Commit 81d977c7 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by rojer

Introduce mg_time() and use it internally

    PUBLISHED_FROM=024a2d7849a800381d58460877eacccfc93b1cdd
parent f477898c
...@@ -2161,7 +2161,7 @@ MG_INTERNAL struct mg_connection *mg_create_connection( ...@@ -2161,7 +2161,7 @@ MG_INTERNAL struct mg_connection *mg_create_connection(
conn->sock = INVALID_SOCKET; conn->sock = INVALID_SOCKET;
conn->handler = callback; conn->handler = callback;
conn->mgr = mgr; conn->mgr = mgr;
conn->last_io_time = cs_time(); conn->last_io_time = mg_time();
conn->flags = opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK; conn->flags = opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK;
conn->user_data = opts.user_data; conn->user_data = opts.user_data;
/* /*
...@@ -2460,7 +2460,7 @@ MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) { ...@@ -2460,7 +2460,7 @@ MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) {
} }
void mg_send(struct mg_connection *nc, const void *buf, int len) { void mg_send(struct mg_connection *nc, const void *buf, int len) {
nc->last_io_time = cs_time(); nc->last_io_time = mg_time();
if (nc->flags & MG_F_UDP) { if (nc->flags & MG_F_UDP) {
mg_if_udp_send(nc, buf, len); mg_if_udp_send(nc, buf, len);
} else { } else {
...@@ -2491,7 +2491,7 @@ static void mg_recv_common(struct mg_connection *nc, void *buf, int len) { ...@@ -2491,7 +2491,7 @@ static void mg_recv_common(struct mg_connection *nc, void *buf, int len) {
MG_FREE(buf); MG_FREE(buf);
return; return;
} }
nc->last_io_time = cs_time(); nc->last_io_time = mg_time();
if (nc->recv_mbuf.len == 0) { if (nc->recv_mbuf.len == 0) {
/* Adopt buf as recv_mbuf's backing store. */ /* Adopt buf as recv_mbuf's backing store. */
mbuf_free(&nc->recv_mbuf); mbuf_free(&nc->recv_mbuf);
...@@ -2618,7 +2618,7 @@ static void resolve_cb(struct mg_dns_message *msg, void *data, ...@@ -2618,7 +2618,7 @@ static void resolve_cb(struct mg_dns_message *msg, void *data,
} }
if (e == MG_RESOLVE_TIMEOUT) { if (e == MG_RESOLVE_TIMEOUT) {
double now = cs_time(); double now = mg_time();
mg_call(nc, NULL, MG_EV_TIMER, &now); mg_call(nc, NULL, MG_EV_TIMER, &now);
} }
...@@ -2832,6 +2832,10 @@ double mg_set_timer(struct mg_connection *c, double timestamp) { ...@@ -2832,6 +2832,10 @@ double mg_set_timer(struct mg_connection *c, double timestamp) {
} }
return result; return result;
} }
double mg_time() {
return cs_time();
}
#ifdef NS_MODULE_LINES #ifdef NS_MODULE_LINES
#line 1 "./src/net_if_socket.c" #line 1 "./src/net_if_socket.c"
/**/ /**/
...@@ -2840,7 +2844,6 @@ double mg_set_timer(struct mg_connection *c, double timestamp) { ...@@ -2840,7 +2844,6 @@ double mg_set_timer(struct mg_connection *c, double timestamp) {
/* Amalgamated: #include "mongoose/src/internal.h" */ /* Amalgamated: #include "mongoose/src/internal.h" */
/* Amalgamated: #include "mongoose/src/util.h" */ /* Amalgamated: #include "mongoose/src/util.h" */
/* Amalgamated: #include "common/cs_time.h" */
#define MG_TCP_RECV_BUFFER_SIZE 1024 #define MG_TCP_RECV_BUFFER_SIZE 1024
#define MG_UDP_RECV_BUFFER_SIZE 1500 #define MG_UDP_RECV_BUFFER_SIZE 1500
...@@ -3420,7 +3423,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms) { ...@@ -3420,7 +3423,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int timeout_ms) {
double now; double now;
num_ev = epoll_wait(epoll_fd, events, MG_EPOLL_MAX_EVENTS, timeout_ms); num_ev = epoll_wait(epoll_fd, events, MG_EPOLL_MAX_EVENTS, timeout_ms);
now = cs_time(); now = mg_time();
DBG(("epoll_wait @ %ld num_ev=%d", (long) now, num_ev)); DBG(("epoll_wait @ %ld num_ev=%d", (long) now, num_ev));
while (num_ev-- > 0) { while (num_ev-- > 0) {
...@@ -3501,7 +3504,7 @@ void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) { ...@@ -3501,7 +3504,7 @@ void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) {
} }
time_t mg_mgr_poll(struct mg_mgr *mgr, int milli) { time_t mg_mgr_poll(struct mg_mgr *mgr, int milli) {
double now = cs_time(); double now = mg_time();
struct mg_connection *nc, *tmp; struct mg_connection *nc, *tmp;
struct timeval tv; struct timeval tv;
fd_set read_set, write_set, err_set; fd_set read_set, write_set, err_set;
...@@ -3542,7 +3545,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int milli) { ...@@ -3542,7 +3545,7 @@ time_t mg_mgr_poll(struct mg_mgr *mgr, int milli) {
tv.tv_usec = (milli % 1000) * 1000; tv.tv_usec = (milli % 1000) * 1000;
num_selected = select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv); num_selected = select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv);
now = cs_time(); now = mg_time();
DBG(("select @ %ld num_ev=%d of %d", (long) now, num_selected, num_fds)); DBG(("select @ %ld num_ev=%d of %d", (long) now, num_selected, num_fds));
#ifndef MG_DISABLE_SOCKETPAIR #ifndef MG_DISABLE_SOCKETPAIR
......
...@@ -1119,7 +1119,7 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7, ...@@ -1119,7 +1119,7 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
* *
* ``` * ```
* c = mg_connect(&mgr, "cesanta.com", ev_handler); * c = mg_connect(&mgr, "cesanta.com", ev_handler);
* mg_set_timer(c, time(NULL) + 1.5); * mg_set_timer(c, mg_time() + 1.5);
* ... * ...
* *
* void ev_handler(struct mg_connection *c, int ev, void *ev_data) { * void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
...@@ -1131,13 +1131,14 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7, ...@@ -1131,13 +1131,14 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
* log("Connect timeout"); * log("Connect timeout");
* c->flags |= MG_F_CLOSE_IMMEDIATELY; * c->flags |= MG_F_CLOSE_IMMEDIATELY;
* break; * break;
```
*
* NOTE: sub-second precision is not implemented yet, current granularity
* is 1 second.
*/ */
double mg_set_timer(struct mg_connection *c, double timestamp); double mg_set_timer(struct mg_connection *c, double timestamp);
/*
* A sub-second precision version of time().
*/
double mg_time();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment