Skip to content

Commit 3f9b045

Browse files
committed
implement blocking timeouts for synchronous send/recv
1 parent c7b2976 commit 3f9b045

File tree

7 files changed

+187
-97
lines changed

7 files changed

+187
-97
lines changed

NEWS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
#### New Features
44

5-
* New 'stream' interface exposes low-level byte stream functionality in the NNG library, intended for communicating with non-NNG endpoints, including but not limited to websocket servers.
5+
* New `stream()` interface exposes low-level byte stream functionality in the NNG library, intended for communicating with non-NNG endpoints, including but not limited to websocket servers.
66

77
#### Updates
88

9-
* Unified synchronous `send()` and `recv()` functions, and their async counterparts `send_aio()` and `recv_aio()`, are now S3 generics and can be used across Sockets, Contexts as well as Streams.
10-
* A timeout can now be specified directly using the 'block' argument for synchronous sends over a Context/Stream.
9+
* Unified synchronous `send()` and `recv()` functions, and their asynchronous counterparts `send_aio()` and `recv_aio()`, are now S3 generics and can be used across Sockets, Contexts and Streams.
10+
* Revised 'block' argument for synchronous `send()` and `recv()` allows setting a timeout for all methods.
1111
* `send_ctx()` and `recv_ctx()` are deprecated and will be removed in a future package version - the methods for `send()` and `recv()` should be used instead.
1212

1313
# nanonext 0.3.0

R/sendrecv.R

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
#' Send data over a connection (Socket, Context or Stream).
66
#'
77
#' @param con a Socket, Context or Stream.
8-
#' @param data an object (if mode = 'raw', a vector).
9-
#' @param mode whether data will be sent serialized or as a raw vector. Specify
10-
#' 'serial' for sending and receiving objects within R for perfect
11-
#' reproducibility. Specify 'raw' for sending vectors of any type (converted
12-
#' to a raw byte vector for sending) - essential when interfacing with
13-
#' external applications. For Streams, 'raw' is the only choice and any other
14-
#' value is ignored.
15-
#' @param block <Sockets> [default FALSE] logical flag whether to block until
16-
#' successful or return immediately (e.g. if no connection is available).
17-
#' <Contexts and Streams> [default TRUE] optionally an integer maximum time
18-
#' to block in milliseconds, after which the operation will time out.
19-
#' @param echo [default TRUE] logical flag whether to return the raw vector of
20-
#' sent data. Set to FALSE for performance-critical applications.
8+
#' @param data an object (a vector, if mode = 'raw').
9+
#' @param mode either 'serial' for sending serialised R objects, or 'raw' for
10+
#' sending vectors of any type (converted to a raw byte vector for sending).
11+
#' For Streams, 'raw' is the only choice and any other value is ignored. Use
12+
#' 'serial' for perfect reproducibility within R, although 'raw' must be used
13+
#' when interfacing with external applications that do not understand R
14+
#' serialisation.
15+
#' @param block logical TRUE to block until successful or FALSE to return
16+
#' immediately even if unsuccessful (e.g. if no connection is available),
17+
#' or else an integer value specifying the maximum time to block in
18+
#' milliseconds, after which the operation will time out.
19+
#' @param echo [default TRUE] logical TRUE to return the raw vector of sent data,
20+
#' or FALSE to return an integer exit code (invisibly).
2121
#'
2222
#' @return Raw vector of sent data, or (invisibly) an integer exit code (zero on
2323
#' success) if 'echo' is set to FALSE.
2424
#'
25-
#' @section Contexts: Will block if the send is in progress and has not yet
26-
#' completed - certain protocol / transport combinations may limit the
27-
#' number of messages that can be queued if they have yet to be received.
28-
#' Set a timeout to ensure the function returns under all scenarios.
25+
#' @section Blocking:
2926
#'
30-
#' @section Streams: Sending a byte stream synchronously will block if the send
31-
#' is in progress and has not yet completed. Set a timeout to ensure the
32-
#' function returns under all scenarios.
27+
#' For Sockets: the default behaviour is non-blocking with \code{block = FALSE}.
28+
#' This will return immediately with an error if the message could not be
29+
#' queued for sending.
30+
#'
31+
#' For Contexts and Streams: the default behaviour is blocking with \code{block = TRUE}.
32+
#' This will wait until the send has completed. For Contexts, certain protocol /
33+
#' transport combinations may limit the number of messages that can be queued
34+
#' if they have yet to be received. Set a timeout in this case to ensure that
35+
#' the function returns under all scenarios. As the underlying implementation
36+
#' differs to that for Sockets, it is recommended to set a positive integer
37+
#' value for \code{block} rather than FALSE.
3338
#'
3439
#' @examples
3540
#' pub <- socket("pub", dial = "inproc://nanonext")
@@ -54,7 +59,11 @@
5459
#' @rdname send
5560
#' @export
5661
#'
57-
send <- function(con, data, mode, block, echo) UseMethod("send")
62+
send <- function(con,
63+
data,
64+
mode = c("serial", "raw"),
65+
block,
66+
echo = TRUE) UseMethod("send")
5867

5968
#' @rdname send
6069
#' @method send nanoSocket
@@ -156,12 +165,19 @@ send.nanoStream <- function(con,
156165
#' specified), the received raw vector will always be returned to allow for
157166
#' the data to be recovered.
158167
#'
159-
#' @section Contexts: Will block while awaiting the receive operation to complete.
160-
#' Set a timeout to ensure that the function returns under all scenarios.
168+
#' @section Blocking:
169+
#'
170+
#' For Sockets: the default behaviour is non-blocking with \code{block = FALSE}.
171+
#' This will return immediately with an error if the message could not be
172+
#' queued for sending.
161173
#'
162-
#' @section Streams: Receivng a byte stream synchronously will block while
163-
#' awaiting the receive operation to complete. Set a timeout to ensure that
164-
#' the function returns under all scenarios.
174+
#' For Contexts and Streams: the default behaviour is blocking with \code{block = TRUE}.
175+
#' This will wait until the send has completed. For Contexts, certain protocol /
176+
#' transport combinations may limit the number of messages that can be queued
177+
#' if they have yet to be received. Set a timeout in this case to ensure that
178+
#' the function returns under all scenarios. As the underlying implementation
179+
#' differs to that for Sockets, it is recommended to set a positive integer
180+
#' value for \code{block} rather than FALSE.
165181
#'
166182
#' @examples
167183
#' s1 <- socket("bus", listen = "inproc://nanonext")
@@ -202,7 +218,7 @@ send.nanoStream <- function(con,
202218
recv <- function(con,
203219
mode = c("serial", "character", "complex", "double",
204220
"integer", "logical", "numeric", "raw"),
205-
block = FALSE,
221+
block,
206222
keep.raw = TRUE,
207223
...,
208224
n) UseMethod("recv")

man/recv.Rd

Lines changed: 18 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/send.Rd

Lines changed: 27 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/send_aio.Rd

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/send_ctx.Rd

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)