我想使用 Kestrel 和 System.IO.Pipelines 包创建一个 TCP 侦听器。我收到的消息将始终是HL7 消息。一个示例消息可能是
MSH|^~&|MegaReg|XYZHospC|SuperOE|XYZImgCtr|20060529090131-0500||ADT^A01^ADT_A01|01052901|P|2.5 EVN||200605290901||||200605290900 PID|||56782445^^^UA ||KLEINSAMPLE^BARRY^Q^JR||19620910|M||2028-9^^HL70005^RA99113^^XYZ|260 古德温克雷斯特大道^^伯明翰^AL^35209^^M~尼克尔的泡菜^10000 W 100th AVE ^伯明翰^AL^35200^^O|||||||0105I30001^^^99DEF^AN PV1||I|W^389^1^UABH^^^^3||||12345^摩根^REX^ J^^^MD^0010^UAMC^L||67890^GRAINGER^LUCY^X^^^MD^0010^UAMC^L|MED|||||A0||13579^POTTER^SHERMAN^T^^^ MD^0010^UAMC^L|||||||||||||||||||||||||200605290900 OBX|1|NM|^体高||1.80|m^米^ISO+|||||F OBX|2|NM|^体重||79|kg^公斤^ISO+|||||F AL1|1||^阿司匹林 DG1|1||786.50^胸痛,未指定^I9|||A
唯一需要注意的是,每条传入的 HL7 消息都以垂直制表符开头,因此您知道消息从哪里开始。每个 HL7 消息都包含多个段,所以我想我必须遍历每个段。处理完请求后,我想发回一条 HL7 消息作为响应。首先我想出了这个
internal class HL7Listener : ConnectionHandler
{
public override async Task OnConnectedAsync(ConnectionContext connection)
{
IDuplexPipe pipe = connection.Transport;
await FillPipe(pipe.Output);
await ReadPipe(pipe.Input);
}
private async Task FillPipe(PipeWriter pipeWriter)
{
const int minimumBufferSize = 512;
while (true)
{
Memory<byte> memory = pipeWriter.GetMemory(minimumBufferSize);
try
{
int bytesRead = 32; // not sure what to do here
if (bytesRead == 0)
{
break;
}
pipeWriter.Advance(bytesRead);
}
catch (Exception ex)
{
// ... something failed ...
break;
}
FlushResult result = await pipeWriter.FlushAsync();
if (result.IsCompleted)
{
break;
}
}
pipeWriter.Complete();
}
private async Task ReadPipe(PipeReader pipeReader)
{
while (true)
{
ReadResult result = await pipeReader.ReadAsync();
ReadOnlySequence<byte> buffer = result.Buffer;
SequencePosition? position;
do
{
position = buffer.PositionOf((byte)'\v');
if (position != null)
{
ReadOnlySequence<byte> line = buffer.Slice(0, position.Value);
// ... Process the line ...
buffer = buffer.Slice(buffer.GetPosition(1, position.Value));
}
}
while (position != null);
pipeReader.AdvanceTo(buffer.Start, buffer.End);
if (result.IsCompleted)
{
break;
}
}
pipeReader.Complete();
}
}
不幸的是,我在一些事情上苦苦挣扎:
- 该部分
int bytesRead = 32;
,我如何知道已读取了多少字节?或者如何使用 writer 实例阅读? - 目前调试器不会在
// ... Process the line ...
. 基本上我必须提取整个 HL7 消息,以便我可以使用我的 HL7 解析器来转换消息字符串。 - 我必须在哪里回应?打电话后
await ReadPipe(pipe.Input);
?通过使用await connection.Transport.Output.WriteAsync(/* the HL7 message to send back */);
?