private IP4Addr resolveSubDomain(string domain, IP4Addr start) { string[] domains = domain.Split('.'); Array.Reverse(domains); IP4Addr res = start; for (var i = 0; i < domains.Length; i++) { var sub = domains[i]; string cacheKey = ""; for (var j = i; j >= 0; j--) { cacheKey += domains[j] + (j == 0 ? "" : "."); } Task <IP4Addr> t; mut.WaitOne(); if (IpCache.ContainsKey(cacheKey)) { t = IpCache[cacheKey]; dbg("Cache hit " + cacheKey); } else { dbg("Setting cache for " + cacheKey); t = dnsClient.Resolve(res, sub); IpCache[cacheKey] = t; } mut.ReleaseMutex(); if (t.IsCompleted) { dbg("Cache needs verifying"); // task has already been finished, verify cache Task <String> reverse = dnsClient.Reverse(t.Result); reverse.Wait(); if (reverse.Result != cacheKey) { dbg("Cache verification failed. " + reverse.Result); IpCache.Remove(cacheKey, out Task <IP4Addr> v); // go level back i = Math.Max(-1, i - 2); continue; } } t.Wait(); res = t.Result; } return(res); }
private async Task <String> AsignReverserAndReverse(IP4Addr ip) { Task <string> reverser; var hasReverser = reversers.TryGetValue(ip, out reverser); if (hasReverser) { return(reverser.Result); } else { reverser = reversers.GetOrAdd(ip, dnsClient.Reverse(ip)); return(reverser.Result); } }