Skip to content

Commit

Permalink
use qs2 as serialization alternative rather than option
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Mar 2, 2025
1 parent 2ca5ae0 commit 26284ce
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 59 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export(unresolved)
export(unsubscribe)
export(until)
export(until_)
export(use_qs2)
export(wait)
export(wait_)
export(write_cert)
Expand Down
4 changes: 2 additions & 2 deletions R/aio.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#'
#' @export
#'
send_aio <- function(con, data, mode = c("serial", "raw", "qs2"), timeout = NULL, pipe = 0L)
send_aio <- function(con, data, mode = c("serial", "raw"), timeout = NULL, pipe = 0L)
data <- .Call(rnng_send_aio, con, data, mode, timeout, pipe, environment())

#' Receive Async
Expand Down Expand Up @@ -141,7 +141,7 @@ send_aio <- function(con, data, mode = c("serial", "raw", "qs2"), timeout = NULL
#'
recv_aio <- function(con,
mode = c("serial", "character", "complex", "double", "integer",
"logical", "numeric", "raw", "string", "qs2"),
"logical", "numeric", "raw", "string"),
timeout = NULL,
cv = NULL,
n = 65536L)
Expand Down
4 changes: 2 additions & 2 deletions R/context.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ reply <- function(context,
#'
request <- function(context,
data,
send_mode = c("serial", "raw", "qs2"),
send_mode = c("serial", "raw"),
recv_mode = c("serial", "character", "complex", "double", "integer",
"logical", "numeric", "raw", "string", "qs2"),
"logical", "numeric", "raw", "string"),
timeout = NULL,
cv = NULL)
data <- .Call(rnng_request, context, data, send_mode, recv_mode, timeout, cv, environment())
4 changes: 4 additions & 0 deletions R/nano.R
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,7 @@ print.tlsConfig <- function(x, ...) {
#'
.DollarNames.ncurlAio <- function(x, pattern = "")
grep(pattern, c("status", "headers", "data"), value = TRUE, fixed = TRUE)

#' @export
#'
use_qs2 <- function() .Call(rnng_use_qs2)
4 changes: 2 additions & 2 deletions R/sendrecv.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#'
#' @export
#'
send <- function(con, data, mode = c("serial", "raw", "qs2"), block = NULL, pipe = 0L)
send <- function(con, data, mode = c("serial", "raw"), block = NULL, pipe = 0L)
.Call(rnng_send, con, data, mode, block, pipe)

#' Receive
Expand Down Expand Up @@ -170,7 +170,7 @@ send <- function(con, data, mode = c("serial", "raw", "qs2"), block = NULL, pipe
#'
recv <- function(con,
mode = c("serial", "character", "complex", "double",
"integer", "logical", "numeric", "raw", "string", "qs2"),
"integer", "logical", "numeric", "raw", "string"),
block = NULL,
n = 65536L)
.Call(rnng_recv, con, mode, block, n)
2 changes: 1 addition & 1 deletion man/recv.Rd

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

2 changes: 1 addition & 1 deletion man/recv_aio.Rd

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

4 changes: 2 additions & 2 deletions man/request.Rd

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

2 changes: 1 addition & 1 deletion man/send.Rd

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

8 changes: 1 addition & 7 deletions man/send_aio.Rd

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

20 changes: 10 additions & 10 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,16 +487,16 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP pipe, SEXP

const int pipeid = sock ? nano_integer(pipe) : 0;
const int mod = nano_encodes(mode);
switch (mod) {
case 2:
if (mod == 2) {
nano_encode(&buf, data);
break;
case 3:
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
break;
default:
nano_serialize(&buf, data, NANO_PROT(con));
} else {
if (serial_alt) {
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
buf.len = buf.cur;
} else {
nano_serialize(&buf, data, NANO_PROT(con));
}
}

nng_msg *msg;
Expand All @@ -522,7 +522,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP pipe, SEXP
nng_aio_set_timeout(saio->aio, dur);
sock ? nng_send_aio(*(nng_socket *) NANO_PTR(con), saio->aio) :
nng_ctx_send(*(nng_ctx *) NANO_PTR(con), saio->aio);
NANO_FREE(buf); else if (mod == 3) qs2_free(buf.buf);
NANO_FREE(buf);

PROTECT(aio = R_MakeExternalPtr(saio, nano_AioSymbol, R_NilValue));
R_RegisterCFinalizerEx(aio, saio_finalizer, TRUE);
Expand Down
20 changes: 10 additions & 10 deletions src/comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,16 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block, SEXP pipe) {

const int pipeid = sock ? nano_integer(pipe) : 0;
const int mod = nano_encodes(mode);
switch (mod) {
case 2:
if (mod == 2) {
nano_encode(&buf, data);
break;
case 3:
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
break;
default:
nano_serialize(&buf, data, NANO_PROT(con));
} else {
if (serial_alt) {
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
buf.len = buf.cur;
} else {
nano_serialize(&buf, data, NANO_PROT(con));
}
}

nng_msg *msgp;
Expand All @@ -339,7 +339,7 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block, SEXP pipe) {
goto exitlevel1;
}

NANO_FREE(buf); else if (mod == 3) qs2_free(buf.buf);
NANO_FREE(buf);

} else {

Expand Down
26 changes: 15 additions & 11 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz, SEXP hook) {
}
cur = 12;
goto resume;
case 0x0b:
nano_qs2_loaded();
return qs2_deserialize(buf, sz, false, 1);
}
}

Expand Down Expand Up @@ -521,13 +524,9 @@ SEXP nano_decode(unsigned char *buf, const size_t sz, const uint8_t mod, SEXP ho
case 9:
data = rawToChar(buf, sz);
return data;
case 10:
nano_qs2_loaded();
data = qs2_deserialize(buf, sz, false, 1);
return data;
default:
data = nano_unserialize(buf, sz, hook);
return data;
return data;
}

memcpy(NANO_DATAPTR(data), buf, sz);
Expand Down Expand Up @@ -590,13 +589,12 @@ int nano_encodes(const SEXP mode) {
case 2:
case 3:
if (!memcmp(mod, "raw", slen)) return 2;
if (!memcmp(mod, "qs2", slen)) return 3;
case 4:
case 5:
case 6:
if (!memcmp(mod, "serial", slen)) return 1;
default:
Rf_error("'mode' should be one of serial, raw, qs2");
Rf_error("'mode' should be either serial or raw");
}
}

Expand All @@ -612,11 +610,10 @@ int nano_matcharg(const SEXP mode) {
switch (slen) {
case 1:
if (!memcmp(mod, "c", slen) || !memcmp(mod, "s", slen))
Rf_error("'mode' should be one of serial, character, complex, double, integer, logical, numeric, raw, string, qs2");
Rf_error("'mode' should be one of serial, character, complex, double, integer, logical, numeric, raw, string");
case 2:
case 3:
if (!memcmp(mod, "raw", slen)) return 8;
if (!memcmp(mod, "qs2", slen)) return 10;
case 4:
case 5:
case 6:
Expand All @@ -632,7 +629,7 @@ int nano_matcharg(const SEXP mode) {
case 9:
if (!memcmp(mod, "character", slen)) return 2;
default:
Rf_error("'mode' should be one of serial, character, complex, double, integer, logical, numeric, raw, string, qs2");
Rf_error("'mode' should be one of serial, character, complex, double, integer, logical, numeric, raw, string");
}
}

Expand All @@ -643,7 +640,7 @@ int nano_matcharg(const SEXP mode) {
int nano_matchargs(const SEXP mode) {

if (TYPEOF(mode) != INTSXP) {
const char *mod = CHAR(STRING_ELT(mode, XLENGTH(mode) == 10));
const char *mod = CHAR(STRING_ELT(mode, XLENGTH(mode) == 9));
size_t slen = strlen(mod);
switch (slen) {
case 1:
Expand Down Expand Up @@ -679,3 +676,10 @@ SEXP rnng_eval_safe(SEXP arg) {
return R_ToplevelExec(nano_eval_safe, arg) ? nano_eval_res : Rf_allocVector(RAWSXP, 1);

}

SEXP rnng_use_qs2(void) {

serial_alt = 1;
return R_NilValue;

}
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "nanonext.h"

int serial_alt = 0;
unsigned char *(*qs2_serialize)(SEXP, size_t *, const int, const bool, const int) = NULL;
SEXP (*qs2_deserialize)(const unsigned char *, const size_t, const bool, const int) = NULL;
bool (*qs2_free)(void *) = NULL;
Expand Down Expand Up @@ -199,6 +200,7 @@ static const R_CallMethodDef callMethods[] = {
{"rnng_version", (DL_FUNC) &rnng_version, 0},
{"rnng_wait_thread_create", (DL_FUNC) &rnng_wait_thread_create, 1},
{"rnng_write_cert", (DL_FUNC) &rnng_write_cert, 2},
{"rnng_use_qs2", (DL_FUNC) &rnng_use_qs2, 0},
{NULL, NULL, 0}
};

Expand Down
2 changes: 2 additions & 0 deletions src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ typedef struct nano_buf_s {
size_t cur;
} nano_buf;

extern int serial_alt;
extern unsigned char *(*qs2_serialize)(SEXP, size_t *, const int, const bool, const int);
extern SEXP (*qs2_deserialize)(const unsigned char *, const size_t, const bool, const int);
extern bool (*qs2_free)(void *);
Expand Down Expand Up @@ -362,5 +363,6 @@ SEXP rnng_url_parse(SEXP);
SEXP rnng_version(void);
SEXP rnng_wait_thread_create(SEXP);
SEXP rnng_write_cert(SEXP, SEXP);
SEXP rnng_use_qs2(void);

#endif
20 changes: 10 additions & 10 deletions src/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,16 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
int xc;

const int mod = nano_encodes(sendmode);
switch (mod) {
case 2:
if (mod == 2) {
nano_encode(&buf, data);
break;
case 3:
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
break;
default:
nano_serialize(&buf, data, NANO_PROT(con));
} else {
if (serial_alt) {
nano_qs2_loaded();
buf.buf = qs2_serialize(data, &buf.cur, 3, true, 1);
buf.len = buf.cur;
} else {
nano_serialize(&buf, data, NANO_PROT(con));
}
}

saio = R_Calloc(1, nano_saio);
Expand Down Expand Up @@ -466,7 +466,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou

nng_aio_set_timeout(raio->aio, dur);
nng_ctx_recv(*ctx, raio->aio);
NANO_FREE(buf); else if (mod == 3) qs2_free(buf.buf);
NANO_FREE(buf);

PROTECT(aio = R_MakeExternalPtr(raio, nano_AioSymbol, NANO_PROT(con)));
R_RegisterCFinalizerEx(aio, request_finalizer, TRUE);
Expand Down

0 comments on commit 26284ce

Please sign in to comment.