Send data over a connection (Socket, Context or Stream).

send(con, data, mode = c("serial", "raw"), block, echo = TRUE)

# S3 method for nanoSocket
send(con, data, mode = c("serial", "raw"), block = FALSE, echo = TRUE)

# S3 method for nanoContext
send(con, data, mode = c("serial", "raw"), block = TRUE, echo = TRUE)

# S3 method for nanoStream
send(con, data, mode = "raw", block = TRUE, echo = TRUE)

Arguments

con

a Socket, Context or Stream.

data

an object (a vector, if mode = 'raw').

mode

[default 'serial'] for sending serialised R objects, or 'raw' for sending vectors of any type (converted to a raw byte vector for sending). For Streams, 'raw' is the only option and any other value is ignored. Use 'serial' for perfect reproducibility within R, although 'raw' must be used when interfacing with external applications that do not understand R serialisation.

block

logical TRUE to block until successful or FALSE to return immediately even if unsuccessful (e.g. if no connection is available), or else an integer value specifying the maximum time to block in milliseconds, after which the operation will time out.

echo

[default TRUE] logical TRUE to return the raw vector of sent data, or FALSE to return an integer exit code (invisibly).

Value

Raw vector of sent data, or (invisibly) an integer exit code (zero on success) if 'echo' is set to FALSE.

Blocking

For Sockets: the default behaviour is non-blocking with block = FALSE. This will return immediately with an error if the message could not be queued for sending. Certain protocol / transport combinations may limit the number of messages that can be queued if they have yet to be received.

For Contexts and Streams: the default behaviour is blocking with block = TRUE. This will wait until the send has completed. Set a timeout in this case to ensure that the function returns under all scenarios. As the underlying implementation uses an asynchronous send with a wait, it is recommended to set a positive integer value for block rather than FALSE.

Examples

pub <- socket("pub", dial = "inproc://nanonext")

send(pub, data.frame(a = 1, b = 2))
#>   [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#>  [26] 03 13 00 00 00 02 00 00 00 0e 00 00 00 01 3f f0 00 00 00 00 00 00 00 00 00
#>  [51] 0e 00 00 00 01 40 00 00 00 00 00 00 00 00 00 04 02 00 00 00 01 00 04 00 09
#>  [76] 00 00 00 05 6e 61 6d 65 73 00 00 00 10 00 00 00 02 00 04 00 09 00 00 00 01
#> [101] 61 00 04 00 09 00 00 00 01 62 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00
#> [126] 05 63 6c 61 73 73 00 00 00 10 00 00 00 01 00 04 00 09 00 00 00 0a 64 61 74
#> [151] 61 2e 66 72 61 6d 65 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 09 72 6f
#> [176] 77 2e 6e 61 6d 65 73 00 00 00 0d 00 00 00 02 80 00 00 00 ff ff ff ff 00 00
#> [201] 00 fe
send(pub, c(10.1, 20.2, 30.3), mode = "raw", block = 100)
#>  [1] 33 33 33 33 33 33 24 40 33 33 33 33 33 33 34 40 cd cc cc cc cc 4c 3e 40

close(pub)

req <- socket("req", listen = "inproc://nanonext")
rep <- socket("rep", dial = "inproc://nanonext")

ctx <- context(req)
send(ctx, data.frame(a = 1, b = 2), block = 100)
#>   [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
#>  [26] 03 13 00 00 00 02 00 00 00 0e 00 00 00 01 3f f0 00 00 00 00 00 00 00 00 00
#>  [51] 0e 00 00 00 01 40 00 00 00 00 00 00 00 00 00 04 02 00 00 00 01 00 04 00 09
#>  [76] 00 00 00 05 6e 61 6d 65 73 00 00 00 10 00 00 00 02 00 04 00 09 00 00 00 01
#> [101] 61 00 04 00 09 00 00 00 01 62 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00
#> [126] 05 63 6c 61 73 73 00 00 00 10 00 00 00 01 00 04 00 09 00 00 00 0a 64 61 74
#> [151] 61 2e 66 72 61 6d 65 00 00 04 02 00 00 00 01 00 04 00 09 00 00 00 09 72 6f
#> [176] 77 2e 6e 61 6d 65 73 00 00 00 0d 00 00 00 02 80 00 00 00 ff ff ff ff 00 00
#> [201] 00 fe

msg <- recv_aio(rep, timeout = 100)
send(ctx, c(1.1, 2.2, 3.3), mode = "raw", block = 100)
#>  [1] 9a 99 99 99 99 99 f1 3f 9a 99 99 99 99 99 01 40 66 66 66 66 66 66 0a 40

close(req)
close(rep)