diff --git a/lib/Connection.js b/lib/Connection.js index 76fcbb049..d75d1ab51 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -80,19 +80,9 @@ Connection.prototype.connect = function connect(options, callback) { this._socket.domain = this.domain; } - var connection = this; - this._protocol.on('data', function(data) { - connection._socket.write(data); - }); - this._socket.on('data', wrapToDomain(connection, function (data) { - connection._protocol.write(data); - })); - this._protocol.on('end', function() { - connection._socket.end(); - }); - this._socket.on('end', wrapToDomain(connection, function () { - connection._protocol.end(); - })); + // connection <-> protocol + this._protocol.pipe(this._socket); + this._socket.pipe(this._protocol); this._socket.on('error', this._handleNetworkError.bind(this)); this._socket.on('connect', this._handleProtocolConnect.bind(this)); @@ -357,15 +347,11 @@ if (tls.TLSSocket) { // socket <-> encrypted securePair.encrypted.pipe(this._socket); - this._socket.on('data', function(data) { - securePair.encrypted.write(data); - }); + this._socket.pipe(securePair.encrypted, { end: false }); // cleartext <-> protocol securePair.cleartext.pipe(this._protocol); - this._protocol.on('data', function(data) { - securePair.cleartext.write(data); - }); + this._protocol.pipe(securePair.cleartext, { end: false }); // secure established securePair.on('secure', function() { diff --git a/lib/protocol/Protocol.js b/lib/protocol/Protocol.js index ab371059b..34bfa23c2 100644 --- a/lib/protocol/Protocol.js +++ b/lib/protocol/Protocol.js @@ -186,7 +186,7 @@ Protocol.prototype._enqueue = function(sequence) { sequence.on('end', function () { self._handshaked = true; - if (!self._fatalError) { + if (!self._fatalError && !self._destroyed) { self.emit('handshake', self._handshakeInitializationPacket); } });