From 4bdfc9f3b10b4466610a841ab6b388da24ff54c9 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sat, 21 Nov 2015 16:47:22 +0800 Subject: [PATCH] Triger timer only if there are packets received --- src/local.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/local.c b/src/local.c index 024a97c0..5ec57450 100644 --- a/src/local.c +++ b/src/local.c @@ -84,7 +84,6 @@ int verbose = 0; int vpn = 0; uint64_t tx = 0; uint64_t rx = 0; -ev_timer stat_timer; #endif static int acl = 0; @@ -584,9 +583,13 @@ static void server_send_cb(EV_P_ ev_io *w, int revents) } #ifdef ANDROID -static void stat_update_cb(EV_P_ ev_timer *watcher, int revents) +static void stat_update_cb(struct ev_loop *loop) { - send_traffic_stat(tx, rx); + static ev_tstamp last = ev_now(loop); + if (ev_now(EV_A_) - last > 1.0) { + send_traffic_stat(tx, rx); + last = ev_now(loop); + } } #endif @@ -613,6 +616,10 @@ static void remote_recv_cb(EV_P_ ev_io *w, int revents) ev_timer_again(EV_A_ & remote->recv_ctx->watcher); +#ifdef ANDROID + stat_update_cb(loop); +#endif + ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0); if (r == 0) { @@ -1154,11 +1161,6 @@ int main(int argc, char **argv) struct ev_loop *loop = EV_DEFAULT; -#ifdef ANDROID - ev_timer_init(&stat_timer, stat_update_cb, 1, 1); - ev_timer_start(loop, &stat_timer); -#endif - // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); @@ -1201,10 +1203,6 @@ int main(int argc, char **argv) // Clean up -#ifdef ANDROID - ev_timer_stop(loop, &stat_timer); -#endif - ev_io_stop(loop, &listen_ctx.io); free_connections(loop);