nng/payload
an owned, length-prefixed byte buffer for nng messages.
Since: Phase NG2
Payload
(Payload)
an owned length-prefixed byte buffer (ABI: ptr<void>).
payload-alloc
(payload-alloc [n : int] :)
allocate a zeroed buffer of `n` bytes.
| n | number of data bytes (not counting the 8-byte length prefix) |
A fresh owned Payload; release it with payload-free.
(let [b (payload-alloc 4)] (payload-len b)) ; => 4
Since: Phase NG2
payload-of-cstr
(payload-of-cstr [s : cstr] :)
copy a NUL-terminated string into a fresh buffer.
| s | the string to copy (borrowed; the caller keeps ownership) |
A fresh owned Payload; release it with payload-free.
(let [b (payload-of-cstr "job:42")] (payload-len b)) ; => 6
Since: Phase NG2
payload-len
(payload-len [b : Payload] :)
the number of data bytes the buffer holds.
| b | the buffer to measure |
int -- the byte length (0 for a null handle).
(payload-len (payload-of-cstr "abc")) ; => 3
Since: Phase NG2
payload-data
(payload-data [b : Payload] :)
a borrowed pointer to the buffer's first data byte.
| b | the buffer to borrow from |
ptr<void> -- the data region (past the length prefix).
(let [b (payload-alloc 8)] (payload-data b))
Since: Phase NG2
payload-free
(payload-free [b : Payload] :)
release the buffer.
| b | the buffer whose owning handle is being dropped |
(payload-free (ok-val (recv-payload s)))
Since: Phase NG2
payload-byte
(payload-byte [b : Payload i : int] :)
the byte at index i, or -1 when out of range.
| b | the buffer to read | |
| i | zero-based byte index |
int -- 0..255, or -1 if i is outside [0, len).
(payload-byte (payload-of-cstr "A") 0) ; => 65
Since: Phase NG2
payload-empty?
(payload-empty? [b : Payload] :)
true when the buffer carries no data bytes.
| b | the buffer to test |
bool -- true for a zero-length buffer (and for a null handle).
(payload-empty? (payload-alloc 0)) ; => true
Since: Phase NG2
payload->cstr
(payload->cstr [b : Payload] :)
a fresh NUL-terminated copy of the payload.
| b | the buffer to copy from (borrowed) |
cstr -- a malloc'd string the caller owns.
(payload->cstr (payload-of-cstr "pong")) ; => "pong"
Since: Phase NG2
payload->hex
(payload->hex [b : Payload] :)
lowercase hex rendering of the payload, for tests and logs.
| b | the buffer to render (borrowed) |
cstr -- a malloc'd string the caller owns; "" for an empty buffer.
(payload->hex (payload-of-cstr "A")) ; => "41"
Since: Phase NG2
Internal definitions
__payload-alloc-raw-- internal: allocate a zeroed buffer, as a bare pointer.__payload-of-cstr-raw-- internal: copy a cstr's bytes, as a bare pointer.payload-- byte-wise equality.