The use case is:
- Connect to a TCP server that delivers a data stream (from a Terminal Server in the real world)
- Extract a message that is framed by a SOM and length field
- Decode the message into its length and payload (real world has many fields)
This code is most conveniently tested via netcat, using human readable hex input.
- tcpFlow connects to server at 127.0.0.1 port 2558
- bytesFlow converts ByteString to hex string representation to ByteString. Only exists to support test via netcat.
- bitsFlow frames ByteStrings into a stream of zero or BitVectors. Each BitVector contains a fully framed message or “noise” bytes that appear between the frames.
- decodedFlow is a stream of (length,payload)
- runWith executes the flow, printing the decoded tuples.
Use scodec to extract the length field and following bytes.
Extract a frame that starts with a SOM byte (0x2b), followed by a one byte unsigned length field. An escape byte (0x10) is used when SOM or escape appears within the payload.
The instance is stateful since a valid frame can span many incoming ByteStrings, requiring the buffering of the data received to this point. A single ByteString might also contain several frames.
The bytes outside of the frame are also delivered, to support error reporting. The real code has checksums and precise decoding of the structure so the two cases are easily distinguished.