Skip to content

Commit 3727b13

Browse files
committed
C code style
1 parent 70eb283 commit 3727b13

File tree

7 files changed

+97
-73
lines changed

7 files changed

+97
-73
lines changed

NEWS.md

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

88
#### Updates
99

10+
* For receives, if an error occurs in unserialisation or data conversion (e.g. mode was incorrectly specified), the received raw vector is now available at both `$raw` and `$data` if `keep.raw = TRUE`.
1011
* Setting 'NANONEXT_TLS=1' now allows the downloaded NNG library to be built against a system mbedtls installation.
1112
* Deprecated `send_ctx()`, `recv_ctx()` and logging are removed.
1213
* All-round internal performance optimisations.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ aio
379379
#> < recvAio >
380380
#> - $data for message data
381381
aio$data |> str()
382-
#> num [1:100000000] 0.112 0.003 2.633 -0.609 -0.476 ...
382+
#> num [1:100000000] 0.1703 -0.0154 0.0138 -0.3083 -0.1477 ...
383383
```
384384

385385
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -516,11 +516,11 @@ ncurl("http://httpbin.org/headers")
516516
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
517517
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
518518
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
519-
#> [76] 2d 36 32 36 38 37 33 65 39 2d 36 66 33 37 32 32 39 34 32 61 34 31 39 31 36
520-
#> [101] 35 30 35 37 31 38 35 33 31 22 0a 20 20 7d 0a 7d 0a
519+
#> [76] 2d 36 32 36 39 34 30 66 39 2d 32 37 38 31 35 33 34 36 36 61 31 61 35 38 35
520+
#> [101] 35 30 39 38 34 30 66 35 62 22 0a 20 20 7d 0a 7d 0a
521521
#>
522522
#> $data
523-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626873e9-6f3722942a41916505718531\"\n }\n}\n"
523+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626940f9-278153466a1a585509840f5b\"\n }\n}\n"
524524
```
525525

526526
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -535,7 +535,7 @@ res
535535
#> - $raw for raw message
536536

537537
call_aio(res)$data
538-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626873e9-0c5fdf1b0c8e1f21452df988\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
538+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626940f9-73f729e64784f75507dcee00\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"79.173.189.204\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
539539
```
540540

541541
In this respect, it may be used as a performant and lightweight method

src/aio.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ SEXP rnng_aio_get_msg(SEXP aio, SEXP mode, SEXP keep) {
180180
if (res)
181181
return mk_error(res);
182182

183-
const int mod = *INTEGER(mode), kpr = *LOGICAL(keep);
183+
const int mod = INTEGER(mode)[0], kpr = LOGICAL(keep)[0];
184184
void *buf = nng_msg_body(raio->data);
185185
size_t sz = nng_msg_len(raio->data);
186186

187-
return nano_decode((unsigned char *) buf, sz, mod, kpr);
187+
return nano_decode(buf, sz, mod, kpr);
188188

189189
}
190190

@@ -208,11 +208,11 @@ SEXP rnng_aio_stream_in(SEXP aio, SEXP mode, SEXP keep) {
208208
if (res)
209209
return mk_error(res);
210210

211-
const int mod = *INTEGER(mode), kpr = *LOGICAL(keep);
211+
const int mod = INTEGER(mode)[0], kpr = LOGICAL(keep)[0];
212212
nng_iov *iov = (nng_iov *) iaio->data;
213213
size_t sz = nng_aio_count(iaio->aio);
214214

215-
return nano_decode((unsigned char *) iov->iov_buf, sz, mod, kpr);
215+
return nano_decode(iov->iov_buf, sz, mod, kpr);
216216

217217
}
218218

@@ -647,9 +647,7 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP method, SEXP headers, SEXP data) {
647647
}
648648
if (headers != R_NilValue) {
649649
R_xlen_t hlen = Rf_xlength(headers);
650-
SEXP names;
651-
PROTECT(names = Rf_getAttrib(headers, R_NamesSymbol));
652-
650+
SEXP names = Rf_getAttrib(headers, R_NamesSymbol);
653651
switch (TYPEOF(headers)) {
654652
case STRSXP:
655653
for (R_xlen_t i = 0; i < hlen; i++) {
@@ -662,7 +660,6 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP method, SEXP headers, SEXP data) {
662660
nng_url_free(handle->url);
663661
R_Free(handle);
664662
R_Free(haio);
665-
UNPROTECT(1);
666663
return mk_error(xc);
667664
}
668665
}
@@ -678,18 +675,15 @@ SEXP rnng_ncurl_aio(SEXP http, SEXP method, SEXP headers, SEXP data) {
678675
nng_url_free(handle->url);
679676
R_Free(handle);
680677
R_Free(haio);
681-
UNPROTECT(1);
682678
return mk_error(xc);
683679
}
684680
}
685681
break;
686682
}
687-
688-
UNPROTECT(1);
689683
}
690684
if (data != R_NilValue) {
691685
unsigned char *dp = RAW(data);
692-
const size_t dlen = (size_t) Rf_xlength(data) - 1;
686+
const size_t dlen = Rf_xlength(data) - 1;
693687
xc = nng_http_req_set_data(handle->req, dp, dlen);
694688
if (xc) {
695689
nng_http_req_free(handle->req);
@@ -804,7 +798,7 @@ SEXP rnng_aio_http(SEXP aio) {
804798

805799
nng_http_res_get_data(handle->res, &dat, &sz);
806800
vec = Rf_allocVector(RAWSXP, sz);
807-
memcpy(RAW(vec), (unsigned char *) dat, sz);
801+
memcpy(RAW(vec), dat, sz);
808802

809803
return vec;
810804

src/core.c

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ SEXP rnng_serial(SEXP mode) {
8282
SEXP nano_encode(SEXP object) {
8383

8484
R_xlen_t xlen = Rf_xlength(object);
85-
unsigned char *buf;
85+
void *buf;
8686
size_t sz;
8787
SEXP out;
8888

@@ -105,25 +105,25 @@ SEXP nano_encode(SEXP object) {
105105
} else {
106106
switch (TYPEOF(object)) {
107107
case REALSXP:
108-
buf = (unsigned char *) REAL(object);
108+
buf = REAL(object);
109109
sz = xlen * sizeof(double);
110110
out = Rf_allocVector(RAWSXP, sz);
111111
memcpy(RAW(out), buf, sz);
112112
break;
113113
case INTSXP:
114-
buf = (unsigned char *) INTEGER(object);
114+
buf = INTEGER(object);
115115
sz = xlen * sizeof(int);
116116
out = Rf_allocVector(RAWSXP, sz);
117117
memcpy(RAW(out), buf, sz);
118118
break;
119119
case LGLSXP:
120-
buf = (unsigned char *) LOGICAL(object);
120+
buf = LOGICAL(object);
121121
sz = xlen * sizeof(int);
122122
out = Rf_allocVector(RAWSXP, sz);
123123
memcpy(RAW(out), buf, sz);
124124
break;
125125
case CPLXSXP:
126-
buf = (unsigned char *) COMPLEX(object);
126+
buf = COMPLEX(object);
127127
sz = xlen * (sizeof(double) + sizeof(double));
128128
out = Rf_allocVector(RAWSXP, sz);
129129
memcpy(RAW(out), buf, sz);
@@ -478,7 +478,7 @@ SEXP rnng_matchargs(SEXP mode) {
478478

479479
}
480480

481-
SEXP nano_decode(unsigned char *buf, size_t sz, const int mod, const int kpr) {
481+
SEXP nano_decode(void *buf, size_t sz, const int mod, const int kpr) {
482482

483483
int tryErr = 0;
484484
SEXP raw, data;
@@ -491,7 +491,6 @@ SEXP nano_decode(unsigned char *buf, size_t sz, const int mod, const int kpr) {
491491
data = R_tryEval(expr, R_BaseEnv, &tryErr);
492492
if (tryErr) {
493493
data = raw;
494-
raw = R_NilValue;
495494
}
496495
UNPROTECT(2);
497496
} else if (mod == 2) {
@@ -506,26 +505,62 @@ SEXP nano_decode(unsigned char *buf, size_t sz, const int mod, const int kpr) {
506505
SETLENGTH(data, m);
507506
UNPROTECT(1);
508507
} else {
508+
size_t size;
509509
switch (mod) {
510510
case 3:
511-
data = Rf_allocVector(CPLXSXP, sz / (sizeof(double) + sizeof(double)));
512-
memcpy(COMPLEX(data), buf, sz);
511+
size = sizeof(double) + sizeof(double);
512+
if (sz % size == 0) {
513+
data = Rf_allocVector(CPLXSXP, sz / size);
514+
memcpy(COMPLEX(data), buf, sz);
515+
} else {
516+
Rf_warning("received data could not be converted to complex");
517+
data = Rf_allocVector(RAWSXP, sz);
518+
memcpy(RAW(data), buf, sz);
519+
}
513520
break;
514521
case 4:
515-
data = Rf_allocVector(REALSXP, sz / sizeof(double));
516-
memcpy(REAL(data), buf, sz);
522+
size = sizeof(double);
523+
if (sz % size == 0) {
524+
data = Rf_allocVector(REALSXP, sz / size);
525+
memcpy(REAL(data), buf, sz);
526+
} else {
527+
Rf_warning("received data could not be converted to double");
528+
data = Rf_allocVector(RAWSXP, sz);
529+
memcpy(RAW(data), buf, sz);
530+
}
517531
break;
518532
case 5:
519-
data = Rf_allocVector(INTSXP, sz / sizeof(int));
520-
memcpy(INTEGER(data), buf, sz);
533+
size = sizeof(int);
534+
if (sz % size == 0) {
535+
data = Rf_allocVector(INTSXP, sz / size);
536+
memcpy(INTEGER(data), buf, sz);
537+
} else {
538+
Rf_warning("received data could not be converted to integer");
539+
data = Rf_allocVector(RAWSXP, sz);
540+
memcpy(RAW(data), buf, sz);
541+
}
521542
break;
522543
case 6:
523-
data = Rf_allocVector(LGLSXP, sz / sizeof(int));
524-
memcpy(LOGICAL(data), buf, sz);
544+
size = sizeof(int);
545+
if (sz % size == 0) {
546+
data = Rf_allocVector(LGLSXP, sz / size);
547+
memcpy(LOGICAL(data), buf, sz);
548+
} else {
549+
Rf_warning("received data could not be converted to logical");
550+
data = Rf_allocVector(RAWSXP, sz);
551+
memcpy(RAW(data), buf, sz);
552+
}
525553
break;
526554
case 7:
527-
data = Rf_allocVector(REALSXP, sz / sizeof(double));
528-
memcpy(REAL(data), buf, sz);
555+
size = sizeof(double);
556+
if (sz % size == 0) {
557+
data = Rf_allocVector(REALSXP, sz / size);
558+
memcpy(REAL(data), buf, sz);
559+
} else {
560+
Rf_warning("received data could not be converted to numeric");
561+
data = Rf_allocVector(RAWSXP, sz);
562+
memcpy(RAW(data), buf, sz);
563+
}
529564
break;
530565
case 8:
531566
data = Rf_allocVector(RAWSXP, sz);
@@ -606,7 +641,7 @@ void context_finalizer(SEXP xptr) {
606641

607642
SEXP rnng_protocol_open(SEXP protocol) {
608643

609-
const int pro = *INTEGER(protocol);
644+
const int pro = INTEGER(protocol)[0];
610645
nng_socket *sock;
611646
char *pname;
612647
int xc;
@@ -944,7 +979,7 @@ SEXP rnng_send(SEXP socket, SEXP data, SEXP block, SEXP echo) {
944979
nng_socket *sock = (nng_socket *) R_ExternalPtrAddr(socket);
945980

946981
const nng_duration blk = (nng_duration) Rf_asInteger(block);
947-
const int ech = *LOGICAL(echo);
982+
const int ech = LOGICAL(echo)[0];
948983
int xc;
949984
nng_msg *msgp;
950985
nng_aio *aiop;
@@ -993,19 +1028,19 @@ SEXP rnng_recv(SEXP socket, SEXP mode, SEXP block, SEXP keep) {
9931028
nng_socket *sock = (nng_socket *) R_ExternalPtrAddr(socket);
9941029

9951030
mode = rnng_matcharg(mode);
996-
const int mod = *INTEGER(mode), kpr = *LOGICAL(keep);
1031+
const int mod = INTEGER(mode)[0], kpr = LOGICAL(keep)[0];
9971032
int xc;
9981033
void *buf;
9991034
size_t sz;
10001035
nng_aio *aiop;
10011036
SEXP res;
10021037

10031038
if (TYPEOF(block) == LGLSXP) {
1004-
const int blk = *LOGICAL(block);
1039+
const int blk = LOGICAL(block)[0];
10051040
xc = blk ? nng_recv(*sock, &buf, &sz, 1u): nng_recv(*sock, &buf, &sz, 3u);
10061041
if (xc)
10071042
return mk_error(xc);
1008-
res = nano_decode((unsigned char *) buf, sz, mod, kpr);
1043+
res = nano_decode(buf, sz, mod, kpr);
10091044
nng_free(buf, sz);
10101045

10111046
} else {
@@ -1024,7 +1059,7 @@ SEXP rnng_recv(SEXP socket, SEXP mode, SEXP block, SEXP keep) {
10241059
nng_msg *msgp = nng_aio_get_msg(aiop);
10251060
buf = nng_msg_body(msgp);
10261061
sz = nng_msg_len(msgp);
1027-
res = nano_decode((unsigned char *) buf, sz, mod, kpr);
1062+
res = nano_decode(buf, sz, mod, kpr);
10281063
nng_msg_free(msgp);
10291064
nng_aio_free(aiop);
10301065
}
@@ -1039,14 +1074,14 @@ SEXP rnng_ctx_send(SEXP context, SEXP data, SEXP timeout, SEXP echo) {
10391074
error_return("'con' is not a valid Context");
10401075
nng_ctx *ctxp = (nng_ctx *) R_ExternalPtrAddr(context);
10411076

1042-
const int ech = *LOGICAL(echo);
1077+
const int ech = LOGICAL(echo)[0];
10431078
nng_duration dur;
10441079
int xc;
10451080
nng_msg *msgp;
10461081
nng_aio *aiop;
10471082

10481083
if (TYPEOF(timeout) == LGLSXP) {
1049-
const int blk = *LOGICAL(timeout);
1084+
const int blk = LOGICAL(timeout)[0];
10501085
dur = blk ? -2 : 0;
10511086
} else {
10521087
dur = (nng_duration) Rf_asInteger(timeout);
@@ -1089,14 +1124,14 @@ SEXP rnng_ctx_recv(SEXP context, SEXP mode, SEXP timeout, SEXP keep) {
10891124
nng_duration dur;
10901125

10911126
mode = rnng_matcharg(mode);
1092-
const int mod = *INTEGER(mode), kpr = *LOGICAL(keep);
1127+
const int mod = INTEGER(mode)[0], kpr = LOGICAL(keep)[0];
10931128
int xc;
10941129
void *buf;
10951130
size_t sz;
10961131
SEXP res;
10971132

10981133
if (TYPEOF(timeout) == LGLSXP) {
1099-
const int blk = *LOGICAL(timeout);
1134+
const int blk = LOGICAL(timeout)[0];
11001135
dur = blk ? -2 : 0;
11011136
} else {
11021137
dur = (nng_duration) Rf_asInteger(timeout);
@@ -1118,7 +1153,7 @@ SEXP rnng_ctx_recv(SEXP context, SEXP mode, SEXP timeout, SEXP keep) {
11181153
nng_msg *msgp = nng_aio_get_msg(aiop);
11191154
buf = nng_msg_body(msgp);
11201155
sz = nng_msg_len(msgp);
1121-
res = nano_decode((unsigned char *) buf, sz, mod, kpr);
1156+
res = nano_decode(buf, sz, mod, kpr);
11221157
nng_msg_free(msgp);
11231158
nng_aio_free(aiop);
11241159

@@ -1132,14 +1167,14 @@ SEXP rnng_stream_send(SEXP stream, SEXP data, SEXP timeout, SEXP echo) {
11321167
error_return("'con' is not a valid Stream");
11331168
nng_stream *sp = (nng_stream *) R_ExternalPtrAddr(stream);
11341169

1135-
const int ech = *LOGICAL(echo);
1170+
const int ech = LOGICAL(echo)[0];
11361171
nng_duration dur;
11371172
int xc;
11381173
nng_iov iov;
11391174
nng_aio *aiop;
11401175

11411176
if (TYPEOF(timeout) == LGLSXP) {
1142-
const int blk = *LOGICAL(timeout);
1177+
const int blk = LOGICAL(timeout)[0];
11431178
dur = blk ? -2 : 0;
11441179
} else {
11451180
dur = (nng_duration) Rf_asInteger(timeout);
@@ -1148,7 +1183,7 @@ SEXP rnng_stream_send(SEXP stream, SEXP data, SEXP timeout, SEXP echo) {
11481183
const R_xlen_t xlen = Rf_xlength(enc);
11491184
unsigned char *dp = RAW(enc);
11501185

1151-
const int frames = *LOGICAL(Rf_getAttrib(stream, nano_TextframesSymbol));
1186+
const int frames = LOGICAL(Rf_getAttrib(stream, nano_TextframesSymbol))[0];
11521187
iov.iov_len = frames == 1 ? xlen - 1 : xlen;
11531188
iov.iov_buf = dp;
11541189

@@ -1186,7 +1221,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
11861221
nng_stream *sp = (nng_stream *) R_ExternalPtrAddr(stream);
11871222

11881223
mode = rnng_matchargs(mode);
1189-
const int mod = *INTEGER(mode), kpr = *LOGICAL(keep);
1224+
const int mod = INTEGER(mode)[0], kpr = LOGICAL(keep)[0];
11901225
const size_t xlen = (size_t) Rf_asInteger(bytes);
11911226
nng_duration dur;
11921227
int xc;
@@ -1196,7 +1231,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
11961231
SEXP res;
11971232

11981233
if (TYPEOF(timeout) == LGLSXP) {
1199-
const int blk = *LOGICAL(timeout);
1234+
const int blk = LOGICAL(timeout)[0];
12001235
dur = blk ? -2 : 0;
12011236
} else {
12021237
dur = (nng_duration) Rf_asInteger(timeout);
@@ -1230,7 +1265,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
12301265
}
12311266

12321267
sz = nng_aio_count(aiop);
1233-
res = nano_decode((unsigned char *) iov.iov_buf, sz, mod, kpr);
1268+
res = nano_decode(iov.iov_buf, sz, mod, kpr);
12341269
nng_aio_free(aiop);
12351270
R_Free(iov.iov_buf);
12361271

0 commit comments

Comments
 (0)