| 1 |
|
| 2 |
|
| 3 |
#include "nanonext.h" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
static int nano_fail_mode(SEXP mode) { |
| 8 |
|
| 9 |
if (TYPEOF(mode) == INTSXP) |
| 10 |
return NANO_INTEGER(mode); |
| 11 |
|
| 12 |
const char *mod = CHAR(STRING_ELT(mode, 0)); |
| 13 |
const size_t slen = strlen(mod); |
| 14 |
|
| 15 |
switch (slen) { |
| 16 |
case 4: |
| 17 |
if (!memcmp(mod, "warn", slen)) return 1; |
| 18 |
if (!memcmp(mod, "none", slen)) return 3; |
| 19 |
break; |
| 20 |
case 5: |
| 21 |
if (!memcmp(mod, "error", slen)) return 2; |
| 22 |
break; |
| 23 |
} |
| 24 |
|
| 25 |
Rf_error("`fail` should be one of: warn, error, none"); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static void context_finalizer(SEXP xptr) { |
| 32 |
|
| 33 |
if (NANO_PTR(xptr) == NULL) return; |
| 34 |
nng_ctx *xp = (nng_ctx *) NANO_PTR(xptr); |
| 35 |
nng_ctx_close(*xp); |
| 36 |
free(xp); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
SEXP rnng_ctx_open(SEXP socket) { |
| 43 |
|
| 44 |
if (NANO_PTR_CHECK(socket, nano_SocketSymbol)) |
| 45 |
Rf_error("`socket` is not a valid Socket"); |
| 46 |
|
| 47 |
nng_socket *sock = (nng_socket *) NANO_PTR(socket); |
| 48 |
SEXP context; |
| 49 |
int xc; |
| 50 |
nng_ctx *ctx = malloc(sizeof(nng_ctx)); |
| 51 |
NANO_ENSURE_ALLOC(ctx); |
| 52 |
|
| 53 |
if ((xc = nng_ctx_open(ctx, *sock))) |
| 54 |
goto fail; |
| 55 |
|
| 56 |
PROTECT(context = R_MakeExternalPtr(ctx, nano_ContextSymbol, NANO_PROT(socket))); |
| 57 |
R_RegisterCFinalizerEx(context, context_finalizer, TRUE); |
| 58 |
|
| 59 |
NANO_CLASS2(context, "nanoContext", "nano"); |
| 60 |
Rf_setAttrib(context, nano_IdSymbol, Rf_ScalarInteger(nng_ctx_id(*ctx))); |
| 61 |
Rf_setAttrib(context, nano_StateSymbol, Rf_mkString("opened")); |
| 62 |
Rf_setAttrib(context, nano_ProtocolSymbol, Rf_getAttrib(socket, nano_ProtocolSymbol)); |
| 63 |
Rf_setAttrib(context, nano_SocketSymbol, Rf_ScalarInteger(nng_socket_id(*sock))); |
| 64 |
|
| 65 |
UNPROTECT(1); |
| 66 |
return context; |
| 67 |
|
| 68 |
fail: |
| 69 |
free(ctx); |
| 70 |
failmem: |
| 71 |
ERROR_OUT(xc); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
SEXP rnng_ctx_create(SEXP socket) { |
| 76 |
|
| 77 |
if (NANO_PTR_CHECK(socket, nano_SocketSymbol)) |
| 78 |
Rf_error("`socket` is not a valid Socket"); |
| 79 |
|
| 80 |
nng_socket *sock = (nng_socket *) NANO_PTR(socket); |
| 81 |
SEXP context; |
| 82 |
int xc; |
| 83 |
nng_ctx *ctx = malloc(sizeof(nng_ctx)); |
| 84 |
NANO_ENSURE_ALLOC(ctx); |
| 85 |
|
| 86 |
if ((xc = nng_ctx_open(ctx, *sock))) |
| 87 |
goto fail; |
| 88 |
|
| 89 |
PROTECT(context = R_MakeExternalPtr(ctx, nano_ContextSymbol, NANO_PROT(socket))); |
| 90 |
R_RegisterCFinalizerEx(context, context_finalizer, TRUE); |
| 91 |
UNPROTECT(1); |
| 92 |
return context; |
| 93 |
|
| 94 |
fail: |
| 95 |
free(ctx); |
| 96 |
failmem: |
| 97 |
ERROR_OUT(xc); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
SEXP rnng_ctx_close(SEXP context) { |
| 102 |
|
| 103 |
if (NANO_PTR_CHECK(context, nano_ContextSymbol)) |
| 104 |
Rf_error("`context` is not a valid Context"); |
| 105 |
nng_ctx *ctx = (nng_ctx *) NANO_PTR(context); |
| 106 |
|
| 107 |
const int xc = nng_ctx_close(*ctx); |
| 108 |
if (xc) |
| 109 |
ERROR_RET(xc); |
| 110 |
|
| 111 |
Rf_setAttrib(context, nano_StateSymbol, Rf_mkString("closed")); |
| 112 |
return nano_success; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
SEXP rnng_dial(SEXP socket, SEXP url, SEXP tls, SEXP autostart, SEXP fail) { |
| 119 |
|
| 120 |
if (NANO_PTR_CHECK(socket, nano_SocketSymbol)) |
| 121 |
Rf_error("`socket` is not a valid Socket"); |
| 122 |
|
| 123 |
const int sec = tls != R_NilValue; |
| 124 |
|
| 125 |
if (sec && NANO_PTR_CHECK(tls, nano_TlsSymbol)) |
| 126 |
Rf_error("`tls` is not a valid TLS Configuration"); |
| 127 |
|
| 128 |
const int failmode = nano_fail_mode(fail); |
| 129 |
|
| 130 |
nng_socket *sock = (nng_socket *) NANO_PTR(socket); |
| 131 |
const int start = NANO_INTEGER(autostart); |
| 132 |
const char *ur = CHAR(STRING_ELT(url, 0)); |
| 133 |
|
| 134 |
SEXP dialer, attr, newattr, xp; |
| 135 |
nng_tls_config *cfg = NULL; |
| 136 |
nng_url *up = NULL; |
| 137 |
int xc; |
| 138 |
|
| 139 |
nng_dialer *dp = malloc(sizeof(nng_dialer)); |
| 140 |
NANO_ENSURE_ALLOC(dp); |
| 141 |
|
| 142 |
if (sec) { |
| 143 |
cfg = (nng_tls_config *) NANO_PTR(tls); |
| 144 |
if ((xc = nng_dialer_create(dp, *sock, ur)) || |
| 145 |
(xc = nng_url_parse(&up, ur)) || |
| 146 |
(xc = nng_tls_config_server_name(cfg, up->u_hostname)) || |
| 147 |
(xc = nng_dialer_set_ptr(*dp, NNG_OPT_TLS_CONFIG, cfg))) |
| 148 |
goto fail; |
| 149 |
nng_url_free(up); |
| 150 |
if (start && (xc = nng_dialer_start(*dp, start == 1 ? NNG_FLAG_NONBLOCK : 0))) |
| 151 |
goto fail; |
| 152 |
nng_tls_config_hold(cfg); |
| 153 |
|
| 154 |
PROTECT_INDEX pxi; |
| 155 |
PROTECT_WITH_INDEX(xp = R_MakeExternalPtr(cfg, nano_TlsSymbol, R_NilValue), &pxi); |
| 156 |
R_RegisterCFinalizerEx(xp, tls_finalizer, TRUE); |
| 157 |
REPROTECT(dialer = R_MakeExternalPtr(dp, nano_DialerSymbol, xp), pxi); |
| 158 |
|
| 159 |
} else { |
| 160 |
|
| 161 |
if ((xc = start ? nng_dial(*sock, ur, dp, start == 1 ? NNG_FLAG_NONBLOCK : 0) : nng_dialer_create(dp, *sock, ur))) |
| 162 |
goto fail; |
| 163 |
|
| 164 |
PROTECT(dialer = R_MakeExternalPtr(dp, nano_DialerSymbol, R_NilValue)); |
| 165 |
|
| 166 |
} |
| 167 |
R_RegisterCFinalizerEx(dialer, dialer_finalizer, TRUE); |
| 168 |
|
| 169 |
NANO_CLASS2(dialer, "nanoDialer", "nano"); |
| 170 |
Rf_setAttrib(dialer, nano_IdSymbol, Rf_ScalarInteger(nng_dialer_id(*dp))); |
| 171 |
Rf_setAttrib(dialer, nano_UrlSymbol, url); |
| 172 |
Rf_setAttrib(dialer, nano_StateSymbol, Rf_mkString(start ? "started" : "not started")); |
| 173 |
Rf_setAttrib(dialer, nano_SocketSymbol, Rf_ScalarInteger(nng_socket_id(*sock))); |
| 174 |
|
| 175 |
PROTECT(attr = Rf_getAttrib(socket, nano_DialerSymbol)); |
| 176 |
R_xlen_t xlen = Rf_xlength(attr); |
| 177 |
PROTECT(newattr = Rf_allocVector(VECSXP, xlen + 1)); |
| 178 |
for (R_xlen_t i = 0; i < xlen; i++) { |
| 179 |
SET_VECTOR_ELT(newattr, i, VECTOR_ELT(attr, i)); |
| 180 |
} |
| 181 |
SET_VECTOR_ELT(newattr, xlen, dialer); |
| 182 |
Rf_setAttrib(socket, nano_DialerSymbol, newattr); |
| 183 |
|
| 184 |
UNPROTECT(3); |
| 185 |
return nano_success; |
| 186 |
|
| 187 |
fail: |
| 188 |
nng_url_free(up); |
| 189 |
free(dp); |
| 190 |
failmem: |
| 191 |
if (failmode == 2) { |
| 192 |
ERROR_OUT(xc); |
| 193 |
} else if (failmode == 3) { |
| 194 |
return mk_error(xc); |
| 195 |
} |
| 196 |
ERROR_RET(xc); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
SEXP rnng_listen(SEXP socket, SEXP url, SEXP tls, SEXP autostart, SEXP fail) { |
| 201 |
|
| 202 |
if (NANO_PTR_CHECK(socket, nano_SocketSymbol)) |
| 203 |
Rf_error("`socket` is not a valid Socket"); |
| 204 |
|
| 205 |
const int sec = tls != R_NilValue; |
| 206 |
|
| 207 |
if (sec && NANO_PTR_CHECK(tls, nano_TlsSymbol)) |
| 208 |
Rf_error("`tls` is not a valid TLS Configuration"); |
| 209 |
|
| 210 |
const int failmode = nano_fail_mode(fail); |
| 211 |
|
| 212 |
nng_socket *sock = (nng_socket *) NANO_PTR(socket); |
| 213 |
const int start = NANO_INTEGER(autostart); |
| 214 |
const char *ur = CHAR(STRING_ELT(url, 0)); |
| 215 |
|
| 216 |
SEXP listener, attr, newattr, xp; |
| 217 |
nng_tls_config *cfg = NULL; |
| 218 |
int xc; |
| 219 |
|
| 220 |
nng_listener *lp = malloc(sizeof(nng_listener)); |
| 221 |
NANO_ENSURE_ALLOC(lp); |
| 222 |
|
| 223 |
if (sec) { |
| 224 |
cfg = (nng_tls_config *) NANO_PTR(tls); |
| 225 |
if ((xc = nng_listener_create(lp, *sock, ur)) || |
| 226 |
(xc = nng_listener_set_ptr(*lp, NNG_OPT_TLS_CONFIG, cfg)) || |
| 227 |
(start && (xc = nng_listener_start(*lp, 0)))) |
| 228 |
goto fail; |
| 229 |
nng_tls_config_hold(cfg); |
| 230 |
|
| 231 |
PROTECT_INDEX pxi; |
| 232 |
PROTECT_WITH_INDEX(xp = R_MakeExternalPtr(cfg, nano_TlsSymbol, R_NilValue), &pxi); |
| 233 |
R_RegisterCFinalizerEx(xp, tls_finalizer, TRUE); |
| 234 |
REPROTECT(listener = R_MakeExternalPtr(lp, nano_ListenerSymbol, xp), pxi); |
| 235 |
|
| 236 |
} else { |
| 237 |
|
| 238 |
if ((xc = start ? nng_listen(*sock, ur, lp, 0) : nng_listener_create(lp, *sock, ur))) |
| 239 |
goto fail; |
| 240 |
|
| 241 |
PROTECT(listener = R_MakeExternalPtr(lp, nano_ListenerSymbol, R_NilValue)); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
R_RegisterCFinalizerEx(listener, listener_finalizer, TRUE); |
| 246 |
|
| 247 |
NANO_CLASS2(listener, "nanoListener", "nano"); |
| 248 |
Rf_setAttrib(listener, nano_IdSymbol, Rf_ScalarInteger(nng_listener_id(*lp))); |
| 249 |
if (start) { |
| 250 |
nng_url *up; |
| 251 |
if (nng_url_parse(&up, ur) == 0) { |
| 252 |
if (up->u_port != NULL && up->u_port[0] == '0' && up->u_port[1] == '\0') { |
| 253 |
int port; |
| 254 |
if (nng_listener_get_int(*lp, NNG_OPT_TCP_BOUND_PORT, &port) == 0) |
| 255 |
url = nano_url_with_port(up, port); |
| 256 |
} |
| 257 |
nng_url_free(up); |
| 258 |
} |
| 259 |
} |
| 260 |
Rf_setAttrib(listener, nano_UrlSymbol, url); |
| 261 |
Rf_setAttrib(listener, nano_StateSymbol, Rf_mkString(start ? "started" : "not started")); |
| 262 |
Rf_setAttrib(listener, nano_SocketSymbol, Rf_ScalarInteger(nng_socket_id(*sock))); |
| 263 |
|
| 264 |
PROTECT(attr = Rf_getAttrib(socket, nano_ListenerSymbol)); |
| 265 |
R_xlen_t xlen = Rf_xlength(attr); |
| 266 |
PROTECT(newattr = Rf_allocVector(VECSXP, xlen + 1)); |
| 267 |
for (R_xlen_t i = 0; i < xlen; i++) { |
| 268 |
SET_VECTOR_ELT(newattr, i, VECTOR_ELT(attr, i)); |
| 269 |
} |
| 270 |
SET_VECTOR_ELT(newattr, xlen, listener); |
| 271 |
Rf_setAttrib(socket, nano_ListenerSymbol, newattr); |
| 272 |
|
| 273 |
UNPROTECT(3); |
| 274 |
return nano_success; |
| 275 |
|
| 276 |
fail: |
| 277 |
free(lp); |
| 278 |
failmem: |
| 279 |
if (failmode == 2) { |
| 280 |
ERROR_OUT(xc); |
| 281 |
} else if (failmode == 3) { |
| 282 |
return mk_error(xc); |
| 283 |
} |
| 284 |
ERROR_RET(xc); |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
SEXP rnng_dialer_start(SEXP dialer, SEXP async) { |
| 289 |
|
| 290 |
if (NANO_PTR_CHECK(dialer, nano_DialerSymbol)) |
| 291 |
Rf_error("`dialer` is not a valid Dialer"); |
| 292 |
nng_dialer *dial = (nng_dialer *) NANO_PTR(dialer); |
| 293 |
const int flags = (NANO_INTEGER(async) == 1) * NNG_FLAG_NONBLOCK; |
| 294 |
const int xc = nng_dialer_start(*dial, flags); |
| 295 |
if (xc) |
| 296 |
ERROR_RET(xc); |
| 297 |
|
| 298 |
Rf_setAttrib(dialer, nano_StateSymbol, Rf_mkString("started")); |
| 299 |
return nano_success; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
SEXP rnng_listener_start(SEXP listener) { |
| 304 |
|
| 305 |
if (NANO_PTR_CHECK(listener, nano_ListenerSymbol)) |
| 306 |
Rf_error("`listener` is not a valid Listener"); |
| 307 |
nng_listener *list = (nng_listener *) NANO_PTR(listener); |
| 308 |
const int xc = nng_listener_start(*list, 0); |
| 309 |
if (xc) |
| 310 |
ERROR_RET(xc); |
| 311 |
|
| 312 |
SEXP url = Rf_getAttrib(listener, nano_UrlSymbol); |
| 313 |
nng_url *up; |
| 314 |
if (nng_url_parse(&up, CHAR(STRING_ELT(url, 0))) == 0) { |
| 315 |
if (up->u_port != NULL && up->u_port[0] == '0' && up->u_port[1] == '\0') { |
| 316 |
int port; |
| 317 |
if (nng_listener_get_int(*list, NNG_OPT_TCP_BOUND_PORT, &port) == 0) |
| 318 |
Rf_setAttrib(listener, nano_UrlSymbol, nano_url_with_port(up, port)); |
| 319 |
} |
| 320 |
nng_url_free(up); |
| 321 |
} |
| 322 |
Rf_setAttrib(listener, nano_StateSymbol, Rf_mkString("started")); |
| 323 |
return nano_success; |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
SEXP rnng_dialer_close(SEXP dialer) { |
| 328 |
|
| 329 |
if (NANO_PTR_CHECK(dialer, nano_DialerSymbol)) |
| 330 |
Rf_error("`dialer` is not a valid Dialer"); |
| 331 |
nng_dialer *dial = (nng_dialer *) NANO_PTR(dialer); |
| 332 |
const int xc = nng_dialer_close(*dial); |
| 333 |
if (xc) |
| 334 |
ERROR_RET(xc); |
| 335 |
Rf_setAttrib(dialer, nano_StateSymbol, Rf_mkString("closed")); |
| 336 |
return nano_success; |
| 337 |
|
| 338 |
} |
| 339 |
|
| 340 |
SEXP rnng_listener_close(SEXP listener) { |
| 341 |
|
| 342 |
if (NANO_PTR_CHECK(listener, nano_ListenerSymbol)) |
| 343 |
Rf_error("`listener` is not a valid Listener"); |
| 344 |
nng_listener *list = (nng_listener *) NANO_PTR(listener); |
| 345 |
const int xc = nng_listener_close(*list); |
| 346 |
if (xc) |
| 347 |
ERROR_RET(xc); |
| 348 |
Rf_setAttrib(listener, nano_StateSymbol, Rf_mkString("closed")); |
| 349 |
return nano_success; |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block, SEXP pipe) { |
| 356 |
|
| 357 |
const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : nano_integer(block); |
| 358 |
const int raw = nano_encode_mode(mode); |
| 359 |
nano_buf buf; |
| 360 |
int sock, xc; |
| 361 |
|
| 362 |
if ((sock = !NANO_PTR_CHECK(con, nano_SocketSymbol)) || !NANO_PTR_CHECK(con, nano_ContextSymbol)) { |
| 363 |
|
| 364 |
const int pipeid = sock ? nano_integer(pipe) : 0; |
| 365 |
if (raw) { |
| 366 |
nano_encode(&buf, data); |
| 367 |
} else { |
| 368 |
nano_serialize(&buf, data, NANO_PROT(con), 0); |
| 369 |
} |
| 370 |
nng_msg *msgp = NULL; |
| 371 |
|
| 372 |
if ((xc = nng_msg_alloc(&msgp, 0))) |
| 373 |
goto fail; |
| 374 |
|
| 375 |
nano_msg_set_body(msgp, &buf); |
| 376 |
|
| 377 |
if (pipeid) { |
| 378 |
nng_pipe p; |
| 379 |
p.id = (uint32_t) pipeid; |
| 380 |
nng_msg_set_pipe(msgp, p); |
| 381 |
} |
| 382 |
|
| 383 |
if (flags <= 0) { |
| 384 |
|
| 385 |
if ((xc = sock ? nng_sendmsg(*(nng_socket *) NANO_PTR(con), msgp, flags ? NNG_FLAG_NONBLOCK : (NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK) : |
| 386 |
nng_ctx_sendmsg(*(nng_ctx *) NANO_PTR(con), msgp, flags ? NNG_FLAG_NONBLOCK : (NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK))) { |
| 387 |
nng_msg_free(msgp); |
| 388 |
goto fail; |
| 389 |
} |
| 390 |
|
| 391 |
NANO_FREE(buf); |
| 392 |
|
| 393 |
} else { |
| 394 |
|
| 395 |
nng_aio *aiop = NULL; |
| 396 |
|
| 397 |
if ((xc = nng_aio_alloc(&aiop, NULL, NULL))) { |
| 398 |
nng_msg_free(msgp); |
| 399 |
goto fail; |
| 400 |
} |
| 401 |
|
| 402 |
nng_aio_set_msg(aiop, msgp); |
| 403 |
nng_aio_set_timeout(aiop, flags); |
| 404 |
sock ? nng_send_aio(*(nng_socket *) NANO_PTR(con), aiop) : |
| 405 |
nng_ctx_send(*(nng_ctx *) NANO_PTR(con), aiop); |
| 406 |
NANO_FREE(buf); |
| 407 |
nng_aio_wait(aiop); |
| 408 |
if ((xc = nng_aio_result(aiop))) |
| 409 |
nng_msg_free(nng_aio_get_msg(aiop)); |
| 410 |
nng_aio_free(aiop); |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
} else if (!NANO_PTR_CHECK(con, nano_StreamSymbol)) { |
| 415 |
|
| 416 |
nano_encode(&buf, data); |
| 417 |
|
| 418 |
nano_stream *nst = (nano_stream *) NANO_PTR(con); |
| 419 |
nng_stream *sp = nst->stream; |
| 420 |
nng_aio *aiop = NULL; |
| 421 |
|
| 422 |
if ((xc = nng_aio_alloc(&aiop, NULL, NULL))) |
| 423 |
goto fail; |
| 424 |
|
| 425 |
if (nst->msgmode) { |
| 426 |
nng_msg *msgp; |
| 427 |
const size_t xlen = buf.cur - nst->textframes; |
| 428 |
if ((xc = nng_msg_alloc(&msgp, xlen))) { |
| 429 |
nng_aio_free(aiop); |
| 430 |
goto fail; |
| 431 |
} |
| 432 |
memcpy(nng_msg_body(msgp), buf.buf, xlen); |
| 433 |
nng_aio_set_msg(aiop, msgp); |
| 434 |
} else { |
| 435 |
nng_iov iov = { |
| 436 |
.iov_buf = buf.buf, |
| 437 |
.iov_len = buf.cur - nst->textframes |
| 438 |
}; |
| 439 |
if ((xc = nng_aio_set_iov(aiop, 1u, &iov))) { |
| 440 |
nng_aio_free(aiop); |
| 441 |
goto fail; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
nng_aio_set_timeout(aiop, flags ? flags : (NANO_INTEGER(block) != 0) * NNG_DURATION_DEFAULT); |
| 446 |
nng_stream_send(sp, aiop); |
| 447 |
NANO_FREE(buf); |
| 448 |
nng_aio_wait(aiop); |
| 449 |
if ((xc = nng_aio_result(aiop)) && nst->msgmode) |
| 450 |
nng_msg_free(nng_aio_get_msg(aiop)); |
| 451 |
nng_aio_free(aiop); |
| 452 |
|
| 453 |
} else { |
| 454 |
Rf_error("`con` is not a valid Socket, Context or Stream"); |
| 455 |
} |
| 456 |
|
| 457 |
if (xc) |
| 458 |
return mk_error(xc); |
| 459 |
|
| 460 |
return nano_success; |
| 461 |
|
| 462 |
fail: |
| 463 |
NANO_FREE(buf); |
| 464 |
return mk_error(xc); |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
SEXP rnng_recv(SEXP con, SEXP mode, SEXP block) { |
| 469 |
|
| 470 |
const int flags = block == R_NilValue ? NNG_DURATION_DEFAULT : TYPEOF(block) == LGLSXP ? 0 : nano_integer(block); |
| 471 |
int xc; |
| 472 |
unsigned char *buf = NULL; |
| 473 |
size_t sz; |
| 474 |
SEXP res; |
| 475 |
|
| 476 |
if (!NANO_PTR_CHECK(con, nano_SocketSymbol)) { |
| 477 |
|
| 478 |
const int mod = nano_matcharg(mode); |
| 479 |
nng_socket *sock = (nng_socket *) NANO_PTR(con); |
| 480 |
nng_msg *msgp = NULL; |
| 481 |
|
| 482 |
if (flags <= 0) { |
| 483 |
|
| 484 |
if ((xc = nng_recvmsg(*sock, &msgp, (flags < 0 || NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK))) |
| 485 |
goto fail; |
| 486 |
|
| 487 |
} else { |
| 488 |
|
| 489 |
nng_aio *aiop = NULL; |
| 490 |
if ((xc = nng_aio_alloc(&aiop, NULL, NULL))) |
| 491 |
goto fail; |
| 492 |
nng_aio_set_timeout(aiop, flags); |
| 493 |
nng_recv_aio(*sock, aiop); |
| 494 |
nng_aio_wait(aiop); |
| 495 |
if ((xc = nng_aio_result(aiop))) { |
| 496 |
nng_aio_free(aiop); |
| 497 |
goto fail; |
| 498 |
} |
| 499 |
msgp = nng_aio_get_msg(aiop); |
| 500 |
nng_aio_free(aiop); |
| 501 |
} |
| 502 |
buf = nng_msg_body(msgp); |
| 503 |
sz = nng_msg_len(msgp); |
| 504 |
res = nano_decode(buf, sz, mod, NANO_PROT(con)); |
| 505 |
nng_msg_free(msgp); |
| 506 |
|
| 507 |
} else if (!NANO_PTR_CHECK(con, nano_ContextSymbol)) { |
| 508 |
|
| 509 |
const int mod = nano_matcharg(mode); |
| 510 |
nng_ctx *ctxp = (nng_ctx *) NANO_PTR(con); |
| 511 |
nng_msg *msgp = NULL; |
| 512 |
|
| 513 |
if (flags <= 0) { |
| 514 |
|
| 515 |
if ((xc = nng_ctx_recvmsg(*ctxp, &msgp, (flags < 0 || NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK))) |
| 516 |
goto fail; |
| 517 |
|
| 518 |
buf = nng_msg_body(msgp); |
| 519 |
sz = nng_msg_len(msgp); |
| 520 |
res = nano_decode(buf, sz, mod, NANO_PROT(con)); |
| 521 |
nng_msg_free(msgp); |
| 522 |
|
| 523 |
} else { |
| 524 |
|
| 525 |
nng_aio *aiop = NULL; |
| 526 |
|
| 527 |
if ((xc = nng_aio_alloc(&aiop, NULL, NULL))) |
| 528 |
goto fail; |
| 529 |
nng_aio_set_timeout(aiop, flags); |
| 530 |
nng_ctx_recv(*ctxp, aiop); |
| 531 |
|
| 532 |
nng_aio_wait(aiop); |
| 533 |
if ((xc = nng_aio_result(aiop))) { |
| 534 |
nng_aio_free(aiop); |
| 535 |
goto fail; |
| 536 |
} |
| 537 |
|
| 538 |
msgp = nng_aio_get_msg(aiop); |
| 539 |
nng_aio_free(aiop); |
| 540 |
buf = nng_msg_body(msgp); |
| 541 |
sz = nng_msg_len(msgp); |
| 542 |
res = nano_decode(buf, sz, mod, NANO_PROT(con)); |
| 543 |
nng_msg_free(msgp); |
| 544 |
|
| 545 |
} |
| 546 |
|
| 547 |
} else if (!NANO_PTR_CHECK(con, nano_StreamSymbol)) { |
| 548 |
|
| 549 |
const int mod = nano_matcharg(mode) == 1 ? 2 : nano_matcharg(mode); |
| 550 |
nano_stream *nst = (nano_stream *) NANO_PTR(con); |
| 551 |
nng_stream *sp = nst->stream; |
| 552 |
nng_aio *aiop = NULL; |
| 553 |
|
| 554 |
if ((xc = nng_aio_alloc(&aiop, NULL, NULL))) |
| 555 |
goto fail; |
| 556 |
|
| 557 |
if (nst->msgmode) { |
| 558 |
nng_msg *msgp; |
| 559 |
nng_aio_set_timeout(aiop, flags ? flags : (NANO_INTEGER(block) != 0) * NNG_DURATION_DEFAULT); |
| 560 |
nng_stream_recv(sp, aiop); |
| 561 |
nng_aio_wait(aiop); |
| 562 |
if ((xc = nng_aio_result(aiop))) { |
| 563 |
nng_aio_free(aiop); |
| 564 |
goto fail; |
| 565 |
} |
| 566 |
msgp = nng_aio_get_msg(aiop); |
| 567 |
nng_aio_free(aiop); |
| 568 |
buf = nng_msg_body(msgp); |
| 569 |
sz = nng_msg_len(msgp); |
| 570 |
res = nano_decode(buf, sz, mod, NANO_PROT(con)); |
| 571 |
nng_msg_free(msgp); |
| 572 |
} else { |
| 573 |
size_t xlen = nst->bufsize; |
| 574 |
buf = malloc(xlen); |
| 575 |
NANO_ENSURE_ALLOC(buf); |
| 576 |
nng_iov iov = { |
| 577 |
.iov_buf = buf, |
| 578 |
.iov_len = xlen |
| 579 |
}; |
| 580 |
if ((xc = nng_aio_set_iov(aiop, 1u, &iov))) { |
| 581 |
nng_aio_free(aiop); |
| 582 |
goto fail; |
| 583 |
} |
| 584 |
nng_aio_set_timeout(aiop, flags ? flags : (NANO_INTEGER(block) != 0) * NNG_DURATION_DEFAULT); |
| 585 |
nng_stream_recv(sp, aiop); |
| 586 |
nng_aio_wait(aiop); |
| 587 |
if ((xc = nng_aio_result(aiop))) { |
| 588 |
nng_aio_free(aiop); |
| 589 |
goto fail; |
| 590 |
} |
| 591 |
sz = nng_aio_count(aiop); |
| 592 |
nng_aio_free(aiop); |
| 593 |
res = nano_decode(buf, sz, mod, NANO_PROT(con)); |
| 594 |
free(buf); |
| 595 |
} |
| 596 |
|
| 597 |
} else { |
| 598 |
Rf_error("`con` is not a valid Socket, Context or Stream"); |
| 599 |
} |
| 600 |
|
| 601 |
return res; |
| 602 |
|
| 603 |
fail: |
| 604 |
free(buf); |
| 605 |
failmem: |
| 606 |
return mk_error(xc); |
| 607 |
|
| 608 |
} |