Skip to content

Commit

Permalink
remove redundant buffer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
einaros committed Dec 27, 2011
1 parent 83ceffd commit 4a8c98d
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions lib/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ Sender.prototype.close = function(code, data, mask) {
var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0));
writeUInt16BE.call(dataBuffer, code, 0);
if (dataBuffer.length > 2) dataBuffer.write(data, 2);
var buf = this.frameData(0x8, dataBuffer, true, mask);
try {
this._socket.write(buf, 'binary');
}
catch (e) {
this.emit('error', e);
}
this.frameAndSend(0x8, dataBuffer, true, mask);
}

/**
Expand All @@ -65,13 +59,7 @@ Sender.prototype.close = function(code, data, mask) {

Sender.prototype.ping = function(data, options) {
var mask = options && options.mask;
var buf = this.frameData(0x9, data || '', true, mask);
try {
this._socket.write(buf, 'binary');
}
catch (e) {
this.emit('error', e);
}
this.frameAndSend(0x9, data || '', true, mask);
}

/**
Expand All @@ -82,13 +70,7 @@ Sender.prototype.ping = function(data, options) {

Sender.prototype.pong = function(data, options) {
var mask = options && options.mask;
var buf = this.frameData(0xa, data || '', true, mask);
try {
this._socket.write(buf, 'binary');
}
catch (e) {
this.emit('error', e);
}
this.frameAndSend(0xa, data || '', true, mask);
}

/**
Expand All @@ -98,33 +80,34 @@ Sender.prototype.pong = function(data, options) {
*/

Sender.prototype.send = function(data, options, cb) {
var buf;
var finalFragment = options && options.fin === false ? false : true;
var mask = options && options.mask;
var opcode = options && options.binary ? 2 : 1;
if (this.firstFragment === false) opcode = 0;
else this.firstFragment = false;
buf = this.frameData(opcode, data, finalFragment, mask);
if (finalFragment) this.firstFragment = true
try {
this._socket.write(buf, 'binary', cb);
}
catch (e) {
if (typeof cb == 'function') cb(e);
else this.emit('error', e);
}
this.frameAndSend(opcode, data, finalFragment, mask, cb);
}

/**
* Frames a piece of data according to the HyBi WebSocket protocol.
* Frames and sends a piece of data according to the HyBi WebSocket protocol.
*
* @api private
*/
Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) {
if (!data) return new Buffer([opcode | (finalFragment ? 0x80 : 0), 0]);

Sender.prototype.frameAndSend = function(opcode, data, finalFragment, maskData, cb) {
if (!data) {
try {
this._socket.write(new Buffer([opcode | (finalFragment ? 0x80 : 0), 0]), 'binary', cb);
}
catch (e) {
if (typeof cb == 'function') cb(e);
else this.emit('error', e);
}
return;
}
else if (!Buffer.isBuffer(data)) {
data = (data && typeof data.buffer !== 'undefined')
? getArrayBuffer(data.buffer) : new Buffer(data);
data = (data && typeof data.buffer !== 'undefined') ? getArrayBuffer(data.buffer) : new Buffer(data);
}
var dataLength = data.length
, dataOffset = maskData ? 6 : 2
Expand All @@ -137,10 +120,11 @@ Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) {
dataOffset += 2;
secondByte = 126;
}
var outputBuffer = (this._sendCache && dataLength + dataOffset <= this._sendCacheSize)
? this._sendCache : new Buffer(dataLength + dataOffset);
if (finalFragment) opcode = opcode | 0x80;
outputBuffer[0] = opcode;
var totalLength = maskData ? dataLength + dataOffset : dataOffset;
var outputBuffer = (this._sendCache && totalLength <= this._sendCacheSize)
? (totalLength == this._sendCacheSize ? this._sendCache : this._sendCache.slice(0, totalLength))
: new Buffer(totalLength);
outputBuffer[0] = finalFragment ? opcode | 0x80 : opcode;
switch (secondByte) {
case 126:
writeUInt16BE.call(outputBuffer, dataLength, 2);
Expand All @@ -150,20 +134,36 @@ Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) {
writeUInt32BE.call(outputBuffer, dataLength, 6);
}
if (maskData) {
outputBuffer[1] = secondByte | 0x80;
var mask = this._randomMask || (this._randomMask = getRandomMask());
//faster:
outputBuffer[dataOffset - 4] = mask[0];
outputBuffer[dataOffset - 3] = mask[1];
outputBuffer[dataOffset - 2] = mask[2];
outputBuffer[dataOffset - 1] = mask[3];
//slower:
//mask.copy(outputBuffer, dataOffset - 4);
bufferUtil.mask(data, mask, outputBuffer, dataOffset, dataLength);
secondByte = secondByte | 0x80;
try {
this._socket.write(outputBuffer, 'binary', cb);
}
catch (e) {
if (typeof cb == 'function') cb(e);
else this.emit('error', e);
}
}
else {
outputBuffer[1] = secondByte;
var done = 0;
function callback() {
if (++done == 2 && typeof cb == 'function') cb(null);
}
try {
this._socket.write(outputBuffer, 'binary', callback);
this._socket.write(data, 'binary', callback);
}
catch (e) {
if (typeof cb == 'function') cb(e);
else this.emit('error', e);
}
}
else data.copy(outputBuffer, dataOffset);
outputBuffer[1] = secondByte;
return outputBuffer.slice(0, dataOffset + dataLength);
}

module.exports = Sender;
Expand Down

0 comments on commit 4a8c98d

Please sign in to comment.