diff --git a/Limelight/Network/DiscoveryWorker.m b/Limelight/Network/DiscoveryWorker.m index 8d80402d..ba6a411f 100644 --- a/Limelight/Network/DiscoveryWorker.m +++ b/Limelight/Network/DiscoveryWorker.m @@ -41,29 +41,68 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds } } +- (NSArray*) getHostAddressList { + NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:3]; + + if (_host.localAddress != nil) { + [array addObject:_host.localAddress]; + } + if (_host.externalAddress != nil) { + [array addObject:_host.externalAddress]; + } + if (_host.address != nil) { + [array addObject:_host.address]; + } + + // Remove duplicate addresses from the list. + // This is done using an array rather than a set + // to preserve insertion order of addresses. + for (int i = 0; i < [array count]; i++) { + NSString *addr1 = [array objectAtIndex:i]; + + for (int j = 1; j < [array count]; j++) { + if (i == j) { + continue; + } + + NSString *addr2 = [array objectAtIndex:j]; + + if ([addr1 isEqualToString:addr2]) { + // Remove the last address + [array removeObjectAtIndex:j]; + + // Begin searching again from the start + i = -1; + break; + } + } + } + + return array; +} + - (void) discoverHost { BOOL receivedResponse = NO; - if (!self.cancelled && _host.localAddress != nil) { - ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.localAddress]; + NSArray *addresses = [self getHostAddressList]; + + Log(LOG_D, @"%@ has %d unique addresses", _host.name, [addresses count]); + + for (NSString *address in addresses) { + if (self.cancelled) { + // Get out without updating the status because + // it might not have finished checking the various + // addresses + return; + } + + ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:address]; receivedResponse = [self checkResponse:serverInfoResp]; if (receivedResponse) { - _host.activeAddress = _host.localAddress; - } - } - if (!self.cancelled && !receivedResponse && _host.externalAddress != nil) { - ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.externalAddress]; - receivedResponse = [self checkResponse:serverInfoResp]; - if (receivedResponse) { - _host.activeAddress = _host.externalAddress; - } - } - if (!self.cancelled && !receivedResponse && _host.address != nil) { - ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.address]; - receivedResponse = [self checkResponse:serverInfoResp]; - if (receivedResponse) { - _host.activeAddress = _host.address; + _host.activeAddress = address; + break; } } + _host.online = receivedResponse; if (receivedResponse) { Log(LOG_D, @"Received response from: %@\n{\n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n\t activeAddress:%@ \n}", _host.name, _host.address, _host.localAddress, _host.externalAddress, _host.uuid, _host.mac, _host.pairState, _host.online, _host.activeAddress); diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index ade4d656..71601bc9 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -444,8 +444,15 @@ static NSArray* appList; // Initialize the non-persistent host state for (Host* host in hostList) { - host.online = NO; - host.activeAddress = host.address; + if (host.activeAddress == nil) { + host.activeAddress = host.localAddress; + } + if (host.activeAddress == nil) { + host.activeAddress = host.externalAddress; + } + if (host.activeAddress == nil) { + host.activeAddress = host.address; + } } } }