Fix DiscoveryManager trying to start discovery again on all machines when any machine is discovered

This commit is contained in:
Cameron Gutman
2018-06-01 19:50:32 -07:00
parent cdb5f51ab8
commit 862b8632e3
3 changed files with 18 additions and 31 deletions
+11 -14
View File
@@ -159,27 +159,24 @@
} }
// Override from MDNSCallback // Override from MDNSCallback
- (void)updateHosts:(NSArray *)hosts { - (void)updateHost:(TemporaryHost*)host {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Discover the hosts before adding to eliminate duplicates // Discover the hosts before adding to eliminate duplicates
for (TemporaryHost* host in hosts) { Log(LOG_I, @"Found host through MDNS: %@:", host.name);
Log(LOG_I, @"Found host through MDNS: %@:", host.name); // Since this is on a background thread, we do not need to use the opQueue
// Since this is on a background thread, we do not need to use the opQueue DiscoveryWorker* worker = (DiscoveryWorker*)[self createWorkerForHost:host];
DiscoveryWorker* worker = (DiscoveryWorker*)[self createWorkerForHost:host]; [worker discoverHost];
[worker discoverHost]; if ([self addHostToDiscovery:host]) {
if ([self addHostToDiscovery:host]) { Log(LOG_D, @"Adding host to discovery: %@", host.name);
Log(LOG_D, @"Adding host to discovery: %@", host.name); [self->_callback updateAllHosts:self->_hostQueue];
[self->_callback updateAllHosts:self->_hostQueue]; } else {
} else { Log(LOG_D, @"Not adding host to discovery: %@", host.name);
Log(LOG_D, @"Not adding host to discovery: %@", host.name);
}
} }
}); });
} }
- (TemporaryHost*) getHostInDiscovery:(NSString*)uuidString { - (TemporaryHost*) getHostInDiscovery:(NSString*)uuidString {
for (int i = 0; i < _hostQueue.count; i++) { for (TemporaryHost* discoveredHost in _hostQueue) {
TemporaryHost* discoveredHost = [_hostQueue objectAtIndex:i];
if (discoveredHost.uuid.length > 0 && [discoveredHost.uuid isEqualToString:uuidString]) { if (discoveredHost.uuid.length > 0 && [discoveredHost.uuid isEqualToString:uuidString]) {
return discoveredHost; return discoveredHost;
} }
+3 -1
View File
@@ -6,9 +6,11 @@
// Copyright (c) 2014 Moonlight Stream. All rights reserved. // Copyright (c) 2014 Moonlight Stream. All rights reserved.
// //
#import "TemporaryHost.h"
@protocol MDNSCallback <NSObject> @protocol MDNSCallback <NSObject>
- (void) updateHosts:(NSArray*)hosts; - (void) updateHost:(TemporaryHost*)host;
@end @end
+4 -16
View File
@@ -11,7 +11,6 @@
@implementation MDNSManager { @implementation MDNSManager {
NSNetServiceBrowser* mDNSBrowser; NSNetServiceBrowser* mDNSBrowser;
NSMutableArray* domains;
NSMutableArray* services; NSMutableArray* services;
BOOL scanActive; BOOL scanActive;
} }
@@ -28,7 +27,6 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
mDNSBrowser = [[NSNetServiceBrowser alloc] init]; mDNSBrowser = [[NSNetServiceBrowser alloc] init];
[mDNSBrowser setDelegate:self]; [mDNSBrowser setDelegate:self];
domains = [[NSMutableArray alloc] init];
services = [[NSMutableArray alloc] init]; services = [[NSMutableArray alloc] init];
return self; return self;
@@ -54,22 +52,12 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp";
[mDNSBrowser stop]; [mDNSBrowser stop];
} }
- (NSArray*) getFoundHosts {
NSMutableArray* hosts = [[NSMutableArray alloc] init];
for (NSNetService* service in services) {
if (service.hostName != nil) {
TemporaryHost* host = [[TemporaryHost alloc] init];
host.activeAddress = host.localAddress = service.hostName;
host.name = service.hostName;
[hosts addObject:host];
}
}
return hosts;
}
- (void)netServiceDidResolveAddress:(NSNetService *)service { - (void)netServiceDidResolveAddress:(NSNetService *)service {
Log(LOG_D, @"Resolved address: %@ -> %@", service, service.hostName); Log(LOG_D, @"Resolved address: %@ -> %@", service, service.hostName);
[self.callback updateHosts:[self getFoundHosts]]; TemporaryHost* host = [[TemporaryHost alloc] init];
host.activeAddress = host.localAddress = service.hostName;
host.name = service.hostName;
[self.callback updateHost:host];
} }
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict { - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {