Created
July 14, 2011 23:45
-
-
Save txdv/1083740 to your computer and use it in GitHub Desktop.
DNS dislove
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ParseHost(string host, TimeSpan timeSpan, Action<Exception, string, IPAddress> callback) | |
{ | |
IPAddress ipaddress; | |
if (IPAddress.TryParse(host, out ipaddress)) { | |
callback(null, host, ipaddress); | |
} else { | |
bool timeout = false; | |
Context.CreateTimerWatcher(timeSpan, delegate { | |
callback(new Exception("Timeout while resolving"), null, null); | |
timeout = true; | |
}); | |
Dns.BeginGetHostAddresses(host, (ar) => { | |
IPAddress[] list = null; | |
try { | |
list = Dns.EndGetHostAddresses(ar); | |
} catch (Exception exception) { | |
callback(exception, null, null); | |
} finally { | |
if (list != null) { | |
Manos.Threading.Boundary.Instance.ExecuteOnTargetLoop(delegate { | |
if (timeout) { | |
return; | |
} | |
if (list.Length == 0) { | |
callback(new Exception(string.Format("Couldn't resolve {0}", host)), null, null); | |
} else { | |
ipaddress = list.First(); | |
callback(null, host, ipaddress); | |
} | |
}); | |
} | |
} | |
}, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment