5353public final class DnsPinger extends Handler {
5454 private static final boolean V = true ;
5555
56- private static final int RECEIVE_POLL_INTERVAL_MS = 30 ;
56+ private static final int RECEIVE_POLL_INTERVAL_MS = 200 ;
5757 private static final int DNS_PORT = 53 ;
5858
5959 /** Short socket timeout so we don't block one any 'receive' call */
@@ -70,6 +70,9 @@ public final class DnsPinger extends Handler {
7070 private final ArrayList <InetAddress > mDefaultDns ;
7171 private String TAG ;
7272
73+ //Invalidates old dns requests upon a cancel
74+ private AtomicInteger mCurrentToken = new AtomicInteger ();
75+
7376 private static final int BASE = Protocol .BASE_DNS_PINGER ;
7477
7578 /**
@@ -102,6 +105,17 @@ private class ActivePing {
102105 long start = SystemClock .elapsedRealtime ();
103106 }
104107
108+ /* Message argument for ACTION_PING_DNS */
109+ private class DnsArg {
110+ InetAddress dns ;
111+ int seq ;
112+
113+ DnsArg (InetAddress d , int s ) {
114+ dns = d ;
115+ seq = s ;
116+ }
117+ }
118+
105119 public DnsPinger (Context context , String TAG , Looper looper ,
106120 Handler target , int connectionType ) {
107121 super (looper );
@@ -122,9 +136,13 @@ public DnsPinger(Context context, String TAG, Looper looper,
122136 public void handleMessage (Message msg ) {
123137 switch (msg .what ) {
124138 case ACTION_PING_DNS :
139+ DnsArg dnsArg = (DnsArg ) msg .obj ;
140+ if (dnsArg .seq != mCurrentToken .get ()) {
141+ break ;
142+ }
125143 try {
126144 ActivePing newActivePing = new ActivePing ();
127- InetAddress dnsAddress = ( InetAddress ) msg . obj ;
145+ InetAddress dnsAddress = dnsArg . dns ;
128146 newActivePing .internalId = msg .arg1 ;
129147 newActivePing .timeout = msg .arg2 ;
130148 newActivePing .socket = new DatagramSocket ();
@@ -248,11 +266,13 @@ public List<InetAddress> getDnsList() {
248266 */
249267 public int pingDnsAsync (InetAddress dns , int timeout , int delay ) {
250268 int id = sCounter .incrementAndGet ();
251- sendMessageDelayed (obtainMessage (ACTION_PING_DNS , id , timeout , dns ), delay );
269+ sendMessageDelayed (obtainMessage (ACTION_PING_DNS , id , timeout ,
270+ new DnsArg (dns , mCurrentToken .get ())), delay );
252271 return id ;
253272 }
254273
255274 public void cancelPings () {
275+ mCurrentToken .incrementAndGet ();
256276 obtainMessage (ACTION_CANCEL_ALL_PINGS ).sendToTarget ();
257277 }
258278
0 commit comments