2022-09-27 08:46:27 +00:00
|
|
|
// pipe-ipc.js
|
|
|
|
|
|
|
|
var STD = require("lib/std");
|
|
|
|
|
|
|
|
// https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/opentextfile-method
|
|
|
|
var ForReading = 1;
|
|
|
|
var ForWriting = 2;
|
|
|
|
var ForAppending = 8;
|
|
|
|
|
|
|
|
var TristateUseDefault = -2;
|
|
|
|
var TristateTrue = -1;
|
|
|
|
var TristateFalse = 0;
|
|
|
|
|
|
|
|
function PipeIPC() {
|
|
|
|
this.path = "\\.\pipe\:path";
|
|
|
|
this.reader = null;
|
|
|
|
this.writer = null;
|
|
|
|
|
|
|
|
this.connect = function(path, callback) {
|
|
|
|
this.path = this.path.replace(":path", path);
|
|
|
|
//this.createWriter();
|
|
|
|
this.createReader();
|
|
|
|
if (typeof callback === "function") {
|
|
|
|
callback(this, this.reader, this.writer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createWriter = function() {
|
|
|
|
//this.writer = CreateObject("Scripting.FileSystemObject").CreateTextFile(this.path, true, true);
|
|
|
|
this.writer = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, ForWriting, true, TristateTrue);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.closeWriter = function() {
|
|
|
|
this.writer.Close();
|
|
|
|
this.writer = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createReader = function() {
|
|
|
|
this.reader = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, ForReading, true, TristateTrue);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.closeReader = function() {
|
|
|
|
this.reader.Close();
|
|
|
|
this.reader = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.write = function(message) {
|
2022-09-27 08:48:00 +00:00
|
|
|
var isWritten = false;
|
2022-09-27 08:46:27 +00:00
|
|
|
|
2022-09-27 08:48:00 +00:00
|
|
|
while (!isWritten) {
|
2022-09-27 08:46:27 +00:00
|
|
|
try {
|
|
|
|
this.createWriter();
|
|
|
|
this.writer.Write(message);
|
|
|
|
sleep(1);
|
|
|
|
this.writer.Close();
|
2022-09-27 08:48:00 +00:00
|
|
|
isWritten = true;
|
2022-09-27 08:46:27 +00:00
|
|
|
} catch (e) {
|
2022-09-27 08:48:00 +00:00
|
|
|
isWritten = false;
|
2022-09-27 08:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.read = function() {
|
|
|
|
var isRead = false;
|
|
|
|
var text = "";
|
|
|
|
|
|
|
|
while (!isRead) {
|
|
|
|
try {
|
|
|
|
text = this.reader.ReadAll();
|
|
|
|
isRead = true;
|
|
|
|
} catch (e) {
|
|
|
|
this.closeReader();
|
|
|
|
this.createReader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2022-09-27 09:31:45 +00:00
|
|
|
this.readContinuously = function(callback) {
|
|
|
|
var isNext = true;
|
|
|
|
|
|
|
|
if (typeof callback === "function") {
|
|
|
|
while (isNext) {
|
|
|
|
isNext = callback(this, this.read());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-27 08:46:27 +00:00
|
|
|
this.close = function() {
|
|
|
|
this.closeWriter();
|
|
|
|
this.closeReader();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.create = function() {
|
|
|
|
return new PipeIPC();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1";
|
|
|
|
exports.AUTHOR = "Nathan Catswords <catswords@protonmail.com>";
|
|
|
|
exports.global = global;
|
|
|
|
exports.require = require;
|