mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-21 12:03:02 +00:00
Fix some issues with the new behavior of jmDNS 3.4.2
This commit is contained in:
parent
64aa01b2cf
commit
5175e68b99
@ -1,6 +1,5 @@
|
|||||||
package com.limelight.nvstream.mdns;
|
package com.limelight.nvstream.mdns;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -13,6 +12,7 @@ import java.util.TimerTask;
|
|||||||
|
|
||||||
import javax.jmdns.JmmDNS;
|
import javax.jmdns.JmmDNS;
|
||||||
import javax.jmdns.ServiceEvent;
|
import javax.jmdns.ServiceEvent;
|
||||||
|
import javax.jmdns.ServiceInfo;
|
||||||
import javax.jmdns.ServiceListener;
|
import javax.jmdns.ServiceListener;
|
||||||
|
|
||||||
import com.limelight.LimeLog;
|
import com.limelight.LimeLog;
|
||||||
@ -29,11 +29,15 @@ public class MdnsDiscoveryAgent {
|
|||||||
public void serviceAdded(ServiceEvent event) {
|
public void serviceAdded(ServiceEvent event) {
|
||||||
LimeLog.info("mDNS: Machine appeared: "+event.getInfo().getName());
|
LimeLog.info("mDNS: Machine appeared: "+event.getInfo().getName());
|
||||||
|
|
||||||
// This machine is pending resolution until we get the serviceResolved callback
|
ServiceInfo[] infos = resolver.getServiceInfos(SERVICE_TYPE, event.getInfo().getName(), 500);
|
||||||
|
if (infos == null || infos.length == 0) {
|
||||||
|
// This machine is pending resolution
|
||||||
pendingResolution.add(event.getInfo().getName());
|
pendingResolution.add(event.getInfo().getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// We call this to kick the resolver
|
LimeLog.info("mDNS: Resolved (blocking) with "+infos.length+" service entries");
|
||||||
resolver.requestServiceInfo(SERVICE_TYPE, event.getInfo().getName());
|
handleResolvedServiceInfo(infos[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void serviceRemoved(ServiceEvent event) {
|
public void serviceRemoved(ServiceEvent event) {
|
||||||
@ -52,21 +56,25 @@ public class MdnsDiscoveryAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void serviceResolved(ServiceEvent event) {
|
public void serviceResolved(ServiceEvent event) {
|
||||||
|
LimeLog.info("mDNS: Machine resolved (callback): "+event.getInfo().getName());
|
||||||
|
handleResolvedServiceInfo(event.getInfo());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private void handleResolvedServiceInfo(ServiceInfo info) {
|
||||||
MdnsComputer computer;
|
MdnsComputer computer;
|
||||||
|
|
||||||
LimeLog.info("mDNS: Machine resolved: "+event.getInfo().getName());
|
pendingResolution.remove(info.getName());
|
||||||
|
|
||||||
pendingResolution.remove(event.getInfo().getName());
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
computer = parseMdnsResponse(event);
|
computer = parseServerInfo(info);
|
||||||
if (computer == null) {
|
if (computer == null) {
|
||||||
LimeLog.info("mDNS: Invalid response for machine: "+event.getInfo().getName());
|
LimeLog.info("mDNS: Invalid response for machine: "+info.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
// Invalid DNS response
|
// Invalid DNS response
|
||||||
LimeLog.info("mDNS: Invalid response for machine: "+event.getInfo().getName());
|
LimeLog.info("mDNS: Invalid response for machine: "+info.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,17 +85,16 @@ public class MdnsDiscoveryAgent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
private static MdnsComputer parseMdnsResponse(ServiceEvent event) throws UnsupportedEncodingException {
|
private static MdnsComputer parseServerInfo(ServiceInfo info) throws UnsupportedEncodingException {
|
||||||
Inet4Address addrs[] = event.getInfo().getInet4Addresses();
|
Inet4Address addrs[] = info.getInet4Addresses();
|
||||||
if (addrs.length == 0) {
|
if (addrs.length == 0) {
|
||||||
LimeLog.info("Missing addresses");
|
LimeLog.info("mDNS: "+info.getName()+" is missing addresses");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Inet4Address address = addrs[0];
|
Inet4Address address = addrs[0];
|
||||||
String name = event.getInfo().getName();
|
String name = info.getName();
|
||||||
|
|
||||||
return new MdnsComputer(name, address);
|
return new MdnsComputer(name, address);
|
||||||
}
|
}
|
||||||
@ -100,8 +107,6 @@ public class MdnsDiscoveryAgent {
|
|||||||
|
|
||||||
public void startDiscovery(final int discoveryIntervalMs) {
|
public void startDiscovery(final int discoveryIntervalMs) {
|
||||||
stop = false;
|
stop = false;
|
||||||
resolver = JmmDNS.Factory.getInstance();
|
|
||||||
resolver.addServiceListener(SERVICE_TYPE, nvstreamListener);
|
|
||||||
|
|
||||||
final Timer t = new Timer();
|
final Timer t = new Timer();
|
||||||
t.schedule(new TimerTask() {
|
t.schedule(new TimerTask() {
|
||||||
@ -113,16 +118,22 @@ public class MdnsDiscoveryAgent {
|
|||||||
// There will be no further timer invocations now
|
// There will be no further timer invocations now
|
||||||
t.cancel();
|
t.cancel();
|
||||||
|
|
||||||
// Close the resolver
|
|
||||||
if (resolver != null) {
|
if (resolver != null) {
|
||||||
try {
|
resolver.removeServiceListener(SERVICE_TYPE, nvstreamListener);
|
||||||
resolver.close();
|
|
||||||
} catch (IOException e) {}
|
|
||||||
resolver = null;
|
resolver = null;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Perform initialization
|
||||||
|
if (resolver == null) {
|
||||||
|
resolver = JmmDNS.Factory.getInstance();
|
||||||
|
|
||||||
|
// This will cause the listener to be invoked for known hosts immediately.
|
||||||
|
// We do this in the timer callback to be off the main thread when this happens.
|
||||||
|
resolver.addServiceListener(SERVICE_TYPE, nvstreamListener);
|
||||||
|
}
|
||||||
|
|
||||||
// Send another mDNS query
|
// Send another mDNS query
|
||||||
resolver.requestServiceInfo(SERVICE_TYPE, null, discoveryIntervalMs);
|
resolver.requestServiceInfo(SERVICE_TYPE, null, discoveryIntervalMs);
|
||||||
|
|
||||||
@ -130,7 +141,11 @@ public class MdnsDiscoveryAgent {
|
|||||||
ArrayList<String> pendingNames = new ArrayList<String>(pendingResolution);
|
ArrayList<String> pendingNames = new ArrayList<String>(pendingResolution);
|
||||||
for (String name : pendingNames) {
|
for (String name : pendingNames) {
|
||||||
LimeLog.info("mDNS: Retrying service resolution for machine: "+name);
|
LimeLog.info("mDNS: Retrying service resolution for machine: "+name);
|
||||||
resolver.requestServiceInfo(SERVICE_TYPE, name);
|
ServiceInfo[] infos = resolver.getServiceInfos(SERVICE_TYPE, name, 500);
|
||||||
|
if (infos != null && infos.length != 0) {
|
||||||
|
LimeLog.info("mDNS: Resolved (retry) with "+infos.length+" service entries");
|
||||||
|
handleResolvedServiceInfo(infos[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user