Debian Codegraph @

src/aio.c

raw symbols SCIP
1 // nanonext - C level - Async Functions ----------------------------------------
2
3 #define NANONEXT_SIGNALS
4 #include "nanonext.h"
5
6 // internals -------------------------------------------------------------------
7
8 static SEXP mk_error_aio(const int xc, SEXP env) {
9
10 SEXP err;
11 PROTECT(err = Rf_ScalarInteger(xc));
12 Rf_classgets(err, nano_error);
13 Rf_defineVar(nano_ValueSymbol, err, env);
14 Rf_defineVar(nano_AioSymbol, nano_success, env);
15 UNPROTECT(1);
16 return err;
17
18 }
19
20 // aio completion callbacks ----------------------------------------------------
21
22 void nano_list_do(nano_list_op listop, nano_aio *saio) {
23
24 static nano_aio *free_list = NULL;
25 static nng_mtx *free_mtx = NULL;
26
27 switch (listop) {
28 case INIT:
29 if (free_mtx == NULL && nng_mtx_alloc(&free_mtx))
30 Rf_error("NNG library init failure");
31 break;
32 case FINALIZE:
33 nng_mtx_lock(free_mtx);
34 nano_list_do(FREE, NULL);
35 if (saio->mode == 0x1) {
36 nng_mtx_unlock(free_mtx);
37 nng_aio_free(saio->aio);
38 if (saio->data != NULL)
39 free(saio->data);
40 free(saio);
41 } else {
42 saio->mode = 0x1;
43 nng_mtx_unlock(free_mtx);
44 }
45 break;
46 case COMPLETE:
47 nng_mtx_lock(free_mtx);
48 if (saio->mode == 0x1) {
49 saio->next = free_list;
50 free_list = saio;
51 } else {
52 saio->mode = 0x1;
53 }
54 nng_mtx_unlock(free_mtx);
55 break;
56 case SHUTDOWN:
57 if (free_mtx == NULL) break;
58 nng_mtx_lock(free_mtx);
59 nano_list_do(FREE, NULL);
60 nng_mtx_unlock(free_mtx);
61 nng_mtx_free(free_mtx);
62 free_mtx = NULL;
63 break;
64 case FREE: // must be entered under lock
65 while (free_list != NULL) {
66 nano_aio *current = free_list;
67 free_list = (nano_aio *) current->next;
68 nng_aio_free(current->aio);
69 if (current->data != NULL)
70 free(current->data);
71 free(current);
72 }
73 break;
74 }
75
76 }
77
78 static void saio_complete(void *arg) {
79
80 nano_aio *saio = (nano_aio *) arg;
81 const int res = nng_aio_result(saio->aio);
82 if (res)
83 nng_msg_free(nng_aio_get_msg(saio->aio));
84 saio->result = res - !res;
85
86 nano_list_do(COMPLETE, saio);
87
88 }
89
90 static void isaio_complete(void *arg) {
91
92 nano_aio *iaio = (nano_aio *) arg;
93 const int res = nng_aio_result(iaio->aio);
94 iaio->result = res - !res;
95
96 nano_list_do(COMPLETE, iaio);
97
98 }
99
100 static void raio_complete(void *arg) {
101
102 nano_aio *raio = (nano_aio *) arg;
103 int res = nng_aio_result(raio->aio);
104 if (res == 0) {
105 nng_msg *msg = nng_aio_get_msg(raio->aio);
106 raio->data = msg;
107 nng_pipe p = nng_msg_get_pipe(msg);
108 res = - (int) p.id;
109 }
110
111 if (raio->next != NULL) {
112 nano_cv *ncv = (nano_cv *) raio->next;
113 nng_cv *cv = ncv->cv;
114 nng_mtx *mtx = ncv->mtx;
115
116 nng_mtx_lock(mtx);
117 raio->result = res;
118 ncv->condition++;
119 nng_cv_wake(cv);
120 nng_mtx_unlock(mtx);
121 } else {
122 raio->result = res;
123 }
124
125 if (raio->cb != NULL)
126 later2(raio_invoke_cb, raio->cb);
127
128 }
129
130 static void raio_complete_interrupt(void *arg) {
131
132 nano_aio *raio = (nano_aio *) arg;
133 int res = nng_aio_result(raio->aio);
134 if (res == 0) {
135 nng_msg *msg = nng_aio_get_msg(raio->aio);
136 raio->data = msg;
137 nng_pipe p = nng_msg_get_pipe(msg);
138 res = - (int) p.id;
139 }
140
141 raio->result = res;
142
143 if (raio->cb != NULL)
144 later2(raio_invoke_cb, raio->cb);
145
146 if (res <= 0) {
147 #ifdef _WIN32
148 UserBreak = 1;
149 #else
150 R_interrupts_pending = 1;
151 kill(getpid(), SIGINT);
152 #endif
153 }
154
155 }
156
157 static void iraio_complete(void *arg) {
158
159 nano_aio *iaio = (nano_aio *) arg;
160 const int res = nng_aio_result(iaio->aio);
161
162 if (iaio->next != NULL) {
163 nano_cv *ncv = (nano_cv *) iaio->next;
164 nng_cv *cv = ncv->cv;
165 nng_mtx *mtx = ncv->mtx;
166
167 nng_mtx_lock(mtx);
168 iaio->result = res - !res;
169 ncv->condition++;
170 nng_cv_wake(cv);
171 nng_mtx_unlock(mtx);
172 } else {
173 iaio->result = res - !res;
174 }
175
176 if (iaio->cb != NULL)
177 later2(raio_invoke_cb, iaio->cb);
178
179 }
180
181 // finalisers ------------------------------------------------------------------
182
183 static void saio_finalizer(SEXP xptr) {
184
185 if (NANO_PTR(xptr) == NULL) return;
186 nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
187 nano_list_do(FINALIZE, xp);
188
189 }
190
191 static void iaio_finalizer(SEXP xptr) {
192
193 if (NANO_PTR(xptr) == NULL) return;
194 nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
195 nng_aio_free(xp->aio);
196 if (xp->data != NULL)
197 free(xp->data);
198 free(xp);
199
200 }
201
202 static void raio_finalizer(SEXP xptr) {
203
204 if (NANO_PTR(xptr) == NULL) return;
205 nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
206 nng_aio_free(xp->aio);
207 if (xp->data != NULL)
208 nng_msg_free((nng_msg *) xp->data);
209 free(xp);
210
211 }
212
213 // core aio - internal ---------------------------------------------------------
214
215 static inline SEXP create_aio_result(SEXP env, nano_aio *saio) {
216
217 if (saio->result > 0)
218 return mk_error_aio(saio->result, env);
219
220 Rf_defineVar(nano_ValueSymbol, nano_success, env);
221 Rf_defineVar(nano_AioSymbol, R_NilValue, env);
222 return nano_success;
223
224 }
225
226 static inline SEXP create_aio_msg(SEXP env, SEXP aio, nano_aio *raio, int res) {
227
228 SEXP out, pipe;
229 unsigned char *buf;
230 size_t sz;
231
232 if (raio->type == IOV_RECVAIO || raio->type == IOV_RECVAIOS) {
233 buf = raio->data;
234 sz = nng_aio_count(raio->aio);
235 PROTECT(out = nano_decode(buf, sz, raio->mode, NANO_PROT(aio)));
236 free(raio->data);
237 } else {
238 nng_msg *msg = (nng_msg *) raio->data;
239 buf = nng_msg_body(msg);
240 sz = nng_msg_len(msg);
241 PROTECT(out = nano_decode(buf, sz, raio->mode, NANO_PROT(aio)));
242 nng_msg_free(msg);
243 }
244 raio->data = NULL;
245
246 PROTECT(pipe = Rf_ScalarInteger(-res));
247 Rf_defineVar(nano_ValueSymbol, out, env);
248 Rf_defineVar(nano_AioSymbol, pipe, env);
249
250 UNPROTECT(2);
251 return out;
252
253 }
254
255
256 SEXP nano_aio_result(SEXP env) {
257
258 int found;
259 SEXP exist = nano_findVarInFrame(env, nano_ValueSymbol, &found);
260 if (found)
261 return exist;
262
263 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
264 nano_aio *saio = (nano_aio *) NANO_PTR(aio);
265
266 return create_aio_result(env, saio);
267
268 }
269
270 SEXP nano_aio_get_msg(SEXP env) {
271
272 int found;
273 SEXP exist = nano_findVarInFrame(env, nano_ValueSymbol, &found);
274 if (found)
275 return exist;
276
277 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
278 nano_aio *raio = (nano_aio *) NANO_PTR(aio);
279
280 int res;
281 switch (raio->type) {
282 case RECVAIO:
283 case REQAIO:
284 case IOV_RECVAIO:
285 case RECVAIOS:
286 case REQAIOS:
287 case IOV_RECVAIOS:
288 res = raio->result;
289 if (res > 0)
290 return mk_error_aio(res, env);
291 break;
292 default:
293 /* not reached */
294 res = 0;
295 }
296
297 return create_aio_msg(env, aio, raio, res);
298
299 }
300
301 // core aio --------------------------------------------------------------------
302
303 SEXP rnng_aio_result(SEXP env) {
304
305 int found;
306 SEXP exist = nano_findVarInFrame(env, nano_ValueSymbol, &found);
307 if (found)
308 return exist;
309
310 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
311 nano_aio *saio = (nano_aio *) NANO_PTR(aio);
312
313 if (nng_aio_busy(saio->aio))
314 return nano_unresolved;
315
316 return create_aio_result(env, saio);
317
318 }
319
320 SEXP rnng_aio_get_msg(SEXP env) {
321
322 int found;
323 SEXP exist = nano_findVarInFrame(env, nano_ValueSymbol, &found);
324 if (found)
325 return exist;
326
327 const SEXP aio = nano_findVarInFrame(env, nano_AioSymbol, NULL);
328 nano_aio *raio = (nano_aio *) NANO_PTR(aio);
329
330 int res;
331 switch (raio->type) {
332 case RECVAIO:
333 case REQAIO:
334 case IOV_RECVAIO:
335 if (nng_aio_busy(raio->aio))
336 return nano_unresolved;
337
338 res = raio->result;
339 if (res > 0)
340 return mk_error_aio(res, env);
341
342 break;
343 case RECVAIOS:
344 case REQAIOS:
345 case IOV_RECVAIOS: {
346 nng_mtx *mtx = ((nano_cv *) raio->next)->mtx;
347 nng_mtx_lock(mtx);
348 res = raio->result;
349 nng_mtx_unlock(mtx);
350
351 if (res == 0)
352 return nano_unresolved;
353
354 if (res > 0)
355 return mk_error_aio(res, env);
356
357 break;
358 }
359 default:
360 /* not reached */
361 res = 0;
362 }
363
364 return create_aio_msg(env, aio, raio, res);
365
366 }
367
368 SEXP rnng_aio_call(SEXP x) {
369
370 switch (TYPEOF(x)) {
371 case ENVSXP: {
372 const SEXP coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL);
373 if (NANO_PTR_CHECK(coreaio, nano_AioSymbol))
374 return x;
375
376 nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio);
377 nng_aio_wait(aiop->aio);
378 switch (aiop->type) {
379 case RECVAIO:
380 case REQAIO:
381 case IOV_RECVAIO:
382 case RECVAIOS:
383 case REQAIOS:
384 case IOV_RECVAIOS:
385 nano_aio_get_msg(x);
386 break;
387 case SENDAIO:
388 case IOV_SENDAIO:
389 nano_aio_result(x);
390 break;
391 case HTTP_AIO:
392 nano_aio_http_status(x);
393 break;
394 }
395 break;
396 }
397 case VECSXP: {
398 const R_xlen_t xlen = Rf_xlength(x);
399 for (R_xlen_t i = 0; i < xlen; i++) {
400 rnng_aio_call(VECTOR_PTR_RO(x)[i]);
401 }
402 break;
403 }
404 }
405
406 return x;
407
408 }
409
410 static SEXP rnng_aio_collect_impl(SEXP x, SEXP (*const func)(SEXP)) {
411
412 SEXP out;
413 int found;
414
415 switch (TYPEOF(x)) {
416 case ENVSXP: {
417 out = nano_findVarInFrame(func(x), nano_ValueSymbol, &found);
418 if (!found) goto fail;
419 break;
420 }
421 case VECSXP: {
422 SEXP env, val, names;
423 const R_xlen_t xlen = Rf_xlength(x);
424 PROTECT(out = Rf_allocVector(VECSXP, xlen));
425 for (R_xlen_t i = 0; i < xlen; i++) {
426 env = func(VECTOR_PTR_RO(x)[i]);
427 if (TYPEOF(env) != ENVSXP) goto fail;
428 val = nano_findVarInFrame(env, nano_ValueSymbol, &found);
429 if (!found) goto fail;
430 SET_VECTOR_ELT(out, i, val);
431 }
432 names = Rf_getAttrib(x, R_NamesSymbol);
433 if (names != R_NilValue)
434 out = Rf_namesgets(out, names);
435 UNPROTECT(1);
436 break;
437 }
438 default:
439 goto fail;
440 }
441
442 return out;
443
444 fail:
445 Rf_error("object is not an Aio or list of Aios");
446
447 }
448
449 SEXP rnng_aio_collect(SEXP x) {
450
451 return rnng_aio_collect_impl(x, rnng_aio_call);
452
453 }
454
455 SEXP rnng_aio_collect_safe(SEXP x) {
456
457 return rnng_aio_collect_impl(x, rnng_wait_thread_create);
458
459 }
460
461 SEXP rnng_aio_stop(SEXP x) {
462
463 switch (TYPEOF(x)) {
464 case ENVSXP: {
465 const SEXP coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL);
466 if (NANO_PTR_CHECK(coreaio, nano_AioSymbol)) break;
467 nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio);
468 nng_aio_stop(aiop->aio);
469 // See #194, this is to reset the R interrupts state after an interrupt
470 // requested by `stop_request()` has already triggered
471 #ifdef _WIN32
472 UserBreak = 0;
473 #else
474 R_interrupts_pending = 0;
475 #endif
476 break;
477 }
478 case VECSXP: {
479 const R_xlen_t xlen = Rf_xlength(x);
480 for (R_xlen_t i = 0; i < xlen; i++) {
481 rnng_aio_stop(VECTOR_PTR_RO(x)[i]);
482 }
483 break;
484 }
485 }
486
487 return R_NilValue;
488
489 }
490
491 SEXP rnng_request_stop(SEXP x) {
492
493 SEXP out;
494 switch (TYPEOF(x)) {
495 case ENVSXP: {
496 int res = 0;
497 SEXP coreaio;
498 PROTECT(coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL));
499 if (NANO_PTR_CHECK(coreaio, nano_AioSymbol)) goto fail;
500 nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio);
501 if (aiop->type != REQAIOS && aiop->type != REQAIO) goto fail;
502 nng_aio_stop(aiop->aio);
503 nano_saio *saio = (nano_saio *) aiop->cb;
504 if (saio->disp == NULL) goto fail;
505 if (saio->type) {
506 nng_msg *msgp = NULL;
507 nng_ctx *ctx = (nng_ctx *) saio->disp;
508 const nng_duration dur = NANONEXT_WAIT_DUR;
509 if (nng_ctx_set_ms(*ctx, "send-timeout", dur) ||
510 nng_ctx_set_ms(*ctx, "recv-timeout", dur) ||
511 nng_msg_alloc(&msgp, 0) ||
512 nng_msg_append_u32(msgp, 0) ||
513 nng_msg_append(msgp, &saio->id, sizeof(int)) ||
514 nng_ctx_sendmsg(*ctx, msgp, 0)) {
515 nng_msg_free(msgp);
516 goto fail;
517 }
518 msgp = NULL;
519 if (nng_ctx_recvmsg(*ctx, &msgp, 0)) {
520 nng_msg_free(msgp);
521 goto fail;
522 }
523 memcpy(&res, nng_msg_body(msgp), sizeof(int));
524 nng_msg_free(msgp);
525 } else {
526 res = dispatch_cancel_direct(saio->disp, saio->id);
527 }
528
529 fail:
530 UNPROTECT(1);
531 out = Rf_ScalarLogical(res != 0);
532 break;
533 }
534 case VECSXP: {
535 const R_xlen_t xlen = Rf_xlength(x);
536 PROTECT(out = Rf_allocVector(LGLSXP, xlen));
537 for (R_xlen_t i = xlen - 1; i >= 0; i--) {
538 SEXP item = rnng_request_stop(VECTOR_PTR_RO(x)[i]);
539 SET_LOGICAL_ELT(out, i, NANO_INTEGER(item));
540 }
541 UNPROTECT(1);
542 break;
543 }
544 default:
545 out = Rf_ScalarLogical(0);
546 }
547
548 return out;
549
550 }
551
552 static int rnng_unresolved_impl(SEXP x) {
553
554 int xc;
555 switch (TYPEOF(x)) {
556 case ENVSXP: {
557 const SEXP coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL);
558 if (NANO_PTR_CHECK(coreaio, nano_AioSymbol)) {
559 xc = 0; break;
560 }
561 SEXP value;
562 nano_aio *aio = (nano_aio *) NANO_PTR(coreaio);
563 switch (aio->type) {
564 case SENDAIO:
565 case IOV_SENDAIO:
566 value = rnng_aio_result(x);
567 break;
568 case HTTP_AIO:
569 value = rnng_aio_http_status(x);
570 break;
571 default:
572 value = rnng_aio_get_msg(x);
573 break;
574 }
575 xc = value == nano_unresolved;
576 break;
577 }
578 case LGLSXP:
579 xc = x == nano_unresolved;
580 break;
581 default:
582 xc = 0;
583 }
584
585 return xc;
586
587 }
588
589 SEXP rnng_unresolved(SEXP x) {
590
591 switch (TYPEOF(x)) {
592 case ENVSXP:
593 case LGLSXP:
594 return Rf_ScalarLogical(rnng_unresolved_impl(x));
595 case VECSXP: {
596 const R_xlen_t xlen = Rf_xlength(x);
597 for (R_xlen_t i = 0; i < xlen; i++) {
598 if (rnng_unresolved_impl(VECTOR_PTR_RO(x)[i]))
599 return Rf_ScalarLogical(1);
600 }
601 }
602 }
603
604 return Rf_ScalarLogical(0);
605
606 }
607
608 static int rnng_unresolved2_impl(SEXP x) {
609
610 if (TYPEOF(x) == ENVSXP) {
611 const SEXP coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL);
612 if (NANO_PTR_CHECK(coreaio, nano_AioSymbol))
613 return 0;
614 nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio);
615 return nng_aio_busy(aiop->aio);
616 }
617
618 return 0;
619
620 }
621
622 SEXP rnng_unresolved2(SEXP x) {
623
624 switch (TYPEOF(x)) {
625 case ENVSXP:
626 return Rf_ScalarLogical(rnng_unresolved2_impl(x));
627 case VECSXP: {
628 int xc = 0;
629 const R_xlen_t xlen = Rf_xlength(x);
630 for (R_xlen_t i = 0; i < xlen; i++) {
631 xc += rnng_unresolved2_impl(VECTOR_PTR_RO(x)[i]);
632 }
633 return Rf_ScalarInteger(xc);
634 }
635 }
636
637 return Rf_ScalarLogical(0);
638
639 }
640
641 static inline R_xlen_t race_find_resolved(SEXP x, R_xlen_t xlen) {
642 for (R_xlen_t i = 0; i < xlen; i++) {
643 SEXP elem = VECTOR_PTR_RO(x)[i];
644 if (TYPEOF(elem) == ENVSXP && !rnng_unresolved2_impl(elem))
645 return i + 1;
646 }
647 return 0;
648 }
649
650 static inline int race_count_unresolved(SEXP x, R_xlen_t xlen) {
651 int n = 0;
652 for (R_xlen_t i = 0; i < xlen; i++)
653 n += rnng_unresolved2_impl(VECTOR_PTR_RO(x)[i]);
654 return n;
655 }
656
657 SEXP rnng_race_aio(SEXP x, SEXP cvar) {
658
659 if (TYPEOF(x) != VECSXP)
660 return Rf_ScalarInteger(0);
661
662 const R_xlen_t xlen = Rf_xlength(x);
663 if (xlen == 0)
664 return Rf_ScalarInteger(0);
665
666 R_xlen_t idx = race_find_resolved(x, xlen);
667 if (idx)
668 return Rf_ScalarInteger((int) idx);
669
670 if (NANO_PTR_CHECK(cvar, nano_CvSymbol))
671 return Rf_ScalarInteger(0);
672
673 nano_cv *ncv = (nano_cv *) NANO_PTR(cvar);
674 nng_cv *cv = ncv->cv;
675 nng_mtx *mtx = ncv->mtx;
676
677 nng_mtx_lock(mtx);
678 ncv->flag = ncv->flag < 0;
679 ncv->condition = 0;
680 int n = race_count_unresolved(x, xlen);
681 nng_mtx_unlock(mtx);
682 if (n == 0)
683 return Rf_ScalarInteger((int) race_find_resolved(x, xlen));
684
685 nng_time time = nng_clock();
686 int current_n, flag;
687
688 do {
689 time = time + 400;
690 int signalled = 1;
691 nng_mtx_lock(mtx);
692 while (ncv->condition == 0) {
693 if (nng_cv_until(cv, time) == NNG_ETIMEDOUT) {
694 signalled = 0;
695 break;
696 }
697 }
698 if (signalled)
699 ncv->condition--;
700 flag = ncv->flag;
701 nng_mtx_unlock(mtx);
702
703 if (flag < 0)
704 return Rf_ScalarInteger(0);
705
706 if (!signalled)
707 R_CheckUserInterrupt();
708
709 current_n = race_count_unresolved(x, xlen);
710
711 } while (current_n == n);
712
713 return Rf_ScalarInteger((int) race_find_resolved(x, xlen));
714
715 }
716
717 // send recv aio functions -----------------------------------------------------
718
719 SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP pipe, SEXP clo) {
720
721 const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
722 const int raw = nano_encode_mode(mode);
723 SEXP aio, env, fun;
724 nano_aio *saio = NULL;
725 nano_buf buf;
726 int sock, xc;
727
728 if ((sock = !NANO_PTR_CHECK(con, nano_SocketSymbol)) || !NANO_PTR_CHECK(con, nano_ContextSymbol)) {
729
730 const int pipeid = sock ? nano_integer(pipe) : 0;
731 if (raw) {
732 nano_encode(&buf, data);
733 } else {
734 nano_serialize(&buf, data, NANO_PROT(con), 0);
735 }
736 nng_msg *msg = NULL;
737
738 saio = calloc(1, sizeof(nano_aio));
739 NANO_ENSURE_ALLOC(saio);
740 saio->type = SENDAIO;
741
742 if ((xc = nng_msg_alloc(&msg, 0)) ||
743 (xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
744 nng_msg_free(msg);
745 goto fail;
746 }
747
748 nano_msg_set_body(msg, &buf);
749
750 if (pipeid) {
751 nng_pipe p;
752 p.id = (uint32_t) pipeid;
753 nng_msg_set_pipe(msg, p);
754 }
755
756 nng_aio_set_msg(saio->aio, msg);
757 nng_aio_set_timeout(saio->aio, dur);
758 sock ? nng_send_aio(*(nng_socket *) NANO_PTR(con), saio->aio) :
759 nng_ctx_send(*(nng_ctx *) NANO_PTR(con), saio->aio);
760 NANO_FREE(buf);
761
762 PROTECT(aio = R_MakeExternalPtr(saio, nano_AioSymbol, R_NilValue));
763 R_RegisterCFinalizerEx(aio, saio_finalizer, TRUE);
764
765 } else if (!NANO_PTR_CHECK(con, nano_StreamSymbol)) {
766
767 nano_encode(&buf, data);
768
769 nano_stream *nst = (nano_stream *) NANO_PTR(con);
770 nng_stream *sp = nst->stream;
771
772 saio = calloc(1, sizeof(nano_aio));
773 NANO_ENSURE_ALLOC(saio);
774
775 if (nst->msgmode) {
776 nng_msg *msg;
777 const size_t xlen = buf.cur - nst->textframes;
778 saio->type = SENDAIO;
779 if ((xc = nng_msg_alloc(&msg, xlen)))
780 goto fail;
781 memcpy(nng_msg_body(msg), buf.buf, xlen);
782
783 if ((xc = nng_aio_alloc(&saio->aio, saio_complete, saio))) {
784 nng_msg_free(msg);
785 goto fail;
786 }
787 nng_aio_set_msg(saio->aio, msg);
788 } else {
789 saio->type = IOV_SENDAIO;
790 saio->data = malloc(buf.cur);
791 NANO_ENSURE_ALLOC(saio->data);
792 memcpy(saio->data, buf.buf, buf.cur);
793 nng_iov iov = {
794 .iov_buf = saio->data,
795 .iov_len = buf.cur - nst->textframes
796 };
797
798 if ((xc = nng_aio_alloc(&saio->aio, isaio_complete, saio)) ||
799 (xc = nng_aio_set_iov(saio->aio, 1u, &iov)))
800 goto fail;
801 }
802
803 nng_aio_set_timeout(saio->aio, dur);
804 nng_stream_send(sp, saio->aio);
805 NANO_FREE(buf);
806
807 PROTECT(aio = R_MakeExternalPtr(saio, nano_AioSymbol, R_NilValue));
808 R_RegisterCFinalizerEx(aio, saio_finalizer, TRUE);
809
810 } else {
811 Rf_error("`con` is not a valid Socket, Context, or Stream");
812 }
813
814 PROTECT(env = R_NewEnv(R_NilValue, 0, 0));
815 Rf_classgets(env, nano_sendAio);
816 Rf_defineVar(nano_AioSymbol, aio, env);
817
818 PROTECT(fun = R_mkClosure(R_NilValue, nano_aioFuncRes, clo));
819 R_MakeActiveBinding(nano_ResultSymbol, fun, env);
820
821 UNPROTECT(3);
822 return env;
823
824 fail:
825 nng_aio_free(saio->aio);
826 free(saio->data);
827 failmem:
828 NANO_FREE(buf);
829 free(saio);
830 return mk_error_data(-xc);
831
832 }
833
834 SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP clo) {
835
836 const nng_duration dur = timeout == R_NilValue ? NNG_DURATION_DEFAULT : (nng_duration) nano_integer(timeout);
837 const int signal = cvar != R_NilValue && !NANO_PTR_CHECK(cvar, nano_CvSymbol);
838 const int interrupt = TYPEOF(cvar) == SYMSXP;
839 nano_cv *ncv = signal ? (nano_cv *) NANO_PTR(cvar) : NULL;
840 nano_aio *raio = NULL;
841 SEXP aio, env, fun;
842 int sock, xc;
843
844 if ((sock = !NANO_PTR_CHECK(con, nano_SocketSymbol)) || !NANO_PTR_CHECK(con, nano_ContextSymbol)) {
845
846 const uint8_t mod = (uint8_t) nano_matcharg(mode);
847 raio = calloc(1, sizeof(nano_aio));
848 NANO_ENSURE_ALLOC(raio);
849 raio->next = ncv;
850 raio->type = signal ? RECVAIOS : RECVAIO;
851 raio->mode = mod;
852
853 if ((xc = nng_aio_alloc(&raio->aio, interrupt ? raio_complete_interrupt : raio_complete, raio)))
854 goto fail;
855
856 nng_aio_set_timeout(raio->aio, dur);
857 sock ? nng_recv_aio(*(nng_socket *) NANO_PTR(con), raio->aio) :
858 nng_ctx_recv(*(nng_ctx *) NANO_PTR(con), raio->aio);
859
860 PROTECT(aio = R_MakeExternalPtr(raio, nano_AioSymbol, NANO_PROT(con)));
861 R_RegisterCFinalizerEx(aio, raio_finalizer, TRUE);
862
863 } else if (!NANO_PTR_CHECK(con, nano_StreamSymbol)) {
864
865 const uint8_t mod = (uint8_t) (nano_matcharg(mode) == 1 ? 2 : nano_matcharg(mode));
866 nano_stream *nst = (nano_stream *) NANO_PTR(con);
867 nng_stream *sp = nst->stream;
868
869 raio = calloc(1, sizeof(nano_aio));
870 NANO_ENSURE_ALLOC(raio);
871 raio->next = ncv;
872 raio->mode = mod;
873
874 if (nst->msgmode) {
875 raio->type = signal ? RECVAIOS : RECVAIO;
876 if ((xc = nng_aio_alloc(&raio->aio, raio_complete, raio)))
877 goto fail;
878 } else {
879 size_t xlen = nst->bufsize;
880 raio->type = signal ? IOV_RECVAIOS : IOV_RECVAIO;
881 raio->data = malloc(xlen);
882 NANO_ENSURE_ALLOC(raio->data);
883 nng_iov iov = {
884 .iov_buf = raio->data,
885 .iov_len = xlen
886 };
887 if ((xc = nng_aio_alloc(&raio->aio, iraio_complete, raio)) ||
888 (xc = nng_aio_set_iov(raio->aio, 1u, &iov)))
889 goto fail;
890 }
891
892 nng_aio_set_timeout(raio->aio, dur);
893 nng_stream_recv(sp, raio->aio);
894
895 PROTECT(aio = R_MakeExternalPtr(raio, nano_AioSymbol, R_NilValue));
896 R_RegisterCFinalizerEx(aio, nst->msgmode ? raio_finalizer : iaio_finalizer, TRUE);
897
898 } else {
899 Rf_error("`con` is not a valid Socket, Context or Stream");
900 }
901
902 PROTECT(env = R_NewEnv(R_NilValue, 0, 0));
903 Rf_classgets(env, nano_recvAio);
904 Rf_defineVar(nano_AioSymbol, aio, env);
905
906 PROTECT(fun = R_mkClosure(R_NilValue, nano_aioFuncMsg, clo));
907 R_MakeActiveBinding(nano_DataSymbol, fun, env);
908
909 UNPROTECT(3);
910 return env;
911
912 fail:
913 nng_aio_free(raio->aio);
914 free(raio->data);
915 failmem:
916 free(raio);
917 return mk_error_data(xc);
918
919 }