Skip to content

Commit c455432

Browse files
committed
reflow socket, send + recv docs
1 parent 7d78f5a commit c455432

File tree

13 files changed

+309
-308
lines changed

13 files changed

+309
-308
lines changed

R/sendrecv.R

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@
2323
#' @param con a Socket, Context or Stream.
2424
#' @param data an object (a vector, if mode = \sQuote{raw}).
2525
#' @param mode [default 'serial'] character value or integer equivalent - either
26-
#' \sQuote{serial} (1L) to send serialised R objects, or \sQuote{raw} (2L)
27-
#' to send atomic vectors of any type as a raw byte vector. For Streams,
28-
#' \sQuote{raw} is the only option and this argument is ignored.
26+
#' \sQuote{serial} (1L) to send serialised R objects, or \sQuote{raw} (2L) to
27+
#' send atomic vectors of any type as a raw byte vector. For Streams,
28+
#' \sQuote{raw} is the only option and this argument is ignored.
2929
#' @param block [default NULL] which applies the connection default (see section
30-
#' \sQuote{Blocking} below). Specify logical TRUE to block until successful
31-
#' or FALSE to return immediately even if unsuccessful (e.g. if no
32-
#' connection is available), or else an integer value specifying the maximum
33-
#' time to block in milliseconds, after which the operation will time out.
30+
#' \sQuote{Blocking} below). Specify logical TRUE to block until successful or
31+
#' FALSE to return immediately even if unsuccessful (e.g. if no connection is
32+
#' available), or else an integer value specifying the maximum time to block
33+
#' in milliseconds, after which the operation will time out.
3434
#'
3535
#' @return Integer exit code (zero on success).
3636
#'
3737
#' @section Blocking:
3838
#'
39-
#' For Sockets and Contexts: the default behaviour is non-blocking with
40-
#' \code{block = FALSE}. This will return immediately with an error if the
41-
#' message could not be queued for sending. Certain protocol / transport
42-
#' combinations may limit the number of messages that can be queued if they
43-
#' have yet to be received.
39+
#' For Sockets and Contexts: the default behaviour is non-blocking with
40+
#' \code{block = FALSE}. This will return immediately with an error if the
41+
#' message could not be queued for sending. Certain protocol / transport
42+
#' combinations may limit the number of messages that can be queued if they have
43+
#' yet to be received.
4444
#'
45-
#' For Streams: the default behaviour is blocking with \code{block = TRUE}.
46-
#' This will wait until the send has completed. Set a timeout to ensure that
47-
#' the function returns under all scenarios. As the underlying
48-
#' implementation uses an asynchronous send with a wait, it is recommended
49-
#' to set a small positive value for \code{block} rather than FALSE.
45+
#' For Streams: the default behaviour is blocking with \code{block = TRUE}. This
46+
#' will wait until the send has completed. Set a timeout to ensure that the
47+
#' function returns under all scenarios. As the underlying implementation uses
48+
#' an asynchronous send with a wait, it is recommended to set a small positive
49+
#' value for \code{block} rather than FALSE.
5050
#'
5151
#' @section Send Modes:
5252
#'
53-
#' The default mode \sQuote{serial} sends serialised R objects to ensure
54-
#' perfect reproducibility within R. When receiving, the corresponding mode
55-
#' \sQuote{serial} should be used. Custom serialization and unserialization
56-
#' functions for reference objects may be enabled by the function
57-
#' \code{\link{serial_config}}.
53+
#' The default mode \sQuote{serial} sends serialised R objects to ensure perfect
54+
#' reproducibility within R. When receiving, the corresponding mode
55+
#' \sQuote{serial} should be used. Custom serialization and unserialization
56+
#' functions for reference objects may be enabled by the function
57+
#' \code{\link{serial_config}}.
5858
#'
59-
#' Mode \sQuote{raw} sends atomic vectors of any type as a raw byte vector,
60-
#' and must be used when interfacing with external applications or raw
61-
#' system sockets, where R serialization is not in use. When receiving, the
62-
#' mode corresponding to the vector sent should be used.
59+
#' Mode \sQuote{raw} sends atomic vectors of any type as a raw byte vector, and
60+
#' must be used when interfacing with external applications or raw system
61+
#' sockets, where R serialization is not in use. When receiving, the mode
62+
#' corresponding to the vector sent should be used.
6363
#'
6464
#' @seealso \code{\link{send_aio}} for asynchronous send.
6565
#' @examples
@@ -93,39 +93,40 @@ send <- function(con, data, mode = c("serial", "raw"), block = NULL)
9393
#'
9494
#' @inheritParams send
9595
#' @param mode [default 'serial'] character value or integer equivalent - one of
96-
#' \sQuote{serial} (1L), \sQuote{character} (2L), \sQuote{complex} (3L),
97-
#' \sQuote{double} (4L), \sQuote{integer} (5L), \sQuote{logical} (6L),
98-
#' \sQuote{numeric} (7L), \sQuote{raw} (8L), or \sQuote{string} (9L). The
99-
#' default \sQuote{serial} means a serialised R object; for the other modes,
100-
#' received bytes are converted into the respective mode. \sQuote{string} is
101-
#' a faster option for length one character vectors. For Streams,
102-
#' \sQuote{serial} is not an option and the default is \sQuote{character}.
96+
#' \sQuote{serial} (1L), \sQuote{character} (2L), \sQuote{complex} (3L),
97+
#' \sQuote{double} (4L), \sQuote{integer} (5L), \sQuote{logical} (6L),
98+
#' \sQuote{numeric} (7L), \sQuote{raw} (8L), or \sQuote{string} (9L). The
99+
#' default \sQuote{serial} means a serialised R object; for the other modes,
100+
#' received bytes are converted into the respective mode. \sQuote{string} is a
101+
#' faster option for length one character vectors. For Streams,
102+
#' \sQuote{serial} is not an option and the default is \sQuote{character}.
103103
#' @param n [default 65536L] applicable to Streams only, the maximum number of
104-
#' bytes to receive. Can be an over-estimate, but note that a buffer of this
105-
#' size is reserved.
104+
#' bytes to receive. Can be an over-estimate, but note that a buffer of this
105+
#' size is reserved.
106106
#'
107107
#' @return The received data in the \sQuote{mode} specified.
108108
#'
109-
#' @details In case of an error, an integer \sQuote{errorValue} is returned (to
110-
#' be distiguishable from an integer message value). This can be verified
111-
#' using \code{\link{is_error_value}}.
109+
#' @section Errors:
112110
#'
113-
#' If an error occurred in unserialization or conversion of the message data
114-
#' to the specified mode, a raw vector will be returned instead to allow
115-
#' recovery (accompanied by a warning).
111+
#' In case of an error, an integer \sQuote{errorValue} is returned (to be
112+
#' distiguishable from an integer message value). This can be verified using
113+
#' \code{\link{is_error_value}}.
114+
#'
115+
#' If an error occurred in unserialization or conversion of the message data to
116+
#' the specified mode, a raw vector will be returned instead to allow recovery
117+
#' (accompanied by a warning).
116118
#'
117119
#' @section Blocking:
118120
#'
119-
#' For Sockets and Contexts: the default behaviour is non-blocking with
120-
#' \code{block = FALSE}. This will return immediately with an error if no
121-
#' messages are available.
121+
#' For Sockets and Contexts: the default behaviour is non-blocking with
122+
#' \code{block = FALSE}. This will return immediately with an error if no
123+
#' messages are available.
122124
#'
123-
#' For Streams: the default behaviour is blocking with \code{block = TRUE}.
124-
#' This will wait until a message is received. Set a timeout to ensure that
125-
#' the function returns under all scenarios. As the underlying
126-
#' implementation uses an asynchronous receive with a wait, it is
127-
#' recommended to set a small positive value for \code{block} rather than
128-
#' FALSE.
125+
#' For Streams: the default behaviour is blocking with \code{block = TRUE}. This
126+
#' will wait until a message is received. Set a timeout to ensure that the
127+
#' function returns under all scenarios. As the underlying implementation uses
128+
#' an asynchronous receive with a wait, it is recommended to set a small
129+
#' positive value for \code{block} rather than FALSE.
129130
#'
130131
#' @seealso \code{\link{recv_aio}} for asynchronous receive.
131132
#' @examples

R/socket.R

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -19,78 +19,78 @@
1919
#' Open Socket
2020
#'
2121
#' Open a Socket implementing \sQuote{protocol}, and optionally dial (establish
22-
#' an outgoing connection) or listen (accept an incoming connection) at an
23-
#' address.
22+
#' an outgoing connection) or listen (accept an incoming connection) at an
23+
#' address.
24+
#'
25+
#' NNG presents a socket view of networking. The sockets are constructed using
26+
#' protocol-specific functions, as a given socket implements precisely one
27+
#' protocol.
28+
#'
29+
#' Each socket may be used to send and receive messages (if the protocol
30+
#' supports it, and implements the appropriate protocol semantics). For example,
31+
#' sub sockets automatically filter incoming messages to discard those for
32+
#' topics that have not been subscribed.
33+
#'
34+
#' This function (optionally) binds a single Dialer and/or Listener to a Socket.
35+
#' More complex network topologies may be created by binding further Dialers /
36+
#' Listeners to the Socket as required using \code{\link{dial}} and
37+
#' \code{\link{listen}}.
38+
#'
39+
#' New contexts may also be created using \code{\link{context}} if the protocol
40+
#' supports it.
2441
#'
2542
#' @param protocol [default 'bus'] choose protocol - \sQuote{bus}, \sQuote{pair},
26-
#' \sQuote{poly}, \sQuote{push}, \sQuote{pull}, \sQuote{pub}, \sQuote{sub},
27-
#' \sQuote{req}, \sQuote{rep}, \sQuote{surveyor}, or \sQuote{respondent} -
28-
#' see \link{protocols}.
43+
#' \sQuote{poly}, \sQuote{push}, \sQuote{pull}, \sQuote{pub}, \sQuote{sub},
44+
#' \sQuote{req}, \sQuote{rep}, \sQuote{surveyor}, or \sQuote{respondent} - see
45+
#' \link{protocols}.
2946
#' @param dial (optional) a URL to dial, specifying the transport and address as
30-
#' a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555'
31-
#' (see \link{transports}).
47+
#' a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555' (see
48+
#' \link{transports}).
3249
#' @param listen (optional) a URL to listen at, specifying the transport and
33-
#' address as a character string e.g. 'inproc://anyvalue' or
34-
#' 'tcp://127.0.0.1:5555' (see \link{transports}).
50+
#' address as a character string e.g. 'inproc://anyvalue' or
51+
#' 'tcp://127.0.0.1:5555' (see \link{transports}).
3552
#' @param autostart [default TRUE] whether to start the dialer/listener. Set to
36-
#' FALSE if setting configuration options on the dialer/listener as it is
37-
#' not generally possible to change these once started. For dialers only:
38-
#' set to NA to start synchronously - this is less resilient if a
39-
#' connection is not immediately possible, but avoids subtle errors from
40-
#' attempting to use the socket before an asynchronous dial has completed.
53+
#' FALSE if setting configuration options on the dialer/listener as it is not
54+
#' generally possible to change these once started. For dialers only: set to
55+
#' NA to start synchronously - this is less resilient if a connection is not
56+
#' immediately possible, but avoids subtle errors from attempting to use the
57+
#' socket before an asynchronous dial has completed.
4158
#' @param raw [default FALSE] whether to open raw mode sockets. Note: not for
42-
#' general use - do not enable unless you have a specific need (refer to NNG
43-
#' documentation).
59+
#' general use - do not enable unless you have a specific need (refer to NNG
60+
#' documentation).
4461
#' @inheritParams dial
4562
#'
4663
#' @return A Socket (object of class \sQuote{nanoSocket} and \sQuote{nano}).
4764
#'
48-
#' @details NNG presents a socket view of networking. The sockets are
49-
#' constructed using protocol-specific functions, as a given socket
50-
#' implements precisely one protocol.
51-
#'
52-
#' Each socket may be used to send and receive messages (if the protocol
53-
#' supports it, and implements the appropriate protocol semantics). For
54-
#' example, sub sockets automatically filter incoming messages to discard
55-
#' those for topics that have not been subscribed.
56-
#'
57-
#' This function (optionally) binds a single Dialer and/or Listener to a
58-
#' Socket. More complex network topologies may be created by binding further
59-
#' Dialers/Listeners to the Socket as required using \code{\link{dial}} and
60-
#' \code{\link{listen}}.
61-
#'
62-
#' New contexts may also be created using \code{\link{context}} if the
63-
#' protocol supports it.
64-
#'
6565
#' @section Protocols:
6666
#'
67-
#' The following Scalability Protocols (communication patterns) are
68-
#' implemented:
69-
#' \itemize{
70-
#' \item Bus (mesh networks) - protocol: 'bus'
71-
#' \item Pair (two-way radio) - protocol: 'pair'
72-
#' \item Poly (one-to-one of many) - protocol: 'poly'
73-
#' \item Pipeline (one-way pipe) - protocol: 'push', 'pull'
74-
#' \item Publisher/Subscriber (topics & broadcast) - protocol: 'pub', 'sub'
75-
#' \item Request/Reply (RPC) - protocol: 'req', 'rep'
76-
#' \item Survey (voting & service discovery) - protocol: 'surveyor',
77-
#' 'respondent'
78-
#' }
79-
#'
80-
#' Please see \link{protocols} for further documentation.
67+
#' The following Scalability Protocols (communication patterns) are implemented:
68+
#' \itemize{
69+
#' \item Bus (mesh networks) - protocol: 'bus'
70+
#' \item Pair (two-way radio) - protocol: 'pair'
71+
#' \item Poly (one-to-one of many) - protocol: 'poly'
72+
#' \item Pipeline (one-way pipe) - protocol: 'push', 'pull'
73+
#' \item Publisher/Subscriber (topics & broadcast) - protocol: 'pub', 'sub'
74+
#' \item Request/Reply (RPC) - protocol: 'req', 'rep'
75+
#' \item Survey (voting & service discovery) - protocol: 'surveyor',
76+
#' 'respondent'
77+
#' }
78+
#'
79+
#' Please see \link{protocols} for further documentation.
8180
#'
8281
#' @section Transports:
8382
#'
84-
#' The following communications transports may be used:
85-
#' \itemize{
86-
#' \item Inproc (in-process) - url: 'inproc://'
87-
#' \item IPC (inter-process communications) - url: 'ipc://' (or
88-
#' 'abstract://' on Linux)
89-
#' \item TCP and TLS over TCP - url: 'tcp://' and 'tls+tcp://'
90-
#' \item WebSocket and TLS over WebSocket - url: 'ws://' and 'wss://'
91-
#' }
83+
#' The following communications transports may be used:
84+
#'
85+
#' \itemize{
86+
#' \item Inproc (in-process) - url: 'inproc://'
87+
#' \item IPC (inter-process communications) - url: 'ipc://' (or 'abstract://'
88+
#' on Linux)
89+
#' \item TCP and TLS over TCP - url: 'tcp://' and 'tls+tcp://'
90+
#' \item WebSocket and TLS over WebSocket - url: 'ws://' and 'wss://'
91+
#' }
9292
#'
93-
#' Please see \link{transports} for further documentation.
93+
#' Please see \link{transports} for further documentation.
9494
#'
9595
#' @examples
9696
#' s <- socket(protocol = "req", listen = "inproc://nanosocket")
@@ -118,15 +118,15 @@ socket <- function(protocol = c("bus", "pair", "poly", "push", "pull", "pub",
118118
#' Collect the Pipe from an Aio
119119
#'
120120
#' This function retrieves the Pipe used to receive a message from the Aio. It
121-
#' will block if the Aio has yet to complete. The message is still available
122-
#' for retrieval by the usual means. A Pipe is a low-level object and it is
123-
#' not normally necessary to deal with them directly.
121+
#' will block if the Aio has yet to complete. The message is still available for
122+
#' retrieval by the usual means. A Pipe is a low-level object and it is not
123+
#' normally necessary to deal with them directly.
124124
#'
125-
#' @param x a 'recvAio' object.
125+
#' As Pipes are always owned by a Socket, removing (and garbage collecting) a
126+
#' Pipe does not close it or free its resources. A Pipe may, however, be
127+
#' explicitly closed.
126128
#'
127-
#' @details As Pipes are always owned by a Socket, removing (and garbage
128-
#' collecting) a Pipe does not close it or free its resources. A Pipe may,
129-
#' however, be explicitly closed.
129+
#' @param x a 'recvAio' object.
130130
#'
131131
#' @return A Pipe (object of class \sQuote{nanoPipe}).
132132
#'
@@ -152,38 +152,37 @@ collect_pipe <- function(x) .Call(rnng_aio_collect_pipe, x)
152152
#' Close Connection
153153
#'
154154
#' Close Connection on a Socket, Context, Dialer, Listener, Stream, Pipe, or
155-
#' ncurl Session.
155+
#' ncurl Session.
156156
#'
157-
#' @param con a Socket, Context, Dialer, Listener, Stream, Pipe, or
158-
#' \sQuote{ncurlSession}.
159-
#' @param ... not used.
157+
#' Closing an object explicitly frees its resources. An object can also be
158+
#' removed directly in which case its resources are freed when the object is
159+
#' garbage collected.
160160
#'
161-
#' @return Invisibly, an integer exit code (zero on success).
161+
#' Closing a Socket associated with a Context also closes the Context.
162162
#'
163-
#' @details Closing an object explicitly frees its resources. An object can also
164-
#' be removed directly in which case its resources are freed when the object
165-
#' is garbage collected.
163+
#' Dialers and Listeners are implicitly closed when the Socket they are
164+
#' associated with is closed.
166165
#'
167-
#' Closing a Socket associated with a Context also closes the Context.
166+
#' Closing a Socket or a Context: messages that have been submitted for sending
167+
#' may be flushed or delivered, depending upon the transport. Closing the Socket
168+
#' while data is in transmission will likely lead to loss of that data. There is
169+
#' no automatic linger or flush to ensure that the Socket send buffers have
170+
#' completely transmitted.
168171
#'
169-
#' Dialers and Listeners are implicitly closed when the Socket they are
170-
#' associated with is closed.
172+
#' Closing a Stream: if any send or receive operations are pending, they will be
173+
#' terminated and any new operations will fail after the connection is closed.
171174
#'
172-
#' Closing a Socket or a Context: messages that have been submitted for
173-
#' sending may be flushed or delivered, depending upon the transport. Closing
174-
#' the Socket while data is in transmission will likely lead to loss of that
175-
#' data. There is no automatic linger or flush to ensure that the Socket
176-
#' send buffers have completely transmitted.
175+
#' Closing an \sQuote{ncurlSession} closes the http(s) connection.
177176
#'
178-
#' Closing a Stream: if any send or receive operations are pending, they
179-
#' will be terminated and any new operations will fail after the connection
180-
#' is closed.
177+
#' As Pipes are owned by the corresponding Socket, removing (and garbage
178+
#' collecting) a Pipe does not close it or free its resources. A Pipe may,
179+
#' however, be explicitly closed.
181180
#'
182-
#' Closing an \sQuote{ncurlSession} closes the http(s) connection.
181+
#' @param con a Socket, Context, Dialer, Listener, Stream, Pipe, or
182+
#' \sQuote{ncurlSession}.
183+
#' @param ... not used.
183184
#'
184-
#' As Pipes are owned by the corresponding Socket, removing (and garbage
185-
#' collecting) a Pipe does not close it or free its resources. A Pipe may,
186-
#' however, be explicitly closed.
185+
#' @return Invisibly, an integer exit code (zero on success).
187186
#'
188187
#' @seealso \code{\link{reap}}
189188
#'
@@ -207,16 +206,16 @@ close.nanoPipe <- function(con, ...) invisible(.Call(rnng_pipe_close, con))
207206
#' Reap
208207
#'
209208
#' An alternative to \code{close} for Sockets, Contexts, Listeners, Dialers and
210-
#' Pipes avoiding S3 method dispatch.
209+
#' Pipes avoiding S3 method dispatch.
210+
#'
211+
#' May be used on unclassed external pointers e.g. those created by
212+
#' \code{\link{.context}}. Returns silently and does not warn or error, nor does
213+
#' it update the state of object attributes.
211214
#'
212215
#' @param con a Socket, Context, Listener, Dialer or Pipe.
213216
#'
214217
#' @return An integer exit code (zero on success).
215218
#'
216-
#' @details May be used on unclassed external pointers e.g. those created by
217-
#' \code{\link{.context}}. Returns silently and does not warn or error, nor
218-
#' does it update the state of object attributes.
219-
#'
220219
#' @seealso \code{\link{close}}
221220
#'
222221
#' @examples

0 commit comments

Comments
 (0)