nng/socket
socket lifecycle for nng (nanomsg-next-generation).
Since: Phase NG1
Socket
(Socket)
a linear handle to an open nng socket.
Since: Phase NG1
req-open
(req-open :)
open a REQ (request) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (req-open))]
(dial s "inproc://demo")
(close s))
Since: Phase NG1
rep-open
(rep-open :)
open a REP (reply) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (rep-open))]
(listen s "inproc://demo")
(close s))
Since: Phase NG1
pub-open
(pub-open :)
open a PUB (publish) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (pub-open))]
(listen s "inproc://feed")
(close s))
Since: Phase NG1
sub-open
(sub-open :)
open a SUB (subscribe) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (sub-open))]
(dial s "inproc://feed")
(sub-subscribe s "")
(close s))
Since: Phase NG1
push-open
(push-open :)
open a PUSH (pipeline producer) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (push-open))]
(listen s "inproc://jobs")
(close s))
Since: Phase NG1
pull-open
(pull-open :)
open a PULL (pipeline consumer) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (pull-open))]
(dial s "inproc://jobs")
(close s))
Since: Phase NG1
pair-open
(pair-open :)
open a PAIR (one-to-one) socket, protocol version 1.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (pair-open))]
(listen s "inproc://link")
(close s))
Since: Phase NG1
bus-open
(bus-open :)
open a BUS (many-to-many broadcast) socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (bus-open))]
(listen s "inproc://bus")
(close s))
Since: Phase NG1
surveyor-open
(surveyor-open :)
open a SURVEYOR socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (surveyor-open))]
(listen s "inproc://survey")
(close s))
Since: Phase NG1
respondent-open
(respondent-open :)
open a RESPONDENT socket.
(Result Socket int) -- err carries the nng error code.
(let [s (ok-val (respondent-open))]
(dial s "inproc://survey")
(close s))
Since: Phase NG1
dial
(dial [^borrow s : Socket url : cstr])
connect the socket to a remote address.
| s | the socket (borrowed) | |
| url | e.g. "inproc://jobs", "ipc:///tmp/x.sock", "tcp://host:5555", | |
| "ws://host:8080/jobs" |
(Result nil int) -- err carries the nng error code.
(dial s "inproc://jobs")
Since: Phase NG1
listen
(listen [^borrow s : Socket url : cstr])
bind the socket to a local address.
| s | the socket (borrowed) | |
| url | e.g. "inproc://jobs", "ipc:///tmp/x.sock", "tcp://0.0.0.0:5555", | |
| "ws://0.0.0.0:8080/jobs" |
(Result nil int) -- err carries the nng error code.
(listen s "inproc://jobs")
Since: Phase NG1
listen-bound-port
(listen-bound-port [^borrow s : Socket url : cstr])
bind the socket and report the TCP port it got.
| s | the socket (borrowed) | |
| url | a tcp:// or ws:// address; port 0 asks the OS for a free one |
(Result int int) -- ok carries the bound port; err carries the nng error code.
(let [port (ok-val (listen-bound-port s "ws://127.0.0.1:0/jobs"))]
(println port)) ; => e.g. 41733
Since: Phase NG-WS
close
(close [s : Socket] :)
close the socket. The single linear consumer.
| s | the socket, consumed. This is its one legal consumption, so any | |
| later use of the handle is rejected under -Xsubstructural. |
(close s)
Since: Phase NG1
set-recv-timeout-ms
(set-recv-timeout-ms [^borrow s : Socket ms : int] :)
bound how long a receive may block.
| s | the socket (borrowed) | |
| ms | milliseconds, or a negative value for "wait forever" |
(Result nil int) -- err carries the nng error code.
(set-recv-timeout-ms s 100)
Since: Phase NG1
set-send-timeout-ms
(set-send-timeout-ms [^borrow s : Socket ms : int] :)
bound how long a send may block.
| s | the socket (borrowed) | |
| ms | milliseconds, or a negative value for "wait forever" |
(Result nil int) -- err carries the nng error code.
(set-send-timeout-ms s 100)
Since: Phase NG1
err-str
(err-str [code : int] :)
human-readable message for an nng error code.
| code | the int an err result carries |
cstr -- e.g. "Timed out", "Connection refused".
(println (err-str (err-val r)))
Since: Phase NG1
timed-out?
(timed-out? [code : int] :)
does this error code mean the deadline expired?
| code | the int an err result carries |
bool -- true for NNG_ETIMEDOUT.
(if (timed-out? (err-val r)) (retry) (report r))
Since: Phase NG1
PollFd
(PollFd)
a file descriptor that may only be polled.
Since: Phase NG-A
poll-fd->int
(poll-fd->int [fd : PollFd] :)
the raw descriptor, for reactor-add-fd / local-park-fd.
| fd | the poll fd |
int -- the descriptor number. Poll it; never read, write or close it.
(reactor-add-fd r (poll-fd->int (ok-val (recv-poll-fd s))) READ cb ud)
Since: Phase NG-A
recv-poll-fd
(recv-poll-fd [^borrow s : Socket] :)
an fd that is readable while a message is waiting.
| s | the socket (borrowed) |
(Result PollFd int) -- err carries NNG_ENOTSUP on a protocol that cannot receive (PUB, PUSH), or another nng error code.
(let [fd (ok-val (recv-poll-fd sub))]
(reactor-add-fd r (poll-fd->int fd) READ drain-cb ud))
Since: Phase NG-A
send-poll-fd
(send-poll-fd [^borrow s : Socket] :)
an fd that is READABLE while a send would not block.
| s | the socket (borrowed) |
(Result PollFd int) -- err carries NNG_ENOTSUP on a protocol that cannot send (SUB, PULL), or another nng error code.
(let [fd (ok-val (send-poll-fd push))]
(reactor-add-fd r (poll-fd->int fd) READ flush-cb ud))
Since: Phase NG-A