Skip to content

Commit eca2f17

Browse files
committed
move decode to C level for recv
1 parent 41a1859 commit eca2f17

File tree

11 files changed

+195
-88
lines changed

11 files changed

+195
-88
lines changed

R/aio.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout =
5353
force(data)
5454
data <- encode(data = data, mode = mode)
5555
aio <- .Call(rnng_send_aio, con, data, timeout)
56-
is.integer(aio) && return(invisible(aio))
56+
is.integer(aio) && return(aio)
5757

5858
data <- result <- NULL
5959
unresolv <- TRUE
@@ -82,7 +82,7 @@ send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout =
8282
force(data)
8383
data <- encode(data = data, mode = mode)
8484
aio <- .Call(rnng_ctx_send_aio, con, data, timeout)
85-
is.integer(aio) && return(invisible(aio))
85+
is.integer(aio) && return(aio)
8686

8787
data <- result <- NULL
8888
unresolv <- TRUE
@@ -110,7 +110,7 @@ send_aio.nanoStream <- function(con, data, mode = "raw", timeout = -2L) {
110110
force(data)
111111
data <- encode(data = data, mode = 2L)
112112
aio <- .Call(rnng_stream_send_aio, con, data, timeout)
113-
is.integer(aio) && return(invisible(aio))
113+
is.integer(aio) && return(aio)
114114

115115
data <- result <- NULL
116116
unresolv <- TRUE
@@ -207,7 +207,7 @@ recv_aio.nanoSocket <- function(con,
207207
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
208208
"integer", "logical", "numeric", "raw"))
209209
aio <- .Call(rnng_recv_aio, con, timeout)
210-
is.integer(aio) && return(invisible(aio))
210+
is.integer(aio) && return(aio)
211211

212212
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
213213
data <- raw <- NULL
@@ -222,7 +222,7 @@ recv_aio.nanoSocket <- function(con,
222222
data <<- raw <<- res
223223
aio <<- env[["aio"]] <<- NULL
224224
unresolv <<- FALSE
225-
return(invisible(res))
225+
return(res)
226226
}
227227
on.exit(expr = {
228228
raw <<- res
@@ -248,7 +248,7 @@ recv_aio.nanoSocket <- function(con,
248248
data <<- raw <<- res
249249
aio <<- env[["aio"]] <<- NULL
250250
unresolv <<- FALSE
251-
return(invisible(res))
251+
return(res)
252252
}
253253
on.exit(expr = {
254254
data <<- res
@@ -283,7 +283,7 @@ recv_aio.nanoContext <- function(con,
283283
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
284284
"integer", "logical", "numeric", "raw"))
285285
aio <- .Call(rnng_ctx_recv_aio, con, timeout)
286-
is.integer(aio) && return(invisible(aio))
286+
is.integer(aio) && return(aio)
287287

288288
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
289289
data <- raw <- NULL
@@ -298,7 +298,7 @@ recv_aio.nanoContext <- function(con,
298298
data <<- raw <<- res
299299
aio <<- env[["aio"]] <<- NULL
300300
unresolv <<- FALSE
301-
return(invisible(res))
301+
return(res)
302302
}
303303
on.exit(expr = {
304304
raw <<- res
@@ -324,7 +324,7 @@ recv_aio.nanoContext <- function(con,
324324
data <<- raw <<- res
325325
aio <<- env[["aio"]] <<- NULL
326326
unresolv <<- FALSE
327-
return(invisible(res))
327+
return(res)
328328
}
329329
on.exit(expr = {
330330
data <<- res
@@ -360,7 +360,7 @@ recv_aio.nanoStream <- function(con,
360360
mode <- match.arg2(mode, c("character", "complex", "double", "integer",
361361
"logical", "numeric", "raw")) + 1L
362362
aio <- .Call(rnng_stream_recv_aio, con, n, timeout)
363-
is.integer(aio) && return(invisible(aio))
363+
is.integer(aio) && return(aio)
364364

365365
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
366366
data <- raw <- NULL
@@ -375,7 +375,7 @@ recv_aio.nanoStream <- function(con,
375375
data <<- raw <<- res
376376
aio <<- env[["aio"]] <<- NULL
377377
unresolv <<- FALSE
378-
return(invisible(res))
378+
return(res)
379379
}
380380
on.exit(expr = {
381381
raw <<- res
@@ -401,7 +401,7 @@ recv_aio.nanoStream <- function(con,
401401
data <<- raw <<- res
402402
aio <<- env[["aio"]] <<- NULL
403403
unresolv <<- FALSE
404-
return(invisible(res))
404+
return(res)
405405
}
406406
on.exit(expr = {
407407
data <<- res

R/context.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ reply <- function(context,
116116
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
117117
"integer", "logical", "numeric", "raw"))
118118
send_mode <- match.arg2(send_mode, c("serial", "raw"))
119-
res <- .Call(rnng_ctx_recv, context, timeout)
120-
is.integer(res) && return(invisible(res))
119+
res <- .Call(rnng_ctx_recv, context, recv_mode, timeout, FALSE)
120+
is_error_value(res) && return(res)
121121
on.exit(expr = send_aio(context, as.raw(0L), mode = send_mode))
122-
data <- decode(con = res, mode = recv_mode)
123-
data <- execute(data, ...)
122+
data <- execute(res, ...)
124123
data <- encode(data = data, mode = send_mode)
125124
on.exit()
126125
invisible(.Call(rnng_ctx_send, context, data, timeout))
@@ -188,10 +187,10 @@ request <- function(context,
188187
force(data)
189188
data <- encode(data = data, mode = send_mode)
190189
res <- .Call(rnng_ctx_send_aio, context, data, -2L)
191-
is.integer(res) && return(invisible(res))
190+
is.integer(res) && return(res)
192191

193192
aio <- .Call(rnng_ctx_recv_aio, context, timeout)
194-
is.integer(aio) && return(invisible(aio))
193+
is.integer(aio) && return(aio)
195194

196195
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
197196
data <- raw <- NULL
@@ -206,7 +205,7 @@ request <- function(context,
206205
data <<- raw <<- res
207206
aio <<- env[["aio"]] <<- NULL
208207
unresolv <<- FALSE
209-
return(invisible(res))
208+
return(res)
210209
}
211210
on.exit(expr = {
212211
raw <<- res
@@ -232,7 +231,7 @@ request <- function(context,
232231
data <<- raw <<- res
233232
aio <<- env[["aio"]] <<- NULL
234233
unresolv <<- FALSE
235-
return(invisible(res))
234+
return(res)
236235
}
237236
on.exit(expr = {
238237
data <<- res

R/listdial.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ dial <- function(socket,
7878
} else {
7979
.Call(rnng_dialer_create, socket, url)
8080
}
81-
is.integer(res) && return(invisible(res))
81+
is.integer(res) && return(res)
8282

8383
if (is.environment(socket)) {
8484

@@ -181,7 +181,7 @@ listen <- function(socket,
181181
} else {
182182
.Call(rnng_listener_create, socket, url)
183183
}
184-
is.integer(res) && return(invisible(res))
184+
is.integer(res) && return(res)
185185

186186
if (is.environment(socket)) {
187187

R/ncurl.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ ncurl <- function(url,
5959
if (missing(async) || !isTRUE(async)) {
6060

6161
res <- .Call(rnng_ncurl, url, method, headers, data)
62-
is.integer(res) && return(invisible(res))
62+
is.integer(res) && return(res)
6363

6464
if (is.character(res)) {
6565
continue <- if (interactive()) readline(sprintf("Follow redirect to <%s>? [Y/n] ", res)) else "n"
66-
continue %in% c("n", "N", "no", "NO") && return(invisible(res))
66+
continue %in% c("n", "N", "no", "NO") && return(res)
6767
return(ncurl(res))
6868
}
6969

@@ -74,7 +74,7 @@ ncurl <- function(url,
7474
} else {
7575

7676
aio <- .Call(rnng_ncurl_aio, url, method, headers, data)
77-
is.integer(aio) && return(invisible(aio))
77+
is.integer(aio) && return(aio)
7878

7979
convert <- missing(convert) || isTRUE(convert)
8080
data <- raw <- NULL
@@ -88,7 +88,7 @@ ncurl <- function(url,
8888
data <<- raw <<- res
8989
aio <<- env[["aio"]] <<- NULL
9090
unresolv <<- FALSE
91-
return(invisible(res))
91+
return(res)
9292
}
9393
raw <<- res
9494
data <<- if (convert) tryCatch(rawToChar(res), error = function(e) NULL)
@@ -105,7 +105,7 @@ ncurl <- function(url,
105105
data <<- raw <<- res
106106
aio <<- env[["aio"]] <<- NULL
107107
unresolv <<- FALSE
108-
return(invisible(res))
108+
return(res)
109109
}
110110
raw <<- res
111111
data <<- if (convert) tryCatch(rawToChar(res), error = function(e) NULL)

R/sendrecv.R

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,8 @@ recv.nanoSocket <- function(con,
227227

228228
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
229229
"integer", "logical", "numeric", "raw"))
230-
res <- .Call(rnng_recv, con, block)
231-
is.integer(res) && return(invisible(res))
232-
on.exit(expr = return(res))
233-
data <- decode(con = res, mode = mode)
234-
on.exit()
235-
if (missing(keep.raw) || isTRUE(keep.raw)) list(raw = res, data = data) else data
230+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
231+
.Call(rnng_recv, con, mode, block, keep.raw)
236232

237233
}
238234

@@ -250,13 +246,8 @@ recv.nanoContext <- function(con,
250246
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
251247
"integer", "logical", "numeric", "raw"))
252248
if (missing(block) || isTRUE(block)) block <- -2L
253-
res <- .Call(rnng_ctx_recv, con, block)
254-
is.integer(res) && return(invisible(res))
255-
on.exit(expr = return(res))
256-
data <- decode(con = res, mode = mode)
257-
on.exit()
258-
missing(data) && return(.Call(rnng_scm))
259-
if (missing(keep.raw) || isTRUE(keep.raw)) list(raw = res, data = data) else data
249+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
250+
.Call(rnng_ctx_recv, con, mode, block, keep.raw)
260251

261252
}
262253

@@ -275,12 +266,8 @@ recv.nanoStream <- function(con,
275266
mode <- match.arg2(mode, c("character", "complex", "double", "integer",
276267
"logical", "numeric", "raw")) + 1L
277268
if (missing(block) || isTRUE(block)) block <- -2L
278-
res <- .Call(rnng_stream_recv, con, n, block)
279-
is.integer(res) && return(invisible(res))
280-
on.exit(expr = return(res))
281-
data <- decode(con = res, mode = mode)
282-
on.exit()
283-
if (missing(keep.raw) || isTRUE(keep.raw)) list(raw = res, data = data) else data
269+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
270+
.Call(rnng_stream_recv, con, mode, block, keep.raw, n)
284271

285272
}
286273

R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ nng_version <- function() .Call(rnng_version)
7575
#'
7676
#' @export
7777
#'
78-
nng_error <- function(xc) .Call(rnng_strerror, as.integer(xc))
78+
nng_error <- function(xc) .Call(rnng_strerror, xc)
7979

8080
#' Is Nano
8181
#'

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ aio
377377
#> < recvAio >
378378
#> - $data for message data
379379
aio$data |> str()
380-
#> num [1:100000000] 0.00681 1.00605 -1.18936 1.87709 -0.56687 ...
380+
#> num [1:100000000] -0.42 1.154 0.243 -0.609 1.335 ...
381381
```
382382

383383
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -416,6 +416,7 @@ pub |> send(c("other", "this other topic will not be received"), mode = "raw", e
416416
sub |> recv(mode = "character", keep.raw = FALSE)
417417
#> Warning in recv.nanoSocket(sub, mode = "character", keep.raw = FALSE): 8 | Try
418418
#> again
419+
#> 'errorValue' int 8
419420

420421
# specify NULL to subscribe to ALL topics
421422
sub |> subscribe(topic = NULL)
@@ -427,6 +428,7 @@ sub |> unsubscribe(topic = NULL)
427428
pub |> send(c("newTopic", "this topic will now not be received"), mode = "raw", echo = FALSE)
428429
sub |> recv("character", keep.raw = FALSE)
429430
#> Warning in recv.nanoSocket(sub, "character", keep.raw = FALSE): 8 | Try again
431+
#> 'errorValue' int 8
430432

431433
# however the topics explicitly subscribed to are still received
432434
pub |> send(c("examples", "this example will still be received"), mode = "raw", echo = FALSE)
@@ -512,11 +514,11 @@ ncurl("http://httpbin.org/headers")
512514
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
513515
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
514516
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
515-
#> [76] 2d 36 32 36 30 36 30 35 39 2d 37 36 37 63 36 35 33 35 37 32 33 35 65 39 63
516-
#> [101] 32 32 31 66 30 31 65 61 31 22 0a 20 20 7d 0a 7d 0a
517+
#> [76] 2d 36 32 36 31 63 32 31 62 2d 34 61 33 30 62 32 36 31 31 32 62 64 30 33 63
518+
#> [101] 65 33 38 64 30 31 66 31 64 22 0a 20 20 7d 0a 7d 0a
517519
#>
518520
#> $data
519-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-62606059-767c65357235e9c221f01ea1\"\n }\n}\n"
521+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-6261c21b-4a30b26112bd03ce38d01f1d\"\n }\n}\n"
520522
```
521523

522524
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -531,7 +533,7 @@ res
531533
#> - $raw for raw message
532534

533535
call_aio(res)$data
534-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-62606059-39fc69c11a4b361a78323483\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
536+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-6261c21b-46a08c5728a80a39448bdeca\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
535537
```
536538

537539
In this respect, it may be used as a performant and lightweight method

0 commit comments

Comments
 (0)