]> git.meshlink.io Git - meshlink/blobdiff - src/discovery.c
Add a poll callback to channels.
[meshlink] / src / discovery.c
index b50aea8d80f689991355db5dac9269eb292fa06e..fa4e13c3435ec928e5c2e626e111344509032803 100644 (file)
@@ -32,6 +32,8 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup
     assert(mesh->avahi_server != NULL);
     assert(mesh->avahi_poll != NULL);
 
+    pthread_mutex_lock(&(mesh->mesh_mutex));
+
     /* Called whenever the entry group state changes */
     switch(state)
     {
@@ -55,6 +57,8 @@ static void discovery_entry_group_callback(AvahiServer *server, AvahiSEntryGroup
         case AVAHI_ENTRY_GROUP_REGISTERING:
             ;
     }
+
+    pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 
@@ -71,6 +75,8 @@ static void discovery_create_services(meshlink_handle_t *mesh)
     assert(mesh->avahi_servicetype != NULL);
     assert(mesh->self != NULL);
 
+    pthread_mutex_lock(&(mesh->mesh_mutex));
+
     logger(mesh, MESHLINK_DEBUG, "Adding service\n");
 
     /* Ifthis is the first time we're called, let's create a new entry group */
@@ -121,6 +127,8 @@ fail:
 done:
     if(txt_name)
         { free(txt_name); }
+
+    pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 static void discovery_server_callback(AvahiServer *server, AvahiServerState state, void * userdata)
@@ -129,6 +137,8 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat
 
     // asserts
     assert(mesh != NULL);
+    
+    pthread_mutex_lock(&(mesh->mesh_mutex));
 
     switch(state)
     {
@@ -163,7 +173,6 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat
                 {
                     logger(mesh, MESHLINK_ERROR, "Avahi failed to set new host name: %s\n", avahi_strerror(result));
                     avahi_simple_poll_quit(mesh->avahi_poll);
-                    return;
                 }
             }
             break;
@@ -196,6 +205,8 @@ static void discovery_server_callback(AvahiServer *server, AvahiServerState stat
         case AVAHI_SERVER_INVALID:
             break;
     }
+
+    pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata)
@@ -207,6 +218,8 @@ static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfI
     assert(mesh != NULL);
     assert(mesh->avahi_server != NULL);
 
+    pthread_mutex_lock(&(mesh->mesh_mutex));
+
     /* Called whenever a service has been resolved successfully or timed out */
     switch(event)
     {
@@ -328,6 +341,8 @@ static void discovery_resolve_callback(AvahiSServiceResolver *resolver, AvahiIfI
     }
 
     avahi_s_service_resolver_free(resolver);
+
+    pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata)
@@ -339,6 +354,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde
     assert(mesh->avahi_server != NULL);
     assert(mesh->avahi_poll != NULL);
 
+    pthread_mutex_lock(&(mesh->mesh_mutex));
+
     /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
     switch (event)
     {
@@ -347,7 +364,7 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde
                 logger(mesh, MESHLINK_ERROR, "(Browser) %s\n", avahi_strerror(avahi_server_errno(mesh->avahi_server)));
                 avahi_simple_poll_quit(mesh->avahi_poll);
             }
-            return;
+            break;
 
         case AVAHI_BROWSER_NEW:
             {
@@ -386,6 +403,8 @@ static void discovery_browse_callback(AvahiSServiceBrowser *browser, AvahiIfInde
             }
             break;
     }
+    
+    pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
 static void *discovery_loop(void *userdata)