X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet.c;h=20b799ff1cd1f8725abf9159ca653824aa37fe43;hb=5809d16e2bc41dbc90358f4324b77482f4ca03de;hp=d8f7d642e4ac0307d1ef6f84c9156cf70d46f75d;hpb=3f6d9a24c1d4c78548bc9dab0e66e2a240a72df7;p=meshlink diff --git a/src/net.c b/src/net.c index d8f7d642..20b799ff 100644 --- a/src/net.c +++ b/src/net.c @@ -31,6 +31,10 @@ #include "protocol.h" #include "xalloc.h" +static const int min(int a, int b) { + return a < b ? a : b; +} + /* Terminate a connection: - Mark it as inactive @@ -210,6 +214,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { mesh->contradicting_add_edge = 0; mesh->contradicting_del_edge = 0; + int timeout = 5; + /* If AutoConnect is set, check if we need to make or break connections. */ if(autoconnect && mesh->nodes->count > 1) { @@ -298,9 +304,12 @@ static void periodic_handler(event_loop_t *loop, void *data) { } } } + + if (nc + mesh->outgoings->count < min(autoconnect, mesh->nodes->count - 1)) + timeout = 0; } - timeout_set(&mesh->loop, data, &(struct timeval){5, rand() % 100000}); + timeout_set(&mesh->loop, data, &(struct timeval){timeout, rand() % 100000}); } void handle_meta_connection_data(meshlink_handle_t *mesh, connection_t *c) { @@ -333,7 +342,7 @@ void retry(meshlink_handle_t *mesh) { */ int main_loop(meshlink_handle_t *mesh) { timeout_add(&mesh->loop, &mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); - timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){mesh->pingtimeout, rand() % 100000}); + timeout_add(&mesh->loop, &mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){0, 0}); //Add signal handler mesh->datafromapp.signum = 0;