dotnet-core_mail-server/MailServer/DNS/DnsUdpMessageHandler.cs

64 lines
1.7 KiB
C#
Raw Permalink Normal View History

2016-11-26 11:11:27 +00:00
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using DnsClient.Protocol;
namespace DnsClient
{
public class DnsUdpMessageHandler : DnsMessageHandler, IDisposable
{
private readonly UdpClient _client = new UdpClient();
private bool _disposedValue = false;
public override bool IsTransientException<T>(T exception)
{
Debug.WriteLine("Check transient {0}.", exception);
if (exception is SocketException) return true;
return false;
}
public override async Task<DnsResponseMessage> QueryAsync(
IPEndPoint server,
DnsRequestMessage request,
CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
//using (var udpClient = new UdpClient())
//{
var data = GetRequestData(request);
await _client.SendAsync(data, data.Length, server);
var result = await _client.ReceiveAsync();
var response = GetResponseMessage(result.Buffer);
if (request.Header.Id != response.Header.Id)
{
throw new DnsResponseException("Header id missmatch.");
}
return response;
//}
}
protected override void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_client.Dispose();
}
_disposedValue = true;
}
base.Dispose(disposing);
}
}
}