nng/msg
blocking send and receive over an nng socket.
Since: Phase NG2
send-str
(send-str [^borrow s : Socket msg : cstr])
send a NUL-terminated string as one message.
| s | the socket (borrowed) | |
| msg | the payload (borrowed; the caller keeps ownership) |
(Result nil int) -- err carries the nng error code.
(send-str s "job:42")
Since: Phase NG2
recv-str
(recv-str [^borrow s : Socket])
receive one message as a malloc'd cstr; the caller frees it.
| s | the socket (borrowed) |
(Result cstr int) -- ok carries a fresh malloc'd string; err carries the nng error code.
(let [r (recv-str s)]
(if (ok? r) (println (ok-val r)) (println (err-str (err-val r)))))
Since: Phase NG2
send-payload
(send-payload [^borrow s : Socket b : Payload])
send a byte buffer as one message.
| s | the socket (borrowed) | |
| b | the payload (borrowed) |
(Result nil int) -- err carries the nng error code.
(let [b (payload-of-cstr "job:42")]
(send-payload s b)
(payload-free b))
Since: Phase NG2
recv-payload
(recv-payload [^borrow s : Socket])
receive one message as an owned Payload; the caller frees it.
| s | the socket (borrowed) |
(Result Payload int) -- ok carries a fresh owned Payload; release it with `payload-free`. err carries the nng error code.
(let [r (recv-payload s)]
(when (ok? r)
(let [b (ok-val r)]
(println (payload->hex b))
(payload-free b))))
Since: Phase NG2
try-recv-str
(try-recv-str [^borrow s : Socket])
receive one message if one is waiting; never blocks.
| s | the socket (borrowed) |
(Result (Option cstr) int) -- (ok (some m)) a fresh malloc'd string the caller frees; (ok (none)) nothing is waiting; (err code) any other failure, e.g. NNG_ENOTSUP on a socket that cannot receive.
(match (ok-val (try-recv-str s))
(Some m) (handle m)
(None) (println "nothing yet"))
Since: Phase NG-A
try-recv-payload
(try-recv-payload [^borrow s : Socket])
receive one binary message if one is waiting.
| s | the socket (borrowed) |
(Result (Option Payload) int) -- (ok (some b)) a fresh owned Payload the caller releases with payload-free; (ok (none)) nothing is waiting; (err code) any other failure.
(match (ok-val (try-recv-payload s))
(Some b) (do (handle b) (payload-free b))
(None) nil)
Since: Phase NG-A
try-send-str
(try-send-str [^borrow s : Socket msg : cstr])
send a string if the socket can take it now; never blocks.
| s | the socket (borrowed) | |
| msg | the payload (borrowed) |
(Result bool int) -- (ok true) sent; (ok false) the send would block (no peer ready, or its queue is full) and nothing was sent; (err code) any other failure, e.g. NNG_ENOTSUP on a socket that cannot send.
(when (not (ok-val (try-send-str s "job:42")))
(retry-later))
Since: Phase NG-A
try-send-payload
(try-send-payload [^borrow s : Socket b : Payload])
send a byte buffer if the socket can take it now.
| s | the socket (borrowed) | |
| b | the payload (borrowed) |
(Result bool int) -- (ok true) sent; (ok false) the send would block and nothing was sent; (err code) any other failure.
(let [b (payload-of-cstr "job:42")]
(try-send-payload s b)
(payload-free b))
Since: Phase NG-A
sub-subscribe
(sub-subscribe [^borrow s : Socket topic : cstr] :)
add a topic prefix filter to a SUB socket.
| s | the socket (borrowed); must be a SUB socket, or the result is | |
| `err` carrying NNG_ENOTSUP. v0 ships one `Socket` type, so that | ||
| is a runtime error rather than a compile-time one. | ||
| topic | the byte prefix to accept; "" accepts every message |
(Result nil int) -- err carries the nng error code.
(sub-subscribe s "temp:")
Since: Phase NG3
sub-unsubscribe
(sub-unsubscribe [^borrow s : Socket topic : cstr] :)
drop a topic prefix filter from a SUB socket.
| s | the socket (borrowed); must be a SUB socket | |
| topic | the exact prefix passed to `sub-subscribe` |
(Result nil int) -- err carries the nng error code.
(sub-unsubscribe s "temp:")
Since: Phase NG3