Permalink
Browse files

TCP clients should buffer writes before connection

  • Loading branch information...
1 parent 08f1bf4 commit 39a2a9d1b691961e19ae4226ff378fbba03bbb16 @ry ry committed Oct 11, 2010
Showing with 17 additions and 1 deletion.
  1. +17 −1 lib/net.js
View
@@ -638,13 +638,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;
@@ -850,6 +857,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'));
}
@@ -872,6 +887,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) {

0 comments on commit 39a2a9d

Please sign in to comment.