Skip to content

Commit b9435db

Browse files
committed
Re-implement serialization header without global variable
1 parent 7c4ed0f commit b9435db

File tree

11 files changed

+14
-51
lines changed

11 files changed

+14
-51
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export("%~>%")
4444
export("opt<-")
4545
export(.advance)
4646
export(.context)
47-
export(.header)
4847
export(.interrupt)
4948
export(.keep)
5049
export(.mark)

R/utils.R

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -368,23 +368,11 @@ ip_addr <- function() .Call(rnng_ip_addr)
368368
#'
369369
#' Internal package functions.
370370
#'
371-
#' @param value integer value.
372-
#'
373-
#' @return For `.header()`: the integer `value` supplied.
374-
#'
375-
#' @keywords internal
376-
#' @export
377-
#'
378-
.header <- function(value = 0L) .Call(rnng_header_set, value)
379-
380-
#' Read Serialization Header
381-
#'
382371
#' @param x raw vector.
383372
#'
384373
#' @return For `.read_header()`: integer value.
385374
#'
386375
#' @keywords internal
387-
#' @rdname dot-header
388376
#' @export
389377
#'
390378
.read_header <- function(x) .Call(rnng_header_read, x)
@@ -396,7 +384,7 @@ ip_addr <- function() .Call(rnng_ip_addr)
396384
#' @return For `.mark()`: the logical `bool` supplied.
397385
#'
398386
#' @keywords internal
399-
#' @rdname dot-header
387+
#' @rdname dot-read_header
400388
#' @export
401389
#'
402390
.mark <- function(bool = TRUE) .Call(rnng_marker_set, bool)
@@ -408,7 +396,7 @@ ip_addr <- function() .Call(rnng_ip_addr)
408396
#' @return For `.read_marker()`: logical value `TRUE` or `FALSE`.
409397
#'
410398
#' @keywords internal
411-
#' @rdname dot-header
399+
#' @rdname dot-read_header
412400
#' @export
413401
#'
414402
.read_marker <- function(x) .Call(rnng_marker_read, x)

man/dot-header.Rd renamed to man/dot-read_header.Rd

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

man/nanonext-package.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP pipe, SEXP
522522
if (raw) {
523523
nano_encode(&buf, data);
524524
} else {
525-
nano_serialize(&buf, data, NANO_PROT(con));
525+
nano_serialize(&buf, data, NANO_PROT(con), 0);
526526
}
527527
nng_msg *msg = NULL;
528528

src/comms.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block, SEXP pipe) {
344344
if (raw) {
345345
nano_encode(&buf, data);
346346
} else {
347-
nano_serialize(&buf, data, NANO_PROT(con));
347+
nano_serialize(&buf, data, NANO_PROT(con), 0);
348348
}
349349
nng_msg *msgp = NULL;
350350

src/core.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// internals -------------------------------------------------------------------
66

7-
int special_header = 0;
87
static int special_marker = 0;
98
static nano_serial_bundle nano_bundle;
109
static SEXP nano_eval_res;
@@ -316,16 +315,16 @@ SEXP nano_raw_char(const unsigned char *buf, const size_t sz) {
316315

317316
}
318317

319-
void nano_serialize(nano_buf *buf, SEXP object, SEXP hook) {
318+
void nano_serialize(nano_buf *buf, SEXP object, SEXP hook, int header) {
320319

321320
NANO_ALLOC(buf, NANONEXT_INIT_BUFSIZE);
322321
struct R_outpstream_st output_stream;
323322

324-
if (special_header || special_marker) {
323+
if (header || special_marker) {
325324
buf->buf[0] = 0x7;
326325
buf->buf[3] = (uint8_t) special_marker;
327-
if (special_header)
328-
memcpy(buf->buf + 4, &special_header, sizeof(int));
326+
if (header)
327+
memcpy(buf->buf + 4, &header, sizeof(int));
329328
buf->cur += 8;
330329
}
331330

@@ -616,13 +615,6 @@ SEXP rnng_marker_read(SEXP x) {
616615

617616
}
618617

619-
SEXP rnng_header_set(SEXP x) {
620-
621-
special_header = NANO_INTEGER(x);
622-
return x;
623-
624-
}
625-
626618
SEXP rnng_header_read(SEXP x) {
627619

628620
int res = 0;

src/init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ static const R_CallMethodDef callMethods[] = {
129129
{"rnng_fini_priors", (DL_FUNC) &rnng_fini_priors, 0},
130130
{"rnng_get_opt", (DL_FUNC) &rnng_get_opt, 2},
131131
{"rnng_header_read", (DL_FUNC) &rnng_header_read, 1},
132-
{"rnng_header_set", (DL_FUNC) &rnng_header_set, 1},
133132
{"rnng_interrupt_switch", (DL_FUNC) &rnng_interrupt_switch, 1},
134133
{"rnng_ip_addr", (DL_FUNC) &rnng_ip_addr, 0},
135134
{"rnng_is_error_value", (DL_FUNC) &rnng_is_error_value, 1},

src/nanonext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ typedef struct nano_node_s {
244244
struct nano_node_s *next;
245245
} nano_node;
246246

247-
extern int special_header;
248247
extern void (*eln2)(void (*)(void *), void *, double, int);
249248

250249
extern SEXP nano_AioSymbol;
@@ -297,7 +296,7 @@ int nano_integer(const SEXP);
297296
SEXP mk_error(const int);
298297
SEXP mk_error_data(const int);
299298
SEXP nano_raw_char(const unsigned char *, const size_t);
300-
void nano_serialize(nano_buf *, const SEXP, SEXP);
299+
void nano_serialize(nano_buf *, const SEXP, SEXP, int);
301300
SEXP nano_unserialize(unsigned char *, const size_t, SEXP);
302301
SEXP nano_decode(unsigned char *, const size_t, const uint8_t, SEXP);
303302
void nano_encode(nano_buf *, const SEXP);
@@ -341,7 +340,6 @@ SEXP rnng_fini(void);
341340
SEXP rnng_fini_priors(void);
342341
SEXP rnng_get_opt(SEXP, SEXP);
343342
SEXP rnng_header_read(SEXP);
344-
SEXP rnng_header_set(SEXP);
345343
SEXP rnng_interrupt_switch(SEXP);
346344
SEXP rnng_ip_addr(void);
347345
SEXP rnng_is_error_value(SEXP);

src/sync.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
429429
if (raw) {
430430
nano_encode(&buf, data);
431431
} else {
432-
special_header = id;
433-
nano_serialize(&buf, data, NANO_PROT(con));
432+
nano_serialize(&buf, data, NANO_PROT(con), id);
434433
}
435434

436435
saio = calloc(1, sizeof(nano_saio));

tests/tests.R

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,15 @@ test_error(req$opt("false", list()), "type")
213213

214214
test_class("nanoContext", ctx <- context(rep))
215215
test_print(ctx)
216-
test_equal(.header(12345L), 12345L)
217216
test_true(.mark())
218217
test_class("sendAio", csaio <- req$send_aio(data.frame(), mode = 1L, timeout = 500))
219218
test_zero(call_aio_(csaio)$result)
220219
test_class("recvAio", craio <- recv_aio(ctx, mode = 8L, timeout = 500))
221220
test_type("raw", res <- collect_aio(craio))
222221
test_true(.read_marker(res))
223222
test_true(!.read_marker("not"))
224-
test_equal(.read_header(res), 12345L)
225223
test_equal(.read_header("not"), 0L)
226224
test_type("list", unserialize(res[9:length(res)]))
227-
test_equal(.header(0L), 0L)
228225
test_true(!.mark(FALSE))
229226
test_zero(req$send("context test", mode ="raw", block = 500))
230227
test_equal(recv(ctx, mode = "string", block = 500), "context test")
@@ -234,7 +231,6 @@ test_type("logical", .unresolved(msg))
234231
test_type("logical", unresolved(msg))
235232
test_class("data.frame", call_aio(msg)$data)
236233
test_true(!unresolved(msg))
237-
test_equal(.header(2025250L), 2025250L)
238234
test_zero(req$send(c(TRUE, FALSE, TRUE), mode = 2L, block = 500))
239235
test_class("recvAio", msg <- recv_aio(ctx, mode = 6L, timeout = 500))
240236
test_type("logical", msg[])
@@ -260,7 +256,6 @@ test_class("recvAio", rek <- request(req$context, c(1+3i, 4+2i), send_mode = "se
260256
test_zero(reply(ctx, execute = identity, recv_mode = 1L, send_mode = 1L, timeout = 500))
261257
test_type("complex", call_aio(rek)[["data"]])
262258
test_type("integer", rek[["aio"]])
263-
test_equal(.header(0L), 0L)
264259

265260
test_type("list", cfg <- serial_config(class = c("invalid", "custom"), sfunc = list(identity, function(x) raw(1L)), ufunc = list(identity, as.integer)))
266261
opt(req$socket, "serial") <- cfg
@@ -294,9 +289,9 @@ test_equal(call_aio(cr)$data, "test")
294289
test_type("integer", cr$aio)
295290
test_type("integer", send(ctxn, TRUE, mode = 0L, block = FALSE))
296291
test_type("externalptr", ctxn <- .context(rep))
297-
test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, cv = NA))
292+
test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, cv = NA, id = 12345L))
298293
test_notnull(cs$data)
299-
test_true(recv(ctxn, block = 500))
294+
test_equal(.read_header(recv(ctxn, mode = 8L, block = 500)), 12345L)
300295
test_zero(send(ctxn, TRUE, mode = 1L, block = 500))
301296
test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, timeout = 5, id = TRUE))
302297
test_zero(reap(ctxn))

0 commit comments

Comments
 (0)