Skip to content

Commit

Permalink
NormStream Refactor
Browse files Browse the repository at this point in the history
* Updated NormStream Write to use fixed statement

* Updated NormStream Read to use fixed statement

* Updated design
  • Loading branch information
mullerj committed Aug 28, 2024
1 parent 32c3eb5 commit 6ae32b1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 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 @@ -56,7 +56,7 @@ class NormApi <<static>> {
+ {static} <<extern>> NormRequeueObject(sessionHandle:long, objectHandle:long) : bool
+ {static} <<extern>> NormStreamOpen(sessionHandle:long, bufferSize:long, infoPtr:nint, infoLen:int) : long
+ {static} <<extern>> NormStreamClose(streamHandle:long, graceful:bool) : void
<<internal>> {static} <<extern>> NormStreamWrite(streamHandle:long, buffer:nint, numBytes:int) : int
+ <<unsafe>> {static} <<extern>> NormStreamWrite(streamHandle:long, buffer:byte*, numBytes:int) : int
+ {static} <<extern>> NormStreamFlush(streamHandle:long, eom:bool, flushMode:NormFlushMode) : void
+ {static} <<extern>> NormStreamSetAutoFlush(streamHandle:long, flushMode:NormFlushMode) : void
+ {static} <<extern>> NormStreamSetPushEnable(streamHandle:long, pushEnable:bool) : void
Expand Down Expand Up @@ -85,7 +85,7 @@ class NormApi <<static>> {
+ {static} <<extern>> NormNodeSetRepairBoundary(remoteSender:long, repairBoundary:NormRepairBoundary) : void
+ {static} <<extern>> NormSetDefaultRxRobustFactor(sessionHandle:long, robustFactor:int) : void
+ {static} <<extern>> NormNodeSetRxRobustFactor(remoteSender:long, robustFactor:int) : void
+ {static} <<extern>> NormStreamRead(streamHandle:long, buffer:nint, numBytes:int) : bool
+ <<unsafe>> {static} <<extern>> NormStreamRead(streamHandle:long, buffer:byte*, numBytes:int) : bool
+ {static} <<extern>> NormStreamSeekMsgStart(streamHandle:long) : bool
+ {static} <<extern>> NormStreamGetReadOffset(streamHandle:long) : long
+ {static} <<extern>> NormObjectGetType(objectHandle:long) : NormObjectType
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 @@ -591,7 +591,7 @@ public struct NormEvent
/// <param name="numBytes">The numBytes parameter indicates the length of the data content.</param>
/// <returns>This function returns the number of bytes of data successfully enqueued for NORM stream transmission.</returns>
[DllImport(NORM_LIBRARY)]
internal static extern int NormStreamWrite(long streamHandle, nint buffer, int numBytes);
public unsafe static extern int NormStreamWrite(long streamHandle, byte* buffer, int numBytes);

/// <summary>
/// This function causes an immediate "flush" of the transmit stream specified by the streamHandle parameter.
Expand Down Expand Up @@ -895,7 +895,7 @@ public struct NormEvent
/// <returns>This function normally returns a value of true. However, if a break in the integrity of the reliable received stream
/// occurs(or the stream has been ended by the sender), a value of false is returned to indicate the break. </returns>
[DllImport(NORM_LIBRARY)]
public static extern bool NormStreamRead(long streamHandle, nint buffer, ref int numBytes);
public unsafe static extern bool NormStreamRead(long streamHandle, byte* buffer, ref int numBytes);

/// <summary>
/// This function advances the read offset of the receive stream referenced by the streamHandle parameter to align
Expand Down
30 changes: 11 additions & 19 deletions src/dotnet/src/Mil.Navy.Nrl.Norm/NormStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,12 @@ public int Write(byte[] buffer, int offset, int length)
}

int numBytes;
var bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);

try
{
var bufferPtr = bufferHandle.AddrOfPinnedObject() + offset;
numBytes = NormStreamWrite(_handle, bufferPtr, length);
}
finally
unsafe
{
bufferHandle.Free();
fixed (byte* bufferPtr = buffer)
{
numBytes = NormStreamWrite(_handle, bufferPtr + offset, length);
}
}

return numBytes;
Expand Down Expand Up @@ -158,20 +154,16 @@ public int Read(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 (!NormStreamRead(_handle, bufferPtr, ref length))
fixed (byte* bufferPtr = buffer)
{
length = -1;
if (!NormStreamRead(_handle, bufferPtr + offset, ref length))
{
length = -1;
}
}
}
finally
{
bufferHandle.Free();
}

return length;
}
Expand Down

0 comments on commit 6ae32b1

Please sign in to comment.