]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Speed up reconnections on network interface changes.
[meshlink] / src / meshlink.c
index 9b13dca1652d22658f282be6cfbfbce02c81357e..b1f5c918fa68dd9d1d29e09a710c60d73b3bd51e 100644 (file)
@@ -2310,7 +2310,15 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        //Before doing meshlink_join make sure we are not connected to another mesh
        if(mesh->threadstarted) {
-               logger(mesh, MESHLINK_DEBUG, "Already connected to a mesh\n");
+               logger(mesh, MESHLINK_ERROR, "Cannot join while started\n");
+               meshlink_errno = MESHLINK_EINVAL;
+               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               return false;
+       }
+
+       // Refuse to join a mesh if we are already part of one. We are part of one if we know at least one other node.
+       if(mesh->nodes->count > 1) {
+               logger(mesh, MESHLINK_ERROR, "Already part of an existing mesh\n");
                meshlink_errno = MESHLINK_EINVAL;
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
@@ -3082,6 +3090,21 @@ end:
 #endif
 }
 
+void handle_network_change(meshlink_handle_t *mesh, bool online) {
+       if(!mesh->connections) {
+               return;
+       }
+
+       if(online) {
+               retry(mesh);
+       } else {
+               // We are off-line. Terminate all active connections.
+               for list_each(connection_t, c, mesh->connections) {
+                       terminate_connection(mesh, c, false);
+               }
+       }
+}
+
 static void __attribute__((constructor)) meshlink_init(void) {
        crypto_init();
        unsigned int seed;