Supports calling Erlang functions on remote nodes. ei_rpc_to() sends an RPC request to a remote node and ei_rpc_from() receives the results of such a call. ei_rpc() combines the functionality of these two functions by sending an RPC request and waiting for the results.
The ei_xrpc_to() function is equivalent to ei_rpc_to() when its flags parameter is set to 0. When the flags parameter of ei_xrpc_to() is set to EI_RPC_FETCH_STDOUT, stdout (standard output) data are forwarded. See the documentation for the flags parameter for more information about the EI_RPC_FETCH_STDOUT flag.
rpc:call/4 in Kernel.
-
ec is the C-node structure previously initiated by a call to ei_connect_init() or ei_connect_xinit().
-
fd is an open descriptor to an Erlang connection.
-
timeout is the maximum time (in milliseconds) to wait for results. Specify ERL_NO_TIMEOUT to wait forever. ei_rpc() waits infinitely for the answer, that is, the call will never time out.
-
mod is the name of the module containing the function to be run on the remote node.
-
fun is the name of the function to run.
-
argbuf is a pointer to a buffer with an encoded Erlang list, without a version magic number, containing the arguments to be passed to the function.
-
argbuflen is the length of the buffer containing the encoded Erlang list.
-
msg is structure of type erlang_msg and contains information on the message received. For a description of the erlang_msg format, see ei_receive_msg.
-
x points to the dynamic buffer that receives the result. For ei_rpc() this is the result without the version magic number. For an ei_rpc_from() call the result consists of a version magic number and a 2-tuple. The 2-tuple can be in one of the following two forms:
{rex,Reply} - This response value means that the RPC has completed. The result value is the
Reply term. This is the only type of response that one can get from an RPC triggered by a call to ei_rpc_to() or ei_xrpc_to() without the EI_RPC_FETCH_STDOUT flag. If the RPC was triggered by a call to ei_xrpc_to() with the EI_RPC_FETCH_STDOUT flag set, then all forwarded stdout data has been received. {rex_stdout,StdOutUTF8Binary} - This response value can only be obtained if the RPC call was triggered by a call to
ei_xrpc_to() with the EI_RPC_FETCH_STDOUT flag set. This response value means that forwarded stdout data has been received. The stdout data is stored in a binary and is UTF-8 encoded. One may need to call ei_rpc_from() multiple times to read all the stdout data. The stdout data is received in the same order as it was written. All forwarded stdout data have been received when a {rex,Reply} tuple has been obtained from an ei_rpc_from() call.
-
flags The flag EI_RPC_FETCH_STDOUT is currently the only flag that is supported by ei_xrpc_to(). When EI_RPC_FETCH_STDOUT is set, the called function is executed in a new process with a group leader that forwards all stdout data. This means that stdout data that are written during the execution of the called function, by the called function and by descendant processes, will be forwarded (given that the group leader has not been changed by a call to erlang:group_leader/2). The forwarded stdout data need to be collected by a sequence of calls to ei_rpc_from(). See the description of the x parameter for how ei_rpc_from() is used to receive stdout data. See the documentation of the the I/O protocol, for more information about the group leader concept.
Note
The flag EI_RPC_FETCH_STDOUT only works when interacting with a node with a version greater or equal to OTP-24.
ei_rpc() returns the number of bytes in the result on success and -1 on failure. ei_rpc_from() returns the number of bytes, otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. The functions ei_rpc_to() and ei_xrpc_to() returns 0 if successful, otherwise -1. When failing, all four functions set erl_errno to one of the following:
EIO - I/O error.
ETIMEDOUT - Time-out expired.
EAGAIN - Temporary error: Try again.
Example:
Check to see if an Erlang process is alive:
int index = 0, is_alive;
ei_x_buff args, result;
ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);
if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
args.buff, args.index, &result) < 0)
handle_error();
if (ei_decode_version(result.buff, &index) < 0
|| ei_decode_bool(result.buff, &index, &is_alive) < 0)
handle_error();