Updated NormNode.Address to use stackalloc and create IPAddress from bytes

pull/85/head
mullerj 2024-08-25 14:39:24 -04:00 committed by GitHub
parent f04faca08a
commit 022e1204ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 17 deletions

View File

@ -1,5 +1,4 @@
using Mil.Navy.Nrl.Norm.Buffers; using System.Net;
using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Mil.Navy.Nrl.Norm namespace Mil.Navy.Nrl.Norm
@ -43,29 +42,21 @@ namespace Mil.Navy.Nrl.Norm
/// <summary> /// <summary>
/// The current network source address detected for packets received from remote NORM sender. /// The current network source address detected for packets received from remote NORM sender.
/// </summary> /// </summary>
public IPEndPoint Address public unsafe IPEndPoint Address
{ {
get get
{ {
var bufferLength = 256; var bufferLength = 256;
using var buffer = ByteBuffer.AllocateDirect(bufferLength); var buffer = stackalloc byte[bufferLength];
int port; var addrBuffer = (nint)buffer;
unsafe if (!NormNodeGetAddress(_handle, addrBuffer, ref bufferLength, out int port))
{ {
byte* addrBuffer = null; throw new IOException("Failed to get node address");
buffer.AcquirePointer(ref addrBuffer);
if (!NormNodeGetAddress(_handle, (nint)addrBuffer, ref bufferLength, out port))
{
throw new IOException("Failed to get node address");
}
} }
var addressBytes = new byte[bufferLength]; var addressBytes = new ReadOnlySpan<byte>(buffer, bufferLength);
buffer.ReadArray(0, addressBytes, 0, bufferLength); var ipAddress = new IPAddress(addressBytes);
var ipAddressText = string.Join('.', addressBytes);
var ipAddress = IPAddress.Parse(ipAddressText);
return new IPEndPoint(ipAddress, port); return new IPEndPoint(ipAddress, port);
} }
} }