Fix mDNS detection of hosts with the same IP address

This is the case for PCs running GFE and Sunshine side-by-side.
This commit is contained in:
Cameron Gutman
2023-02-25 20:29:17 -06:00
parent 94b202f7b6
commit f659af29da
4 changed files with 7 additions and 44 deletions

View File

@@ -431,11 +431,6 @@ public class ComputerManagerService extends Service {
}
}
@Override
public void notifyComputerRemoved(MdnsComputer computer) {
// Nothing to do here
}
@Override
public void notifyDiscoveryFailure(Exception e) {
LimeLog.severe("mDNS discovery failed");

View File

@@ -54,13 +54,6 @@ public class DiscoveryService extends Service {
}
}
@Override
public void notifyComputerRemoved(MdnsComputer computer) {
if (boundListener != null) {
boundListener.notifyComputerRemoved(computer);
}
}
@Override
public void notifyDiscoveryFailure(Exception e) {
if (boundListener != null) {

View File

@@ -7,7 +7,6 @@ import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -25,13 +24,13 @@ public class MdnsDiscoveryAgent implements ServiceListener {
private MdnsDiscoveryListener listener;
private Thread discoveryThread;
private HashMap<InetAddress, MdnsComputer> computers = new HashMap<InetAddress, MdnsComputer>();
private HashSet<String> pendingResolution = new HashSet<String>();
private HashSet<MdnsComputer> computers = new HashSet<>();
private HashSet<String> pendingResolution = new HashSet<>();
// The resolver factory's instance member has a static lifetime which
// means our ref count and listener must be static also.
private static int resolverRefCount = 0;
private static HashSet<ServiceListener> listeners = new HashSet<ServiceListener>();
private static HashSet<ServiceListener> listeners = new HashSet<>();
private static ServiceListener nvstreamListener = new ServiceListener() {
@Override
public void serviceAdded(ServiceEvent event) {
@@ -107,7 +106,7 @@ public class MdnsDiscoveryAgent implements ServiceListener {
return false;
}
}
};
}
static {
// Override jmDNS's default topology discovery class with ours
@@ -261,7 +260,7 @@ public class MdnsDiscoveryAgent implements ServiceListener {
for (Inet4Address v4Addr : v4Addrs) {
synchronized (computers) {
MdnsComputer computer = new MdnsComputer(info.getName(), v4Addr, v6GlobalAddr, info.getPort());
if (computers.put(computer.getLocalAddress(), computer) == null) {
if (computers.add(computer)) {
// This was a new entry
listener.notifyComputerAdded(computer);
}
@@ -274,8 +273,7 @@ public class MdnsDiscoveryAgent implements ServiceListener {
if (v6LocalAddr != null || v6GlobalAddr != null) {
MdnsComputer computer = new MdnsComputer(info.getName(), v6LocalAddr, v6GlobalAddr, info.getPort());
if (computers.put(v6LocalAddr != null ?
computer.getLocalAddress() : computer.getIpv6Address(), computer) == null) {
if (computers.add(computer)) {
// This was a new entry
listener.notifyComputerAdded(computer);
}
@@ -353,7 +351,7 @@ public class MdnsDiscoveryAgent implements ServiceListener {
public List<MdnsComputer> getComputerSet() {
synchronized (computers) {
return new ArrayList<MdnsComputer>(computers.values());
return new ArrayList<>(computers);
}
}
@@ -377,28 +375,6 @@ public class MdnsDiscoveryAgent implements ServiceListener {
@Override
public void serviceRemoved(ServiceEvent event) {
LimeLog.info("mDNS: Machine disappeared: "+event.getInfo().getName());
Inet4Address v4Addrs[] = event.getInfo().getInet4Addresses();
for (Inet4Address addr : v4Addrs) {
synchronized (computers) {
MdnsComputer computer = computers.remove(addr);
if (computer != null) {
listener.notifyComputerRemoved(computer);
break;
}
}
}
Inet6Address v6Addrs[] = event.getInfo().getInet6Addresses();
for (Inet6Address addr : v6Addrs) {
synchronized (computers) {
MdnsComputer computer = computers.remove(addr);
if (computer != null) {
listener.notifyComputerRemoved(computer);
break;
}
}
}
}
@Override

View File

@@ -2,6 +2,5 @@ package com.limelight.nvstream.mdns;
public interface MdnsDiscoveryListener {
void notifyComputerAdded(MdnsComputer computer);
void notifyComputerRemoved(MdnsComputer computer);
void notifyDiscoveryFailure(Exception e);
}