var FLAG_GOT_EOF = 1 << 0;
var FLAG_SHUTDOWN = 1 << 1;
var FLAG_DESTROY_SOON = 1 << 2;
+var FLAG_SHUTDOWNQUED = 1 << 3;
var debug;
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;
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
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;
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'));