protocol.header

Read and write header data.

Reader functions take [buf start ctx] and return [end value] where end is the offset in buf after the read value(s).

Writer functions take [buf msg-map start ctx] and return end where end is the offset in buf after the written value(s).

DEFAULT-BUF-SIZE

read-header

(read-header buf start {:keys [readers spec path length], :as ctx})

Reads/decodes buf starting at start based on (:spec ctx) using reader functions in (:readers ctx).

Parameters:

  • buf: is the buffer to read from
  • start: start reading from buf at this offset
  • ctx: a map of additional parsing context:
    • readers: map of types to reader functions
    • spec: sequence of name, type, extra-ctx
    • name: field key used in returned map.
    • type: type to lookup in readers map
    • extra-ctx: extra context merged into ctx before reading:
      • default: ignored for reading
      • length:
        • number: the number of bytes from the start of field (offset)
        • field: the name of a previously read field that contains number of bytes from start of the field (offset)
        • defaults to length of remaining bytes in current context
    • msg-map: map of values previously read

read-header-full

(read-header-full buf start ctx)

Like read-header but returns only the read value.

readers

Header readers

write-header

(write-header buf msg-map start {:keys [writers spec path], :as ctx})

Writes/encodes data in msg-map into buf starting at start based on (:spec ctx) using writer functions in (:writers ctx).

Parameters:

  • buf: is the buffer to write into (if nil then allocates a buffer of DEFAULT-BUF-SIZE bytes)
  • msg-map: map of field names -> field values to encode.
  • start: start writing at offset
  • ctx: a map of additional parsing context:
    • writers is a map of types to writer functions
    • writer functions that take buf val start ctx
    • spec: sequence of name, type, lenth, default
      • name: field key to lookup in msg-map.
      • type: type to lookup in writers map
      • extra-ctx: extra context merged into ctx before writing
        • default: if msg-map does not contain name then this is used instead
        • length:
          • number: the number of bytes from the start of field (offset)
          • field: the name of a field in msg-map that contains number of bytes from start of the field (offset)
    • msg-map: parent msg-map for compound writers

write-header-full

(write-header-full buf msg-map start ctx)

Like write-header but allocates a default sized buffer if not provided and returns the encoded buffer sliced to size of the actual written data.

writers

Header writers