NormNode Refactor

* Updated NormNodeGetAddress to use byte* to avoid casting

* Updated NormNode GetCommand to use fixed statement

* Updated design
pull/85/head
mullerj 2024-08-27 21:40:12 -04:00 committed by GitHub
parent 6ae32b1d0e
commit e4513ccd53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 20 deletions

View File

@ -102,9 +102,9 @@ class NormApi <<static>> {
+ {static} <<extern>> NormDataAccessData(objectHandle:long) : nint
+ {static} <<extern>> NormObjectGetSender(objectHandle:long) : long
+ {static} <<extern>> NormNodeGetId(nodeHandle:long) : long
+ {static} <<extern>> NormNodeGetAddress(nodeHandle:long, addrBuffer:nint, bufferLen:int, port:int) : bool
+ <<unsafe>> {static} <<extern>> NormNodeGetAddress(nodeHandle:long, addrBuffer:byte*, bufferLen:int, port:int) : bool
+ {static} <<extern>> NormNodeGetGrtt(nodeHandle:long) : double
+ {static} <<extern>> NormNodeGetCommand(remoteSender:long, cmdBuffer:nint, buflen:int) : bool
+ <<unsafe>> {static} <<extern>> NormNodeGetCommand(remoteSender:long, cmdBuffer:byte*, buflen:int) : bool
+ {static} <<extern>> NormNodeFreeBuffers(remoteSender:long) : void
+ {static} <<extern>> NormNodeRetain(nodeHandle:long) : void
+ {static} <<extern>> NormNodeRelease(nodeHandle:long) : void

View File

@ -1081,7 +1081,7 @@ namespace Mil.Navy.Nrl.Norm
/// <param name="port">port number and/or specify a specific source address binding that is used for packet transmission.</param>
/// <returns>A value of true is returned upon success and false upon failure. An invalid nodeHandle parameter value would lead to such failure.</returns>
[DllImport(NORM_LIBRARY)]
public static extern bool NormNodeGetAddress(long nodeHandle, nint addrBuffer, ref int bufferLen, out int port);
public unsafe static extern bool NormNodeGetAddress(long nodeHandle, byte* addrBuffer, ref int bufferLen, out int port);
/// <summary>
/// This function retrieves the advertised estimate of group round-trip timing (GRTT) for the remote sender referenced by the given nodeHandle value.
@ -1104,7 +1104,7 @@ namespace Mil.Navy.Nrl.Norm
/// either no command was available or the provided buffer size (buflen parameter) was inadequate.
/// The value referenced by the buflen parameter is adjusted to indicate the actual command length (in bytes) upon return.</returns>
[DllImport(NORM_LIBRARY)]
public static extern bool NormNodeGetCommand(long remoteSender, nint cmdBuffer, ref int buflen);
public unsafe static extern bool NormNodeGetCommand(long remoteSender, byte* cmdBuffer, ref int buflen);
/// <summary>
/// This function releases memory resources that were allocated for a remote sender.

View File

@ -1,5 +1,4 @@
using System.Net;
using System.Runtime.InteropServices;
namespace Mil.Navy.Nrl.Norm
{
@ -48,9 +47,8 @@ namespace Mil.Navy.Nrl.Norm
{
var bufferLength = 256;
var buffer = stackalloc byte[bufferLength];
var addrBuffer = (nint)buffer;
if (!NormNodeGetAddress(_handle, addrBuffer, ref bufferLength, out int port))
if (!NormNodeGetAddress(_handle, buffer, ref bufferLength, out int port))
{
throw new IOException("Failed to get node address");
}
@ -81,20 +79,17 @@ namespace Mil.Navy.Nrl.Norm
throw new ArgumentOutOfRangeException(nameof(length), "The length is out of range");
}
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
try
unsafe
{
var bufferPtr = bufferHandle.AddrOfPinnedObject() + offset;
if (!NormNodeGetCommand(_handle, bufferPtr, ref length))
fixed (byte* bufferPtr = buffer)
{
if (!NormNodeGetCommand(_handle, bufferPtr + offset, ref length))
{
throw new IOException("Failed to get command");
}
}
finally
{
bufferHandle.Free();
}
return length;
}

View File

@ -1,6 +1,4 @@
using System.Runtime.InteropServices;
namespace Mil.Navy.Nrl.Norm
namespace Mil.Navy.Nrl.Norm
{
/// <summary>
/// A transport object of type NORM_OBJECT_STREAM.