Ensure double-starting or stopping DiscoveryManager and MDNSManager doesn't cause side-effects, since we do this when Home is pressed while streaming

This commit is contained in:
Cameron Gutman 2018-05-31 00:33:24 -07:00
parent 67b08cd8b9
commit 4243bcd5d1
2 changed files with 29 additions and 3 deletions

View File

@ -70,6 +70,10 @@
} }
- (void) startDiscovery { - (void) startDiscovery {
if (shouldDiscover) {
return;
}
Log(LOG_I, @"Starting discovery"); Log(LOG_I, @"Starting discovery");
shouldDiscover = YES; shouldDiscover = YES;
[_mdnsMan searchForHosts]; [_mdnsMan searchForHosts];
@ -79,6 +83,10 @@
} }
- (void) stopDiscovery { - (void) stopDiscovery {
if (!shouldDiscover) {
return;
}
Log(LOG_I, @"Stopping discovery"); Log(LOG_I, @"Stopping discovery");
shouldDiscover = NO; shouldDiscover = NO;
[_mdnsMan stopSearching]; [_mdnsMan stopSearching];
@ -87,10 +95,18 @@
- (void) stopDiscoveryBlocking { - (void) stopDiscoveryBlocking {
Log(LOG_I, @"Stopping discovery and waiting for workers to stop"); Log(LOG_I, @"Stopping discovery and waiting for workers to stop");
if (shouldDiscover) {
shouldDiscover = NO; shouldDiscover = NO;
[_mdnsMan stopSearching]; [_mdnsMan stopSearching];
[_opQueue cancelAllOperations]; [_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]; [_opQueue waitUntilAllOperationsAreFinished];
Log(LOG_I, @"All discovery workers stopped"); Log(LOG_I, @"All discovery workers stopped");
} }

View File

@ -23,6 +23,8 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
self.callback = callback; self.callback = callback;
scanActive = FALSE;
mDNSBrowser = [[NSNetServiceBrowser alloc] init]; mDNSBrowser = [[NSNetServiceBrowser alloc] init];
[mDNSBrowser setDelegate:self]; [mDNSBrowser setDelegate:self];
@ -33,12 +35,20 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
} }
- (void) searchForHosts { - (void) searchForHosts {
if (scanActive) {
return;
}
Log(LOG_I, @"Starting mDNS discovery"); Log(LOG_I, @"Starting mDNS discovery");
scanActive = TRUE; scanActive = TRUE;
[mDNSBrowser searchForServicesOfType:NV_SERVICE_TYPE inDomain:@""]; [mDNSBrowser searchForServicesOfType:NV_SERVICE_TYPE inDomain:@""];
} }
- (void) stopSearching { - (void) stopSearching {
if (!scanActive) {
return;
}
Log(LOG_I, @"Stopping mDNS discovery"); Log(LOG_I, @"Stopping mDNS discovery");
scanActive = FALSE; scanActive = FALSE;
[mDNSBrowser stop]; [mDNSBrowser stop];