Skip to content

Commit

Permalink
Use obj as parameter to module ClosePipe
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusd committed Oct 1, 2018
1 parent 5d098f5 commit 7f61105
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/main_dev/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const closeDCRW = () => {
} else if (require("is-running")(dcrwPID)) {
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
try {
win32ipc.closePipe(dcrwPipeRx.readEnd, dcrwPipeRx.writeEnd);
win32ipc.closePipe(dcrwPipeRx);
} catch (e) {
logger.log("error", "Error closing dcrwallet piperx: " + e);
}
Expand Down
33 changes: 29 additions & 4 deletions app/modules/win32ipc/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,42 @@ void CreatePipe(v8::FunctionCallbackInfo<v8::Value> const& args) {
void ClosePipe(v8::FunctionCallbackInfo<v8::Value> const& args) {
auto isolate = v8::Isolate::GetCurrent();

if (args.Length() != 2) {
if (args.Length() != 1) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}

uintptr_t read_end_handle = (uintptr_t) args[0]->Int32Value();
uintptr_t write_end_handle = (uintptr_t) args[1]->Int32Value();
if (!args[0]->IsObject()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument type error")));
return;
}

auto obj = args[0]->ToObject();
auto read_end_prop = v8::String::NewFromUtf8(isolate, "readEnd");
auto write_end_prop = v8::String::NewFromUtf8(isolate, "writeEnd");
auto context = isolate->GetCurrentContext();

if (!obj->Has(context, read_end_prop).ToChecked()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Object does not have readEnd prop")));
return;
}

if (!obj->Has(context, write_end_prop).ToChecked()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Object does not have writeEnd prop")));
return;
}

uintptr_t read_end_handle = (uintptr_t) obj->Get(context, read_end_prop)
.ToLocalChecked()->Int32Value(context).ToChecked();
uintptr_t write_end_handle = (uintptr_t) obj->Get(context, write_end_prop)
.ToLocalChecked()->Int32Value(context).ToChecked();
char const *close_error_msg = pipe_wrapper::close_pipe_end(read_end_handle, write_end_handle);

if (close_result.err_msg != nullptr) {
if (close_error_msg != nullptr) {
isolate->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8(isolate, close_error_msg)));
return;
Expand Down
40 changes: 40 additions & 0 deletions app/modules/win32ipc/moduleTestWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Sample script to test the namedpiperx option of dcrwallet in windows.
// Requires that a wallet config exists in the default decrediton config dir,
// with a wallet name "default-wallet".

const childProcess = require("child_process");
const addon = require("./build/Release/win32ipc");
const path = require("path");
const os = require("os");

//const pipeFname = "\\\\.\\pipe\\dcrwallet-test";
const walletConfPath = path.join(os.homedir(), "AppData", "Local", "Decrediton",
"wallets", "testnet", "default-wallet", "dcrwallet.conf");

function sleep(milli) {
return new Promise(resolve => setTimeout(resolve, milli));
}

async function test() {
try {
const pipe = addon.createPipe("out");
childProcess.spawn("dcrwallet", [
`-C ${walletConfPath}`,
`--piperx ${pipe.readEnd}`, "--debuglevel DCRW=TRACE"
], { "detached": true, "shell": true });

console.log(pipe);
await sleep(7000);
console.log("Slept to test some. Will try to close the pipe.");
console.log(addon.closePipe(pipe));
console.log("Closed the pipe!");
} catch (error) {
console.log("Error");
console.log(error);
return;
}
}

setTimeout(test, 3000);

setTimeout(function () { process.exit(0); }, 15000);
2 changes: 1 addition & 1 deletion app/modules/win32ipc/pipe_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ result<pipe> create_pipe(pipe_direction direction) {
return result<pipe>::error(err_msg);
}

result<int> close_pipe_end(uintptr_t read_end_handle, uintptr_t write_end_handle) {
char const* close_pipe_end(uintptr_t read_end_handle, uintptr_t write_end_handle) {
if (!CloseHandle((HANDLE) read_end_handle)) {
return "Close(read_end_handle)";
}
Expand Down

0 comments on commit 7f61105

Please sign in to comment.