lib/connectors/hybrid/tcpsocket.js
function(socket, data, offset) {
  var hlen = socket.headSize - socket.headOffset;
  var dlen = data.length - offset;
  var len = Math.min(hlen, dlen);
  var dend = offset + len;

  data.copy(socket.headBuffer, socket.headOffset, offset, dend);
  socket.headOffset += len;

  if(socket.headOffset === socket.headSize) {
    // if head segment finished
    var size = socket.headHandler(socket.headBuffer);
    if(size < 0) {
      throw new Error('invalid body size: ' + size);
    }
    // check if header contains a valid type
    if(checkTypeData(socket.headBuffer[0])) {
      socket.packageSize = size + socket.headSize;
      socket.packageBuffer = new Buffer(socket.packageSize);
      socket.headBuffer.copy(socket.packageBuffer, 0, 0, socket.headSize);
      socket.packageOffset = socket.headSize;
      socket.state = ST_BODY;
    } else {
      dend = data.length;
      logger.error('close the connection with invalid head message, the remote ip is %s && port is %s && message is %j', socket._socket.remoteAddress, socket._socket.remotePort, data);
      socket.close();
    }

  }

  return dend;
}
function(socket, data, offset) {
  var hlen = socket.headSize - socket.headOffset;
  var dlen = data.length - offset;
  var len = Math.min(hlen, dlen);
  var dend = offset + len;

  data.copy(socket.headBuffer, socket.headOffset, offset, dend);
  socket.headOffset += len;

  if(socket.headOffset === socket.headSize) {
    // if head segment finished
    var size = socket.headHandler(socket.headBuffer);
    if(size < 0) {
      throw new Error('invalid body size: ' + size);
    }
    // check if header contains a valid type
    if(checkTypeData(socket.headBuffer[0])) {
      socket.packageSize = size + socket.headSize;
      socket.packageBuffer = new Buffer(socket.packageSize);
      socket.headBuffer.copy(socket.packageBuffer, 0, 0, socket.headSize);
      socket.packageOffset = socket.headSize;
      socket.state = ST_BODY;
    } else {
      dend = data.length;
      logger.error('close the connection with invalid head message, the remote ip is %s && port is %s && message is %j', socket._socket.remoteAddress, socket._socket.remotePort, data);
      socket.close();
    }

  }

  return dend;
}
function ( socket , data , offset ) { var hlen = socket . headSize - socket . headOffset ; var dlen = data . length - offset ; var len = Math . min ( hlen , dlen ) ; var dend = offset + len ; data . copy ( socket . headBuffer , socket . headOffset , offset , dend ) ; socket . headOffset += len ; if ( socket . headOffset === socket . headSize ) { // if head segment finished var size = socket . headHandler ( socket . headBuffer ) ; if ( size < 0 ) { throw new Error ( 'invalid body size: ' + size ) ; } // check if header contains a valid type if ( checkTypeData ( socket . headBuffer [ 0 ] ) ) { socket . packageSize = size + socket . headSize ; socket . packageBuffer = new Buffer ( socket . packageSize ) ; socket . headBuffer . copy ( socket . packageBuffer , 0 , 0 , socket . headSize ) ; socket . packageOffset = socket . headSize ; socket . state = ST_BODY ; } else { dend = data . length ; logger . error ( 'close the connection with invalid head message, the remote ip is %s && port is %s && message is %j' , socket . _socket . remoteAddress , socket . _socket . remotePort , data ) ; socket . close ( ) ; } } return dend ; }
Read head segment from data to socket.headBuffer.

@param  {Object} socket Socket instance
@param  {Object} data   Buffer instance
@param  {Number} offset offset read star from data
@return {Number}        new offset of data after read
Read head segment from data to socket . headBuffer .