@@ -48,7 +48,7 @@ class SubscriberImpl extends EventEmitter {
4848
4949 this . _type = options . type ;
5050
51- if ( options . queueSize ) {
51+ if ( options . hasOwnProperty ( ' queueSize' ) ) {
5252 this . _queueSize = options . queueSize ;
5353 }
5454 else {
@@ -67,6 +67,10 @@ class SubscriberImpl extends EventEmitter {
6767 this . _throttleMs = 0 ;
6868 }
6969
70+ // tcpNoDelay will be set as a field in the connection header sent to the
71+ // relevant publisher - the publisher should then set tcpNoDelay on the socket
72+ this . _tcpNoDelay = ! ! options . tcpNoDelay ;
73+
7074 this . _msgHandleTime = null ;
7175
7276 this . _nodeHandle = nodeHandle ;
@@ -294,51 +298,51 @@ class SubscriberImpl extends EventEmitter {
294298 let port = info [ 2 ] ;
295299 let address = info [ 1 ] ;
296300
297- let client = new Socket ( ) ;
298- client . name = address + ':' + port ;
299- client . nodeUri = nodeUri ;
301+ let socket = new Socket ( ) ;
302+ socket . name = address + ':' + port ;
303+ socket . nodeUri = nodeUri ;
300304
301- client . on ( 'end' , ( ) => {
305+ socket . on ( 'end' , ( ) => {
302306 this . _log . info ( 'Subscriber client socket %s on topic %s ended the connection' ,
303- client . name , this . getTopic ( ) ) ;
307+ socket . name , this . getTopic ( ) ) ;
304308 } ) ;
305309
306- client . on ( 'error' , ( err ) => {
310+ socket . on ( 'error' , ( err ) => {
307311 this . _log . warn ( 'Subscriber client socket %s on topic %s had error: %s' ,
308- client . name , this . getTopic ( ) , err ) ;
312+ socket . name , this . getTopic ( ) , err ) ;
309313 } ) ;
310314
311315 // hook into close event to clean things up
312- client . on ( 'close' , ( ) => {
316+ socket . on ( 'close' , ( ) => {
313317 this . _log . info ( 'Subscriber client socket %s on topic %s disconnected' ,
314- client . name , this . getTopic ( ) ) ;
315- this . _disconnectClient ( client . nodeUri ) ;
318+ socket . name , this . getTopic ( ) ) ;
319+ this . _disconnectClient ( socket . nodeUri ) ;
316320 } ) ;
317321
318322 // open the socket at the provided address, port
319- client . connect ( port , address , ( ) => {
323+ socket . connect ( port , address , ( ) => {
320324 if ( this . isShutdown ( ) ) {
321- client . end ( ) ;
325+ socket . end ( ) ;
322326 return ;
323327 }
324328
325329 this . _log . debug ( 'Subscriber on ' + this . getTopic ( ) + ' connected to publisher at ' + address + ':' + port ) ;
326- client . write ( this . _createTcprosHandshake ( ) ) ;
330+ socket . write ( this . _createTcprosHandshake ( ) ) ;
327331 } ) ;
328332
329333 // create a DeserializeStream to chunk out messages
330334 let deserializer = new DeserializeStream ( ) ;
331- client . $deserializer = deserializer ;
332- client . pipe ( deserializer ) ;
335+ socket . $deserializer = deserializer ;
336+ socket . pipe ( deserializer ) ;
333337
334338 // cache client in "pending" map.
335339 // It's not validated yet so we don't want it to show up as a client.
336340 // Need to keep track of it in case we're shutdown before it can be validated.
337- this . _pendingPubClients [ client . nodeUri ] = client ;
341+ this . _pendingPubClients [ socket . nodeUri ] = socket ;
338342
339343 // create a one-time handler for the connection header
340344 // if the connection is validated, we'll listen for more events
341- deserializer . once ( 'message' , this . _handleConnectionHeader . bind ( this , client ) ) ;
345+ deserializer . once ( 'message' , this . _handleConnectionHeader . bind ( this , socket ) ) ;
342346 }
343347
344348 /**
@@ -347,18 +351,18 @@ class SubscriberImpl extends EventEmitter {
347351 */
348352 _createTcprosHandshake ( ) {
349353 return TcprosUtils . createSubHeader ( this . _nodeHandle . getNodeName ( ) , this . _messageHandler . md5sum ( ) ,
350- this . getTopic ( ) , this . getType ( ) , this . _messageHandler . messageDefinition ( ) ) ;
354+ this . getTopic ( ) , this . getType ( ) , this . _messageHandler . messageDefinition ( ) , this . _tcpNoDelay ) ;
351355 }
352356
353357 /**
354358 * Handles the connection header from a publisher. If connection is validated,
355359 * we'll start handling messages from the client.
356- * @param client {Socket} publisher client who sent the connection header
360+ * @param socket {Socket} publisher client who sent the connection header
357361 * @param msg {string} message received from the publisher
358362 */
359- _handleConnectionHeader ( client , msg ) {
363+ _handleConnectionHeader ( socket , msg ) {
360364 if ( this . isShutdown ( ) ) {
361- this . _disconnectClient ( client . nodeUri ) ;
365+ this . _disconnectClient ( socket . nodeUri ) ;
362366 return ;
363367 }
364368
@@ -373,22 +377,21 @@ class SubscriberImpl extends EventEmitter {
373377 const error = TcprosUtils . validatePubHeader ( header , this . getType ( ) , this . _messageHandler . md5sum ( ) ) ;
374378 if ( error ) {
375379 this . _log . error ( `Unable to validate subscriber ${ this . getTopic ( ) } connection header ${ JSON . stringify ( header ) } ` ) ;
376- TcprosUtils . parsePubHeader ( msg ) ;
377- client . end ( Serialize ( error ) ) ;
380+ socket . end ( Serialize ( error ) ) ;
378381 return ;
379382 }
380383 // connection header was valid - we're good to go!
381384 this . _log . debug ( 'Subscriber ' + this . getTopic ( ) + ' got connection header ' + JSON . stringify ( header ) ) ;
382385
383386 // cache client now that we've verified the connection header
384- this . _pubClients [ client . nodeUri ] = client ;
387+ this . _pubClients [ socket . nodeUri ] = socket ;
385388 // remove client from pending map now that it's validated
386- delete this . _pendingPubClients [ client . nodeUri ] ;
389+ delete this . _pendingPubClients [ socket . nodeUri ] ;
387390
388391 // pipe all future messages to _handleMessage
389- client . $deserializer . on ( 'message' , this . _handleMessage . bind ( this ) ) ;
392+ socket . $deserializer . on ( 'message' , this . _handleMessage . bind ( this ) ) ;
390393
391- this . emit ( 'connection' , header , client . name ) ;
394+ this . emit ( 'connection' , header , socket . name ) ;
392395 }
393396
394397 /**
0 commit comments