nng/aio
asynchronous send and receive: submit now, collect later.
Since: Phase NG-B
Aio
(Aio)
a linear handle to one asynchronous operation slot.
Since: Phase NG-B
aio-alloc
(aio-alloc :)
allocate an operation slot.
(Result Aio int) -- err carries the nng error code (NNG_ENOMEM).
(let [a (ok-val (aio-alloc))]
(recv-aio s a)
(aio-wait a)
(aio-free a))
Since: Phase NG-B
aio-free
(aio-free [a : Aio] :)
release the slot. The single linear consumer.
| a | the slot, consumed |
(aio-free a)
Since: Phase NG-B
aio-set-timeout
(aio-set-timeout [^borrow a : Aio ms : int] :)
the deadline for this slot's later operations.
| a | the slot (borrowed) | |
| ms | milliseconds, or -2 / -1 / 0 as above |
(aio-set-timeout a 100)
Since: Phase NG-B
aio-cancel
(aio-cancel [^borrow a : Aio] :)
ask an in-flight operation to stop.
| a | the slot (borrowed) |
(aio-cancel a) (aio-wait a) ; => (err NNG_ECANCELED)
Since: Phase NG-B
send-payload-aio
(send-payload-aio [^borrow s : Socket ^borrow a : Aio b : Payload])
submit a send of a byte buffer.
| s | the socket (borrowed) | |
| a | the slot (borrowed) | |
| b | the payload (borrowed) |
(Result nil int) -- ok: submitted, collect with aio-wait / aio-try-result / aio-poll-fd; err: nothing submitted.
(send-payload-aio s a b)
Since: Phase NG-B
send-str-aio
(send-str-aio [^borrow s : Socket ^borrow a : Aio msg : cstr])
submit a send of a NUL-terminated string.
| s | the socket (borrowed) | |
| a | the slot (borrowed) | |
| msg | the payload (borrowed) |
(Result nil int) -- ok: submitted; err: nothing submitted.
(send-str-aio s a "job:42")
Since: Phase NG-B
recv-aio
(recv-aio [^borrow s : Socket ^borrow a : Aio])
submit a receive.
| s | the socket (borrowed) | |
| a | the slot (borrowed) |
(Result nil int) -- ok: submitted; err: nothing submitted.
(recv-aio s a)
Since: Phase NG-B
aio-wait
(aio-wait [^borrow a : Aio] :)
block until the operation finishes; its outcome.
| a | the slot (borrowed) |
(Result nil int) -- ok, or err carrying the operation's nng error (NNG_ETIMEDOUT, NNG_ECANCELED, NNG_ECLOSED, NNG_ENOTSUP, ...).
(when (ok? (aio-wait a)) (aio-take-payload a))
Since: Phase NG-B
aio-try-result
(aio-try-result [^borrow a : Aio] :)
the outcome if the operation has finished; never blocks.
| a | the slot (borrowed) |
(Option (Result nil int)) -- (none) while in flight; (some outcome) once finished.
(match (aio-try-result a)
(Some r) (finish r)
(None) (do-other-work))
Since: Phase NG-B
aio-take-payload
(aio-take-payload [^borrow a : Aio] :)
the received message, as an owned Payload.
| a | the slot (borrowed) |
(Result Payload int) -- ok: a fresh Payload the caller releases with payload-free; err: the receive's own error, or NNG_ESTATE when there is nothing to take (not a receive, still in flight, already taken).
(let [r (aio-take-payload a)]
(when (ok? r) (do (use (ok-val r)) (payload-free (ok-val r)))))
Since: Phase NG-B
aio-take-str
(aio-take-str [^borrow a : Aio] :)
the received message, as a malloc'd cstr.
| a | the slot (borrowed) |
(Result cstr int) -- ok: a fresh string the caller frees; err as for aio-take-payload.
(println (ok-val (aio-take-str a)))
Since: Phase NG-B
aio-poll-fd
(aio-poll-fd [^borrow a : Aio] :)
an fd that is readable while a finished operation awaits collection.
| a | the slot (borrowed) |
(Result PollFd int) -- err carries NNG_ENOMEM if the pipe could not be made.
(local-park-fd g (poll-fd->int (ok-val (aio-poll-fd a))) READ 2000)
Since: Phase NG-B