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

send_aio(con, data, mode = c("serial", "raw", "next"), timeout = NULL)

Arguments

con

a Socket, Context or Stream.

data

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

mode

[default 'serial'] character value or integer equivalent - one of 'serial' (1L) to send serialised R objects, 'raw' (2L) to send atomic vectors of any type as a raw byte vector, or 'next' (3L) - see 'Send Modes' section below. For Streams, 'raw' is the only option and this argument is ignored.

timeout

[default NULL] integer value in milliseconds or NULL, which applies a socket-specific default, usually the same as no timeout.

Value

A 'sendAio' (object of class 'sendAio') (invisibly).

Details

Async send is always non-blocking and returns a 'sendAio' immediately.

For a 'sendAio', the send result is available at $result. An 'unresolved' logical NA is returned if the async operation is yet to complete. The resolved value will be zero on success, or else an integer error code.

To wait for and check the result of the send operation, use call_aio on the returned 'sendAio' object.

Alternatively, to stop the async operation, use stop_aio.

Send Modes

The default mode 'serial' sends serialised R objects to ensure perfect reproducibility within R. When receiving, the corresponding mode 'serial' should be used.

Mode 'raw' sends atomic vectors of any type as a raw byte vector, and must be used when interfacing with external applications or raw system sockets, where R serialization is not in use. When receiving, the mode corresponding to the vector sent should be used.

Mode 'next' sends serialised R objects, with native extensions enabled by next_config. This configures custom serialization and unserialization functions for external pointer reference objects. When receiving, mode 'serial' should be used as 'next' sends are fully compatible.

Examples

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

res <- send_aio(pub, data.frame(a = 1, b = 2), timeout = 100)
res
#> < sendAio | $result >
res$result
#> [1] 0

res <- send_aio(pub, "example message", mode = "raw", timeout = 100)
call_aio(res)$result
#> [1] 0

close(pub)