void OnTimeout(object obj) { SimpleResolverEventArgs args = (SimpleResolverEventArgs)obj; SimpleResolverEventArgs args2; lock (queries) { if (!queries.TryGetValue(args.QueryID, out args2)) { return; // Already processed. } if (args != args2) { throw new Exception("Should not happen: args != args2"); } args.Retries++; if (args.Retries > 1) { // Error timeout args.ResolverError = ResolverError.Timeout; args.OnCompleted(this); } else { SendAQuery(args, false); } } }
void OnReceive(IAsyncResult ares) { if (disposed) { return; } int nread = 0; EndPoint remote_ep = client.RemoteEndPoint; try { nread = client.EndReceive(ares); } catch (Exception e) { Console.Error.WriteLine(e); } BeginReceive(); byte [] buffer = (byte [])ares.AsyncState; if (nread > 12) { DnsResponse response = new DnsResponse(buffer, nread); int id = response.Header.ID; SimpleResolverEventArgs args = null; lock (queries) { if (queries.TryGetValue(id, out args)) { queries.Remove(id); } } if (args != null) { Timer t = args.Timer; if (t != null) { t.Change(Timeout.Infinite, Timeout.Infinite); } try { ProcessResponse(args, response, remote_ep); } catch (Exception e) { args.ResolverError = (ResolverError)(-1); args.ErrorMessage = e.Message; } IPHostEntry entry = args.HostEntry; if (args.ResolverError != 0 && args.PTRAddress != null && entry != null && entry.HostName != null) { args.PTRAddress = null; SendAQuery(args, entry.HostName, true); args.Timer.Change(5000, Timeout.Infinite); } else { args.OnCompleted(this); } } } FreeBuffer(buffer); }