Commit 31323b01 authored by Sergey Lyubka's avatar Sergey Lyubka Committed by rojer

Introduce MG_EV_TIMER event

    PUBLISHED_FROM=2e90ee87eda8bc742e427671bb9df1ce0cd8bc25
parent 3a138ecc
......@@ -1891,6 +1891,20 @@ MG_INTERNAL void mg_call(struct mg_connection *nc,
(int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
}
void mg_if_timer(struct mg_connection *c, time_t now) {
if (c->ev_timer_time > 0 && now >= c->ev_timer_time) {
double dnow = now, old_value = c->ev_timer_time;
mg_call(c, NULL, MG_EV_TIMER, &dnow);
/*
* To prevent timer firing all the time, reset the timer after delivery.
* However, in case user sets it to new value, do not reset.
*/
if (c->ev_timer_time == old_value) {
c->ev_timer_time = 0;
}
}
}
void mg_if_poll(struct mg_connection *nc, time_t now) {
mg_call(nc, NULL, MG_EV_POLL, &now);
}
......@@ -3115,6 +3129,7 @@ void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, time_t now) {
if (!(fd_flags & (_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE))) {
mg_if_poll(nc, now);
}
mg_if_timer(nc, now);
DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags,
(int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
......
......@@ -698,6 +698,7 @@ typedef void (*mg_event_handler_t)(struct mg_connection *, int ev, void *);
#define MG_EV_RECV 3 /* Data has benn received. int *num_bytes */
#define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */
#define MG_EV_CLOSE 5 /* Connection is closed. NULL */
#define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */
/*
* Mongoose event manager.
......@@ -730,6 +731,7 @@ struct mg_connection {
SSL *ssl;
SSL_CTX *ssl_ctx;
time_t last_io_time; /* Timestamp of the last socket IO */
double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */
mg_event_handler_t proto_handler; /* Protocol-specific event handler */
void *proto_data; /* Protocol-specific data */
mg_event_handler_t handler; /* Event handler function */
......@@ -1116,6 +1118,9 @@ void mg_if_recved(struct mg_connection *nc, size_t len);
/* Deliver a POLL event to the connection. */
void mg_if_poll(struct mg_connection *nc, time_t now);
/* Deliver a TIMER event to the connection. */
void mg_if_timer(struct mg_connection *c, time_t now);
/* Perform interface-related cleanup on connection before destruction. */
void mg_if_destroy_conn(struct mg_connection *nc);
......
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