From 1d3142a882d08a5cd1bf221cf37e76692bd71205 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 10 Oct 2010 23:09:14 -0700 Subject: [PATCH] TCP clients should buffer writes before connection --- lib/net.js | 18 +++++++++++++++++- test/disabled/pipe-test.js | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/net.js b/lib/net.js index c1ec5f42bf3..5813ca96deb 100644 --- a/lib/net.js +++ b/lib/net.js @@ -652,13 +652,20 @@ Object.defineProperty(Stream.prototype, 'readyState', { // something was queued. If data was queued, then the "drain" event will // signal when it has been finally flushed to socket. Stream.prototype.write = function (data, encoding, fd) { - if (this._writeQueue && this._writeQueue.length) { + if (this._connecting || (this._writeQueue && this._writeQueue.length)) { + if (!this._writeQueue) { + this._writeQueue = []; + this._writeQueueEncoding = []; + this._writeQueueFD = []; + } + // Slow. There is already a write queue, so let's append to it. if (this._writeQueueLast() === END_OF_FILE) { throw new Error('Stream.end() called already; cannot write.'); } if (typeof data == 'string' && + this._writeQueueEncoding.length && this._writeQueueEncoding[this._writeQueueEncoding.length-1] === encoding) { // optimization - concat onto last this._writeQueue[this._writeQueue.length-1] += data; @@ -864,6 +871,14 @@ function doConnect (socket, port, host) { socket.destroy(e); return; } + + + if (socket._writeQueue && socket._writeQueue.length) { + // Flush socket in case any writes are queued up while connecting. + // ugly + _doFlush.call(socket._writeWatcher); + } + } else if (errno != EINPROGRESS) { socket.destroy(errnoException(errno, 'connect')); } @@ -886,6 +901,7 @@ Stream.prototype.connect = function () { timeout.active(socket); self._connecting = true; // set false in doConnect + self.writable = true; var port = toPort(arguments[0]); if (port === false) { diff --git a/test/disabled/pipe-test.js b/test/disabled/pipe-test.js index 98992ecd707..f82d37b0fa1 100644 --- a/test/disabled/pipe-test.js +++ b/test/disabled/pipe-test.js @@ -32,9 +32,9 @@ var web = http.Server(function (req, res) { socket.on('connect', function () { console.log('socket connected'); - req.pipe(socket); }); + req.pipe(socket); req.on('end', function () { res.writeHead(200);