mirror of
https://github.com/EvernodeXRPL/hpcore.git
synced 2026-04-29 15:37:59 +00:00
Replaced contract I/O pipes with domain sockets. (#140)
This commit is contained in:
committed by
GitHub
parent
37cc27c5ce
commit
7a4515865d
@@ -9,16 +9,16 @@ const hpc = new HotPocketContract();
|
||||
if (!hpc.readonly)
|
||||
fs.appendFileSync("exects.txt", "ts:" + hpc.timestamp + "\n");
|
||||
|
||||
Object.keys(hpc.users).forEach(async (key) => {
|
||||
|
||||
const user = hpc.users[key];
|
||||
const inputBuf = await user.readInput();
|
||||
if (inputBuf) {
|
||||
const userInput = inputBuf.toString("utf8");
|
||||
if (userInput == "ts")
|
||||
user.sendOutput(fs.readFileSync("exects.txt"));
|
||||
else
|
||||
user.sendOutput("Echoing: " + userInput);
|
||||
hpc.events.on("user_message", (pubKey, message) => {
|
||||
const userInput = message.toString("utf8");
|
||||
const user = hpc.users[pubKey];
|
||||
if (userInput == "ts") {
|
||||
user.sendOutput(fs.readFileSync("exects.txt"));
|
||||
user.closeChannel();
|
||||
}
|
||||
else {
|
||||
user.sendOutput("Echoing: " + userInput);
|
||||
user.closeChannel();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -30,6 +30,19 @@ if (npl) {
|
||||
npl.closeNplChannel();
|
||||
}
|
||||
|
||||
// HP <--> SC
|
||||
const hp = hpc.control;
|
||||
hp.closeControlChannel();
|
||||
|
||||
// let i = 0;
|
||||
// hp.events.on('message', (msg) => {
|
||||
// console.log('control msg - ' + msg);
|
||||
// hp.sendOutput(msg);
|
||||
// i++;
|
||||
// if (i == 2)
|
||||
// hp.closeControlChannel();
|
||||
// })
|
||||
|
||||
// Npl message sending and receiving template.
|
||||
// if (npl) {
|
||||
// let i = 0;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const fs = require('fs');
|
||||
const events = require('events');
|
||||
|
||||
MAX_NPL_BUF_SIZE = 128*1024;
|
||||
const MAX_SEQ_PACKET_SIZE = 128 * 1024;
|
||||
|
||||
function HotPocketContract() {
|
||||
const hpargs = JSON.parse(fs.readFileSync(0, 'utf8'));
|
||||
@@ -18,53 +18,62 @@ function HotPocketContract() {
|
||||
this.npl = new HotPocketNplChannel(hpargs.nplfd);
|
||||
}
|
||||
|
||||
this.control = new HotPocketControlChannel(hpargs.hpfd);
|
||||
this.events = new events.EventEmitter();
|
||||
|
||||
this.users = {};
|
||||
Object.keys(hpargs.usrfd).forEach((userPubKey) => {
|
||||
const userfds = hpargs.usrfd[userPubKey];
|
||||
this.users[userPubKey] = new HotPocketChannel(userfds[0], userfds[1]);
|
||||
this.users[userPubKey] = new HotPocketChannel(hpargs.usrfd[userPubKey], userPubKey, this.events);
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to asynchronously read a stream to the end and fill a buffer.
|
||||
const drainStream = function (stream) {
|
||||
|
||||
return new Promise((resolve) => {
|
||||
|
||||
function HotPocketChannel(fd, userPubKey, events) {
|
||||
let socket = null;
|
||||
if (fd > 0) {
|
||||
socket = fs.createReadStream(null, { fd: fd });
|
||||
const dataParts = [];
|
||||
|
||||
const resolveBuffer = function () {
|
||||
if (dataParts.length > 0)
|
||||
return resolve(Buffer.concat(dataParts));
|
||||
else
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
stream.on("data", d => {
|
||||
dataParts.push(d);
|
||||
});
|
||||
stream.on('end', resolveBuffer);
|
||||
stream.on("close", resolveBuffer);
|
||||
stream.on("error", () => {
|
||||
resolve(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function HotPocketChannel(infd, outfd) {
|
||||
this.readInput = function () {
|
||||
return new Promise((resolve) => {
|
||||
if (infd == -1) {
|
||||
resolve(null);
|
||||
let msgLen = -1;
|
||||
let bytesRead = 0;
|
||||
socket.on("data", (buf) => {
|
||||
if (msgLen == -1) {
|
||||
// First two bytes indicate the message len.
|
||||
const msgLenBuf = readBytes(buf, 0, 4);
|
||||
if (msgLenBuf) {
|
||||
msgLen = msgLenBuf.readUInt32BE();
|
||||
const msgBuf = readBytes(buf, 4, buf.byteLength - 4);
|
||||
dataParts.push(msgBuf)
|
||||
bytesRead = msgBuf.byteLength;
|
||||
}
|
||||
} else {
|
||||
dataParts.push(buf);
|
||||
bytesRead += buf.length;
|
||||
}
|
||||
else {
|
||||
const s = fs.createReadStream(null, { fd: infd });
|
||||
drainStream(s).then(buf => resolve(buf));
|
||||
if (bytesRead == msgLen) {
|
||||
msgLen == -1;
|
||||
events.emit("user_message", userPubKey, Buffer.concat(dataParts));
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("error", (e) => {
|
||||
events.emit("user_error", userPubKey, e);
|
||||
})
|
||||
}
|
||||
|
||||
// Read bytes from the given buffer.
|
||||
const readBytes = function (buf, pos, count) {
|
||||
if (pos + count > buf.byteLength)
|
||||
return null;
|
||||
return buf.slice(pos, pos + count);
|
||||
}
|
||||
|
||||
this.sendOutput = function (output) {
|
||||
fs.writeFileSync(outfd, output);
|
||||
fs.writeSync(fd, output);
|
||||
}
|
||||
|
||||
this.closeChannel = function () {
|
||||
if (fd > 0) {
|
||||
socket.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +87,7 @@ function HotPocketNplChannel(fd) {
|
||||
// From the hotpocket when sending the npl messages first it sends the pubkey of the particular node
|
||||
// and then the message, First data buffer is taken as pubkey and the second one as message,
|
||||
// then npl message object is constructed and the event is emmited.
|
||||
socket = fs.createReadStream(null, { fd: fd, highWaterMark: MAX_NPL_BUF_SIZE});
|
||||
socket = fs.createReadStream(null, { fd: fd, highWaterMark: MAX_SEQ_PACKET_SIZE });
|
||||
socket.on("data", d => {
|
||||
if (!isPubKeyReceived) {
|
||||
pubKey = d.toString('hex');
|
||||
@@ -111,6 +120,34 @@ function HotPocketNplChannel(fd) {
|
||||
}
|
||||
}
|
||||
|
||||
function HotPocketControlChannel(fd) {
|
||||
|
||||
this.events = new events.EventEmitter();
|
||||
let socket = null;
|
||||
if (fd > 0) {
|
||||
socket = fs.createReadStream(null, { fd: fd, highWaterMark: MAX_SEQ_PACKET_SIZE });
|
||||
socket.on("data", d => {
|
||||
this.events.emit("message", d);
|
||||
});
|
||||
|
||||
socket.on("error", (e) => {
|
||||
this.events.emit("error", e);
|
||||
});
|
||||
}
|
||||
|
||||
this.sendOutput = (output) => {
|
||||
if (fd > 0) {
|
||||
fs.writeSync(fd, output);
|
||||
}
|
||||
}
|
||||
|
||||
this.closeControlChannel = () => {
|
||||
if (fd > 0) {
|
||||
socket.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
HotPocketContract
|
||||
}
|
||||
Reference in New Issue
Block a user