From e4513ccd53ac2ed5605a1bf01d1c4558d796c8ae Mon Sep 17 00:00:00 2001 From: mullerj Date: Tue, 27 Aug 2024 21:40:12 -0400 Subject: [PATCH] NormNode Refactor * Updated NormNodeGetAddress to use byte* to avoid casting * Updated NormNode GetCommand to use fixed statement * Updated design --- .../design/Mil/Navy/Nrl/Norm/NormApi.puml | 4 ++-- src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs | 4 ++-- src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs | 21 +++++++------------ .../src/Mil.Navy.Nrl.Norm/NormStream.cs | 4 +--- 4 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml b/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml index 1cdc009..bddbac5 100644 --- a/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml +++ b/src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml @@ -102,9 +102,9 @@ class NormApi <> { + {static} <> NormDataAccessData(objectHandle:long) : nint + {static} <> NormObjectGetSender(objectHandle:long) : long + {static} <> NormNodeGetId(nodeHandle:long) : long - + {static} <> NormNodeGetAddress(nodeHandle:long, addrBuffer:nint, bufferLen:int, port:int) : bool + + <> {static} <> NormNodeGetAddress(nodeHandle:long, addrBuffer:byte*, bufferLen:int, port:int) : bool + {static} <> NormNodeGetGrtt(nodeHandle:long) : double - + {static} <> NormNodeGetCommand(remoteSender:long, cmdBuffer:nint, buflen:int) : bool + + <> {static} <> NormNodeGetCommand(remoteSender:long, cmdBuffer:byte*, buflen:int) : bool + {static} <> NormNodeFreeBuffers(remoteSender:long) : void + {static} <> NormNodeRetain(nodeHandle:long) : void + {static} <> NormNodeRelease(nodeHandle:long) : void diff --git a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs index ab63936..d150d99 100644 --- a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs +++ b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs @@ -1081,7 +1081,7 @@ namespace Mil.Navy.Nrl.Norm /// port number and/or specify a specific source address binding that is used for packet transmission. /// A value of true is returned upon success and false upon failure. An invalid nodeHandle parameter value would lead to such failure. [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); /// /// 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. [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); /// /// This function releases memory resources that were allocated for a remote sender. diff --git a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs index 19dcdb4..dc8dc95 100644 --- a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs +++ b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs @@ -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) { - throw new IOException("Failed to get command"); + if (!NormNodeGetCommand(_handle, bufferPtr + offset, ref length)) + { + throw new IOException("Failed to get command"); + } } - } - finally - { - bufferHandle.Free(); } + return length; } diff --git a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs index a0eb365..1b66637 100644 --- a/src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs +++ b/src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs @@ -1,6 +1,4 @@ -using System.Runtime.InteropServices; - -namespace Mil.Navy.Nrl.Norm +namespace Mil.Navy.Nrl.Norm { /// /// A transport object of type NORM_OBJECT_STREAM.