Skip to content

Commit 6ca004c

Browse files
committed
Enhanced to close the Telnet port after done. Enhanced closePort() to prevent closing a null port.
1 parent 7295680 commit 6ca004c

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

loader.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,19 @@ function loadPropeller(sock, portPath, action, payload, debug) {
147147
listen(port, false); //Disable listener
148148
port.mode = (debug !== "none") ? "debug" : "programming";
149149
log(notice(nsDownloadSuccessful), mAll, sock);
150-
if (sock && debug !== "none") {
150+
if (sock && debug !== "none") { //If debug needed, open terminal/graph
151151
sock.send(JSON.stringify({type:"ui-command", action:(debug === "term") ? "open-terminal" : "open-graph"}));
152152
sock.send(JSON.stringify({type:"ui-command", action:"close-compile"}));
153+
} else { //Else
154+
if (port.isWireless) closePort(port, false).catch(function(e) {log(e.message, mAll, sock);}) // Close Telnet port (if wireless)
153155
}
154156
}) //Error? Disable listener and display error
155157
.catch(function(e) {
156158
listen(port, false);
157159
log(e.message, mAll, sock);
158160
log(notice(neDownloadFailed), mAll, sock);
159-
if (port.connId) {changeBaudrate(port, originalBaudrate)}
161+
if ((port.isWired && port.connId) || port.isWireless) {changeBaudrate(port, originalBaudrate)}
162+
if (port.isWireless) {closePort(port, false)}
160163
});
161164
} else {
162165
// Port not found
@@ -215,8 +218,10 @@ function setPropCommTimer(timeout, timeoutError, command) {
215218
log("Timed out in " + timeout + " ms", mDbug);
216219
clearPropCommTimer(); // Clear timer
217220
propComm.stage = sgIdle; // Reset propComm stage to Idle (ignore incoming data)
218-
if (propComm.port.isWireless) {closePort(propComm.port, command)} // Close and/or forget HTTP or Telnet service socket
219-
propComm.response.reject(Error(propComm.timeoutError)); // And reject with error
221+
Promise.resolve()
222+
.then(function() {if (propComm.port.isWireless) {return closePort(propComm.port, command)}}) // Close and/or forget HTTP or Telnet service socket
223+
.then(function() {propComm.response.reject(Error(propComm.timeoutError))}) // And reject with error
224+
.catch(function(e) {propComm.response.reject(Error(propComm.timeoutError))})
220225
}, timeout);
221226
}
222227

serial.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,26 @@ function closePort(port, command) {
8484
// Nullify port's socket reference
8585
let sID = port[socket];
8686
updatePort(port, {[socket]: null});
87-
log("Closing socket", mDbug);
88-
// Disconnect and/or close socket (if necessary)
89-
chrome.sockets.tcp.getInfo(sID, function(info) {
90-
if (info.connected) {
91-
chrome.sockets.tcp.disconnect(sID, function() {
87+
if (sID) {
88+
log("Closing socket", mDbug);
89+
// Disconnect and/or close socket (if necessary)
90+
chrome.sockets.tcp.getInfo(sID, function(info) {
91+
if (info.connected) {
92+
chrome.sockets.tcp.disconnect(sID, function() {
93+
chrome.sockets.tcp.close(sID, function() {
94+
resolve();
95+
})
96+
})
97+
} else {
9298
chrome.sockets.tcp.close(sID, function() {
9399
resolve();
94100
})
95-
})
96-
} else {
97-
chrome.sockets.tcp.close(sID, function() {
98-
resolve();
99-
})
100-
}
101-
});
101+
}
102+
});
103+
} else {
104+
log("Not closing socket", mDbug);
105+
reject(Error(notice(neCanNotClosePort, [port.path])));
106+
}
102107
}
103108

104109
if (port) {
@@ -152,6 +157,9 @@ function changeBaudrate(port, baudrate) {
152157
chrome.sockets.tcp.create(function (info) {
153158
//Update port record with socket to Propeller's HTTP service
154159
updatePort(port, {phSocket: info.socketId});
160+
if (!port.phSocket) {
161+
log("NULL SOCKET!!!", mDbug);
162+
}
155163
let postStr = "POST /wx/setting?name=baud-rate&value=" + baudrate + " HTTP/1.1\r\n\r\n";
156164
chrome.sockets.tcp.connect(port.phSocket, port.ip, 80, function() {
157165
chrome.sockets.tcp.send(port.phSocket, str2ab(postStr), function () {

0 commit comments

Comments
 (0)