Debian Codegraph @

man/reply.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/context.R
3 \name{reply}
4 \alias{reply}
5 \title{Reply over Context (RPC Server for Req/Rep Protocol)}
6 \usage{
7 reply(
8 context,
9 execute,
10 recv_mode = c("serial", "character", "complex", "double", "integer", "logical",
11 "numeric", "raw", "string"),
12 send_mode = c("serial", "raw"),
13 timeout = NULL,
14 ...
15 )
16 }
17 \arguments{
18 \item{context}{a Context.}
19
20 \item{execute}{a function which takes the received (converted) data as its
21 first argument. Can be an anonymous function of the form
22 \code{function(x) do(x)}. Additional arguments can also be passed in through
23 \code{...}.}
24
25 \item{recv_mode}{[default 'serial'] character value or integer equivalent -
26 one of \code{"serial"} (1L), \code{"character"} (2L), \code{"complex"} (3L), \code{"double"}
27 (4L), \code{"integer"} (5L), \code{"logical"} (6L), \code{"numeric"} (7L), \code{"raw"} (8L),
28 or \code{"string"} (9L). The default \code{"serial"} means a serialised R object; for
29 the other modes, received bytes are converted into the respective mode.
30 \code{"string"} is a faster option for length one character vectors.}
31
32 \item{send_mode}{[default 'serial'] character value or integer equivalent -
33 either \code{"serial"} (1L) to send serialised R objects, or \code{"raw"} (2L) to
34 send atomic vectors of any type as a raw byte vector.}
35
36 \item{timeout}{[default NULL] integer value in milliseconds or NULL, which
37 applies a socket-specific default, usually the same as no timeout. Note
38 that this applies to receiving the request. The total elapsed time would
39 also include performing 'execute' on the received data. The timeout then
40 also applies to sending the result (in the event that the requestor has
41 become unavailable since sending the request).}
42
43 \item{...}{additional arguments passed to the function specified by
44 'execute'.}
45 }
46 \value{
47 Integer exit code (zero on success).
48 }
49 \description{
50 Implements an executor/server for the rep node of the req/rep protocol.
51 Awaits data, applies an arbitrary specified function, and returns the result
52 to the caller/client.
53 }
54 \details{
55 Receive will block while awaiting a message to arrive and is usually the
56 desired behaviour. Set a timeout to allow the function to return if no data
57 is forthcoming.
58
59 In the event of an error in either processing the messages or in evaluation
60 of the function with respect to the data, a nul byte \code{00} (or serialized
61 nul byte) will be sent in reply to the client to signal an error. This is to
62 be distinguishable from a possible return value. \code{\link[=is_nul_byte]{is_nul_byte()}} can be used
63 to test for a nul byte.
64 }
65 \section{Send Modes}{
66
67
68 The default mode \code{"serial"} sends serialised R objects to ensure perfect
69 reproducibility within R. When receiving, the corresponding mode \code{"serial"}
70 should be used. Custom serialization and unserialization functions for
71 reference objects may be enabled by the function \code{\link[=serial_config]{serial_config()}}.
72
73 Mode \code{"raw"} sends atomic vectors of any type as a raw byte vector, and must
74 be used when interfacing with external applications or raw system sockets,
75 where R serialization is not in use. When receiving, the mode corresponding
76 to the vector sent should be used.
77 }
78
79 \examples{
80 req <- socket("req", listen = "inproc://req-example")
81 rep <- socket("rep", dial = "inproc://req-example")
82
83 ctxq <- context(req)
84 ctxp <- context(rep)
85
86 send(ctxq, 2022, block = 100)
87 reply(ctxp, execute = function(x) x + 1, send_mode = "raw", timeout = 100)
88 recv(ctxq, mode = "double", block = 100)
89
90 send(ctxq, 100, mode = "raw", block = 100)
91 reply(ctxp, recv_mode = "double", execute = log, base = 10, timeout = 100)
92 recv(ctxq, block = 100)
93
94 close(req)
95 close(rep)
96
97 }