Improve stratum message handling

Prevent the case where multiple messages are chunked together without being processed (occasionally leading to the buffer growing too large and the client being kicked).

Recognize messages consisting only of spaces as malformed.
This commit is contained in:
LucasJones 2014-04-16 03:28:29 +01:00
parent ba492f2782
commit cf3ffa375d

View File

@ -212,10 +212,11 @@ var StratumClient = function(options){
socket.destroy();
return;
}
if (dataBuffer.slice(-1) === '\n'){
if (dataBuffer.indexOf('\n') !== -1){
var messages = dataBuffer.split('\n');
var incomplete = dataBuffer.slice(-1) === '\n' ? '' : messages.pop();
messages.forEach(function(message){
if (message.trim() === '') return;
if (message === '') return;
var messageJson;
try {
messageJson = JSON.parse(message);
@ -231,7 +232,7 @@ var StratumClient = function(options){
handleMessage(messageJson);
}
});
dataBuffer = '';
dataBuffer = incomplete;
}
});
socket.on('close', function() {