summaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-10-05 14:56:17 +0200
committerAleš Smodiš <aless@guru.si>2015-10-05 14:56:17 +0200
commit0818b6655e8fd5ce14febd47470ac0445e3665f1 (patch)
tree801f161ea326bc3120b81d0187c4ac2275043fbf /web
parent361f8245079b625560449324faf111ed6fcf3b1b (diff)
Bugfix: correctly handle python-nodejs transport using bytes.
Diffstat (limited to 'web')
-rw-r--r--web/main.js55
1 files changed, 32 insertions, 23 deletions
diff --git a/web/main.js b/web/main.js
index 8eac85c..16b318d 100644
--- a/web/main.js
+++ b/web/main.js
@@ -8,16 +8,17 @@ log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('web.log'), 'log');
var logger = log4js.getLogger('log');
-Promise.onPossiblyUnhandledRejection(function (error) {
- var msg = 'Unhandled promise rejection: ' + error;
+var logException = function (msg, error) {
if ((error instanceof Error) && (typeof error.stack === 'string')) msg += '\n' + error.stack;
logger.error(msg);
+};
+
+Promise.onPossiblyUnhandledRejection(function (error) {
+ logException('Unhandled promise rejection: ' + error, error);
});
process.on('uncaughtException', function (error) {
- var msg = 'Uncaught exception: ' + error;
- if ((error instanceof Error) && (typeof error.stack === 'string')) msg += '\n' + error.stack;
- logger.error(msg);
+ logException('Uncaught exception: ' + error, error);
});
http_server.listen(8083);
@@ -136,7 +137,9 @@ server.on('connection', function (socket) {
s = null;
});
}
- catch (e) {} // TODO: log
+ catch (e) {
+ logException('Failed to send reply to GUI about a fatal error: ' + e, e);
+ }
socket = null; // this socket will no longer be processed
session.socket = null;
},
@@ -358,7 +361,7 @@ var sendPacketToPython = function sendPacketToPythonFunc(packet) {
if (pythonSaturated || (pythonClient === null)) return false;
logger.debug('Sending to Python: ' + packet);
try {
- pythonSaturated = !pythonClient.write(packet, 'utf8');
+ pythonSaturated = !pythonClient.write(packet);
return true;
}
catch (e) {
@@ -390,7 +393,7 @@ var sendDataToPython = function sendDataToPythontFunc(packetObj) {
}
s = JSON.stringify(packetObj);
- s = s.length + ':' + s;
+ s = new Buffer(Buffer.byteLength(s) + ':' + s);
return new Promise(function (resolve, reject) {
var existingResolver = pythonPromises[key];
@@ -425,7 +428,7 @@ var sendPythonQueue = function sendPythonQueueFunc() {
var connectPython = function connectPythonFunc() {
var receivingHeader = true, // whether we're receiving the length of the next packet, or the packet itself
- receiveBuffer = [], // chunks of the next packet (header or body)
+ receiveBuffer = [], // chunks of the next packet (header or body), Buffer objects
bodyLength = 0, // the required length of the next packet
receivedLength = 0; // the size of the received body inside receiveBuffer up to now
@@ -436,8 +439,6 @@ var connectPython = function connectPythonFunc() {
'port': 19732
});
- pythonClient.setEncoding('utf8');
-
pythonClient.on('connect', function () {
// login to the python server by sending it the list of currently handled sessions
var sid, sessions = [], packet;
@@ -453,6 +454,7 @@ var connectPython = function connectPythonFunc() {
sendPacketToPython(packet);
});
+ // handler for incoming data, chunk is a Buffer object
pythonClient.on('data', function (chunk) {
var i, s,
offset = 0,
@@ -462,16 +464,21 @@ var connectPython = function connectPythonFunc() {
while (offset < N) { // while there's data left to process
if (receivingHeader) {
// receiving the length of the packet
- i = chunk.indexOf(':', offset);
- if (i < 0) {
+ for (i = offset; i < N; i++) {
+ if (chunk[i] === 58) break; // found a colon?
+ }
+ if (i >= N) {
// incomplete header, buffer it for the next 'data' event
- receiveBuffer.push(chunk.substring(offset));
+ s = chunk.slice(offset);
+ receiveBuffer.push(s);
+ receivedLength += s.length;
break;
}
- if (receiveBuffer.length == 0) bodyLength = +chunk.substring(offset, i);
+ if (receiveBuffer.length == 0) bodyLength = +chunk.toString('utf8', offset, i);
else {
- receiveBuffer.push(chunk.substring(offset, i));
- bodyLength = +receiveBuffer.join('');
+ s = chunk.slice(offset, i);
+ receiveBuffer.push(s);
+ bodyLength = +Buffer.concat(receiveBuffer, receivedLength + s.length).toString('utf8');
receiveBuffer.length = 0;
receivedLength = 0;
}
@@ -482,26 +489,28 @@ var connectPython = function connectPythonFunc() {
else if ((receivedLength + N - offset) >= bodyLength) {
if (receivedLength == 0) {
// an optimization in case the buffer is empty
- s = chunk.substr(offset, bodyLength);
+ s = chunk.slice(offset, offset + bodyLength);
offset += s.length;
}
else {
- s = chunk.substr(offset, bodyLength - receivedLength);
+ s = chunk.slice(offset, offset + bodyLength - receivedLength);
offset += s.length;
receiveBuffer.push(s);
- s = receiveBuffer.join('');
+ s = Buffer.concat(receiveBuffer, receivedLength + s.length);
receiveBuffer.length = 0;
receivedLength = 0;
}
receivingHeader = true; // process the packet header in the next iteration
try {
- processPacketFromPython(s);
+ processPacketFromPython(s.toString('utf8'));
+ }
+ catch (e) {
+ logException('Failed to process a packet from Python: ' + e, e);
}
- catch (e) {} // TODO: log the error
}
else {
// incomplete packet body, buffer it for the next 'data' event
- s = chunk.substring(offset);
+ s = chunk.slice(offset);
receiveBuffer.push(s);
receivedLength += s.length;
break;