Skip to content

Commit 778c92d

Browse files
committed
tidying async code
1 parent b8a4bc0 commit 778c92d

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

R/aio.R

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
#' @return The passed Aio object (invisibly), or NULL if non-blocking and the
1313
#' Aio has yet to resolve.
1414
#'
15-
#' @details To access the values directly, use for example on a sendAio 'x':
16-
#' \code{call_aio(x)$result}.
15+
#' @details For a 'recvAio', the received raw vector will be attached in \code{$raw}
16+
#' (unless 'keep.raw' was set to FALSE when receiving), and the converted R
17+
#' object in \code{$data}.
1718
#'
1819
#' For a 'sendAio', the send result will be attached to the Aio in \code{$result}.
1920
#' This will be zero on success.
2021
#'
21-
#' For a 'recvAio', the received raw vector will be attached in \code{$raw}
22-
#' (unless 'keep.raw' was set to FALSE when receiving), and the converted R
23-
#' object in \code{$data}.
22+
#' To access the values directly, use for example on a sendAio 'x':
23+
#' \code{call_aio(x)$result}.
2424
#'
2525
#' For a 'recvAio', in case of an error in unserialisation or data conversion,
2626
#' the received raw vector will always be saved in \code{$raw} to allow the
2727
#' data to be recovered.
2828
#'
29-
#' Once the result is retrieved, the Aio is deallocated and only the result
30-
#' is stored in the Aio object.
29+
#' Once the result has been successfully retrieved, the Aio is deallocated
30+
#' and only the result is stored in the Aio object.
3131
#'
3232
#' @section Non-blocking:
3333
#'
@@ -68,11 +68,11 @@ call_aio <- function(aio, block = TRUE) {
6868

6969
if (inherits(aio, "recvAio")) {
7070

71-
if (!missing(block) && !isTRUE(block)) {
71+
if (missing(block) || isTRUE(block)) {
72+
res <- .Call(rnng_aio_wait_get_msg, .subset2(aio, "aio"))
73+
} else {
7274
res <- .Call(rnng_aio_get_msg, .subset2(aio, "aio"))
7375
missing(res) && return()
74-
} else {
75-
res <- .Call(rnng_aio_wait_get_msg, .subset2(aio, "aio"))
7676
}
7777
mode <- .subset2(aio, "callparams")[[1L]]
7878
keep.raw <- .subset2(aio, "callparams")[[2L]]
@@ -99,11 +99,11 @@ call_aio <- function(aio, block = TRUE) {
9999

100100
} else if (inherits(aio, "sendAio")) {
101101

102-
if (!missing(block) && !isTRUE(block)) {
102+
if (missing(block) || isTRUE(block)) {
103+
res <- .Call(rnng_aio_wait_result, .subset2(aio, "aio"))
104+
} else {
103105
res <- .Call(rnng_aio_result, .subset2(aio, "aio"))
104106
missing(res) && return()
105-
} else {
106-
res <- .Call(rnng_aio_wait_result, .subset2(aio, "aio"))
107107
}
108108
aio[["result"]] <- res
109109
rm("aio", envir = aio)

R/context.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,14 @@ request <- function(context,
313313
message(res, " : ", nng_error(res))
314314
return(invisible(res))
315315
}
316-
aio <- .Call(rnng_recv_aio, context, timeout)
317-
env <- `class<-`(new.env(), "recvAio")
318-
env[["aio"]] <- aio
319-
if (is.integer(aio)) {
320-
message(aio, " : ", nng_error(aio))
321-
} else {
322-
env[["callparams"]] <- list(recv_mode, missing(keep.raw) || isTRUE(keep.raw))
316+
res <- .Call(rnng_recv_aio, context, timeout)
317+
is.integer(res) && {
318+
message(res, " : ", nng_error(res))
319+
return(invisible(res))
323320
}
321+
env <- `class<-`(new.env(), "recvAio")
322+
env[["aio"]] <- res
323+
env[["callparams"]] <- list(recv_mode, missing(keep.raw) || isTRUE(keep.raw))
324324
env
325325

326326
}

man/call_aio.Rd

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,8 @@ SEXP rnng_ctx_send(SEXP context, SEXP data, SEXP timeout) {
306306
xc = nng_msg_alloc(&msgp, 0);
307307
if (xc)
308308
return Rf_ScalarInteger(xc);
309-
xc = nng_msg_append(msgp, dp, xlen);
310-
if (xc) {
311-
nng_msg_free(msgp);
312-
return Rf_ScalarInteger(xc);
313-
}
314-
xc = nng_aio_alloc(&aiop, NULL, NULL);
315-
if (xc) {
309+
if ((xc = nng_msg_append(msgp, dp, xlen)) ||
310+
(xc = nng_aio_alloc(&aiop, NULL, NULL))) {
316311
nng_msg_free(msgp);
317312
return Rf_ScalarInteger(xc);
318313
}

tests/tests.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ suppressMessages(n1$send("test", mode = "raw", block = FALSE, echo = FALSE))
3737
invisible(suppressMessages(n$recv("character", block = FALSE)))
3838
saio <- suppressMessages(n$send_aio(data.frame(), timeout = 500))
3939
invisible(!is.null(saio[["aio"]]) || stop())
40+
invisible(suppressMessages(call_aio(saio, block = FALSE)))
4041
raio <- suppressMessages(n1$recv_aio(timeout = 500))
4142
invisible(!is.null(raio[["aio"]]) || stop())
4243
invisible(is.list(raio[["callparams"]]) || stop())
44+
invisible(suppressMessages(call_aio(raio, block = FALSE)))
4345
suppressMessages(n$send_aio(c(1.1, 2.2), mode = "raw", timeout = 500))
4446
invisible(suppressMessages(n1$recv_aio(timeout = 500)))
4547

@@ -90,4 +92,5 @@ invisible(close(sock) == 0L || stop())
9092

9193
invisible(is.character(ver <- nng_version()) && length(ver) == 2L || stop())
9294
invisible(is.character(nng_error(0L)) || stop())
95+
invisible(is_nul_byte(as.raw(0L)) && !is_nul_byte(NULL) || stop())
9396

0 commit comments

Comments
 (0)