Fix watchdog to stop ping after wifi disconnects
- The pings are delayed async messages that were getting handled
after a disconnect as well
- Increase poll time to 200 ms, so we block on a receive for 1ms every 200ms for a
sent packet
Bug: 5361564
Change-Id: I1931a1c4146e78a87407d541d8c3934ff8232604
diff --git a/core/java/android/net/DnsPinger.java b/core/java/android/net/DnsPinger.java
index 3e27b0d..3291e6b 100644
--- a/core/java/android/net/DnsPinger.java
+++ b/core/java/android/net/DnsPinger.java
@@ -53,7 +53,7 @@
public final class DnsPinger extends Handler {
private static final boolean V = true;
- private static final int RECEIVE_POLL_INTERVAL_MS = 30;
+ private static final int RECEIVE_POLL_INTERVAL_MS = 200;
private static final int DNS_PORT = 53;
/** Short socket timeout so we don't block one any 'receive' call */
@@ -70,6 +70,9 @@
private final ArrayList<InetAddress> mDefaultDns;
private String TAG;
+ //Invalidates old dns requests upon a cancel
+ private AtomicInteger mCurrentToken = new AtomicInteger();
+
private static final int BASE = Protocol.BASE_DNS_PINGER;
/**
@@ -102,6 +105,17 @@
long start = SystemClock.elapsedRealtime();
}
+ /* Message argument for ACTION_PING_DNS */
+ private class DnsArg {
+ InetAddress dns;
+ int seq;
+
+ DnsArg(InetAddress d, int s) {
+ dns = d;
+ seq = s;
+ }
+ }
+
public DnsPinger(Context context, String TAG, Looper looper,
Handler target, int connectionType) {
super(looper);
@@ -122,9 +136,13 @@
public void handleMessage(Message msg) {
switch (msg.what) {
case ACTION_PING_DNS:
+ DnsArg dnsArg = (DnsArg) msg.obj;
+ if (dnsArg.seq != mCurrentToken.get()) {
+ break;
+ }
try {
ActivePing newActivePing = new ActivePing();
- InetAddress dnsAddress = (InetAddress) msg.obj;
+ InetAddress dnsAddress = dnsArg.dns;
newActivePing.internalId = msg.arg1;
newActivePing.timeout = msg.arg2;
newActivePing.socket = new DatagramSocket();
@@ -248,11 +266,13 @@
*/
public int pingDnsAsync(InetAddress dns, int timeout, int delay) {
int id = sCounter.incrementAndGet();
- sendMessageDelayed(obtainMessage(ACTION_PING_DNS, id, timeout, dns), delay);
+ sendMessageDelayed(obtainMessage(ACTION_PING_DNS, id, timeout,
+ new DnsArg(dns, mCurrentToken.get())), delay);
return id;
}
public void cancelPings() {
+ mCurrentToken.incrementAndGet();
obtainMessage(ACTION_CANCEL_ALL_PINGS).sendToTarget();
}