diff --git a/Limelight/Network/DiscoveryManager.m b/Limelight/Network/DiscoveryManager.m index 3d849af7..97b7217b 100644 --- a/Limelight/Network/DiscoveryManager.m +++ b/Limelight/Network/DiscoveryManager.m @@ -70,6 +70,10 @@ } - (void) startDiscovery { + if (shouldDiscover) { + return; + } + Log(LOG_I, @"Starting discovery"); shouldDiscover = YES; [_mdnsMan searchForHosts]; @@ -79,6 +83,10 @@ } - (void) stopDiscovery { + if (!shouldDiscover) { + return; + } + Log(LOG_I, @"Stopping discovery"); shouldDiscover = NO; [_mdnsMan stopSearching]; @@ -87,10 +95,18 @@ - (void) stopDiscoveryBlocking { Log(LOG_I, @"Stopping discovery and waiting for workers to stop"); - shouldDiscover = NO; - [_mdnsMan stopSearching]; - [_opQueue cancelAllOperations]; + + if (shouldDiscover) { + shouldDiscover = NO; + [_mdnsMan stopSearching]; + [_opQueue cancelAllOperations]; + } + + // Ensure we always wait, just in case discovery + // was stopped already but in an async manner that + // left operations in progress. [_opQueue waitUntilAllOperationsAreFinished]; + Log(LOG_I, @"All discovery workers stopped"); } diff --git a/Limelight/Network/MDNSManager.m b/Limelight/Network/MDNSManager.m index 099c1086..190648e9 100644 --- a/Limelight/Network/MDNSManager.m +++ b/Limelight/Network/MDNSManager.m @@ -23,6 +23,8 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; self.callback = callback; + scanActive = FALSE; + mDNSBrowser = [[NSNetServiceBrowser alloc] init]; [mDNSBrowser setDelegate:self]; @@ -33,12 +35,20 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; } - (void) searchForHosts { + if (scanActive) { + return; + } + Log(LOG_I, @"Starting mDNS discovery"); scanActive = TRUE; [mDNSBrowser searchForServicesOfType:NV_SERVICE_TYPE inDomain:@""]; } - (void) stopSearching { + if (!scanActive) { + return; + } + Log(LOG_I, @"Stopping mDNS discovery"); scanActive = FALSE; [mDNSBrowser stop];