| 1 |
# nanonext - Options Configuration and Helper Functions ------------------------ |
| 2 |
|
| 3 |
#' Get and Set Options for a Socket, Context, Stream, Listener or Dialer |
| 4 |
#' |
| 5 |
#' Get and set the value of options for a Socket, Context, Stream, Listener or |
| 6 |
#' Dialer. |
| 7 |
#' |
| 8 |
#' Note: once a dialer or listener has started, it is not generally possible to |
| 9 |
#' change its configuration. Hence create the dialer or listener with |
| 10 |
#' `autostart = FALSE` if configuration needs to be set. |
| 11 |
#' |
| 12 |
#' To get or set options on a Listener or Dialer attached to a Socket or nano |
| 13 |
#' object, pass in the objects directly via for example `$listener[[1]]` for the |
| 14 |
#' first Listener. |
| 15 |
#' |
| 16 |
#' Some options are only meaningful or supported in certain contexts; for |
| 17 |
#' example there is no single meaningful address for a socket, since sockets can |
| 18 |
#' have multiple dialers and endpoints associated with them. |
| 19 |
#' |
| 20 |
#' For an authoritative guide please refer to the online documentation for the |
| 21 |
#' NNG library at <https://nng.nanomsg.org/man/>. |
| 22 |
#' |
| 23 |
#' @param object a Socket, Context, Stream, Listener or Dialer. |
| 24 |
#' @param name name of option, e.g. 'recv-buffer', as a character string. See |
| 25 |
#' below options details. |
| 26 |
#' @param value value of option. Supply character type for 'string' options, |
| 27 |
#' integer or double for 'int', 'duration', 'size' and 'uint64', and logical |
| 28 |
#' for 'bool'. |
| 29 |
#' |
| 30 |
#' @return The value of the option (logical for type 'bool', integer for 'int', |
| 31 |
#' 'duration' and 'size', character for 'string', and double for 'uint64'). |
| 32 |
#' |
| 33 |
#' @section Serialization: |
| 34 |
#' |
| 35 |
#' Apart from the NNG options documented below, there is the following special |
| 36 |
#' option: |
| 37 |
#' |
| 38 |
#' \itemize{ |
| 39 |
#' \item 'serial' (type list) |
| 40 |
#' |
| 41 |
#' For Sockets only. This accepts a configuration created by |
| 42 |
#' [serial_config()]. Setting a new configuration replaces any already set. To |
| 43 |
#' remove entirely, supply an empty list. Note: this option is write-only and |
| 44 |
#' can be set but not retrieved. |
| 45 |
#' } |
| 46 |
#' |
| 47 |
#' @section Global Options: |
| 48 |
#' |
| 49 |
#' \itemize{ |
| 50 |
#' \item 'reconnect-time-min' (type 'ms') |
| 51 |
#' |
| 52 |
#' This is the minimum amount of time (milliseconds) to wait before attempting |
| 53 |
#' to establish a connection after a previous attempt has failed. This can be |
| 54 |
#' set on a socket, but it can also be overridden on an individual dialer. The |
| 55 |
#' option is irrelevant for listeners. |
| 56 |
#' |
| 57 |
#' \item 'reconnect-time-max' (type 'ms') |
| 58 |
#' |
| 59 |
#' This is the maximum amount of time (milliseconds) to wait before attempting |
| 60 |
#' to establish a connection after a previous attempt has failed. If this is |
| 61 |
#' non-zero, then the time between successive connection attempts will start |
| 62 |
#' at the value of 'reconnect-time-min', and grow exponentially, until it |
| 63 |
#' reaches this value. If this value is zero, then no exponential back-off |
| 64 |
#' between connection attempts is done, and each attempt will wait the time |
| 65 |
#' specified by 'reconnect-time-min'. This can be set on a socket, but it can |
| 66 |
#' also be overridden on an individual dialer. The option is irrelevant for |
| 67 |
#' listeners. |
| 68 |
#' |
| 69 |
#' \item 'recv-size-max' (type 'size') |
| 70 |
#' |
| 71 |
#' This is the maximum message size that the will be accepted from a remote |
| 72 |
#' peer. If a peer attempts to send a message larger than this, then the |
| 73 |
#' message will be discarded. If the value of this is zero, then no limit on |
| 74 |
#' message sizes is enforced. This option exists to prevent certain kinds of |
| 75 |
#' denial-of-service attacks, where a malicious agent can claim to want to |
| 76 |
#' send an extraordinarily large message, without sending any data. This |
| 77 |
#' option can be set for the socket, but may be overridden for on a per-dialer |
| 78 |
#' or per-listener basis. NOTE: Applications on hostile networks should set |
| 79 |
#' this to a non-zero value to prevent denial-of-service attacks. NOTE: Some |
| 80 |
#' transports may have further message size restrictions. |
| 81 |
#' |
| 82 |
#' \item 'recv-buffer' (type 'int') |
| 83 |
#' |
| 84 |
#' This is the depth of the socket’s receive buffer as a number of messages. |
| 85 |
#' Messages received by a transport may be buffered until the application has |
| 86 |
#' accepted them for delivery. This value must be an integer between 0 and |
| 87 |
#' 8192, inclusive. NOTE: Not all protocols support buffering received |
| 88 |
#' messages. For example req can only deal with a single reply at a time. |
| 89 |
#' |
| 90 |
#' \item 'recv-timeout' (type 'ms') |
| 91 |
#' |
| 92 |
#' This is the socket receive timeout in milliseconds. When no message is |
| 93 |
#' available for receiving at the socket for this period of time, receive |
| 94 |
#' operations will fail with a return value of 5L ('timed out'). |
| 95 |
#' |
| 96 |
#' \item 'send-buffer' (type 'int') |
| 97 |
#' |
| 98 |
#' This is the depth of the socket send buffer as a number of messages. |
| 99 |
#' Messages sent by an application may be buffered by the socket until a |
| 100 |
#' transport is ready to accept them for delivery. This value must be an |
| 101 |
#' integer between 0 and 8192, inclusive. NOTE: Not all protocols support |
| 102 |
#' buffering sent messages; generally multicast protocols like pub will simply |
| 103 |
#' discard messages when they cannot be delivered immediately. |
| 104 |
#' |
| 105 |
#' \item 'send-timeout' (type 'ms') |
| 106 |
#' |
| 107 |
#' This is the socket send timeout in milliseconds. When a message cannot be |
| 108 |
#' queued for delivery by the socket for this period of time (such as if send |
| 109 |
#' buffers are full), the operation will fail with a return value of 5L |
| 110 |
#' ('timed out'). |
| 111 |
#' |
| 112 |
#' \item 'recv-fd' (type 'int') |
| 113 |
#' |
| 114 |
#' This is the socket receive file descriptor. For supported protocols, this |
| 115 |
#' will become readable when a message is available for receiving on the |
| 116 |
#' socket. Attempts should not be made to read or write to the returned file |
| 117 |
#' descriptor, but it is suitable for use with poll(), select(), or WSAPoll() |
| 118 |
#' on Windows, and similar functions. |
| 119 |
#' |
| 120 |
#' \item 'send-fd' (type 'int') |
| 121 |
#' |
| 122 |
#' This is the socket send file descriptor. Attempts should not be made to |
| 123 |
#' read or write to the returned file descriptor, but it is suitable for use |
| 124 |
#' with poll(), select(), or WSAPoll() on Windows, and similar functions. |
| 125 |
#' |
| 126 |
#' \item 'socket-name' (type 'string') |
| 127 |
#' |
| 128 |
#' This is the socket name. By default this is a string corresponding to the |
| 129 |
#' value of the socket. The string must fit within 64-bytes, including the |
| 130 |
#' terminating NUL byte. The value is intended for application use, and is not |
| 131 |
#' used for anything in the library itself. |
| 132 |
#' |
| 133 |
#' \item 'url' (type 'string') |
| 134 |
#' |
| 135 |
#' This read-only option is used on a listener or dialer to obtain the URL |
| 136 |
#' with which it was configured. |
| 137 |
#' |
| 138 |
#' } |
| 139 |
#' |
| 140 |
#' @section Protocol-specific Options: |
| 141 |
#' |
| 142 |
#' \itemize{ |
| 143 |
#' \item 'req:resend-time' (type 'ms') |
| 144 |
#' |
| 145 |
#' (Request protocol) When a new request is started, a timer of this duration |
| 146 |
#' is also started. If no reply is received before this timer expires, then |
| 147 |
#' the request will be resent. (Requests are also automatically resent if the |
| 148 |
#' peer to whom the original request was sent disconnects, or if a peer |
| 149 |
#' becomes available while the requester is waiting for an available peer.) |
| 150 |
#' |
| 151 |
#' \item 'sub:subscribe' (type 'string') |
| 152 |
#' |
| 153 |
#' (Subscribe protocol) This option registers a topic that the subscriber is |
| 154 |
#' interested in. Each incoming message is checked against the list of |
| 155 |
#' subscribed topics. If the body begins with the entire set of bytes in the |
| 156 |
#' topic, then the message is accepted. If no topic matches, then the message |
| 157 |
#' is discarded. To receive all messages, set the topic to NULL. |
| 158 |
#' |
| 159 |
#' \item 'sub:unsubscribe' (type 'string') |
| 160 |
#' |
| 161 |
#' (Subscribe protocol) This option removes a topic from the subscription |
| 162 |
#' list. Note that if the topic was not previously subscribed to with |
| 163 |
#' 'sub:subscribe' then an 'entry not found' error will result. |
| 164 |
#' |
| 165 |
#' \item 'sub:prefnew' (type 'bool') |
| 166 |
#' |
| 167 |
#' (Subscribe protocol) This option specifies the behavior of the subscriber |
| 168 |
#' when the queue is full. When TRUE (the default), the subscriber will make |
| 169 |
#' room in the queue by removing the oldest message. When FALSE, the |
| 170 |
#' subscriber will reject messages if the message queue does not have room. |
| 171 |
#' |
| 172 |
#' \item 'surveyor:survey-time' (type 'ms') |
| 173 |
#' |
| 174 |
#' (Surveyor protocol) Duration of surveys. When a new survey is started, a |
| 175 |
#' timer of this duration is also started. Any responses arriving after this |
| 176 |
#' time will be discarded. Attempts to receive after the timer expires with no |
| 177 |
#' other surveys started will result in an 'incorrect state' error. Attempts |
| 178 |
#' to receive when this timer expires will result in a 'timed out' error. |
| 179 |
#' |
| 180 |
#' } |
| 181 |
#' |
| 182 |
#' @section Transport-specific Options: |
| 183 |
#' |
| 184 |
#' \itemize{ |
| 185 |
#' \item 'ipc:permissions' (type 'int') |
| 186 |
#' |
| 187 |
#' (IPC transport) This option may be applied to a listener to configure the |
| 188 |
#' permissions that are used on the UNIX domain socket created by that |
| 189 |
#' listener. This property is only supported on POSIX systems. The value is of |
| 190 |
#' type int, representing the normal permission bits on a file, such as 0600 |
| 191 |
#' (typically meaning read-write to the owner, and no permissions for anyone |
| 192 |
#' else.) The default is system-specific, most often 0644. |
| 193 |
#' |
| 194 |
#' \item 'tcp-nodelay' (type 'bool') |
| 195 |
#' |
| 196 |
#' (TCP transport) This option is used to disable (or enable) the use of |
| 197 |
#' Nagle's algorithm for TCP connections. When TRUE (the default), messages |
| 198 |
#' are sent immediately by the underlying TCP stream without waiting to gather |
| 199 |
#' more data. When FALSE, Nagle’s algorithm is enabled, and the TCP stream may |
| 200 |
#' wait briefly in an attempt to coalesce messages. Nagle’s algorithm is |
| 201 |
#' useful on low-bandwidth connections to reduce overhead, but it comes at a |
| 202 |
#' cost to latency. When used on a dialer or a listener, the value affects how |
| 203 |
#' newly created connections will be configured. |
| 204 |
#' |
| 205 |
#' \item 'tcp-keepalive' (type 'bool') |
| 206 |
#' |
| 207 |
#' (TCP transport) This option is used to enable the sending of keep-alive |
| 208 |
#' messages on the underlying TCP stream. This option is FALSE by default. |
| 209 |
#' When enabled, if no messages are seen for a period of time, then a zero |
| 210 |
#' length TCP message is sent with the ACK flag set in an attempt to tickle |
| 211 |
#' some traffic from the peer. If none is still seen (after some |
| 212 |
#' platform-specific number of retries and timeouts), then the remote peer is |
| 213 |
#' presumed dead, and the connection is closed. When used on a dialer or a |
| 214 |
#' listener, the value affects how newly created connections will be |
| 215 |
#' configured. This option has two purposes. First, it can be used to detect |
| 216 |
#' dead peers on an otherwise quiescent network. Second, it can be used to |
| 217 |
#' keep connection table entries in NAT and other middleware from expiring due |
| 218 |
#' to lack of activity. |
| 219 |
#' |
| 220 |
#' \item 'tcp-bound-port' (type 'int') |
| 221 |
#' |
| 222 |
#' (TCP transport) Local TCP port number. This is used on a listener, and is |
| 223 |
#' intended to be used after starting the listener in combination with a |
| 224 |
#' wildcard (0) local port. This determines the actual ephemeral port that was |
| 225 |
#' selected and bound. The value is provided as an integer, but only the low |
| 226 |
#' order 16 bits will be set, and is in native byte order for convenience. |
| 227 |
#' |
| 228 |
#' \item 'ws:request-headers' (type 'string') |
| 229 |
#' |
| 230 |
#' (WebSocket transport) Concatenation of multiple lines terminated by CRLF |
| 231 |
#' sequences, that can be used to add further headers to the HTTP request sent |
| 232 |
#' when connecting. This option can be set on dialers, and must be done before |
| 233 |
#' the transport is started. |
| 234 |
#' |
| 235 |
#' \item 'ws:response-headers' (type 'string') |
| 236 |
#' |
| 237 |
#' (WebSocket transport) Concatenation of multiple lines terminated by CRLF |
| 238 |
#' sequences, that can be used to add further headers to the HTTP response |
| 239 |
#' sent when connecting. This option can be set on listeners, and must be done |
| 240 |
#' before the transport is started. |
| 241 |
#' |
| 242 |
#' \item 'ws:request-uri' (type 'string') |
| 243 |
#' |
| 244 |
#' (WebSocket transport) For obtaining the URI sent by the client. This can be |
| 245 |
#' useful when a handler supports an entire directory tree. |
| 246 |
#' |
| 247 |
#' } |
| 248 |
#' |
| 249 |
#' @examples |
| 250 |
#' s <- socket("pair") |
| 251 |
#' opt(s, "send-buffer") |
| 252 |
#' close(s) |
| 253 |
#' |
| 254 |
#' s <- socket("req") |
| 255 |
#' ctx <- context(s) |
| 256 |
#' opt(ctx, "send-timeout") |
| 257 |
#' close(ctx) |
| 258 |
#' close(s) |
| 259 |
#' |
| 260 |
#' s <- socket("pair", dial = "inproc://nanonext", autostart = FALSE) |
| 261 |
#' opt(s$dialer[[1]], "reconnect-time-min") |
| 262 |
#' close(s) |
| 263 |
#' |
| 264 |
#' s <- socket("pair", listen = "inproc://nanonext", autostart = FALSE) |
| 265 |
#' opt(s$listener[[1]], "recv-size-max") |
| 266 |
#' close(s) |
| 267 |
#' |
| 268 |
#' @export |
| 269 |
#' |
| 270 |
opt <- function(object, name) .Call(rnng_get_opt, object, name) |
| 271 |
|
| 272 |
#' @examples |
| 273 |
#' s <- socket("pair") |
| 274 |
#' opt(s, "recv-timeout") <- 2000 |
| 275 |
#' close(s) |
| 276 |
#' |
| 277 |
#' s <- socket("req") |
| 278 |
#' ctx <- context(s) |
| 279 |
#' opt(ctx, "send-timeout") <- 2000 |
| 280 |
#' close(ctx) |
| 281 |
#' close(s) |
| 282 |
#' |
| 283 |
#' s <- socket("pair", dial = "inproc://nanonext", autostart = FALSE) |
| 284 |
#' opt(s$dialer[[1]], "reconnect-time-min") <- 2000 |
| 285 |
#' start(s$dialer[[1]]) |
| 286 |
#' close(s) |
| 287 |
#' |
| 288 |
#' s <- socket("pair", listen = "inproc://nanonext", autostart = FALSE) |
| 289 |
#' opt(s$listener[[1]], "recv-size-max") <- 1024 |
| 290 |
#' start(s$listener[[1]]) |
| 291 |
#' close(s) |
| 292 |
#' |
| 293 |
#' @rdname opt |
| 294 |
#' @export |
| 295 |
#' |
| 296 |
`opt<-` <- function(object, name, value) .Call(rnng_set_opt, object, name, value) |
| 297 |
|
| 298 |
#' Subscribe / Unsubscribe Topic |
| 299 |
#' |
| 300 |
#' For a socket or context using the sub protocol in a publisher/subscriber |
| 301 |
#' pattern. Set a topic to subscribe to, or remove a topic from the subscription |
| 302 |
#' list. |
| 303 |
#' |
| 304 |
#' To use pub/sub the publisher must: |
| 305 |
#' \itemize{ |
| 306 |
#' \item specify `mode = 'raw'` when sending. |
| 307 |
#' \item ensure the sent vector starts with the topic. |
| 308 |
#' } |
| 309 |
#' The subscriber should then receive specifying the correct mode. |
| 310 |
#' |
| 311 |
#' @param con a Socket or Context using the 'sub' protocol. |
| 312 |
#' @param topic \[default NULL\] an atomic type or `NULL`. The default `NULL` |
| 313 |
#' subscribes to all topics / unsubscribes from all topics (if all topics were |
| 314 |
#' previously subscribed). |
| 315 |
#' |
| 316 |
#' @return Invisibly, the passed Socket or Context. |
| 317 |
#' |
| 318 |
#' @examples |
| 319 |
#' pub <- socket("pub", listen = "inproc://nanonext") |
| 320 |
#' sub <- socket("sub", dial = "inproc://nanonext") |
| 321 |
#' |
| 322 |
#' subscribe(sub, "examples") |
| 323 |
#' |
| 324 |
#' send(pub, c("examples", "this is an example"), mode = "raw") |
| 325 |
#' recv(sub, "character") |
| 326 |
#' send(pub, "examples will also be received", mode = "raw") |
| 327 |
#' recv(sub, "character") |
| 328 |
#' send(pub, c("other", "this other topic will not be received"), mode = "raw") |
| 329 |
#' recv(sub, "character") |
| 330 |
#' unsubscribe(sub, "examples") |
| 331 |
#' send(pub, c("examples", "this example is no longer received"), mode = "raw") |
| 332 |
#' recv(sub, "character") |
| 333 |
#' |
| 334 |
#' subscribe(sub, 2) |
| 335 |
#' send(pub, c(2, 10, 10, 20), mode = "raw") |
| 336 |
#' recv(sub, "double") |
| 337 |
#' unsubscribe(sub, 2) |
| 338 |
#' send(pub, c(2, 10, 10, 20), mode = "raw") |
| 339 |
#' recv(sub, "double") |
| 340 |
#' |
| 341 |
#' close(pub) |
| 342 |
#' close(sub) |
| 343 |
#' |
| 344 |
#' @export |
| 345 |
#' |
| 346 |
subscribe <- function(con, topic = NULL) |
| 347 |
invisible(.Call(rnng_subscribe, con, topic, TRUE)) |
| 348 |
|
| 349 |
#' @rdname subscribe |
| 350 |
#' @export |
| 351 |
#' |
| 352 |
unsubscribe <- function(con, topic = NULL) |
| 353 |
invisible(.Call(rnng_subscribe, con, topic, FALSE)) |
| 354 |
|
| 355 |
#' Set Survey Time |
| 356 |
#' |
| 357 |
#' For a socket or context using the surveyor protocol in a surveyor/respondent |
| 358 |
#' pattern. Set the survey timeout in milliseconds (remains valid for all |
| 359 |
#' subsequent surveys). Messages received by the surveyor after the timer has |
| 360 |
#' ended are discarded. |
| 361 |
#' |
| 362 |
#' After using this function, to start a new survey, the surveyor must: |
| 363 |
#' \itemize{ |
| 364 |
#' \item send a message. |
| 365 |
#' \item switch to receiving responses. |
| 366 |
#' } |
| 367 |
#' |
| 368 |
#' To respond to a survey, the respondent must: |
| 369 |
#' \itemize{ |
| 370 |
#' \item receive the survey message. |
| 371 |
#' \item send a reply using [send_aio()] before the survey has timed out (a |
| 372 |
#' reply can only be sent after receiving a survey). |
| 373 |
#' } |
| 374 |
#' |
| 375 |
#' @param con a Socket or Context using the 'surveyor' protocol. |
| 376 |
#' @param value \[default 1000L\] integer survey timeout in milliseconds. |
| 377 |
#' |
| 378 |
#' @return Invisibly, the passed Socket or Context. |
| 379 |
#' |
| 380 |
#' @examples |
| 381 |
#' sur <- socket("surveyor", listen = "inproc://nanonext") |
| 382 |
#' res <- socket("respondent", dial = "inproc://nanonext") |
| 383 |
#' |
| 384 |
#' survey_time(sur, 1000) |
| 385 |
#' |
| 386 |
#' send(sur, "reply to this survey") |
| 387 |
#' aio <- recv_aio(sur) |
| 388 |
#' |
| 389 |
#' recv(res) |
| 390 |
#' s <- send_aio(res, "replied") |
| 391 |
#' |
| 392 |
#' call_aio(aio)$data |
| 393 |
#' |
| 394 |
#' close(sur) |
| 395 |
#' close(res) |
| 396 |
#' |
| 397 |
#' @export |
| 398 |
#' |
| 399 |
`survey_time` <- function(con, value = 1000L) |
| 400 |
invisible(.Call(rnng_set_opt, con, "surveyor:survey-time", value)) |