diff --git a/Lidgren.Network/NetUtility.Dns.cs b/Lidgren.Network/NetUtility.Dns.cs index a0fa0766..385c8826 100644 --- a/Lidgren.Network/NetUtility.Dns.cs +++ b/Lidgren.Network/NetUtility.Dns.cs @@ -92,17 +92,7 @@ public static void ResolveAsync(string ipOrHost, int port, ResolveEndPointCallba public static void ResolveAsync(string ipOrHost, int port, AddressFamily? allowedFamily, ResolveEndPointCallback callback) { - ResolveAsync(ipOrHost, allowedFamily, adr => - { - if (adr == null) - { - callback(null); - } - else - { - callback(new NetEndPoint(adr, port)); - } - }); + ResolveAsync(ipOrHost, port, allowedFamily).ContinueWith(t => callback(t.Result)); } /// @@ -219,58 +209,7 @@ public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback [Obsolete("This function does not handle network errors properly, prefer task-based ResolveAsync instead.")] public static void ResolveAsync(string ipOrHost, AddressFamily? allowedFamily, ResolveAddressCallback callback) { - if (ResolveHead(ref ipOrHost, allowedFamily, out var resolve)) - { - callback(resolve); - return; - } - - // ok must be a host name - IPHostEntry entry; - try - { - Dns.BeginGetHostEntry(ipOrHost, delegate (IAsyncResult result) - { - try - { - entry = Dns.EndGetHostEntry(result); - } - catch (SocketException ex) - { - if (ex.SocketErrorCode == SocketError.HostNotFound) - { - //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost)); - callback(null); - return; - } - else - { - throw; - } - } - - if (entry == null) - { - callback(null); - return; - } - - // check each entry for a valid IP address - ResolveFilter(allowedFamily, entry.AddressList); - }, null); - } - catch (SocketException ex) - { - if (ex.SocketErrorCode == SocketError.HostNotFound) - { - //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost)); - callback(null); - } - else - { - throw; - } - } + ResolveAsync(ipOrHost, allowedFamily).ContinueWith(t => callback(t.Result)); } /// diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 14b9ed90..f7e35ade 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -6,6 +6,7 @@ - Removed .NET Core 3.1 and .NET 5 target frameworks - Updated dependencies - More and improved doc comments. +- Fix callback-based `NetUtility.ResolveAsync` functions never running their callback. (bug introduced in 0.1.0) ## 0.2.7 diff --git a/UnitTests/NetUtilityTests.cs b/UnitTests/NetUtilityTests.cs index a9d6e71e..37fa9254 100644 --- a/UnitTests/NetUtilityTests.cs +++ b/UnitTests/NetUtilityTests.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Sockets; using System.Threading.Tasks; using Lidgren.Network; @@ -111,6 +112,19 @@ public async Task TestResolveAsyncNothing() Assert.That(addr, Is.Null); } +#pragma warning disable CS0618 // Type or member is obsolete + [Test] + public async Task TestResolveAsyncCallback() + { + var tcs = new TaskCompletionSource(); + NetUtility.ResolveAsync("example.com", result => tcs.SetResult(result)); + + var result = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(30)); + + Assert.That(result, Is.Not.Null); + } +#pragma warning restore CS0618 // Type or member is obsolete + private static void IgnoreIfActions() { // https://github.com/actions/virtual-environments/issues/668