use_uv: Correct readyState on connecting
This commit is contained in:
parent
a1e8fcc5ae
commit
69d20f5352
@ -38,7 +38,7 @@ function initSocketHandle(self) {
|
|||||||
self._writeRequests = [];
|
self._writeRequests = [];
|
||||||
|
|
||||||
self._flags = 0;
|
self._flags = 0;
|
||||||
|
self._connectQueueSize = 0;
|
||||||
self.destroyed = false;
|
self.destroyed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ Object.defineProperty(Socket.prototype, 'readyState', {
|
|||||||
|
|
||||||
Object.defineProperty(Socket.prototype, 'bufferSize', {
|
Object.defineProperty(Socket.prototype, 'bufferSize', {
|
||||||
get: function() {
|
get: function() {
|
||||||
return this._handle.writeQueueSize;
|
return this._handle.writeQueueSize + this._connectQueueSize;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -165,9 +165,18 @@ Socket.prototype.destroySoon = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Socket.prototype._connectQueueCleanUp = function(exception) {
|
||||||
|
this._connecting = false;
|
||||||
|
this._connectQueueSize = 0;
|
||||||
|
this._connectQueue = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Socket.prototype.destroy = function(exception) {
|
Socket.prototype.destroy = function(exception) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
self._connectQueueCleanUp();
|
||||||
|
|
||||||
debug('destroy ' + this.fd);
|
debug('destroy ' + this.fd);
|
||||||
|
|
||||||
this.readable = this.writable = false;
|
this.readable = this.writable = false;
|
||||||
@ -271,6 +280,18 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
|
|||||||
data = new Buffer(data, encoding);
|
data = new Buffer(data, encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we are still connecting, then buffer this for later.
|
||||||
|
if (this._connecting) {
|
||||||
|
this._connectQueueSize += data.length;
|
||||||
|
if (this._connectQueue) {
|
||||||
|
this._connectQueue.push([data, null, fd, cb]);
|
||||||
|
} else {
|
||||||
|
this._connectQueue = [ [data, null, fd, cb] ];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var writeReq = this._handle.write(data);
|
var writeReq = this._handle.write(data);
|
||||||
writeReq.oncomplete = afterWrite;
|
writeReq.oncomplete = afterWrite;
|
||||||
writeReq.cb = cb;
|
writeReq.cb = cb;
|
||||||
@ -307,12 +328,11 @@ function connectip(self, port, ip) {
|
|||||||
// TODO return promise from Socket.prototype.connect which
|
// TODO return promise from Socket.prototype.connect which
|
||||||
// wraps _connectReq.
|
// wraps _connectReq.
|
||||||
|
|
||||||
assert.ok(!self._connecting);
|
assert.ok(self._connecting);
|
||||||
|
|
||||||
var connectReq = self._handle.connect(ip, port);
|
var connectReq = self._handle.connect(ip, port);
|
||||||
|
|
||||||
if (connectReq) {
|
if (connectReq) {
|
||||||
self._connecting = true;
|
|
||||||
connectReq.oncomplete = afterConnect;
|
connectReq.oncomplete = afterConnect;
|
||||||
} else {
|
} else {
|
||||||
self.destroy(errnoException(errno, 'connect'));
|
self.destroy(errnoException(errno, 'connect'));
|
||||||
@ -334,9 +354,10 @@ Socket.prototype.connect = function(port, host /* [cb] */) {
|
|||||||
|
|
||||||
timers.active(this);
|
timers.active(this);
|
||||||
|
|
||||||
if (typeof host == 'undefined') {
|
self._connecting = true;
|
||||||
connectip(self, port, '127.0.0.1');
|
|
||||||
} else {
|
if (typeof host == 'string') {
|
||||||
|
debug("connect: find host " + host);
|
||||||
require('dns').lookup(host, function(err, ip, addressType) {
|
require('dns').lookup(host, function(err, ip, addressType) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self.emit('error', err);
|
self.emit('error', err);
|
||||||
@ -350,6 +371,9 @@ Socket.prototype.connect = function(port, host /* [cb] */) {
|
|||||||
connectip(self, port, ip || '127.0.0.1');
|
connectip(self, port, ip || '127.0.0.1');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
debug("connect: missing host");
|
||||||
|
connectip(self, port, '127.0.0.1');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -358,15 +382,28 @@ function afterConnect(status, handle, req) {
|
|||||||
var self = handle.socket;
|
var self = handle.socket;
|
||||||
assert.equal(handle, self._handle);
|
assert.equal(handle, self._handle);
|
||||||
|
|
||||||
|
debug("afterConnect");
|
||||||
|
|
||||||
assert.ok(self._connecting);
|
assert.ok(self._connecting);
|
||||||
self._connecting = false;
|
self._connecting = false;
|
||||||
|
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
self.readable = self.writable = true;
|
self.readable = self.writable = true;
|
||||||
timers.active(self);
|
timers.active(self);
|
||||||
|
|
||||||
handle.readStart();
|
handle.readStart();
|
||||||
|
|
||||||
|
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.emit('connect');
|
self.emit('connect');
|
||||||
} else {
|
} else {
|
||||||
|
self._connectQueueCleanUp()
|
||||||
self.destroy(errnoException(errno, 'connect'));
|
self.destroy(errnoException(errno, 'connect'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -477,6 +514,8 @@ function onconnection(clientHandle) {
|
|||||||
var handle = this;
|
var handle = this;
|
||||||
var self = handle.socket;
|
var self = handle.socket;
|
||||||
|
|
||||||
|
debug("onconnection");
|
||||||
|
|
||||||
var socket = new Socket({
|
var socket = new Socket({
|
||||||
handle: clientHandle,
|
handle: clientHandle,
|
||||||
allowHalfOpen: self.allowHalfOpen
|
allowHalfOpen: self.allowHalfOpen
|
||||||
|
@ -52,13 +52,15 @@ var tcp = net.Server(function(s) {
|
|||||||
tcp.listen(common.PORT, function () {
|
tcp.listen(common.PORT, function () {
|
||||||
var socket = net.Stream();
|
var socket = net.Stream();
|
||||||
|
|
||||||
console.log('Connecting to socket');
|
console.log('Connecting to socket ');
|
||||||
|
|
||||||
socket.connect(tcpPort, function() {
|
socket.connect(tcpPort, function() {
|
||||||
console.log('socket connected');
|
console.log('socket connected');
|
||||||
connectHappened = true;
|
connectHappened = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log('_connecting = ' + socket._connecting);
|
||||||
|
|
||||||
assert.equal('opening', socket.readyState);
|
assert.equal('opening', socket.readyState);
|
||||||
|
|
||||||
var r = socket.write('foo', function () {
|
var r = socket.write('foo', function () {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user