From: Guus Sliepen Date: Sun, 4 Aug 2019 10:22:52 +0000 (+0200) Subject: Avoid unnecessary calls to time(). X-Git-Url: http://git.meshlink.io/?p=meshlink;a=commitdiff_plain;h=f91651414adcac1c05e1b923bee4cb3caf559c3b Avoid unnecessary calls to time(). We already have the current time from the event loop. --- diff --git a/src/conf.c b/src/conf.c index a9e3e4d6..e0d35950 100644 --- a/src/conf.c +++ b/src/conf.c @@ -735,7 +735,7 @@ bool invitation_read(meshlink_handle_t *mesh, const char *conf_subdir, const cha return false; } - if(time(NULL) > st.st_mtime + mesh->invitation_timeout) { + if(mesh->loop.now.tv_sec > st.st_mtime + mesh->invitation_timeout) { logger(mesh, MESHLINK_ERROR, "Peer tried to use an outdated invitation file %s\n", name); fclose(f); unlink(used_path); diff --git a/src/net.c b/src/net.c index ddfd2cf0..1df0be00 100644 --- a/src/net.c +++ b/src/net.c @@ -393,7 +393,6 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_DEBUG, "* min_connects = %d", min_connects); logger(mesh, MESHLINK_DEBUG, "* max_connects = %d", max_connects); - // find the best one for initial connect if(cur_connects < min_connects) { @@ -402,7 +401,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { for splay_each(node_t, n, mesh->nodes) { logger(mesh, MESHLINK_DEBUG, "* %s->devclass = %d", n->name, n->devclass); - if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { + if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -436,7 +435,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { splay_tree_t *nodes = splay_alloc_tree(node_compare_lsc_desc, NULL); for splay_each(node_t, n, mesh->nodes) { - if(n != mesh->self && n->devclass == devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { + if(n != mesh->self && n->devclass == devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -467,7 +466,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL); for splay_each(node_t, n, mesh->nodes) { - if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && !n->status.blacklisted && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { + if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -486,7 +485,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { // perform connect if(connect_to && !connect_to->connection) { - connect_to->last_connect_try = time(NULL); + connect_to->last_connect_try = mesh->loop.now.tv_sec; logger(mesh, MESHLINK_DEBUG, "Autoconnect trying to connect to %s", connect_to->name); /* check if there is already a connection attempt to this node */ diff --git a/src/protocol_auth.c b/src/protocol_auth.c index e9aa0c51..c7d68829 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -432,7 +432,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { n->devclass = devclass; n->status.dirty = true; - n->last_successfull_connection = time(NULL); + n->last_successfull_connection = mesh->loop.now.tv_sec; n->connection = c; c->node = n;