From 2eddfa1e5b8363a2723f9e1b34ba9c94fe9356c9 Mon Sep 17 00:00:00 2001 From: shrey Date: Tue, 25 Jun 2019 22:36:12 +0530 Subject: [PATCH 1/7] Handling unix socket connection based on OS --- main.js | 4 ++++ src/client.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 02aebc9..51800cf 100644 --- a/main.js +++ b/main.js @@ -85,6 +85,10 @@ app.on('ready',() => { switch (json.name) { // App case consts.eventNames.appCmdQuit: + rl.close() + setTimeout(function(){ + app.quit(); + }, 100) app.quit(); break; diff --git a/src/client.js b/src/client.js index 75b297c..4673f52 100644 --- a/src/client.js +++ b/src/client.js @@ -7,9 +7,18 @@ const url = require('url'); class Client { // init initializes the Client init() { - let u = url.parse("tcp://" + process.argv[2], false, false) - this.socket = new net.Socket() - this.socket.connect(u.port, u.hostname, function() {}); + + this.getconnection() + + this.socket.on('error', function(err){ + // Writing to a file in case of error related to socket + var fs = require('fs'); + fs.appendFile("/tmp/astilectron.log", "Socket Error: "+err, function(e){ + console.log("Error while writing to file:"+e); + }) + process.exit() + }) + this.socket.on('close', function() { process.exit() }) @@ -22,6 +31,22 @@ class Client { if (typeof payload !== "undefined") Object.assign(data, payload) this.socket.write(JSON.stringify(data) + "\n") } + + // for proper socket closing + close() { + this.socket.end() + } + + // getconnection establishes connection based on underlying OS + getconnection() { + if ( os.platform() != "win32" ) { + this.socket = net.createConnection(process.argv[1]); + } else { + let u = url.parse("tcp://" + process.argv[1], false, false) + this.socket = new net.Socket() + this.socket.connect(u.port, u.hostname, function() {}); + } + } } module.exports = new Client() From 7ddc5662fe1d2ad01a99139f451de8d12daa44c8 Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Mon, 26 Aug 2019 10:17:16 +0530 Subject: [PATCH 2/7] removed some unused code lines, rectified connection argument --- main.js | 3 --- src/client.js | 15 +++++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index 51800cf..b29ef29 100644 --- a/main.js +++ b/main.js @@ -86,9 +86,6 @@ app.on('ready',() => { // App case consts.eventNames.appCmdQuit: rl.close() - setTimeout(function(){ - app.quit(); - }, 100) app.quit(); break; diff --git a/src/client.js b/src/client.js index 4673f52..aaa1bae 100644 --- a/src/client.js +++ b/src/client.js @@ -8,7 +8,7 @@ class Client { // init initializes the Client init() { - this.getconnection() + this.connect() this.socket.on('error', function(err){ // Writing to a file in case of error related to socket @@ -32,17 +32,20 @@ class Client { this.socket.write(JSON.stringify(data) + "\n") } - // for proper socket closing + /* + * for proper socket closing, unix socket remains open even after + * quiting the application, this function ends the socket connection + */ close() { this.socket.end() } - // getconnection establishes connection based on underlying OS - getconnection() { + // establishes connection based on underlying OS + connect() { if ( os.platform() != "win32" ) { - this.socket = net.createConnection(process.argv[1]); + this.socket = net.createConnection(process.argv[2]); } else { - let u = url.parse("tcp://" + process.argv[1], false, false) + let u = url.parse("tcp://" + process.argv[2], false, false) this.socket = new net.Socket() this.socket.connect(u.port, u.hostname, function() {}); } From 0e30f0af1d42bbe7be4b7174bd21cac62e79a729 Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Wed, 11 Sep 2019 16:03:54 +0530 Subject: [PATCH 3/7] rectified argv indices --- src/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.js b/src/client.js index aaa1bae..864df61 100644 --- a/src/client.js +++ b/src/client.js @@ -43,9 +43,9 @@ class Client { // establishes connection based on underlying OS connect() { if ( os.platform() != "win32" ) { - this.socket = net.createConnection(process.argv[2]); + this.socket = net.createConnection(process.argv[3]); } else { - let u = url.parse("tcp://" + process.argv[2], false, false) + let u = url.parse("tcp://" + process.argv[3], false, false) this.socket = new net.Socket() this.socket.connect(u.port, u.hostname, function() {}); } From 7473e46390d66b90d76285750d40718e1ba44d79 Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Tue, 17 Sep 2019 15:54:44 +0530 Subject: [PATCH 4/7] added rl.close() for better handling of socket in case of unix-domain socket --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 77186f7..1205cc3 100644 --- a/index.js +++ b/index.js @@ -62,6 +62,7 @@ function onReady () { switch (json.name) { // App case consts.eventNames.appCmdQuit: + rl.close() app.quit(); break; @@ -548,4 +549,4 @@ function sessionCreate(webContents, sessionId) { module.exports = { lastWindow, start -} \ No newline at end of file +} From 7b305235bbc5f8e22cf038f416d5583435babd08 Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Fri, 1 Nov 2019 12:37:14 +0530 Subject: [PATCH 5/7] added proper handling of connection address based on the string tcp/unix --- main.js | 8 ++++++++ src/client.js | 37 +++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/main.js b/main.js index dbe5ba6..089165b 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,17 @@ "use strict"; +process.argv.forEach(function (val, index, array) { + var fs = require('fs') + fs.appendFile("/tmp/astilectron.log", index + ': ' + val, function(e){ + console.log("Error while writing to file:"+e); + }) +}); + const app = require("electron"); const start = require("./index").start; const lastWindow = require("./index").lastWindow; + if (process.argv[3] === "true") { // Lock const singlesInstanceLock = app.requestSingleInstanceLock(); diff --git a/src/client.js b/src/client.js index 3159af4..03db6e7 100644 --- a/src/client.js +++ b/src/client.js @@ -2,21 +2,30 @@ const net = require("net"); const url = require("url"); +const {dialog} = require("electron").remote // Client can read/write messages from a TCP server class Client { // init initializes the Client init(addr) { - this.connect(addr) + var fs = require('fs') + fs.appendFile("/tmp/astilectron.log", "Addr: "+addr, function(e){ + console.log("Error while writing to file:"+e); + }) + + this.socket = net.createConnection(addr); + //this.connect(addr) this.socket.on('error', function(err){ - // Writing to a file in case of error related to socket - var fs = require('fs'); - fs.appendFile("/tmp/astilectron.log", "Socket Error: "+err, function(e){ - console.log("Error while writing to file:"+e); - }) - process.exit() + // Raising an exception in case of any error in socket + const messageBoxOptions = { + type: "error", + title: "Error in Main process", + message: err + }; + dialog.showMessageBox(messageBoxOptions); + process.exit(1) }) this.socket.on('close', function() { @@ -40,15 +49,15 @@ class Client { this.socket.end() } - // establishes connection based on underlying OS connect(addr) { - if ( os.platform() != "win32" ) { - this.socket = net.createConnection(addr); - } else { - let u = url.parse("tcp://" + addr, false, false) + tcp = "tcp://"; + if (addr.indexOf(tcp) == 0) { + let u = url.parse(addr, false, false) this.socket = new net.Socket() - this.socket.connect(u.port, u.hostname, function() {}); - } + this.socket.connect(u.port, u.hostname, function() {}); + } else { + this.socket = net.createConnection(addr); + } } } From c3458f64614aaba39c84e30971268cff528f9628 Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Sun, 10 Nov 2019 01:47:02 +0530 Subject: [PATCH 6/7] changes to check the type of connection in client.js --- main.js | 7 ------- src/client.js | 32 ++++++++++++-------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/main.js b/main.js index 089165b..d1b7412 100644 --- a/main.js +++ b/main.js @@ -1,12 +1,5 @@ "use strict"; -process.argv.forEach(function (val, index, array) { - var fs = require('fs') - fs.appendFile("/tmp/astilectron.log", index + ': ' + val, function(e){ - console.log("Error while writing to file:"+e); - }) -}); - const app = require("electron"); const start = require("./index").start; const lastWindow = require("./index").lastWindow; diff --git a/src/client.js b/src/client.js index 03db6e7..2681e45 100644 --- a/src/client.js +++ b/src/client.js @@ -1,21 +1,14 @@ "use strict"; -const net = require("net"); -const url = require("url"); -const {dialog} = require("electron").remote +const net = require("net") +const url = require("url") +const {dialog} = require("electron") // Client can read/write messages from a TCP server class Client { // init initializes the Client init(addr) { - - var fs = require('fs') - fs.appendFile("/tmp/astilectron.log", "Addr: "+addr, function(e){ - console.log("Error while writing to file:"+e); - }) - - this.socket = net.createConnection(addr); - //this.connect(addr) + this.connect(addr) this.socket.on('error', function(err){ // Raising an exception in case of any error in socket @@ -25,11 +18,11 @@ class Client { message: err }; dialog.showMessageBox(messageBoxOptions); - process.exit(1) - }) + process.exit(1); + }); this.socket.on('close', function() { - process.exit() + process.exit(); }) return this } @@ -46,18 +39,17 @@ class Client { * quiting the application, this function ends the socket connection */ close() { - this.socket.end() + this.socket.end(); } connect(addr) { - tcp = "tcp://"; - if (addr.indexOf(tcp) == 0) { - let u = url.parse(addr, false, false) - this.socket = new net.Socket() + if (net.isIP(addr) > 0) { + let u = url.parse(addr, false, false); + this.socket = new net.Socket(); this.socket.connect(u.port, u.hostname, function() {}); } else { this.socket = net.createConnection(addr); - } + } } } From b5d06bea06054b5cf295fae23b346fd9d8ff90dc Mon Sep 17 00:00:00 2001 From: shrey-gang Date: Wed, 18 Dec 2019 12:20:50 +0530 Subject: [PATCH 7/7] removed client check for TCP/Unix using single function net.createConnection() to create connection --- main.js | 1 - src/client.js | 13 ++----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/main.js b/main.js index d1b7412..dbe5ba6 100644 --- a/main.js +++ b/main.js @@ -4,7 +4,6 @@ const app = require("electron"); const start = require("./index").start; const lastWindow = require("./index").lastWindow; - if (process.argv[3] === "true") { // Lock const singlesInstanceLock = app.requestSingleInstanceLock(); diff --git a/src/client.js b/src/client.js index 2681e45..3a15006 100644 --- a/src/client.js +++ b/src/client.js @@ -8,7 +8,8 @@ const {dialog} = require("electron") class Client { // init initializes the Client init(addr) { - this.connect(addr) + + this.socket = net.createConnection(addr); this.socket.on('error', function(err){ // Raising an exception in case of any error in socket @@ -41,16 +42,6 @@ class Client { close() { this.socket.end(); } - - connect(addr) { - if (net.isIP(addr) > 0) { - let u = url.parse(addr, false, false); - this.socket = new net.Socket(); - this.socket.connect(u.port, u.hostname, function() {}); - } else { - this.socket = net.createConnection(addr); - } - } } module.exports = new Client();