Skip to content

Commit

Permalink
close pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusd committed Oct 1, 2018
1 parent 3dc425f commit 5d098f5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
12 changes: 10 additions & 2 deletions app/main_dev/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const logger = createLogger(debug);

let dcrdPID;
let dcrwPID;
let dcrwPipeRx;

let dcrwPort;

Expand All @@ -38,6 +39,13 @@ export const closeDCRW = () => {
if (require("is-running")(dcrwPID) && os.platform() != "win32") {
logger.log("info", "Sending SIGINT to dcrwallet at pid:" + dcrwPID);
process.kill(dcrwPID, "SIGINT");
} else if (require("is-running")(dcrwPID)) {
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
try {
win32ipc.closePipe(dcrwPipeRx.readEnd, dcrwPipeRx.writeEnd);
} catch (e) {
logger.log("error", "Error closing dcrwallet piperx: " + e);
}
}
dcrwPID = null;
return true;
Expand Down Expand Up @@ -191,8 +199,8 @@ export const launchDCRWallet = (mainWindow, daemonIsAdvanced, walletPath, testne
try {
const util = require("util");
const win32ipc = require("../node_modules/win32ipc/build/Release/win32ipc.node");
const pipe = win32ipc.createPipe("out");
args.push(util.format("--piperx=%d", pipe.readEnd));
dcrwPipeRx = win32ipc.createPipe("out");
args.push(util.format("--piperx=%d", dcrwPipeRx.readEnd));
} catch (e) {
logger.log("error", "can't find proper module to launch dcrwallet: " + e);
}
Expand Down
25 changes: 24 additions & 1 deletion app/modules/win32ipc/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

void CreatePipe(v8::FunctionCallbackInfo<v8::Value> const& args) {
auto isolate = v8::Isolate::GetCurrent();

if (args.Length() != 1) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
Expand Down Expand Up @@ -56,10 +56,33 @@ void CreatePipe(v8::FunctionCallbackInfo<v8::Value> const& args) {
args.GetReturnValue().Set(obj);
}

void ClosePipe(v8::FunctionCallbackInfo<v8::Value> const& args) {
auto isolate = v8::Isolate::GetCurrent();

if (args.Length() != 2) {
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();
char const *close_error_msg = pipe_wrapper::close_pipe_end(read_end_handle, write_end_handle);

if (close_result.err_msg != nullptr) {
isolate->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8(isolate, close_error_msg)));
return;
}
}


void Init(v8::Handle<v8::Object> exports) {
auto isolate = v8::Isolate::GetCurrent();
exports->Set(v8::String::NewFromUtf8(isolate, "createPipe"),
v8::FunctionTemplate::New(isolate, CreatePipe)->GetFunction());
exports->Set(v8::String::NewFromUtf8(isolate, "closePipe"),
v8::FunctionTemplate::New(isolate, ClosePipe)->GetFunction());
}

NODE_MODULE(win32ipc, Init)
15 changes: 14 additions & 1 deletion app/modules/win32ipc/pipe_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,18 @@ result<pipe> create_pipe(pipe_direction direction) {
err:
return result<pipe>::error(err_msg);
}


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


if (!CloseHandle((HANDLE) write_end_handle)) {
return "Close(write_end_handle)";
}

return nullptr;
}

} // namespace pipe_wrapper
1 change: 1 addition & 0 deletions app/modules/win32ipc/pipe_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ struct pipe {
};

result<pipe> create_pipe(pipe_direction direction);
char const* close_pipe_end(uintptr_t read_end_handle, uintptr_t write_end_handle);

} // namespace pipe_wrapper

0 comments on commit 5d098f5

Please sign in to comment.