Skip to content

Commit

Permalink
NormNode Refactor
Browse files Browse the repository at this point in the history
* Updated NormNodeGetAddress to use byte* to avoid casting

* Updated NormNode GetCommand to use fixed statement

* Updated design
  • Loading branch information
mullerj authored Aug 28, 2024
1 parent 6ae32b1 commit e4513cc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/dotnet/design/Mil/Navy/Nrl/Norm/NormApi.puml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ public struct NormEvent
/// <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.
Expand All @@ -1104,7 +1104,7 @@ public struct NormEvent
/// 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.
Expand Down
21 changes: 8 additions & 13 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using System.Runtime.InteropServices;

namespace Mil.Navy.Nrl.Norm
{
Expand Down Expand Up @@ -48,9 +47,8 @@ public unsafe IPEndPoint Address
{
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");
}
Expand Down Expand Up @@ -81,20 +79,17 @@ public int GetCommand(byte[] buffer, int offset, int length)
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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit e4513cc

Please sign in to comment.