Skip to content

Commit 41a1859

Browse files
committed
corrections, optimisations & tests
1 parent 5b0b265 commit 41a1859

File tree

11 files changed

+66
-64
lines changed

11 files changed

+66
-64
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
#### New Features
44

5-
* `$context()` method added for creating new contexts from nano Objects using supported protocols (i.e. req, rep, surveyor, respondent).
5+
* `$context()` method added for creating new contexts from nano Objects using supported protocols (i.e. req, rep, surveyor, respondent) - this replaces the `context()` function for nano Objects.
66
* Added convenience auxiliary functions `is_nano()` and `is_aio()`.
77

88
#### Updates
99

10-
* NNG library used on Windows and for building from source updated to 1.6.0 pre-release.
10+
* NNG library used for building from source (and on Windows) updated to 1.6.0 pre-release.
1111
* Setting 'NANONEXT_TLS=1' now allows the downloaded NNG library to be built against a system mbedtls installation.
1212
* Deprecated `send_ctx()`, `recv_ctx()` and logging are removed.
1313
* Internal performance optimisations.

R/context.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
#' permit applications to share a single socket, with its underlying dialers
77
#' and listeners, while still benefiting from separate state tracking.
88
#'
9-
#' @param socket a Socket or nano object.
9+
#' @param socket a Socket.
1010
#'
1111
#' @return A new Context (object of class 'nanoContext' and 'nano').
1212
#'
13-
#' @details For convenience, this function may be called on a nano object as well
14-
#' as a socket, in which case it is the equivalent of calling the function
15-
#' on the object's socket directly.
16-
#'
17-
#' Contexts allow the independent and concurrent use of stateful
13+
#' @details Contexts allow the independent and concurrent use of stateful
1814
#' operations using the same socket. For example, two different contexts
1915
#' created on a rep socket can each receive requests, and send replies to
2016
#' them, without any regard to or interference with each other.
2117
#'
22-
#' Note: not every protocol supports creation of separate contexts.
18+
#' Note: only certain protocols support creation of separate contexts -
19+
#' namely req, rep, surveyor, respondent.
2320
#'
2421
#' To send and receive over a context use \code{\link{send}} and
2522
#' \code{\link{recv}} or their async counterparts \code{\link{send_aio}} and
2623
#' \code{\link{recv_aio}}.
2724
#'
25+
#' For nano objects, use the \code{$context()} method, which will return a
26+
#' new context.
27+
#'
2828
#' @examples
2929
#' s <- socket("req", listen = "inproc://nanonext")
3030
#' ctx <- context(s)
@@ -33,7 +33,7 @@
3333
#' close(s)
3434
#'
3535
#' n <- nano("req", listen = "inproc://nanonext")
36-
#' ctx <- context(n)
36+
#' ctx <- n$context()
3737
#' ctx
3838
#' close(ctx)
3939
#' n$close()
@@ -42,7 +42,6 @@
4242
#'
4343
context <- function(socket) {
4444

45-
if (is.environment(socket)) socket <- .subset2(socket, "socket")
4645
.Call(rnng_ctx_open, socket)
4746

4847
}

R/listdial.R

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

8483
if (is.environment(socket)) {
@@ -182,7 +181,6 @@ listen <- function(socket,
182181
} else {
183182
.Call(rnng_listener_create, socket, url)
184183
}
185-
186184
is.integer(res) && return(invisible(res))
187185

188186
if (is.environment(socket)) {

R/socket.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ socket <- function(protocol = c("pair", "bus", "req", "rep", "push", "pull",
6565

6666
protocol <- match.arg2(protocol, c("pair", "bus", "req", "rep", "push", "pull",
6767
"pub", "sub", "surveyor", "respondent"))
68-
res <- .Call(rnng_protocol_open, protocol)
69-
is.integer(res) && return(res)
70-
if (!missing(dial)) dial(res, url = dial, autostart = autostart)
71-
if (!missing(listen)) listen(res, url = listen, autostart = autostart)
72-
res
68+
sock <- .Call(rnng_protocol_open, protocol)
69+
is.integer(sock) && return(sock)
70+
if (!missing(dial)) dial(sock, url = dial, autostart = autostart)
71+
if (!missing(listen)) listen(sock, url = listen, autostart = autostart)
72+
sock
7373

7474
}
7575

R/utils.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#' headers are detected in the same location, it is assumed that NNG was
1313
#' built with TLS support (using Mbed TLS) and TLS is configured appropriately.
1414
#'
15-
#' Otherwise, the environment variable `Sys.setenv(NANONEXT_TLS=1)` may be
16-
#' set prior to installation if:
15+
#' Otherwise, the environment variable \code{Sys.setenv(NANONEXT_TLS=1)} may
16+
#' be set prior to installation if:
1717
#'
1818
#' - your system installations of 'libnng' (built with TLS support) and
1919
#' 'libmbedtls' are in different locations; or
@@ -265,12 +265,12 @@ encode <- function(data, mode) {
265265
decode <- function(con, mode) {
266266
switch(mode,
267267
unserialize(connection = con),
268-
(r <- readBin(con = con, what = "character", n = length(con)))[r != ""],
269-
readBin(con = con, what = "complex", n = length(con)),
270-
readBin(con = con, what = "double", n = length(con)),
271-
readBin(con = con, what = "integer", n = length(con)),
272-
readBin(con = con, what = "logical", n = length(con)),
273-
readBin(con = con, what = "numeric", n = length(con)),
268+
(r <- readBin(con = con, what = "character", n = length(con) / 2L))[nzchar(r)],
269+
readBin(con = con, what = "complex", n = length(con) / 16L),
270+
readBin(con = con, what = double(), n = length(con) / 8L),
271+
readBin(con = con, what = integer(), n = length(con) / 4L),
272+
readBin(con = con, what = logical(), n = length(con) / 4L),
273+
readBin(con = con, what = numeric(), n = length(con) / 8L),
274274
con)
275275
}
276276

README.md

Lines changed: 5 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.479 -0.448 0.216 -0.424 0.037 ...
380+
#> num [1:100000000] 0.00681 1.00605 -1.18936 1.87709 -0.56687 ...
381381
```
382382

383383
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -512,11 +512,11 @@ ncurl("http://httpbin.org/headers")
512512
#> [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
513513
#> [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
514514
#> [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 35 66 65 35 62 35 2d 34 38 38 66 62 31 34 63 32 36 36 37 66 39 65
516-
#> [101] 66 32 30 31 65 33 30 32 38 22 0a 20 20 7d 0a 7d 0a
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
517517
#>
518518
#> $data
519-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-625fe5b5-488fb14c2667f9ef201e3028\"\n }\n}\n"
519+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-62606059-767c65357235e9c221f01ea1\"\n }\n}\n"
520520
```
521521

522522
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -531,7 +531,7 @@ res
531531
#> - $raw for raw message
532532

533533
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-625fe5b5-073a52686414e1556c6f9853\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"79.173.189.204\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
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"
535535
```
536536

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

configure

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ else
7171
else
7272
cmake -DCMAKE_C_FLAGS="${CMAKE_C_FLAGS} -fPIC" -DNNG_ELIDE_DEPRECATED=1 -DNNG_TESTS=0 -DNNG_TOOLS=0 ..
7373
fi
74-
make
74+
cmake --build .
7575
cd ../..
7676
mkdir lib
7777
mv -f nanomsg-nng-$LIB_VER/build/libnng.a lib
@@ -124,7 +124,7 @@ if [ $? -ne 0 ]; then
124124
else
125125
cmake -DCMAKE_C_FLAGS="${CMAKE_C_FLAGS} -fPIC" -DNNG_ELIDE_DEPRECATED=1 -DNNG_TESTS=0 -DNNG_TOOLS=0 ..
126126
fi
127-
make
127+
cmake --build .
128128
cd ../..
129129
mkdir lib
130130
mv -f nanomsg-nng-$LIB_VER/build/libnng.a lib

man/context.Rd

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

man/nng_version.Rd

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

src/aio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ SEXP rnng_stream_recv_aio(SEXP stream, SEXP bytes, SEXP timeout) {
397397
int xc;
398398

399399
nano_aio *iaio = R_Calloc(1, nano_aio);
400-
iaio->type = 2;
400+
iaio->type = IOV_RECVAIO;
401401

402402
nng_iov *iov = R_Calloc(1, nng_iov);
403403
iaio->data = iov;
@@ -564,7 +564,7 @@ SEXP rnng_stream_send_aio(SEXP stream, SEXP data, SEXP timeout) {
564564
int xc;
565565

566566
nano_aio *iaio = R_Calloc(1, nano_aio);
567-
iaio->type = 1;
567+
iaio->type = IOV_SENDAIO;
568568

569569
nng_iov *iov = R_Calloc(1, nng_iov);
570570
iaio->data = iov;
@@ -610,7 +610,7 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP method, SEXP headers, SEXP data) {
610610

611611
int xc;
612612
nano_aio *haio = R_Calloc(1, nano_aio);
613-
haio->type = 3;
613+
haio->type = HTTP_AIO;
614614
nano_handle *handle = R_Calloc(1, nano_handle);
615615
handle->cfg = NULL;
616616
haio->data = handle;

0 commit comments

Comments
 (0)