Skip to content

Commit

Permalink
next_config() adds 'mark' special bit
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Oct 31, 2023
1 parent ad35a6e commit 9838d4a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 72 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nanonext
Type: Package
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
Version: 0.10.2.9019
Version: 0.10.2.9020
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
a socket library implementing 'Scalability Protocols', a reliable,
high-performance standard for common communications patterns including
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# nanonext 0.10.2.9019 (development)
# nanonext 0.10.2.9020 (development)

#### New Features

* `next_config()` enables native extensions when using send mode 'next'. Registers hook functions for custom serialization and unserialization of reference objects (such as those accessed via an external pointer).
* `next_config()` configures settings for send mode 'next'. Registers hook functions for custom serialization and unserialization of reference objects (such as those accessed via an external pointer).
* `.until()` contains revised behaviour for this synchronisation primitive, returning FALSE instead of TRUE if the timeout has been reached. This function will replace `until()` in a future package version.

#### Updates
Expand Down
27 changes: 12 additions & 15 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ weakref_value <- function(w) .Call(rnng_weakref_value, w)

#' Configure Next Mode
#'
#' Configures 'next' mode by registering 'refhook' functions for serialization
#' and unserialization. This permits sending and receiving reference
#' Configures send mode 'next'. By registering 'refhook' functions for
#' serialization and unserialization, allows sending and receiving reference
#' objects, such as those accessed via an external pointer, between
#' different R sessions.
#'
Expand All @@ -348,15 +348,14 @@ weakref_value <- function(w) .Call(rnng_weakref_value, w)
#' @param outhook a function (for custom unserialization). The signature for
#' this function must accept a raw vector and return a list, e.g.
#' \code{safetensors::safe_load_file}, or else NULL to reset.
#' @param mark [default FALSE] (for advanced use only) logical value, whether to
#' mark serialized data with a special bit.
#'
#' @return Invisibly, a pairlist comprising the currently-registered 'next'
#' configuration.
#' @return Invisibly, a pairlist comprising the currently-registered 'refhook'
#' functions.
#'
#' @details Calling this function without any arguments returns (invisibly) the
#' currently-registered 'next' configuration.
#'
#' Alternatively, calling this function with a configuration pairlist
#' previously returned by this function registers the supplied configuration.
#' currently-registered 'refhook' functions (and resets 'mark' to FALSE).
#'
#' @section Refhook:
#'
Expand All @@ -367,16 +366,14 @@ weakref_value <- function(w) .Call(rnng_weakref_value, w)
#'
#' @examples
#' cfg <- next_config(inhook = function(x) serialize(x, NULL),
#' outhook = unserialize)
#' outhook = unserialize,
#' mark = TRUE)
#' cfg
#'
#' nul <- next_config(NULL, NULL)
#' next_config(NULL, NULL)
#' print(next_config())
#'
#' print(next_config(cfg))
#' print(next_config(nul))
#'
#' @export
#'
next_config <- function(inhook, outhook)
invisible(.Call(rnng_next_config, if (missing(inhook)) "" else inhook, if (missing(outhook)) "" else outhook))
next_config <- function(inhook, outhook, mark = FALSE)
invisible(.Call(rnng_next_config, if (missing(inhook)) "" else inhook, if (missing(outhook)) "" else outhook, mark))
26 changes: 12 additions & 14 deletions man/next_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,28 @@
#define NANONEXT_SUPPLEMENTALS
#include "nanonext.h"

// statics ---------------------------------------------------------------------
// internals -------------------------------------------------------------------

typedef struct nano_cv_s {
int condition;
uint8_t flag;
nng_mtx *mtx;
nng_cv *cv;
} nano_cv;

typedef struct nano_cv_aio_s {
nng_aio *aio;
nano_aio_typ type;
int mode;
int result;
void *data;
nano_cv *cv;
} nano_cv_aio;

typedef struct nano_cv_duo_s {
nano_cv *cv;
nano_cv *cv2;
} nano_cv_duo;

static SEXP mk_error_data(const int xc) {

Expand Down
11 changes: 6 additions & 5 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

// internals -------------------------------------------------------------------

static uint8_t special_bit = 0;

SEXP mk_error(const int xc) {

SEXP err = Rf_ScalarInteger(xc);
Expand Down Expand Up @@ -196,6 +198,7 @@ void nano_serialize_next(nano_buf *buf, SEXP object) {

NANO_ALLOC(buf, NANONEXT_INIT_BUFSIZE);
buf->buf[0] = 7u;
buf->buf[2] = special_bit;
buf->cur += 8;

struct R_outpstream_st output_stream;
Expand Down Expand Up @@ -1425,13 +1428,11 @@ void rnng_fini(void) {

}

SEXP rnng_next_config(SEXP infun, SEXP outfun) {
SEXP rnng_next_config(SEXP infun, SEXP outfun, SEXP mark) {

special_bit = (uint8_t) LOGICAL(mark)[0];

switch(TYPEOF(infun)) {
case LISTSXP:
if (Rf_xlength(infun) == 2)
rnng_next_config(CAR(infun), CADR(infun));
break;
case CLOSXP:
case BUILTINSXP:
case SPECIALSXP:
Expand Down
2 changes: 1 addition & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static const R_CallMethodDef callMethods[] = {
{"rnng_ncurl_session", (DL_FUNC) &rnng_ncurl_session, 8},
{"rnng_ncurl_session_close", (DL_FUNC) &rnng_ncurl_session_close, 1},
{"rnng_ncurl_transact", (DL_FUNC) &rnng_ncurl_transact, 1},
{"rnng_next_config", (DL_FUNC) &rnng_next_config, 2},
{"rnng_next_config", (DL_FUNC) &rnng_next_config, 3},
{"rnng_pipe_notify", (DL_FUNC) &rnng_pipe_notify, 6},
{"rnng_protocol_open", (DL_FUNC) &rnng_protocol_open, 2},
{"rnng_random", (DL_FUNC) &rnng_random, 2},
Expand Down
29 changes: 1 addition & 28 deletions src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,6 @@ typedef struct nano_handle_s {
nng_tls_config *cfg;
} nano_handle;

typedef struct nano_cv_s {
int condition;
uint8_t flag;
nng_mtx *mtx;
nng_cv *cv;
} nano_cv;

typedef struct nano_cv_aio_s {
nng_aio *aio;
nano_aio_typ type;
int mode;
int result;
void *data;
nano_cv *cv;
} nano_cv_aio;

typedef struct nano_cv_duo_s {
nano_cv *cv;
nano_cv *cv2;
} nano_cv_duo;

#endif

#ifdef NANONEXT_TIME
Expand All @@ -121,12 +100,6 @@ typedef struct nano_cv_duo_s {
#include <mbedtls/sha512.h>
#include <mbedtls/version.h>

#define SHA1_KEY_SIZE 20
#define SHA224_KEY_SIZE 28
#define SHA256_KEY_SIZE 32
#define SHA384_KEY_SIZE 48
#define SHA512_KEY_SIZE 64

#endif

#ifdef NANONEXT_MBED
Expand Down Expand Up @@ -271,7 +244,7 @@ extern SEXP rnng_ncurl_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP rnng_ncurl_session(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP rnng_ncurl_session_close(SEXP);
extern SEXP rnng_ncurl_transact(SEXP);
extern SEXP rnng_next_config(SEXP, SEXP);
extern SEXP rnng_next_config(SEXP, SEXP, SEXP);
extern SEXP rnng_pipe_notify(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
extern SEXP rnng_protocol_open(SEXP, SEXP);
extern SEXP rnng_random(SEXP, SEXP);
Expand Down
8 changes: 7 additions & 1 deletion src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ SEXP nano_hashToChar(unsigned char *buf, const size_t sz) {

}

// SHA-2 Cryptographic Hash Algorithms -----------------------------------------
// SHA-1 and SHA-2 Cryptographic Hash Algorithms -------------------------------

#define SHA1_KEY_SIZE 20
#define SHA224_KEY_SIZE 28
#define SHA256_KEY_SIZE 32
#define SHA384_KEY_SIZE 48
#define SHA512_KEY_SIZE 64

SEXP rnng_sha224(SEXP x, SEXP key, SEXP convert) {

Expand Down
8 changes: 4 additions & 4 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ nanotestaio(rek <- request(req$context, c(1+3i, 4+2i), send_mode = 2L, recv_mode
nanotest(is.integer(reply(ctx, execute = identity, recv_mode = 3L, send_mode = "ra", timeout = 500)))
nanotest(is.complex(call_aio(rek)[["data"]]))

nanotest(length(nxt <- next_config(inhook = function(x) serialize(x, NULL), outhook = unserialize)) == 2L)
nanotest(is.pairlist(nxt <- next_config(inhook = function(x) serialize(x, NULL), outhook = unserialize)))
nanotest(length(nxt) == 2L)
nanotest(is.function(nxt[[1L]]))
nanotest(is.integer(req$send(list(new.env(), new.env()), mode = 3L, block = 500)))
nanotest(is.environment(recv(rep, block = 500)[[1L]]))
nanotest(is.pairlist(nnl <- next_config(NULL, NULL)))
nanotest(is.function(next_config(nxt)[[1L]]))
nanotest(is.pairlist(next_config(nnl)))
nanotest(is.pairlist(next_config(NULL, NULL, mark = TRUE)))
nanotestn(unlist(next_config()))

nanotest(inherits(cv <- cv(), "conditionVariable"))
Expand Down

0 comments on commit 9838d4a

Please sign in to comment.