LPC : Local Procedure Call
This Lpc
is a Local procedure call module. This module provides very similar functionality to RPC, has the following advantages over RPC:
- Only in-process procedure calls are supported, the call efficiency is higher than RPC.
- Less resource overhead, no kernel memory used.
- No message size limit.
User can use the following code to import the Lpc
module.
var Lpc = require("lpc");
Support
The following shows Lpc
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
Lpc.Server | ● | ● |
server.close | ● | ● |
server.reply | ● | ● |
server.reverse | ● | ● |
Lpc.Client | ● | ● |
client.close | ● | ● |
client.call | ● | ● |
client.callSync | ● | ● |
client.fetch | ● | ● |
Lpc Server Class
new Lpc.Server(name[, errCallback])
name
{String} LPC service name.errCallback
{Function} LPC service I/O error callback. default: undefined.- Returns: {Object} LPC server object.
Create an LPC server.
Example
var server = new Lpc.Server("LocalServer_1");
Lpc Server Object
The LPC server object inherits from EventEmitter
, and the on()
method can be used to add processing callbacks for different services.
server.close()
Close this LPC server. this LPC server object is no longer allowed to be used after closed.
Example
var server = new Lpc.Server("LocalServer_1");
server.close();
server.reply(msg, to, seq)
msg
{Object} Reply object.to
{Object} Reply target.seq
{Integer} Client command sequence number, must be the same as the client request message sequence number.- Returns: {Boolean} Whether to reply message send successfully.
to
object must includes:
id
{Integer} Client task id.
Use the emitterfrom
parameter directly.
Send a server reply to the specified client.
Example
var server = new Lpc.Server("LocalServer_1");
server.on("command_echo", (msg, from, seq) => {
console.log("receive command_echo", msg, from);
server.reply({ res: "ok", msg }, from, seq);
});
server.on("command_sth", (msg, from, seq) => {
console.log("receive command_sth", msg, from);
server.reply({ res: "ok" }, from, seq);
});
server.reverse(event, msg, to[, timeout])
event
{String} Client event that you want to trigger.msg
{Object} Reverse message object.to
{Object} Reverse target.- Returns: {Boolean} Whether to reply message send successfully.
to
object must includes:
id
{Integer} Client task id.
Use thefrom
parameter directly.
Send a server asynchronous reverse message to the specified client.
Lpc Server Events
The LPC server object inherits from EventEmitter
, and the on()
method can be used to add processing callbacks for different services.
Client Command
msg
{Object} Client message.from
{Object} Client information.seq
{Integer} Client command sequence number.
from
object includes:
id
{Integer} Client task id.
Example
server.on("command_echo", (msg, from, seq) => {
console.log("receive command_echo", msg, from);
server.reply({ res: "ok", msg }, from, seq);
});
Lpc Client Class
new Lpc.Client(name[, errCallback])
name
{String} LPC service name.errCallback
{Function} LPC client I/O error callback. default: undefined.- Returns: {Object} LPC client object.
Create an LPC client.
Example
var client = new Lpc.Client("LocalServer_1");
Lpc Client Object
The LPC client object inherits from EventEmitter
, and the on()
method can be used to listen server reverse events.
client.close()
Close the LPC client, this LPC client object is no longer allowed to be used after closed.
client.call(event, msg[, callback[, timeout]])
event
{String} Server event that you want to trigger.msg
{Object} Call command message.callback
{Function} Server reply callback function.reply
{Object} Server reply message.undefined
indicates that the server response was not received within the specified timeout.
timeout
{Integer} Wait timeout in milliseconds. default: 60000.- Returns: {Boolean} Whether to reply message send successfully.
Send a call request to the server, this function immediately returns after the request is successfully sent. The callback
function is called until the server responds or times out.
Example
var client = new Lpc.Client("LocalServer_1");
client.call("command_echo", { foo: "foo" }, (reply) => {
console.log("Server reply:", reply);
});
client.callSync(event, msg[, timeout])
event
{String} Server event that you want to trigger.msg
{Object} Call command message.timeout
{Integer} Wait timeout in milliseconds. default: 60000.- Returns: {Object} Server response message.
Send a call request to the server and wait for server reply. If there is no response within the time specified by timeout
, return undefined
.
Example
var client = new Lpc.Client("LocalServer_1");
var reply = client.callSync("command_echo", { foo: "foo" });
if (reply) {
console.log("Server reply:", reply.msg);
}
client.fetch(event, msg[, timeout])
event
{String} Server event that you want to trigger.msg
{Object} Call command message.timeout
{Integer} Wait timeout in milliseconds. default: 60000.- Returns: {Promise} Promise object.
Send a call request to the server, this function is asynchronous requests and return a Promise
object.
Example
var client = new Lpc.Client("LocalServer_1");
async function echo() {
var reply = await client.fetch("command_echo", { foo: "bar" });
console.log(reply.msg);
}
Example
Here is a typical example of LPC.
- Server
var Lpc = require("lpc");
var iosched = require("iosched");
var server = new Lpc.Server("RemoteProxy");
server.on("dev_on", (msg, from, seq) => {
dev.setOn(msg.dev);
server.reply({ res: "ok" }, from, seq);
});
server.on("dev_off", (msg, from, seq) => {
dev.setOff(msg.dev);
server.reply({ res: "ok" }, from, seq);
});
iosched.forever();
- Client
var Lpc = require("lpc");
var iosched = require("iosched");
var client = new Lpc.Client("RemoteProxy");
var state = 0;
setInterval(function() {
if (state) {
state = 0;
client.call("dev_off", { dev: 0 }, (reply) => {
console.log("dev_off:", reply);
});
} else {
state = 1;
client.call("dev_on", { dev: 0 }, (reply) => {
console.log("dev_on:", reply);
});
}
}, 1000);
iosched.forever();