Skip to content

Commit 4b6088b

Browse files
committed
non-erroring rawToChar; removes recover_aio()
1 parent dc46760 commit 4b6088b

File tree

10 files changed

+14
-121
lines changed

10 files changed

+14
-121
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: nanonext
22
Type: Package
33
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
4-
Version: 0.9.2.9033
4+
Version: 0.9.2.9034
55
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
66
a socket library providing high-performance scalability protocols, a
77
cross-platform standard for messaging and communications. Serves as a

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export(opt)
6565
export(parse_url)
6666
export(pipe_notify)
6767
export(random)
68-
export(recover_aio)
6968
export(recv)
7069
export(recv_aio)
7170
export(recv_aio_signal)

NEWS.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# nanonext 0.9.2.9033 (development)
1+
# nanonext 0.9.2.9034 (development)
22

33
#### New Features
44

55
* `ncurl_aio()` has been separated into a dedicated function for async http requests.
6-
* `recover_aio()` allows recovery of 'ncurlAio' where character conversion fails.
76
* Receive functions add `mode = 'string'` as a faster alternative to 'character' when receiving a scalar value.
87

98
#### Updates
@@ -18,8 +17,8 @@
1817

1918
*Other changes:*
2019

21-
* Improvements to recv (mode = 'serial'):
22-
+ Failure to unserialize a message will automatically save the data as a raw vector for recovery, and generate a warning (as was the case prior to v0.9.2).
20+
* Improvements to recv (mode = 'serial') and `ncurl()`:
21+
+ Failure to unserialize, or convert to character, automatically saves the data as a raw vector for recovery, generating a warning instead of an error (as was the case prior to v0.9.2).
2322
* Improvements to vector send/recv (mode = 'raw'):
2423
+ Higher performance sending of vector data.
2524
+ Permits sending of NULL, in which case an empty vector of the corresponding mode is received.

R/aio.R

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -290,28 +290,3 @@ unresolved <- function(aio) .Call(rnng_unresolved, aio)
290290
#' @export
291291
#'
292292
.unresolved <- function(aio) .Call(rnng_unresolved2, aio)
293-
294-
#' Recover an Aio Value
295-
#'
296-
#' For use on 'ncurlAio' when encountering a character conversion error
297-
#' 'embedded nul in string' (received data is not text data). This function
298-
#' allows recovery by retrieving the value as a raw vector.
299-
#'
300-
#' @param aio an ncurlAio (object of class 'ncurlAio').
301-
#'
302-
#' @return The passed object (invisibly).
303-
#'
304-
#' @details A raw vector will be available at \code{$data}, as if
305-
#' \code{convert = FALSE} had been specified.
306-
#'
307-
#' Note: calling this function on an unresolved Aio is an error and any
308-
#' resulting operation is not guaranteed.
309-
#'
310-
#' @examples
311-
#' res <- ncurl_aio("https://shikokuchuo.net/nanonext/reference/figures/logo.png")
312-
#' tryCatch(call_aio(res), error = identity)
313-
#' recover_aio(res)$data
314-
#'
315-
#' @export
316-
#'
317-
recover_aio <- function(aio) invisible(.Call(rnng_aio_recover, aio))

man/recover_aio.Rd

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/aio.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -743,55 +743,6 @@ SEXP rnng_aio_call(SEXP aio) {
743743

744744
}
745745

746-
SEXP rnng_aio_recover(SEXP aio) {
747-
748-
if (TYPEOF(aio) != ENVSXP)
749-
return aio;
750-
751-
const SEXP coreaio = Rf_findVarInFrame(aio, nano_AioSymbol);
752-
if (R_ExternalPtrTag(coreaio) != nano_AioSymbol)
753-
return aio;
754-
755-
nano_aio *raio = (nano_aio *) R_ExternalPtrAddr(coreaio);
756-
if (raio->data == NULL) return aio;
757-
758-
SEXP out;
759-
const int mod = 8;
760-
unsigned char *buf;
761-
size_t sz;
762-
763-
switch (raio->type) {
764-
case IOV_RECVAIO:
765-
buf = raio->data;
766-
sz = nng_aio_count(raio->aio);
767-
break;
768-
case RECVAIO: ;
769-
nng_msg *msg = (nng_msg *) raio->data;
770-
buf = nng_msg_body(msg);
771-
sz = nng_msg_len(msg);
772-
break;
773-
case HTTP_AIO: ;
774-
nano_handle *handle = (nano_handle *) raio->data;
775-
nng_http_res_get_data(handle->res, (void **) &buf, &sz);
776-
out = Rf_allocVector(RAWSXP, sz);
777-
if (buf != NULL)
778-
memcpy(STDVEC_DATAPTR(out), buf, sz);
779-
Rf_defineVar(nano_ResultSymbol, out, ENCLOS(aio));
780-
Rf_defineVar(nano_AioSymbol, R_NilValue, aio);
781-
return aio;
782-
default:
783-
return aio;
784-
}
785-
786-
PROTECT(out = nano_decode(buf, sz, mod));
787-
Rf_defineVar(nano_ResultSymbol, out, ENCLOS(aio));
788-
Rf_defineVar(nano_AioSymbol, R_NilValue, aio);
789-
790-
UNPROTECT(1);
791-
return aio;
792-
793-
}
794-
795746
SEXP rnng_aio_stop(SEXP aio) {
796747

797748
if (TYPEOF(aio) != ENVSXP)

src/core.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ static SEXP rawOneString(unsigned char *bytes, R_xlen_t nbytes, R_xlen_t *np) {
110110
SEXP rawToChar(unsigned char *buf, const size_t sz) {
111111

112112
SEXP out;
113-
int i, j;
114-
for (i = 0, j = -1; i < sz; i++) if (buf[i]) j = i;
113+
const char *cbuf = (const char *) buf;
114+
size_t slen = strnlen(cbuf, sz);
115+
if (sz - slen > 1) {
116+
Rf_warning("data could not be converted to a character string");
117+
out = Rf_allocVector(RAWSXP, sz);
118+
memcpy(STDVEC_DATAPTR(out), buf, sz);
119+
return out;
120+
}
115121

116122
PROTECT(out = Rf_allocVector(STRSXP, 1));
117-
SET_STRING_ELT(out, 0, Rf_mkCharLenCE((const char *) buf, j + 1, CE_NATIVE));
123+
SET_STRING_ELT(out, 0, Rf_mkCharLenCE(cbuf, slen, CE_NATIVE));
118124

119125
UNPROTECT(1);
120126
return out;

src/init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ static const R_CallMethodDef callMethods[] = {
126126
{"rnng_aio_get_msg", (DL_FUNC) &rnng_aio_get_msg, 1},
127127
{"rnng_aio_get_msg2", (DL_FUNC) &rnng_aio_get_msg2, 1},
128128
{"rnng_aio_http", (DL_FUNC) &rnng_aio_http, 3},
129-
{"rnng_aio_recover", (DL_FUNC) &rnng_aio_recover, 1},
130129
{"rnng_aio_result", (DL_FUNC) &rnng_aio_result, 1},
131130
{"rnng_aio_stop", (DL_FUNC) &rnng_aio_stop, 1},
132131
{"rnng_clock", (DL_FUNC) &rnng_clock, 0},

src/nanonext.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ extern SEXP rnng_aio_call(SEXP);
221221
extern SEXP rnng_aio_get_msg(SEXP);
222222
extern SEXP rnng_aio_get_msg2(SEXP);
223223
extern SEXP rnng_aio_http(SEXP, SEXP, SEXP);
224-
extern SEXP rnng_aio_recover(SEXP);
225224
extern SEXP rnng_aio_result(SEXP);
226225
extern SEXP rnng_aio_stop(SEXP);
227226
extern SEXP rnng_base64dec(SEXP, SEXP);

tests/tests.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ haio <- ncurl_aio("https://i.i")
334334
nanotest(is_error_value(call_aio(haio)$data))
335335
nanotestp(haio$data)
336336
ncaio <- ncurl_aio("https://shikokuchuo.net/nanonext/reference/figures/logo.png")
337-
nanotest(is_error_value(tryCatch(call_aio(ncaio)$status, error = function(e) `class<-`("", "errorValue"))))
338-
nanotest(is.raw(recover_aio(ncaio)$data) || is_error_value(ncaio$data))
337+
nanotestw(is.raw(call_aio(ncaio)$data) || is_error_value(ncaio$data))
339338
sess <- ncurl_session("https://postman-echo.com/post", method = "POST", headers = c(`Content-Type` = "text/plain"), data = "test", response = "server", timeout = 3000L)
340339
nanotestnn(sess)
341340
nanotest(is_error_value(sess) || length(transact(sess)) == 3L)
@@ -439,7 +438,6 @@ nanotesterr(.context(fakesock), "valid Socket")
439438
nanotesterr(stat(fakesock, "pipes"), "valid Socket")
440439
nanotesterr(close(fakesock), "valid Socket")
441440
nanotest(!.unresolved(fakesock))
442-
nanotest(identical(recover_aio(fakesock), fakesock))
443441
fakectx <- `class<-`("test", "nanoContext")
444442
nanotest(!.unresolved(fakectx))
445443
nanotesterr(request(fakectx, data = "test"), "valid Context")
@@ -468,7 +466,6 @@ unres <- as.symbol(" unresolvedValue ")
468466
nanotest(unresolved(unres))
469467
nanotest(!unresolved("unresolvedValue"))
470468
nanotest(identical(call_aio("a"), "a"))
471-
nanotest(identical(recover_aio("a"), "a"))
472469
nanotestn(stop_aio("a"))
473470

474471
nanotest(sha256("test") == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")

0 commit comments

Comments
 (0)