Debian Codegraph @

R/listdial.R

raw
1 # nanonext - Listeners / Dialers -----------------------------------------------
2
3 #' Dial an Address from a Socket
4 #'
5 #' Creates a new Dialer and binds it to a Socket.
6 #'
7 #' To view all Dialers bound to a socket use `$dialer` on the socket, which
8 #' returns a list of Dialer objects. To access any individual Dialer (e.g. to
9 #' set options on it), index into the list e.g. `$dialer[[1]]` to return the
10 #' first Dialer.
11 #'
12 #' A Dialer is an external pointer to a dialer object, which creates a single
13 #' outgoing connection at a time. If the connection is broken, or fails, the
14 #' dialer object will automatically attempt to reconnect, and will keep doing so
15 #' until the dialer or socket is destroyed.
16 #'
17 #' @param socket a Socket.
18 #' @param url \[default 'inproc://nanonext'\] a URL to dial, specifying the
19 #' transport and address as a character string e.g. 'inproc://anyvalue' or
20 #' 'tcp://127.0.0.1:5555' (see [transports]).
21 #' @param tls \[default NULL\] for secure tls+tcp:// or wss:// connections only,
22 #' provide a TLS configuration object created by [tls_config()].
23 #' @param autostart \[default TRUE\] whether to start the dialer (by default
24 #' asynchronously). Set to NA to start synchronously - this is less resilient
25 #' if a connection is not immediately possible, but avoids subtle errors from
26 #' attempting to use the socket before an asynchronous dial has completed. Set
27 #' to FALSE if setting configuration options on the dialer as it is not
28 #' generally possible to change these once started.
29 #' @param fail \[default 'warn'\] failure mode - a character value or integer
30 #' equivalent, whether to warn (1L), error (2L), or for none (3L) just return
31 #' an 'errorValue' without any corresponding warning.
32 #'
33 #' @return Invisibly, an integer exit code (zero on success). A new Dialer
34 #' (object of class 'nanoDialer' and 'nano') is created and bound to the
35 #' Socket if successful.
36 #'
37 #' @section Further details:
38 #'
39 #' Dialers and Listeners are always associated with a single socket. A given
40 #' socket may have multiple Listeners and/or multiple Dialers.
41 #'
42 #' The client/server relationship described by dialer/listener is completely
43 #' orthogonal to any similar relationship in the protocols. For example, a rep
44 #' socket may use a dialer to connect to a listener on an req socket. This
45 #' orthogonality can lead to innovative solutions to otherwise challenging
46 #' communications problems.
47 #'
48 #' Any configuration options on the dialer/listener should be set by [opt<-()]
49 #' before starting the dialer/listener with [start()].
50 #'
51 #' Dialers/Listeners may be destroyed by [close()]. They are also closed when
52 #' their associated socket is closed.
53 #'
54 #' @examples
55 #' socket <- socket("rep")
56 #' dial(socket, url = "inproc://nanodial", autostart = FALSE)
57 #' socket$dialer
58 #' start(socket$dialer[[1]])
59 #' socket$dialer
60 #' close(socket$dialer[[1]])
61 #' close(socket)
62 #'
63 #' nano <- nano("bus")
64 #' nano$dial(url = "inproc://nanodial", autostart = FALSE)
65 #' nano$dialer
66 #' nano$dialer_start()
67 #' nano$dialer
68 #' close(nano$dialer[[1]])
69 #' nano$close()
70 #'
71 #' @export
72 #'
73 dial <- function(socket, url = "inproc://nanonext", tls = NULL, autostart = TRUE, fail = c("warn", "error", "none"))
74 invisible(.Call(rnng_dial, socket, url, tls, autostart, fail))
75
76 #' Listen to an Address from a Socket
77 #'
78 #' Creates a new Listener and binds it to a Socket.
79 #'
80 #' To view all Listeners bound to a socket use `$listener` on the socket,
81 #' which returns a list of Listener objects. To access any individual Listener
82 #' (e.g. to set options on it), index into the list e.g. `$listener[[1]]`
83 #' to return the first Listener.
84 #'
85 #' A listener is an external pointer to a listener object, which accepts
86 #' incoming connections. A given listener object may have many connections at
87 #' the same time, much like an HTTP server can have many connections to multiple
88 #' clients simultaneously.
89 #'
90 #' @inheritParams dial
91 #' @param autostart \[default TRUE\] whether to start the listener. Set to FALSE
92 #' if setting configuration options on the listener as it is not generally
93 #' possible to change these once started.
94 #'
95 #' @return Invisibly, an integer exit code (zero on success). A new Listener
96 #' (object of class 'nanoListener' and 'nano') is created and bound to the
97 #' Socket if successful.
98 #'
99 #' @inheritSection dial Further details
100 #'
101 #' @examples
102 #' socket <- socket("req")
103 #' listen(socket, url = "inproc://nanolisten", autostart = FALSE)
104 #' socket$listener
105 #' start(socket$listener[[1]])
106 #' socket$listener
107 #' close(socket$listener[[1]])
108 #' close(socket)
109 #'
110 #' nano <- nano("bus")
111 #' nano$listen(url = "inproc://nanolisten", autostart = FALSE)
112 #' nano$listener
113 #' nano$listener_start()
114 #' nano$listener
115 #' close(nano$listener[[1]])
116 #' nano$close()
117 #'
118 #' @export
119 #'
120 listen <- function(socket, url = "inproc://nanonext", tls = NULL, autostart = TRUE, fail = c("warn", "error", "none"))
121 invisible(.Call(rnng_listen, socket, url, tls, autostart, fail))
122
123 #' Start Listener/Dialer
124 #'
125 #' Start a Listener/Dialer.
126 #'
127 #' @param x a Listener or Dialer.
128 #' @param async \[default TRUE\] (applicable to Dialers only) logical flag
129 #' whether the connection attempt, including any name resolution, is to be
130 #' made asynchronously. This behaviour is more resilient, but also generally
131 #' makes diagnosing failures somewhat more difficult. If FALSE, failure, such
132 #' as if the connection is refused, will be returned immediately, and no
133 #' further action will be taken.
134 #' @param ... not used.
135 #'
136 #' @return Invisibly, an integer exit code (zero on success).
137 #'
138 #' @name start
139 #' @rdname start
140 #'
141 NULL
142
143 #' @rdname start
144 #' @exportS3Method stats::start
145 #'
146 start.nanoListener <- function(x, ...)
147 invisible(.Call(rnng_listener_start, x))
148
149 #' @rdname start
150 #' @exportS3Method stats::start
151 #'
152 start.nanoDialer <- function(x, async = TRUE, ...)
153 invisible(.Call(rnng_dialer_start, x, async))
154
155 #' @rdname close
156 #' @method close nanoDialer
157 #' @export
158 #'
159 close.nanoDialer <- function(con, ...)
160 invisible(.Call(rnng_dialer_close, con))
161
162 #' @rdname close
163 #' @method close nanoListener
164 #' @export
165 #'
166 close.nanoListener <- function(con, ...)
167 invisible(.Call(rnng_listener_close, con))