| 1 | #' Send Receive Functions |
| 2 | #' |
| 3 | #' Send and receive functions |
| 4 | #' |
| 5 | #' \code{zmq.send()} is a high level R function calling ZMQ C API |
| 6 | #' \code{zmq_send()} sending \code{buf} out. |
| 7 | #' |
| 8 | #' \code{zmq.recv()} is a high level R function calling ZMQ C API |
| 9 | #' \code{zmq_recv()} receiving buffers of length \code{len} according to the |
| 10 | #' \code{buf.type}. |
| 11 | #' |
| 12 | #' \code{flags} see \code{\link{ZMQ.SR}()} for detail options of send and |
| 13 | #' receive functions. |
| 14 | #' |
| 15 | #' \code{buf.type} currently supports \code{char} and \code{raw} which are both |
| 16 | #' in R object format. |
| 17 | #' |
| 18 | #' @param socket |
| 19 | #' a ZMQ socket |
| 20 | #' @param buf |
| 21 | #' a buffer to be sent |
| 22 | #' @param len |
| 23 | #' a length of buffer to be received, default 1024 bytes |
| 24 | #' @param flags |
| 25 | #' a flag for the method using by \code{zmq_send} and |
| 26 | #' \code{zmq_recv} |
| 27 | #' @param buf.type |
| 28 | #' buffer type to be received |
| 29 | #' |
| 30 | #' @return \code{zmq.send()} returns number of bytes (invisible) in the sent |
| 31 | #' message if successful, otherwise returns -1 (invisible) and sets |
| 32 | #' \code{errno} to the error value, see ZeroMQ manual for details. |
| 33 | #' |
| 34 | #' \code{zmq.recv()} returns a list (\code{ret}) containing the received buffer |
| 35 | #' \code{ret$buf} and the length of received buffer (\code{ret$len} which is |
| 36 | #' less or equal to the input \code{len}) if successful, otherwise returns -1 |
| 37 | #' and sets \code{errno} to the error value, see ZeroMQ manual for details. |
| 38 | #' |
| 39 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 40 | #' |
| 41 | #' @references ZeroMQ/4.1.0 API Reference: |
| 42 | #' \url{http://api.zeromq.org/4-1:_start} |
| 43 | #' |
| 44 | #' Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 45 | #' |
| 46 | #' @examples |
| 47 | #' \dontrun{ |
| 48 | #' ### Using request-reply pattern. |
| 49 | #' |
| 50 | #' ### At the server, run next in background or the other window. |
| 51 | #' library(pbdZMQ, quietly = TRUE) |
| 52 | #' |
| 53 | #' context <- zmq.ctx.new() |
| 54 | #' responder <- zmq.socket(context, ZMQ.ST()$REP) |
| 55 | #' zmq.bind(responder, "tcp://*:5555") |
| 56 | #' for(i.res in 1:5){ |
| 57 | #' buf <- zmq.recv(responder, 10L) |
| 58 | #' cat(buf$buf, "\n") |
| 59 | #' Sys.sleep(0.5) |
| 60 | #' zmq.send(responder, "World") |
| 61 | #' } |
| 62 | #' zmq.close(responder) |
| 63 | #' zmq.ctx.destroy(context) |
| 64 | #' |
| 65 | #' |
| 66 | #' ### At a client, run next in foreground. |
| 67 | #' library(pbdZMQ, quietly = TRUE) |
| 68 | #' |
| 69 | #' context <- zmq.ctx.new() |
| 70 | #' requester <- zmq.socket(context, ZMQ.ST()$REQ) |
| 71 | #' zmq.connect(requester, "tcp://localhost:5555") |
| 72 | #' for(i.req in 1:5){ |
| 73 | #' cat("Sending Hello ", i.req, "\n") |
| 74 | #' zmq.send(requester, "Hello") |
| 75 | #' buf <- zmq.recv(requester, 10L) |
| 76 | #' cat("Received World ", i.req, "\n") |
| 77 | #' } |
| 78 | #' zmq.close(requester) |
| 79 | #' zmq.ctx.destroy(context) |
| 80 | #' } |
| 81 | #' |
| 82 | #' @keywords programming |
| 83 | #' @seealso \code{\link{zmq.msg.send}()}, \code{\link{zmq.msg.recv}()}. |
| 84 | #' @rdname b0_sendrecv |
| 85 | #' @name Send Receive Functions |
| 86 | NULL |
| 87 | |
| 88 | |
| 89 | |
| 90 | #' @rdname b0_sendrecv |
| 91 | #' @export |
| 92 | zmq.send <- function(socket, buf, flags = ZMQ.SR()$BLOCK){ |
| 93 | if(is.character(buf)){ |
| 94 | ret <- zmq.send.char(socket, buf, nchar(buf), flags = flags) |
| 95 | } else if(is.raw(buf)){ |
| 96 | ret <- zmq.send.raw(socket, buf, length(buf), flags = flags) |
| 97 | } else{ |
| 98 | stop("buf type should be char or raw.") |
| 99 | } |
| 100 | invisible(ret) |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | zmq.send.char <- function(socket, buf, len, flags = ZMQ.SR()$BLOCK){ |
| 106 | ret <- .Call("R_zmq_send_char", socket, buf, as.integer(len), |
| 107 | as.integer(flags), PACKAGE = "pbdZMQ") |
| 108 | invisible(ret) |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | zmq.send.raw <- function(socket, buf, len, flags = ZMQ.SR()$BLOCK){ |
| 114 | ret <- .Call("R_zmq_send_raw", socket, buf, as.integer(len), |
| 115 | as.integer(flags), PACKAGE = "pbdZMQ") |
| 116 | invisible(ret) |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | #' @rdname b0_sendrecv |
| 122 | #' @export |
| 123 | zmq.recv <- function(socket, len = 1024L, flags = ZMQ.SR()$BLOCK, |
| 124 | buf.type = c("char", "raw")){ |
| 125 | if(buf.type[1] == "char"){ |
| 126 | ret <- zmq.recv.char(socket, len, flags = flags) |
| 127 | } else if(buf.type[1] == "raw"){ |
| 128 | ret <- zmq.recv.raw(socket, len, flags = flags) |
| 129 | } else{ |
| 130 | stop("buf type should be char or raw.") |
| 131 | } |
| 132 | invisible(ret) |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | zmq.recv.char <- function(socket, len, flags = ZMQ.SR()$BLOCK){ |
| 138 | ret <- .Call("R_zmq_recv_char", socket, as.integer(len), as.integer(flags), |
| 139 | PACKAGE = "pbdZMQ") |
| 140 | invisible(ret) |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | zmq.recv.raw <- function(socket, len, flags = ZMQ.SR()$BLOCK){ |
| 146 | ret <- .Call("R_zmq_recv_raw", socket, as.integer(len), as.integer(flags), |
| 147 | PACKAGE = "pbdZMQ") |
| 148 | invisible(ret) |
| 149 | } |
| 150 |