Browse Source

Triger timer only if there are packets received

pull/465/head
Max Lv 9 years ago
parent
commit
4bdfc9f3b1
1 changed files with 10 additions and 12 deletions
  1. 22
      src/local.c

22
src/local.c

@ -84,7 +84,6 @@ int verbose = 0;
int vpn = 0; int vpn = 0;
uint64_t tx = 0; uint64_t tx = 0;
uint64_t rx = 0; uint64_t rx = 0;
ev_timer stat_timer;
#endif #endif
static int acl = 0; static int acl = 0;
@ -584,9 +583,13 @@ static void server_send_cb(EV_P_ ev_io *w, int revents)
} }
#ifdef ANDROID #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 #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); 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); ssize_t r = recv(remote->fd, server->buf, BUF_SIZE, 0);
if (r == 0) { if (r == 0) {
@ -1154,11 +1161,6 @@ int main(int argc, char **argv)
struct ev_loop *loop = EV_DEFAULT; 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 // Setup socket
int listenfd; int listenfd;
listenfd = create_and_bind(local_addr, local_port); listenfd = create_and_bind(local_addr, local_port);
@ -1201,10 +1203,6 @@ int main(int argc, char **argv)
// Clean up // Clean up
#ifdef ANDROID
ev_timer_stop(loop, &stat_timer);
#endif
ev_io_stop(loop, &listen_ctx.io); ev_io_stop(loop, &listen_ctx.io);
free_connections(loop); free_connections(loop);

Loading…
Cancel
Save