Xirsys.Sockets.Accumulator.LengthPrefixed (xturn_sockets v2.2.0)

View Source

Frames packets using a fixed-size length header.

The popped packet is the body only (header is stripped). Multiple packets in one push/3 are drained by repeated pop/1.

Options

  • :header_size - header width in bytes (default: 2)
  • :length_offset - byte offset of the length field (default: 0)
  • :length_size - width of the length field in bytes (default: header size)
  • :endianness - :big or :little (default: :big)
  • :length_includes_header - whether length counts the header (default: false)
  • :max_size - max buffered bytes before :buffer_overflow (default: 65536)

Summary

Functions

Builds an empty byte buffer with the given header layout.

Extracts one body when a complete header+payload is buffered.

Appends chunk to the buffer and merges meta.

Functions

init(opts)

Builds an empty byte buffer with the given header layout.

Parameters

  • opts - see module options

    iex> alias Xirsys.Sockets.Accumulator.LengthPrefixed iex> acc = LengthPrefixed.init(header_size: 2) iex> acc = LengthPrefixed.push(acc, <<0, 3, "ABC">>, %{}) iex> {:ok, "ABC", %{}, acc} = LengthPrefixed.pop(acc) iex> elem(LengthPrefixed.pop(acc), 0) :more

pop(acc)

Extracts one body when a complete header+payload is buffered.

Parameters

push(acc, chunk, meta)

Appends chunk to the buffer and merges meta.

Parameters

  • acc - state from init/1
  • chunk - more bytes (may be a partial header or several packets)
  • meta - merged into the next successfully popped packet