]> git.meshlink.io Git - meshlink/blobdiff - src/net.c
Merge branch 'discovery' into everbase
[meshlink] / src / net.c
index d8f7d642e4ac0307d1ef6f84c9156cf70d46f75d..b368e505b763106429badb9a7ef1a855110bc06f 100644 (file)
--- a/src/net.c
+++ b/src/net.c
 #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
@@ -156,6 +160,7 @@ static void cond_add_connection(meshlink_handle_t *mesh, int rand_modulo, bool (
                        //the user will read this debug message "Autoconnecting to %s" that is misleading
                        logger(DEBUG_CONNECTIONS, LOG_INFO, "Autoconnecting to %s", n->name);
                        outgoing_t *outgoing = xzalloc(sizeof *outgoing);
+                       outgoing->mesh = mesh;
                        outgoing->name = xstrdup(n->name);
                        list_insert_tail(mesh->outgoings, outgoing);
                        setup_outgoing_connection(mesh, outgoing);
@@ -210,6 +215,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 +305,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 +343,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;