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).
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 fromstart
: start reading from buf at this offsetctx
: a map of additional parsing context:readers
: map of types to reader functions- reader functions take buf start end ctx
spec
: sequence of name, type, extra-ctxname
: field key used in returned map.type
: type to lookup in readers mapextra-ctx
: extra context merged into ctx before reading:default
: ignored for readinglength
:- 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
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 ofDEFAULT-BUF-SIZE
bytes)msg-map
: map of field names -> field values to encode.start
: start writing at offsetctx
: 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, defaultname
: field key to lookup in msg-map.type
: type to lookup in writers mapextra-ctx
: extra context merged into ctx before writingdefault
: if msg-map does not containname
then this is used insteadlength
:- 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.