How to parser stream data from socket

HI,
I am using :gen_tcp.recv(socket, 0) to get stream data from tcp connection. The data format is like this.

How coud I exact the HL7 message from such kind of data stream?
Seems Pattern match can’t handle such case. I think I need method fo find the 0x1c and 0x0d from the stream data, but have no idear how to do this.

2 Likes

Sunday if often a bit quiet here, and I am far from the right person for this job. But I can point you at some things to think about:

  1. :gen_tcp:recv doesn’t guarantee the delivery of a single message (in the HL7 sense) in a single receive. So you will need to recurse and keep receiving until the socket closes (by you or by the sending socket). There are probably libraries out there that can help you with this.

  2. Then you would have a consumer function that parses the protocol bytes to extract the message.

  3. And then a message parser to parse the HL7 message

I suggest have a look at ranch which is a socket acceptor pool and which has some good guides for writing protocol handlers and binary parsers. In particular these sections of the user guide:

  • Transport which is how you would receive TCP packets
  • Protocol which is where you would decode the protocol format you described and extract the message
  • Parser which is where you can work on parsing the message (albeit there are many other ways to do this too)
1 Like
  1. You can’t do this through pattern matching. Everything but the last item needs to have a length known at compile time. So your conclusion is correct. You can receive packets of a certain length and then search for the <<0x1c, 0x0d>> pattern by yourself. :binary can help you with that. You then need to build your HL7 part from the current and the previous chunks, you also need to store the remainder of the current chunk in the state of your receiver. Sadly there is no other way than that :frowning:
  2. I hope that the message itself is defined in a way that it can’t contain the <<0x1c, 0x0d>> sequence…
  3. Perhaps the HL7 format does itself specify its length as part of the first few bytes, perhaps you can use that info?
3 Likes

Thanks, HL7 don’t contain length info so I will need action 1 and 2 that’s sad :frowning:

Thanks, I will explore ranch which seem has some addtional benifit.