| 1 |
# nanonext - Core Functions - send/recv ---------------------------------------- |
| 2 |
|
| 3 |
#' Send |
| 4 |
#' |
| 5 |
#' Send data over a connection (Socket, Context or Stream). |
| 6 |
#' |
| 7 |
#' @param con a Socket, Context or Stream. |
| 8 |
#' @param data an object (a vector, if `mode = "raw"`). |
| 9 |
#' @param mode \[default 'serial'\] character value or integer equivalent - |
| 10 |
#' either `"serial"` (1L) to send serialised R objects, or `"raw"` (2L) to |
| 11 |
#' send atomic vectors of any type as a raw byte vector. For Streams, `"raw"` |
| 12 |
#' is the only option and this argument is ignored. |
| 13 |
#' @param block \[default NULL\] which applies the connection default (see |
| 14 |
#' section 'Blocking' below). Specify logical `TRUE` to block until successful |
| 15 |
#' or `FALSE` to return immediately even if unsuccessful (e.g. if no |
| 16 |
#' connection is available), or else an integer value specifying the maximum |
| 17 |
#' time to block in milliseconds, after which the operation will time out. |
| 18 |
#' @param pipe \[default 0L\] only applicable to Sockets using the 'poly' |
| 19 |
#' protocol, an integer pipe ID if directing the send via a specific pipe. |
| 20 |
#' |
| 21 |
#' @return An integer exit code (zero on success). |
| 22 |
#' |
| 23 |
#' @section Blocking: |
| 24 |
#' |
| 25 |
#' For Sockets and Contexts: the default behaviour is non-blocking with |
| 26 |
#' `block = FALSE`. This will return immediately with an error if the message |
| 27 |
#' could not be queued for sending. Certain protocol / transport combinations |
| 28 |
#' may limit the number of messages that can be queued if they have yet to be |
| 29 |
#' received. |
| 30 |
#' |
| 31 |
#' For Streams: the default behaviour is blocking with `block = TRUE`. This will |
| 32 |
#' wait until the send has completed. Set a timeout to ensure that the function |
| 33 |
#' returns under all scenarios. As the underlying implementation uses an |
| 34 |
#' asynchronous send with a wait, it is recommended to set a small positive |
| 35 |
#' value for `block` rather than `FALSE`. |
| 36 |
#' |
| 37 |
#' @section Send Modes: |
| 38 |
#' |
| 39 |
#' The default mode `"serial"` sends serialised R objects to ensure perfect |
| 40 |
#' reproducibility within R. When receiving, the corresponding mode `"serial"` |
| 41 |
#' should be used. Custom serialization and unserialization functions for |
| 42 |
#' reference objects may be enabled by the function [serial_config()]. |
| 43 |
#' |
| 44 |
#' Mode `"raw"` sends atomic vectors of any type as a raw byte vector, and must |
| 45 |
#' be used when interfacing with external applications or raw system sockets, |
| 46 |
#' where R serialization is not in use. When receiving, the mode corresponding |
| 47 |
#' to the vector sent should be used. |
| 48 |
#' |
| 49 |
#' @seealso [send_aio()] for asynchronous send. |
| 50 |
#' |
| 51 |
#' @examples |
| 52 |
#' pub <- socket("pub", dial = "inproc://nanonext") |
| 53 |
#' |
| 54 |
#' send(pub, data.frame(a = 1, b = 2)) |
| 55 |
#' send(pub, c(10.1, 20.2, 30.3), mode = "raw", block = 100) |
| 56 |
#' |
| 57 |
#' close(pub) |
| 58 |
#' |
| 59 |
#' req <- socket("req", listen = "inproc://nanonext") |
| 60 |
#' rep <- socket("rep", dial = "inproc://nanonext") |
| 61 |
#' |
| 62 |
#' ctx <- context(req) |
| 63 |
#' send(ctx, data.frame(a = 1, b = 2), block = 100) |
| 64 |
#' |
| 65 |
#' msg <- recv_aio(rep, timeout = 100) |
| 66 |
#' send(ctx, c(1.1, 2.2, 3.3), mode = "raw", block = 100) |
| 67 |
#' |
| 68 |
#' close(req) |
| 69 |
#' close(rep) |
| 70 |
#' |
| 71 |
#' @export |
| 72 |
#' |
| 73 |
send <- function(con, data, mode = c("serial", "raw"), block = NULL, pipe = 0L) |
| 74 |
.Call(rnng_send, con, data, mode, block, pipe) |
| 75 |
|
| 76 |
#' Receive |
| 77 |
#' |
| 78 |
#' Receive data over a connection (Socket, Context or Stream). |
| 79 |
#' |
| 80 |
#' @inheritParams send |
| 81 |
#' @param mode \[default 'serial'\] character value or integer equivalent - one |
| 82 |
#' of `"serial"` (1L), `"character"` (2L), `"complex"` (3L), `"double"` (4L), |
| 83 |
#' `"integer"` (5L), `"logical"` (6L), `"numeric"` (7L), `"raw"` (8L), or |
| 84 |
#' `"string"` (9L). The default `"serial"` means a serialised R object; for |
| 85 |
#' the other modes, received bytes are converted into the respective mode. |
| 86 |
#' `"string"` is a faster option for length one character vectors. For |
| 87 |
#' Streams, `"serial"` will default to `"character"`. |
| 88 |
#' @return The received data in the `mode` specified. |
| 89 |
#' |
| 90 |
#' @section Errors: |
| 91 |
#' |
| 92 |
#' In case of an error, an integer 'errorValue' is returned (to be |
| 93 |
#' distiguishable from an integer message value). This can be verified using |
| 94 |
#' [is_error_value()]. |
| 95 |
#' |
| 96 |
#' If an error occurred in unserialization or conversion of the message data to |
| 97 |
#' the specified mode, a raw vector will be returned instead to allow recovery |
| 98 |
#' (accompanied by a warning). |
| 99 |
#' |
| 100 |
#' @section Blocking: |
| 101 |
#' |
| 102 |
#' For Sockets and Contexts: the default behaviour is non-blocking with |
| 103 |
#' `block = FALSE`. This will return immediately with an error if no messages |
| 104 |
#' are available. |
| 105 |
#' |
| 106 |
#' For Streams: the default behaviour is blocking with `block = TRUE`. This will |
| 107 |
#' wait until a message is received. Set a timeout to ensure that the function |
| 108 |
#' returns under all scenarios. As the underlying implementation uses an |
| 109 |
#' asynchronous receive with a wait, it is recommended to set a small positive |
| 110 |
#' value for `block` rather than `FALSE`. |
| 111 |
#' |
| 112 |
#' @seealso [recv_aio()] for asynchronous receive. |
| 113 |
#' |
| 114 |
#' @examples |
| 115 |
#' s1 <- socket("pair", listen = "inproc://nanonext") |
| 116 |
#' s2 <- socket("pair", dial = "inproc://nanonext") |
| 117 |
#' |
| 118 |
#' send(s1, data.frame(a = 1, b = 2)) |
| 119 |
#' res <- recv(s2) |
| 120 |
#' res |
| 121 |
#' send(s1, data.frame(a = 1, b = 2)) |
| 122 |
#' recv(s2) |
| 123 |
#' |
| 124 |
#' send(s1, c(1.1, 2.2, 3.3), mode = "raw") |
| 125 |
#' res <- recv(s2, mode = "double", block = 100) |
| 126 |
#' res |
| 127 |
#' send(s1, "example message", mode = "raw") |
| 128 |
#' recv(s2, mode = "character") |
| 129 |
#' |
| 130 |
#' close(s1) |
| 131 |
#' close(s2) |
| 132 |
#' |
| 133 |
#' req <- socket("req", listen = "inproc://nanonext") |
| 134 |
#' rep <- socket("rep", dial = "inproc://nanonext") |
| 135 |
#' |
| 136 |
#' ctxq <- context(req) |
| 137 |
#' ctxp <- context(rep) |
| 138 |
#' send(ctxq, data.frame(a = 1, b = 2), block = 100) |
| 139 |
#' recv(ctxp, block = 100) |
| 140 |
#' |
| 141 |
#' send(ctxq, c(1.1, 2.2, 3.3), mode = "raw", block = 100) |
| 142 |
#' recv(ctxp, mode = "double", block = 100) |
| 143 |
#' |
| 144 |
#' close(req) |
| 145 |
#' close(rep) |
| 146 |
#' |
| 147 |
#' @export |
| 148 |
#' |
| 149 |
recv <- function( |
| 150 |
con, |
| 151 |
mode = c("serial", "character", "complex", "double", "integer", "logical", "numeric", "raw", "string"), |
| 152 |
block = NULL |
| 153 |
) |
| 154 |
.Call(rnng_recv, con, mode, block) |