Skip to content

Commit 576e5ed

Browse files
authored
Add context ID to request() serialized message header (#153)
* Encode ctx id in request() serialized message header * Retain id argument compatibility * Update test
1 parent 32903fc commit 576e5ed

File tree

8 files changed

+19
-21
lines changed

8 files changed

+19
-21
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ Config/Needs/website: tidyverse/tidytemplate
3838
Config/usethis/last-upkeep: 2025-04-23
3939
Encoding: UTF-8
4040
Roxygen: list(markdown = TRUE)
41-
RoxygenNote: 7.3.2
41+
RoxygenNote: 7.3.2.9000
4242
SystemRequirements: 'libnng' >= 1.9 and 'libmbedtls' >= 2.5, or 'cmake'
4343
and 'xz' to compile NNG and/or Mbed TLS included in package sources

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Updates
44

5+
* `request()` argument `id` is now a logical rather than integer argument.
56
* The previous `listen()` and `dial()` argument `error`, removed in v1.6.0, is now defunct.
67
* The previous `serial_config()` argument `vec`, unutilised since v1.6.0, is removed.
78

R/context.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ reply <- function(
180180
#' @param cv (optional) a 'conditionVariable' to signal when the async receive
181181
#' is complete, or NULL. If any other value is supplied, this will cause the
182182
#' pipe connection to be dropped when the async receive is complete.
183-
#' @param id (optional) integer message ID to send a special payload to the
184-
#' context upon timeout (asynchronously) consisting of an integer zero,
185-
#' followed by the value of `id` supplied.
183+
#' @param id (optional) specify as TRUE to send a message via the context upon
184+
#' timeout (asynchronously) consisting of an integer zero, followed by the
185+
#' integer context ID.
186186
#'
187187
#' @return A 'recvAio' (object of class 'mirai' and 'recvAio') (invisibly).
188188
#'

man/request.Rd

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

src/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

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

7+
int special_header = 0;
78
static int special_marker = 0;
8-
static int special_header = 0;
99
static nano_serial_bundle nano_bundle;
1010
static SEXP nano_eval_res;
1111

src/nanonext.h

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

247+
extern int special_header;
247248
extern void (*eln2)(void (*)(void *), void *, double, int);
248249

249250
extern SEXP nano_AioSymbol;

src/sync.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,17 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
408408

409409
if (NANO_PTR_CHECK(con, nano_ContextSymbol))
410410
Rf_error("`con` is not a valid Context");
411+
nng_ctx *ctx = (nng_ctx *) NANO_PTR(con);
411412

412413
const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
413414
const uint8_t mod = (uint8_t) nano_matcharg(recvmode);
414415
const int raw = nano_encode_mode(sendmode);
415-
const int id = msgid != R_NilValue ? NANO_INTEGER(msgid) : 0;
416-
int signal, drop, xc;
417-
if (cvar == R_NilValue) {
418-
signal = 0;
419-
drop = 0;
420-
} else {
421-
signal = !NANO_PTR_CHECK(cvar, nano_CvSymbol);
422-
drop = 1 - signal;
423-
}
416+
const int id = msgid == R_NilValue ? 0 : TYPEOF(msgid) == LGLSXP ? nng_ctx_id(*ctx) : NANO_INTEGER(msgid);
417+
const int signal = cvar != R_NilValue && !NANO_PTR_CHECK(cvar, nano_CvSymbol);
418+
const int drop = cvar != R_NilValue && !signal;
419+
int xc;
420+
421+
nano_cv *ncv = signal ? (nano_cv *) NANO_PTR(cvar) : NULL;
424422

425423
nano_saio *saio = NULL;
426424
nano_aio *raio = NULL;
@@ -431,6 +429,7 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
431429
if (raw) {
432430
nano_encode(&buf, data);
433431
} else {
432+
special_header = id;
434433
nano_serialize(&buf, data, NANO_PROT(con));
435434
}
436435

@@ -440,9 +439,6 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
440439
raio = calloc(1, sizeof(nano_aio));
441440
NANO_ENSURE_ALLOC(raio);
442441

443-
nng_ctx *ctx = (nng_ctx *) NANO_PTR(con);
444-
nano_cv *ncv = signal ? (nano_cv *) NANO_PTR(cvar) : NULL;
445-
446442
saio->ctx = ctx;
447443
saio->id = id;
448444

tests/tests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, cv = NA))
298298
test_notnull(cs$data)
299299
test_true(recv(ctxn, block = 500))
300300
test_zero(send(ctxn, TRUE, mode = 1L, block = 500))
301-
test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, timeout = 5, id = 12L))
301+
test_class("recvAio", cs <- request(.context(req$socket), data = TRUE, timeout = 5, id = TRUE))
302302
test_zero(reap(ctxn))
303303
test_equal(reap(ctxn), 7L)
304304
test_zero(pipe_notify(rep, cv, add = TRUE, flag = TRUE))

0 commit comments

Comments
 (0)