connect-buffer play back queued write and end
authorHenry Rawas <henryr@schakra.com>
Wed, 13 Jul 2011 00:00:33 +0000 (17:00 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 15 Jul 2011 00:19:07 +0000 (17:19 -0700)
Makefile
lib/net_uv.js

index aa3ebcb..db013d3 100644 (file)
--- 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 \
index a876f6f..cecc4c7 100644 (file)
@@ -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'));