Parse a binary protocol with Erlang bit syntax

8 steps · about 1 h 58 min · Intermediate

  1. Learn segment sizes, types and endianness (reference, Erlang/OTP documentation, 25 min)

    The full guide to constructing and matching binaries.

  2. Work through the bit syntax section with RGB and TCP examples (tutorial, Learn You Some Erlang, 25 min)

    Friendly worked examples of pulling headers out of packets.

  3. Know big-endian versus little-endian byte order (article, Wikipedia, 10 min)

    Explains why the same bytes mean different numbers on the wire.

  4. Design a type-length-value packet (article, Wikipedia, 8 min)

    A simple framing format you can decode in one match.

  5. Use binary patterns in function heads (reference, Erlang/OTP documentation, 10 min)

    Shows how binary segments fit into ordinary pattern matching.

  6. Split and search binaries with the binary module (reference, Erlang/OTP documentation, 10 min)

    Covers split, match and part when a pattern alone is not enough.

  7. Build and match binaries efficiently (reference, Erlang/OTP documentation, 15 min)

    Explains sub-binaries and appending so parsers stay fast.

  8. Let gen_tcp frame length-prefixed packets (reference, Erlang/OTP documentation, 15 min)

    The {packet, N} option hands you whole messages to decode.