Skip to content

Instantly share code, notes, and snippets.

@txdv
Created July 14, 2011 23:45
Show Gist options
  • Save txdv/1083740 to your computer and use it in GitHub Desktop.
Save txdv/1083740 to your computer and use it in GitHub Desktop.
DNS dislove
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