Skip to content

Commit

Permalink
(#71) Get rid of custom resolver and use DnsClient in a mobile-friend…
Browse files Browse the repository at this point in the history
…ly way
  • Loading branch information
ForNeVeR authored and vitalyster committed Oct 3, 2021
1 parent 3d7203a commit 0f117f3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 135 deletions.
2 changes: 1 addition & 1 deletion SharpXMPP.NUnit/Compat/TcpClientExTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void TestCancellation()
var token = cts.Token;

using var client = new TcpClient();
var task = client.ConnectWithCancellationAsync(IPAddress.Loopback, port, token);
var task = client.ConnectWithCancellationAsync(new[] { IPAddress.Loopback }, port, token);
cts.Cancel();

try
Expand Down
13 changes: 0 additions & 13 deletions SharpXMPP.Shared/Compat/TcpClientEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ private static async Task AbandonOnCancel(this Task task, CancellationToken canc
// the resources will be freed upon its destruction. Which means we are free to just abandon the task in
// question.

public static Task ConnectWithCancellationAsync(
this TcpClient tcpClient,
IPAddress address,
int port,
CancellationToken cancellationToken)
{
#if NET5_0_OR_GREATER
return tcpClient.ConnectAsync(address, port, cancellationToken).AsTask();
#else
return tcpClient.ConnectAsync(address, port).AbandonOnCancel(cancellationToken);
#endif
}

public static Task ConnectWithCancellationAsync(
this TcpClient tcpClient,
IPAddress[] addresses,
Expand Down
117 changes: 0 additions & 117 deletions SharpXMPP.Shared/Resolver.cs

This file was deleted.

4 changes: 4 additions & 0 deletions SharpXMPP.Shared/SharpXMPP.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="System.Net.Security" Version="4.3.2" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="DnsClient" Version="1.5.0" />
</ItemGroup>
</Project>
20 changes: 16 additions & 4 deletions SharpXMPP.Shared/XmppTcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using DnsClient;
using SharpXMPP.Compat;
using SharpXMPP.Errors;
using SharpXMPP.XMPP;
Expand All @@ -24,6 +25,12 @@ public class XmppTcpConnection : XmppConnection
private object _terminationLock = new object();
private TcpClient _client;

/// <summary>
/// A list of name servers to use for the DNS lookup. Uses 1.1.1.1 by default. If empty, then will use the name
/// servers configured by the local network adapter(s).
/// </summary>
public IPAddress[] NameServers { get; set; } = { IPAddress.Parse("1.1.1.1") };

protected virtual int TcpPort
{
get { return 5222; }
Expand Down Expand Up @@ -294,13 +301,18 @@ private async Task<List<IPAddress>> ResolveHostAddresses(CancellationToken cance
{
List<IPAddress> HostAddresses = new List<IPAddress>();

var srvs = await Resolver.ResolveXMPPClient(Jid.Domain, cancellationToken);
if (srvs.Any())
var lookup = NameServers.Length > 0 ? new LookupClient(NameServers) : new LookupClient();

var response = await lookup.QueryAsync(
"_xmpp-client._tcp." + Jid.Domain,
QueryType.SRV,
cancellationToken: cancellationToken);
if (response.Answers.SrvRecords().Any())
{
foreach (var srv in srvs)
foreach (var srv in response.Answers.SrvRecords())
{
cancellationToken.ThrowIfCancellationRequested();
var addresses = await Dns.GetHostAddressesAsync(srv.Host);
var addresses = await Dns.GetHostAddressesAsync(srv.Target.Value);
HostAddresses.AddRange(addresses);
}
}
Expand Down

0 comments on commit 0f117f3

Please sign in to comment.