Skip to content

Commit

Permalink
Use .NET 6.0 methods which are lower allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
alanmcgovern committed Oct 19, 2022
1 parent 4f1a615 commit 48ef888
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Mono.Nat.Console/Mono.Nat.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net6.0;net472</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions Mono.Nat/Mono.Nat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<PropertyGroup>
<GitThisAssembly>false</GitThisAssembly>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>

<MonoNatABIVersion>3.0.0.0</MonoNatABIVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -45,7 +45,7 @@

<Target Name="SetAssemblyVersion" BeforeTargets="GetAssemblyVersion" Condition="'$(RestoreSuccess)' == 'true' And '$(Configuration)' == 'Release' " DependsOnTargets="GitVersion">
<PropertyGroup>
<MonoNatFileVersion>3.0.3</MonoNatFileVersion>
<MonoNatFileVersion>3.0.4</MonoNatFileVersion>
<MonoNatInformationalVersion>$(MonoNatFileVersion)-$(GitBranch)+$(GitCommit)</MonoNatInformationalVersion>

<AssemblyVersion Condition="'$(AssemblyVersion)' == ''">$(MonoNatABIVersion)</AssemblyVersion>
Expand Down
12 changes: 12 additions & 0 deletions Mono.Nat/Pmp/PmpNatDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,22 @@ static async Task<ResponseMessage> SendMessageAsync (IPEndPoint deviceEndpoint,
tcs.Token.Register (() => udpClient.Dispose ());

var data = message.Encode ();
#if NETSTANDARD2_0 || NETSTANDARD2_1
await udpClient.SendAsync (data, data.Length, deviceEndpoint).ConfigureAwait (false);
#else
await udpClient.SendAsync (data, deviceEndpoint, CancellationToken.None).ConfigureAwait (false);
#endif
var receiveTask = ReceiveMessageAsync (udpClient);

var delay = PmpConstants.RetryDelay;
for (int i = 0; i < PmpConstants.RetryAttempts && !receiveTask.IsCompleted; i++) {
await Task.Delay (delay).ConfigureAwait (false);
delay = TimeSpan.FromTicks (delay.Ticks * 2);
#if NETSTANDARD2_0 || NETSTANDARD2_1
await udpClient.SendAsync (data, data.Length, deviceEndpoint).ConfigureAwait (false);
#else
await udpClient.SendAsync (data, deviceEndpoint, tcs.Token).ConfigureAwait (false);
#endif
}

tcs.Dispose ();
Expand All @@ -98,7 +106,11 @@ static async Task<ResponseMessage> SendMessageAsync (IPEndPoint deviceEndpoint,
static async Task<ResponseMessage> ReceiveMessageAsync (UdpClient udpClient)
{
while (true) {
#if NETSTANDARD2_0 || NETSTANDARD2_1
var receiveResult = await udpClient.ReceiveAsync ().ConfigureAwait (false);
#else
var receiveResult = await udpClient.ReceiveAsync (CancellationToken.None).ConfigureAwait (false);
#endif
var message = ResponseMessage.Decode (receiveResult.Buffer);
return message;
}
Expand Down
4 changes: 4 additions & 0 deletions Mono.Nat/Searcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ async Task ListenOneAsync (System.Net.Sockets.UdpClient udpClient, CancellationT
{
while (!token.IsCancellationRequested) {
try {
#if NETSTANDARD2_0 || NETSTANDARD2_1
var data = await udpClient.ReceiveAsync ();
#else
var data = await udpClient.ReceiveAsync (token);
#endif
var localEndPoint = (IPEndPoint) udpClient.Client.LocalEndPoint;
token.ThrowIfCancellationRequested ();

Expand Down

0 comments on commit 48ef888

Please sign in to comment.