Debian Codegraph @

src/proto.c

raw symbols SCIP
1 // nanonext - C level - Socket and Stream Constructors -------------------------
2
3 #define NANONEXT_PROTOCOLS
4 #include "nanonext.h"
5
6 // finalizers ------------------------------------------------------------------
7
8 static void stream_finalizer(SEXP xptr) {
9
10 if (NANO_PTR(xptr) == NULL) return;
11 nano_stream *xp = (nano_stream *) NANO_PTR(xptr);
12 nng_stream_close(xp->stream);
13 nng_stream_free(xp->stream);
14 if (xp->mode == NANO_STREAM_LISTENER) {
15 nng_stream_listener_close(xp->endpoint.list);
16 nng_stream_listener_free(xp->endpoint.list);
17 } else {
18 nng_stream_dialer_close(xp->endpoint.dial);
19 nng_stream_dialer_free(xp->endpoint.dial);
20 }
21 if (xp->tls != NULL)
22 nng_tls_config_free(xp->tls);
23 free(xp);
24
25 }
26
27 // sockets ---------------------------------------------------------------------
28
29 SEXP rnng_protocol_open(SEXP protocol, SEXP dial, SEXP listen, SEXP tls, SEXP autostart, SEXP raw) {
30
31 const char *pro = CHAR(STRING_ELT(protocol, 0));
32 const int rw = NANO_INTEGER(raw);
33 size_t slen = strlen(pro);
34
35 const char *pname;
36 int xc;
37 SEXP socket;
38
39 nng_socket *sock = malloc(sizeof(nng_socket));
40 NANO_ENSURE_ALLOC(sock);
41
42 switch (slen) {
43 case 1:
44 case 2:
45 case 3:
46 if (!memcmp(pro, "bus", slen)) {
47 pname = "bus";
48 xc = rw ? nng_bus0_open_raw(sock) : nng_bus0_open(sock);
49 break;
50 }
51 if (slen > 2) {
52 if (!memcmp(pro, "pub", slen)) {
53 pname = "pub";
54 xc = rw ? nng_pub0_open_raw(sock) : nng_pub0_open(sock);
55 break;
56 }
57 if (!memcmp(pro, "sub", slen)) {
58 pname = "sub";
59 xc = rw ? nng_sub0_open_raw(sock) : nng_sub0_open(sock);
60 break;
61 }
62 if (!memcmp(pro, "req", slen)) {
63 pname = "req";
64 xc = rw ? nng_req0_open_raw(sock) : nng_req0_open(sock);
65 break;
66 }
67 if (!memcmp(pro, "rep", slen)) {
68 pname = "rep";
69 xc = rw ? nng_rep0_open_raw(sock) : nng_rep0_open(sock);
70 break;
71 }
72 }
73 case 4:
74 if (slen > 1) {
75 if (!memcmp(pro, "pair", slen)) {
76 pname = "pair";
77 xc = rw ? nng_pair0_open_raw(sock) : nng_pair0_open(sock);
78 break;
79 }
80 if (!memcmp(pro, "poly", slen)) {
81 pname = "poly";
82 xc = rw ? nng_pair1_open_raw(sock) : nng_pair1_open_poly(sock);
83 break;
84 }
85 if (slen > 2) {
86 if (!memcmp(pro, "push", slen)) {
87 pname = "push";
88 xc = rw ? nng_push0_open_raw(sock) : nng_push0_open(sock);
89 break;
90 }
91 if (!memcmp(pro, "pull", slen)) {
92 pname = "pull";
93 xc = rw ? nng_pull0_open_raw(sock) : nng_pull0_open(sock);
94 break;
95 }
96 }
97 }
98 case 5:
99 case 6:
100 case 7:
101 case 8:
102 if (slen > 2 && !memcmp(pro, "surveyor", slen)) {
103 pname = "surveyor";
104 xc = rw ? nng_surveyor0_open_raw(sock) : nng_surveyor0_open(sock);
105 break;
106 }
107 case 9:
108 case 10:
109 if (slen > 2 && !memcmp(pro, "respondent", slen)) {
110 pname = "respondent";
111 xc = rw ? nng_respondent0_open_raw(sock) : nng_respondent0_open(sock);
112 break;
113 }
114 default:
115 free(sock);
116 Rf_error("`protocol` should be one of: bus, pair, poly, push, pull, pub, sub, req, rep, surveyor, respondent");
117 }
118
119 if (xc)
120 goto failmem;
121
122 PROTECT(socket = R_MakeExternalPtr(sock, nano_SocketSymbol, R_NilValue));
123 R_RegisterCFinalizerEx(socket, socket_finalizer, TRUE);
124
125 NANO_CLASS2(socket, "nanoSocket", "nano");
126 Rf_setAttrib(socket, nano_IdSymbol, Rf_ScalarInteger(nng_socket_id(*sock)));
127 Rf_setAttrib(socket, nano_ProtocolSymbol, Rf_mkString(pname));
128 Rf_setAttrib(socket, nano_StateSymbol, Rf_mkString("opened"));
129
130 if (dial != R_NilValue) {
131 SEXP intd;
132 PROTECT(intd = Rf_ScalarInteger(2));
133 rnng_dial(socket, dial, tls, autostart, intd);
134 UNPROTECT(1);
135 }
136
137 if (listen != R_NilValue) {
138 SEXP intl;
139 PROTECT(intl = Rf_ScalarInteger(2));
140 rnng_listen(socket, listen, tls, autostart, intl);
141 UNPROTECT(1);
142 }
143
144 UNPROTECT(1);
145 return socket;
146
147 failmem:
148 free(sock);
149 ERROR_OUT(xc);
150
151 }
152
153 SEXP rnng_close(SEXP socket) {
154
155 if (NANO_PTR_CHECK(socket, nano_SocketSymbol))
156 Rf_error("`socket` is not a valid Socket");
157
158 nng_socket *sock = (nng_socket *) NANO_PTR(socket);
159 const int xc = nng_close(*sock);
160 if (xc)
161 ERROR_RET(xc);
162
163 Rf_setAttrib(socket, nano_StateSymbol, Rf_mkString("closed"));
164 return nano_success;
165
166 }
167
168 SEXP rnng_reap(SEXP con) {
169
170 int xc;
171
172 if (!NANO_PTR_CHECK(con, nano_ContextSymbol)) {
173 xc = nng_ctx_close(*(nng_ctx *) NANO_PTR(con));
174
175 } else if (!NANO_PTR_CHECK(con, nano_SocketSymbol)) {
176 xc = nng_close(*(nng_socket *) NANO_PTR(con));
177
178 } else if (!NANO_PTR_CHECK(con, nano_ListenerSymbol)) {
179 xc = nng_listener_close(*(nng_listener *) NANO_PTR(con));
180
181 } else if (!NANO_PTR_CHECK(con, nano_DialerSymbol)) {
182 xc = nng_dialer_close(*(nng_dialer *) NANO_PTR(con));
183
184 } else {
185 xc = 3;
186 }
187
188 if (xc)
189 return mk_error(xc);
190
191 return nano_success;
192
193 }
194
195 // streams ---------------------------------------------------------------------
196
197 static SEXP nano_stream_dial(SEXP url, SEXP textframes, SEXP headers, SEXP tls, SEXP buffer) {
198
199 const char *add = CHAR(STRING_ELT(url, 0));
200 const size_t bufsize = (size_t) nano_integer(buffer);
201 if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol))
202 Rf_error("`tls` is not a valid TLS Configuration");
203
204 nng_url *up = NULL;
205 nng_aio *aiop = NULL;
206 int xc;
207 SEXP sd;
208
209 nano_stream *nst = calloc(1, sizeof(nano_stream));
210 NANO_ENSURE_ALLOC(nst);
211 nst->mode = NANO_STREAM_DIALER;
212 nst->bufsize = bufsize;
213 nst->textframes = NANO_INTEGER(textframes) != 0;
214
215 if ((xc = nng_url_parse(&up, add)) ||
216 (xc = nng_stream_dialer_alloc_url(&nst->endpoint.dial, up)) ||
217 (xc = nng_aio_alloc(&aiop, NULL, NULL)))
218 goto fail;
219
220 if (!strcmp(up->u_scheme, "ws") || !strcmp(up->u_scheme, "wss")) {
221 nst->msgmode = 1;
222 if ((xc = nng_stream_dialer_set_bool(nst->endpoint.dial, "ws:msgmode", 1)))
223 goto fail;
224 if (nst->textframes &&
225 ((xc = nng_stream_dialer_set_bool(nst->endpoint.dial, "ws:recv-text", 1)) ||
226 (xc = nng_stream_dialer_set_bool(nst->endpoint.dial, "ws:send-text", 1))))
227 goto fail;
228 if (headers != R_NilValue && TYPEOF(headers) == STRSXP) {
229 const R_xlen_t hlen = XLENGTH(headers);
230 SEXP hnames = Rf_getAttrib(headers, R_NamesSymbol);
231 if (TYPEOF(hnames) == STRSXP && XLENGTH(hnames) == hlen) {
232 const SEXP *hnames_p = STRING_PTR_RO(hnames);
233 const SEXP *headers_p = STRING_PTR_RO(headers);
234 for (R_xlen_t i = 0; i < hlen; i++) {
235 const char *name = CHAR(hnames_p[i]);
236 char optname[256];
237 snprintf(optname, sizeof(optname), "%s%s", NNG_OPT_WS_REQUEST_HEADER, name);
238 if ((xc = nng_stream_dialer_set_string(nst->endpoint.dial, optname, CHAR(headers_p[i]))))
239 goto fail;
240 }
241 }
242 }
243 }
244
245 if (!strcmp(up->u_scheme, "wss")) {
246
247 if (tls == R_NilValue) {
248 if ((xc = nng_tls_config_alloc(&nst->tls, NNG_TLS_MODE_CLIENT)) ||
249 (xc = nng_tls_config_server_name(nst->tls, up->u_hostname)) ||
250 (xc = nng_tls_config_auth_mode(nst->tls, NNG_TLS_AUTH_MODE_NONE)) ||
251 (xc = nng_stream_dialer_set_ptr(nst->endpoint.dial, NNG_OPT_TLS_CONFIG, nst->tls)))
252 goto fail;
253 } else {
254
255 nst->tls = (nng_tls_config *) NANO_PTR(tls);
256 nng_tls_config_hold(nst->tls);
257
258 if ((xc = nng_tls_config_server_name(nst->tls, up->u_hostname)) ||
259 (xc = nng_stream_dialer_set_ptr(nst->endpoint.dial, NNG_OPT_TLS_CONFIG, nst->tls)))
260 goto fail;
261 }
262
263 }
264
265 nng_stream_dialer_dial(nst->endpoint.dial, aiop);
266 nng_aio_wait(aiop);
267 if ((xc = nng_aio_result(aiop)))
268 goto fail;
269
270 nst->stream = nng_aio_get_output(aiop, 0);
271
272 nng_aio_free(aiop);
273 nng_url_free(up);
274
275 PROTECT(sd = R_MakeExternalPtr(nst, nano_StreamSymbol, R_NilValue));
276 R_RegisterCFinalizerEx(sd, stream_finalizer, TRUE);
277
278 NANO_CLASS2(sd, "nanoStream", "nano");
279 Rf_setAttrib(sd, R_ModeSymbol, Rf_mkString(nst->textframes ? "dialer text frames" : "dialer"));
280 Rf_setAttrib(sd, nano_StateSymbol, Rf_mkString("opened"));
281 Rf_setAttrib(sd, nano_UrlSymbol, url);
282
283 UNPROTECT(1);
284 return sd;
285
286 fail:
287 if (nst->tls != NULL)
288 nng_tls_config_free(nst->tls);
289 nng_aio_free(aiop);
290 nng_stream_dialer_free(nst->endpoint.dial);
291 nng_url_free(up);
292 free(nst);
293 failmem:
294 ERROR_OUT(xc);
295
296 }
297
298 static SEXP nano_stream_listen(SEXP url, SEXP textframes, SEXP tls, SEXP buffer) {
299
300 const char *add = CHAR(STRING_ELT(url, 0));
301 const size_t bufsize = (size_t) nano_integer(buffer);
302 if (tls != R_NilValue && NANO_PTR_CHECK(tls, nano_TlsSymbol))
303 Rf_error("`tls` is not a valid TLS Configuration");
304
305 nng_url *up = NULL;
306 nng_aio *aiop = NULL;
307 int xc;
308 SEXP sl;
309
310 nano_stream *nst = calloc(1, sizeof(nano_stream));
311 NANO_ENSURE_ALLOC(nst);
312 nst->mode = NANO_STREAM_LISTENER;
313 nst->bufsize = bufsize;
314 nst->textframes = NANO_INTEGER(textframes) != 0;
315
316 if ((xc = nng_url_parse(&up, add)) ||
317 (xc = nng_stream_listener_alloc_url(&nst->endpoint.list, up)) ||
318 (xc = nng_aio_alloc(&aiop, NULL, NULL)))
319 goto fail;
320
321 if (!strcmp(up->u_scheme, "ws") || !strcmp(up->u_scheme, "wss")) {
322 nst->msgmode = 1;
323 if ((xc = nng_stream_listener_set_bool(nst->endpoint.list, "ws:msgmode", 1)))
324 goto fail;
325 if (nst->textframes &&
326 ((xc = nng_stream_listener_set_bool(nst->endpoint.list, "ws:recv-text", 1)) ||
327 (xc = nng_stream_listener_set_bool(nst->endpoint.list, "ws:send-text", 1))))
328 goto fail;
329 }
330
331 if (!strcmp(up->u_scheme, "wss")) {
332
333 if (tls == R_NilValue) {
334 if ((xc = nng_tls_config_alloc(&nst->tls, NNG_TLS_MODE_SERVER)) ||
335 (xc = nng_tls_config_auth_mode(nst->tls, NNG_TLS_AUTH_MODE_NONE)) ||
336 (xc = nng_stream_listener_set_ptr(nst->endpoint.list, NNG_OPT_TLS_CONFIG, nst->tls)))
337 goto fail;
338 } else {
339
340 nst->tls = (nng_tls_config *) NANO_PTR(tls);
341 nng_tls_config_hold(nst->tls);
342
343 if ((xc = nng_tls_config_server_name(nst->tls, up->u_hostname)) ||
344 (xc = nng_stream_listener_set_ptr(nst->endpoint.list, NNG_OPT_TLS_CONFIG, nst->tls)))
345 goto fail;
346 }
347
348 }
349
350 if ((xc = nng_stream_listener_listen(nst->endpoint.list)))
351 goto fail;
352
353 nng_stream_listener_accept(nst->endpoint.list, aiop);
354 nng_aio_wait(aiop);
355 if ((xc = nng_aio_result(aiop)))
356 goto fail;
357
358 nst->stream = nng_aio_get_output(aiop, 0);
359
360 nng_aio_free(aiop);
361 nng_url_free(up);
362
363 PROTECT(sl = R_MakeExternalPtr(nst, nano_StreamSymbol, R_NilValue));
364 R_RegisterCFinalizerEx(sl, stream_finalizer, TRUE);
365
366 NANO_CLASS2(sl, "nanoStream", "nano");
367 Rf_setAttrib(sl, R_ModeSymbol, Rf_mkString(nst->textframes ? "listener text frames" : "listener"));
368 Rf_setAttrib(sl, nano_StateSymbol, Rf_mkString("opened"));
369 Rf_setAttrib(sl, nano_UrlSymbol, url);
370
371 UNPROTECT(1);
372 return sl;
373
374 fail:
375 if (nst->tls != NULL)
376 nng_tls_config_free(nst->tls);
377 nng_aio_free(aiop);
378 nng_stream_listener_free(nst->endpoint.list);
379 nng_url_free(up);
380 free(nst);
381 failmem:
382 ERROR_OUT(xc);
383
384 }
385
386 SEXP rnng_stream_open(SEXP dial, SEXP listen, SEXP textframes, SEXP headers, SEXP tls, SEXP buffer) {
387
388 if (dial != R_NilValue) {
389 return nano_stream_dial(dial, textframes, headers, tls, buffer);
390 } else if (listen != R_NilValue) {
391 return nano_stream_listen(listen, textframes, tls, buffer);
392 }
393 Rf_error("specify a URL for either `dial` or `listen`");
394
395 }
396
397 SEXP rnng_stream_close(SEXP stream) {
398
399 if (NANO_PTR_CHECK(stream, nano_StreamSymbol))
400 return mk_error(NNG_ECLOSED);
401
402 stream_finalizer(stream);
403 R_ClearExternalPtr(stream);
404 Rf_setAttrib(stream, nano_StateSymbol, Rf_mkString("closed"));
405
406 return nano_success;
407
408 }