Skip to content

Instantly share code, notes, and snippets.

@ferrata
Created May 11, 2018 14:52
Show Gist options
  • Save ferrata/48638deefa3a18836e8121d350137840 to your computer and use it in GitHub Desktop.
Save ferrata/48638deefa3a18836e8121d350137840 to your computer and use it in GitHub Desktop.
internal class Hl7Handler : SimpleChannelInboundHandler<IByteBuffer>
{
private class Markers
{
public static readonly byte[] start = { 0x0b };
public static readonly byte[] end = { 0x1c, 0x0d };
}
protected override void ChannelRead0(IChannelHandlerContext cx, IByteBuffer data)
{
if (data.GetByte(0) != Markers.start[0])
return;
var messageData = data.SkipBytes(1).ToString(Encoding.ASCII);
Console.WriteLine($"{cx.Name}: Received {messageData?.Length} bytes");
var parser = new PipeParser();
var message = parser.Parse(messageData);
Console.WriteLine($"{cx.Name}: Decoded as {message}");
var ack = new Ack("Test", "test-env");
var encoded = parser.Encode(ack.MakeACK(message));
var buff = Encoding.ASCII.GetBytes(encoded);
var ackMessage = Markers.start.Concat(buff).Concat(Markers.end).ToArray();
cx.WriteAsync(Unpooled.WrappedBuffer(ackMessage));
}
public override void ChannelReadComplete(IChannelHandlerContext cx)
{
cx.Flush();
}
public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
{
Console.WriteLine(exception);
context.CloseAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment