Skip to content

Commit

Permalink
Make NetUtility.ResolveAsync callback methods use task-based implemen…
Browse files Browse the repository at this point in the history
…tation.

This fixes them not working (oops) and reduces the amount of code.
  • Loading branch information
PJB3005 committed Jan 23, 2024
1 parent 00b6c28 commit cd916e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 63 deletions.
65 changes: 2 additions & 63 deletions Lidgren.Network/NetUtility.Dns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,7 @@ public static void ResolveAsync(string ipOrHost, int port, ResolveEndPointCallba
public static void ResolveAsync(string ipOrHost, int port, AddressFamily? allowedFamily,

This comment has been minimized.

Copy link
@AgentFire

AgentFire Jun 15, 2024

Mark everything callback-based deprecated and go full async? :)

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));
}

/// <summary>
Expand Down Expand Up @@ -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));
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions UnitTests/NetUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Lidgren.Network;
Expand Down Expand Up @@ -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<IPAddress?>();
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
Expand Down

0 comments on commit cd916e1

Please sign in to comment.