Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Base/Net/SimpleMLLPClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Net.Sockets;
using System.Net.Security;
Expand Down Expand Up @@ -118,14 +120,18 @@ public string SendHL7Message(string message, double timeout = 30000)
sw.Flush();

// Read the reply
List<byte> buffer = new List<byte>();
StringBuilder sb = new StringBuilder();
bool messageComplete = false;
DateTime startReadingTime = DateTime.Now;
while (!messageComplete)
{
int b = streamToUse.ReadByte();
if (b != -1)
sb.Append((char) b);
{
buffer.Add((byte)b);
sb.Append((char)b);
}

messageComplete = MLLP.ValidateMLLPMessage(sb);

Expand All @@ -135,9 +141,8 @@ public string SendHL7Message(string message, double timeout = 30000)
if (!messageComplete && DateTime.Now.Subtract(startReadingTime).TotalMilliseconds > timeout)
throw new TimeoutException($"Reading the HL7 reply timed out after {timeout} milliseconds.");
}
MLLP.StripMLLPContainer(sb);

return sb.ToString();
byte[] messageBytes = MLLP.StripMLLPContainer(buffer);
return (encodingForStream ?? Encoding.UTF8).GetString(messageBytes);
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Base/Util/MLLP.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace NHapiTools.Base.Util
{
Expand All @@ -24,6 +26,12 @@ public static void StripMLLPContainer(StringBuilder sb)
sb.Remove(0, 1);
sb.Remove(sb.Length - 2, 2);
}

public static byte[] StripMLLPContainer(List<byte> bytes)
{
// Strip the message of the MLLP container characters
return bytes.Skip(1).Take(bytes.Count - 3).ToArray();
}

/// <summary>
/// Validate the MLLP message containing the HL7 message
Expand Down