@ -446,14 +446,20 @@ function setupChannel(target, channel) {
channel . onread = function ( nread , pool , recvHandle ) {
channel . onread = function ( nread , pool , recvHandle ) {
// TODO(bnoordhuis) Check that nread > 0.
// TODO(bnoordhuis) Check that nread > 0.
if ( pool ) {
if ( pool ) {
jsonBuffer += decoder . write ( pool ) ;
// Linebreak is used as a message end sign
var chunks = decoder . write ( pool ) . split ( '\n' ) ;
var i , start = 0 ;
var numCompleteChunks = chunks . length - 1 ;
// Last line does not have trailing linebreak
var incompleteChunk = chunks [ numCompleteChunks ] ;
if ( numCompleteChunks === 0 ) {
jsonBuffer += incompleteChunk ;
this . buffering = jsonBuffer . length !== 0 ;
return ;
}
chunks [ 0 ] = jsonBuffer + chunks [ 0 ] ;
//Linebreak is used as a message end sign
for ( var i = 0 ; i < numCompleteChunks ; i ++ ) {
while ( ( i = jsonBuffer . indexOf ( '\n' , start ) ) >= 0 ) {
var message = JSON . parse ( chunks [ i ] ) ;
var json = jsonBuffer . slice ( start , i ) ;
var message = JSON . parse ( json ) ;
// There will be at most one NODE_HANDLE message in every chunk we
// There will be at most one NODE_HANDLE message in every chunk we
// read because SCM_RIGHTS messages don't get coalesced. Make sure
// read because SCM_RIGHTS messages don't get coalesced. Make sure
@ -462,10 +468,8 @@ function setupChannel(target, channel) {
handleMessage ( target , message , recvHandle ) ;
handleMessage ( target , message , recvHandle ) ;
else
else
handleMessage ( target , message , undefined ) ;
handleMessage ( target , message , undefined ) ;
start = i + 1 ;
}
}
jsonBuffer = jsonBuffer . slice ( start ) ;
jsonBuffer = incompleteChunk ;
this . buffering = jsonBuffer . length !== 0 ;
this . buffering = jsonBuffer . length !== 0 ;
} else {
} else {
@ -494,9 +498,10 @@ function setupChannel(target, channel) {
var queue = target . _ handleQueue ;
var queue = target . _ handleQueue ;
target . _ handleQueue = null ;
target . _ handleQueue = null ;
queue . forEach ( function ( args ) {
for ( var i = 0 ; i < queue . length ; i ++ ) {
var args = queue [ i ] ;
target . _ send ( args . message , args . handle , args . options , args . callback ) ;
target . _ send ( args . message , args . handle , args . options , args . callback ) ;
} ) ;
}
// Process a pending disconnect (if any).
// Process a pending disconnect (if any).
if ( ! target . connected && target . channel && ! target . _ handleQueue )
if ( ! target . connected && target . channel && ! target . _ handleQueue )