From e70702c62096860abeab1cc23ffc188850de592b Mon Sep 17 00:00:00 2001 From: Henry Rawas Date: Tue, 12 Jul 2011 17:00:33 -0700 Subject: [PATCH] connect-buffer play back queued write and end --- Makefile | 1 + lib/net_uv.js | 53 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index aa3ebcbd9..db013d3fa 100644 --- a/Makefile +++ b/Makefile @@ -141,6 +141,7 @@ test-uv: all simple/test-mkdir-rmdir \ simple/test-net-binary \ simple/test-net-can-reset-timeout \ + simple/test-net-connect-buffer \ simple/test-net-create-connection \ simple/test-net-eaddrinuse \ simple/test-net-isip \ diff --git a/lib/net_uv.js b/lib/net_uv.js index a876f6fad..cecc4c78e 100644 --- a/lib/net_uv.js +++ b/lib/net_uv.js @@ -9,6 +9,7 @@ var TCP = process.binding('tcp_wrap').TCP; var FLAG_GOT_EOF = 1 << 0; var FLAG_SHUTDOWN = 1 << 1; var FLAG_DESTROY_SOON = 1 << 2; +var FLAG_SHUTDOWNQUED = 1 << 3; var debug; @@ -127,6 +128,12 @@ Socket.prototype.resume = function() { Socket.prototype.end = function(data, encoding) { + if (this._connecting && ((this._flags & FLAG_SHUTDOWNQUED) == 0)) { + // still connecting, add data to buffer + if (data) this.write(data, encoding); + this.writable = false; + this._flags |= FLAG_SHUTDOWNQUED; + } if (!this.writable) return; this.writable = false; @@ -254,25 +261,26 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) { var encoding, fd, cb; // parse arguments - if (typeof arguments[1] == 'string') { + if (typeof arguments[3] == 'function') { + cb = arguments[3]; + fd = arguments[2]; encoding = arguments[1]; - if (typeof arguments[2] == 'number') { - fd = arguments[2]; - cb = arguments[3]; + } else if (typeof arguments[2] == 'function') { + cb = arguments[2]; + if (typeof arguments[1] == 'number') { + fd = arguments[1]; } else { - cb = arguments[2]; + encoding = arguments[1]; } - } else if (typeof arguments[1] == 'number') { - fd = arguments[1]; - cb = arguments[2]; - } else if (typeof arguments[2] == 'number') { - // This case is to support old calls when the encoding argument - // was not optional: s.write(buf, undefined, pipeFDs[1]) - encoding = arguments[1]; - fd = arguments[2]; - cb = arguments[3]; - } else { + } else if (typeof arguments[1] == 'function') { cb = arguments[1]; + } else { + if (typeof arguments[1] == 'number') { + fd = arguments[1]; + } else { + encoding = arguments[1]; + fd = arguments[2]; + } } // Change strings to buffers. SLOW @@ -284,14 +292,13 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) { if (this._connecting) { this._connectQueueSize += data.length; if (this._connectQueue) { - this._connectQueue.push([data, null, fd, cb]); + this._connectQueue.push([data, encoding, fd, cb]); } else { - this._connectQueue = [ [data, null, fd, cb] ]; + this._connectQueue = [ [data, encoding, fd, cb] ]; } return false; } - var writeReq = this._handle.write(data); writeReq.oncomplete = afterWrite; writeReq.cb = cb; @@ -400,15 +407,21 @@ function afterConnect(status, handle, req) { handle.readStart(); + self.emit('connect'); + if (self._connectQueue) { debug('Drain the connect queue'); for (var i = 0; i < self._connectQueue.length; i++) { self.write.apply(self, self._connectQueue[i]); } - self._connectQueueCleanUp() + self._connectQueueCleanUp(); } - self.emit('connect'); + if (self._flags & FLAG_SHUTDOWNQUED) { + // end called before connected - call end now with no data + self._flags &= ~FLAG_SHUTDOWNQUED; + self.end(); + } } else { self._connectQueueCleanUp() self.destroy(errnoException(errno, 'connect')); -- 2.34.1