Skip to content

Commit

Permalink
feat(DohClient): less memory consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 19, 2018
1 parent 7186eed commit 336d624
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
12 changes: 9 additions & 3 deletions src/DohClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ public override async Task<Message> QueryAsync(
new CancellationTokenSource(Timeout).Token);

// Post the request.
var content = new ByteArrayContent(request.ToByteArray());
content.Headers.ContentType = new MediaTypeHeaderValue(DnsWireFormat);
var httpResponse = await HttpClient.PostAsync(ServerUrl, content, cts.Token);
HttpResponseMessage httpResponse;
using (var ms = new MemoryStream())
{
request.Write(ms);
ms.Position = 0;
var content = new StreamContent(ms);
content.Headers.ContentType = new MediaTypeHeaderValue(DnsWireFormat);
httpResponse = await HttpClient.PostAsync(ServerUrl, content, cts.Token);
}

// Check the HTTP response.
httpResponse.EnsureSuccessStatusCode();
Expand Down
18 changes: 9 additions & 9 deletions src/DotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,17 @@ byte[] BuildRequest(Message request)
paddingOption.Padding = new byte[need];
};

var udpRequest = request.ToByteArray();
byte[] length = BitConverter.GetBytes((ushort)udpRequest.Length);
if (BitConverter.IsLittleEndian)
using (var tcpRequest = new MemoryStream())
{
Array.Reverse(length);
tcpRequest.WriteByte(0); // two byte length prefix
tcpRequest.WriteByte(0);
request.Write(tcpRequest); // udpRequest
var length = (ushort)(tcpRequest.Length - 2);
tcpRequest.Position = 0;
tcpRequest.WriteByte((byte) (length >> 8));
tcpRequest.WriteByte((byte) (length));
return tcpRequest.ToArray();
}
var tcpRequest = new byte[length.Length + udpRequest.Length];
length.CopyTo(tcpRequest, 0);
udpRequest.CopyTo(tcpRequest, length.Length);

return tcpRequest;
}

/// <summary>
Expand Down

0 comments on commit 336d624

Please sign in to comment.