From 45b30a879bfec50107913baefe451055953881fb Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 31 Jan 2011 16:37:18 -0800 Subject: [PATCH] tls: superficial clean up --- lib/tls.js | 59 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/lib/tls.js b/lib/tls.js index 95f40af..b6afa3c 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -73,6 +73,7 @@ CryptoStream.prototype.pause = function() { CryptoStream.prototype.resume = function() { debug('resumed cleartext'); this._writeState = true; + this.pair._cycle(); }; @@ -184,22 +185,15 @@ CryptoStream.prototype.__defineGetter__('readyState', // }); // CryptoStream.prototype._blow = function() { - var bytesRead; - var pool; - var chunkBytes; - var chunk; - do { - bytesRead = 0; - chunkBytes = 0; - pool = new Buffer(4096); // alloc every time? - pool.used = 0; + while (this._writeState == true) { + var bytesRead = 0; + var chunkBytes = 0; + var pool = new Buffer(4096); // alloc every time? do { try { - chunkBytes = this._blower(pool, - pool.used + bytesRead, - pool.length - pool.used - bytesRead); + chunkBytes = this._blower(pool, bytesRead, pool.length - bytesRead); } catch (e) { if (this.pair._secureEstablished) { this.pair._error(e); @@ -208,26 +202,30 @@ CryptoStream.prototype._blow = function() { } return; } + if (chunkBytes >= 0) { bytesRead += chunkBytes; } - } while ((chunkBytes > 0) && (pool.used + bytesRead < pool.length)); - if (bytesRead > 0) { - chunk = pool.slice(0, bytesRead); - if (this._decoder) { - var string = this._decoder.write(chunk); - if (string.length) this.emit('data', string); - } else { - if (this._events && this._events['data']) { - this.emit('data', chunk); - } - } + } while ((chunkBytes > 0) && (bytesRead < pool.length)); + + assert(bytesRead >= 0); - // Optimization: emit the original buffer with end points - if (this.ondata) this.ondata(pool, 0, bytesRead); + // Bail out if we didn't read any data. + if (bytesRead == 0) return; + + var chunk = pool.slice(0, bytesRead); + + if (this._decoder) { + var string = this._decoder.write(chunk); + if (string.length) this.emit('data', string); + } else { + this.emit('data', chunk); } - } while (bytesRead > 0 && this._writeState === true); + + // Optimization: emit the original buffer with end points + if (this.ondata) this.ondata(pool, 0, bytesRead); + } }; @@ -246,11 +244,12 @@ CryptoStream.prototype._blow = function() { // }); // CryptoStream.prototype._suck = function() { - var tmp, cb, rv; + var rv; var havePending = this._pending.length > 0; + while (this._pending.length > 0) { - tmp = this._pending.shift(); - cb = this._pendingCallbacks.shift(); + var tmp = this._pending.shift(); + var cb = this._pendingCallbacks.shift(); assert(this._pending.length === this._pendingCallbacks.length); @@ -277,7 +276,7 @@ CryptoStream.prototype._suck = function() { } // If we've cleared all of incoming encrypted data, emit drain. - if (havePending && this._pending && this._pending.length === 0) { + if (havePending && this._pending.length === 0) { debug('drain'); this.emit('drain'); if (this.__destroyOnDrain) this.end(); -- 2.7.4