Last active
July 28, 2020 18:51
-
-
Save Nico-VanHaaster/35342c1386de752674d0c37ceaa65e00 to your computer and use it in GitHub Desktop.
Cancel DNS.BeginGetHostEntry
This file contains 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
using System; | |
using System.Net; | |
using System.Threading.Tasks; | |
namespace DnsLookup | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ResolveTest("asdfasdfasdfasdfasdfa.ca", 15); | |
ResolveTest("google.com", 15); | |
RunTest(); | |
Console.ReadLine(); | |
} | |
public static async void RunTest() | |
{ | |
await Resolve("asdfasdfasdfasdfasdfa.ca", 60); | |
await Resolve("google.com", 60); | |
} | |
public static Task<IPHostEntry> GetHostEntryAsync(string hostNameOrAddress) | |
{ | |
return Task<IPHostEntry>.Factory.FromAsync<string>(new Func<string, AsyncCallback, object, IAsyncResult>(Dns.BeginGetHostEntry), new Func<IAsyncResult, IPHostEntry>(Dns.EndGetHostEntry), hostNameOrAddress, null); | |
} | |
public static bool ResolveTest(string hostNameOrAddress, int millisecond_time_out) | |
{ | |
ResolveState ioContext = new ResolveState(hostNameOrAddress); | |
var result = Dns.BeginGetHostEntry(ioContext.HostName, null, null); | |
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(millisecond_time_out), true); | |
if (!success) | |
{ | |
ioContext.Result = ResolveType.Timeout; | |
} | |
else | |
{ | |
try | |
{ | |
var ipList = Dns.EndGetHostEntry(result); | |
if (ipList == null || ipList.AddressList == null || ipList.AddressList.Length == 0) | |
ioContext.Result = ResolveType.InvalidHost; | |
else | |
ioContext.Result = ResolveType.Completed; | |
} | |
catch | |
{ | |
ioContext.Result = ResolveType.InvalidHost; | |
} | |
} | |
Console.WriteLine($"The result of ResolveTest for {ioContext.HostName} is {ioContext.Result}"); | |
return ioContext.Result == ResolveType.Completed; | |
} | |
public static Task<bool> Resolve(string hostNameOrAddress, int millisecond_time_out) | |
{ | |
return Task.Run(async () => | |
{ | |
bool completed = false; | |
var asCallBack = new AsyncCallback(ar => | |
{ | |
ResolveState context = (ResolveState)ar.AsyncState; | |
if (context.Result == ResolveType.Pending) | |
{ | |
try | |
{ | |
var ipList = Dns.EndGetHostEntry(ar); | |
if (ipList == null || ipList.AddressList == null || ipList.AddressList.Length == 0) | |
context.Result = ResolveType.InvalidHost; | |
else | |
context.Result = ResolveType.Completed; | |
} | |
catch | |
{ | |
context.Result = ResolveType.InvalidHost; | |
} | |
} | |
completed = true; | |
}); | |
ResolveState ioContext = new ResolveState(hostNameOrAddress); | |
var result = Dns.BeginGetHostEntry(ioContext.HostName, asCallBack, ioContext); | |
int miliCount = 0; | |
while (!completed) | |
{ | |
miliCount++; | |
if (miliCount >= millisecond_time_out) | |
{ | |
result.AsyncWaitHandle.Close(); | |
result = null; | |
ioContext.Result = ResolveType.Timeout; | |
break; | |
} | |
await Task.Delay(1); | |
} | |
Console.WriteLine($"The result of Resolve for {ioContext.HostName} is {ioContext.Result}"); | |
return ioContext.Result == ResolveType.Completed; | |
}); | |
} | |
public class ResolveState | |
{ | |
public ResolveState(string hostName) | |
{ | |
if (string.IsNullOrWhiteSpace(hostName)) | |
throw new ArgumentNullException(nameof(hostName)); | |
_hostName = hostName; | |
} | |
readonly string _hostName; | |
public ResolveType Result { get; set; } = ResolveType.Pending; | |
public string HostName => _hostName; | |
} | |
public enum ResolveType | |
{ | |
Pending, | |
Completed, | |
InvalidHost, | |
Timeout | |
} | |
} | |
} |
Updated and added ResolveTest
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result of the tests.