Debian Codegraph @

R/stream.R

raw
1 # nanonext - Byte Stream Interface ---------------------------------------------
2
3 #' Open Stream
4 #'
5 #' Open a Stream by either dialing (establishing an outgoing connection) or
6 #' listening (accepting an incoming connection) at an address. This is a
7 #' low-level interface intended for communicating with non-NNG endpoints.
8 #'
9 #' A Stream is used for raw byte stream connections. Byte streams are reliable
10 #' in that data will not be delivered out of order, or with portions missing.
11 #'
12 #' Can be used to dial a (secure) websocket address starting 'ws://' or
13 #' 'wss://'. It is often the case that `textframes` needs to be set to `TRUE`.
14 #'
15 #' Specify only one of `dial` or `listen`. If both are specified, `listen` will
16 #' be ignored.
17 #'
18 #' Closing a stream renders it invalid and attempting to perform additional
19 #' operations on it will error.
20 #'
21 #' @param dial a URL to dial, specifying the transport and address as a
22 #' character string e.g. 'ipc:///tmp/anyvalue' or 'tcp://127.0.0.1:5555' (not
23 #' all transports are supported).
24 #' @param listen a URL to listen at, specifying the transport and address as a
25 #' character string e.g. 'ipc:///tmp/anyvalue' or 'tcp://127.0.0.1:5555' (not
26 #' all transports are supported).
27 #' @param textframes \[default FALSE\] applicable to the websocket transport
28 #' only, enables sending and receiving of TEXT frames (ignored otherwise).
29 #' @param headers (optional) applicable to websocket connections only, a
30 #' **named** character vector specifying custom request headers to send during
31 #' the WebSocket upgrade handshake e.g.
32 #' `c(Authorization = "Bearer token", Custom = "value")` (ignored for
33 #' non-websocket transports).
34 #' @param tls (optional) applicable to secure websockets only, a client or
35 #' server TLS configuration object created by [tls_config()]. If missing or
36 #' NULL, certificates are not validated.
37 #' @param buffer \[default 65536L\] applicable to non-websocket streams only, the
38 #' maximum number of bytes to receive. Can be an over-estimate, but note that
39 #' a buffer of this size is reserved. Not used for websocket connections,
40 #' which handle framing automatically.
41 #'
42 #' @return A Stream (object of class 'nanoStream' and 'nano').
43 #'
44 #' @examples
45 #' # Will succeed only if there is an open connection at the address:
46 #' s <- tryCatch(stream(dial = "tcp://127.0.0.1:5555"), error = identity)
47 #' s
48 #' @examplesIf interactive()
49 #' # Run in interactive sessions only as connection is not always available:
50 #' s <- tryCatch(
51 #' stream(dial = "wss://echo.websocket.events/", textframes = TRUE),
52 #' error = identity
53 #' )
54 #' s
55 #' if (is_nano(s)) recv(s)
56 #' if (is_nano(s)) send(s, "hello")
57 #' if (is_nano(s)) recv(s)
58 #' if (is_nano(s)) close(s)
59 #'
60 #' @export
61 #'
62 stream <- function(dial = NULL, listen = NULL, textframes = FALSE,
63 headers = NULL, tls = NULL, buffer = 65536L)
64 .Call(rnng_stream_open, dial, listen, textframes, headers, tls, buffer)
65
66 #' @rdname close
67 #' @method close nanoStream
68 #' @export
69 #'
70 close.nanoStream <- function(con, ...) invisible(.Call(rnng_stream_close, con))