|
|
@ -73,6 +73,22 @@ function allocRecvBuffer () { |
|
|
|
recvBuffer.used = 0; |
|
|
|
} |
|
|
|
|
|
|
|
function _doFlush () { |
|
|
|
var socket = this.socket; |
|
|
|
// Socket becomes writeable on connect() but don't flush if there's
|
|
|
|
// nothing actually to write
|
|
|
|
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (socket.flush()) { |
|
|
|
assert(socket._writeQueueSize == 0); |
|
|
|
assert(socket._writeMessageQueueSize == 0); |
|
|
|
|
|
|
|
if (socket._events && socket._events['drain']) socket.emit("drain"); |
|
|
|
if (socket.ondrain) socket.ondrain(); // Optimization
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function initSocket (self) { |
|
|
|
self._readWatcher = ioWatchers.alloc(); |
|
|
|
self._readWatcher.callback = function () { |
|
|
@ -163,22 +179,9 @@ function initSocket (self) { |
|
|
|
self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length!
|
|
|
|
self._writeMessageQueueSize = 0; // number of messages remaining to be sent
|
|
|
|
|
|
|
|
self._doFlush = function () { |
|
|
|
// Socket becomes writeable on connect() but don't flush if there's
|
|
|
|
// nothing actually to write
|
|
|
|
if ((self._writeQueueSize == 0) && (self._writeMessageQueueSize == 0)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (self.flush()) { |
|
|
|
assert(self._writeQueueSize == 0); |
|
|
|
assert(self._writeMessageQueueSize == 0); |
|
|
|
|
|
|
|
if (self._events && self._events['drain']) self.emit("drain"); |
|
|
|
if (self.ondrain) self.ondrain(); // Optimization
|
|
|
|
} |
|
|
|
}; |
|
|
|
self._writeWatcher = ioWatchers.alloc(); |
|
|
|
self._writeWatcher.callback = self._doFlush; |
|
|
|
self._writeWatcher.socket = self; |
|
|
|
self._writeWatcher.callback = _doFlush; |
|
|
|
self.writable = false; |
|
|
|
} |
|
|
|
|
|
|
@ -210,6 +213,21 @@ exports.createConnection = function (port, host) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(Socket.prototype, 'readyState', { |
|
|
|
get: function () { |
|
|
|
if (this.readable && this.writable) { |
|
|
|
return 'open'; |
|
|
|
} else if (this.readable && !this.writable){ |
|
|
|
return 'readOnly'; |
|
|
|
} else if (!this.readable && this.writable){ |
|
|
|
return 'writeOnly'; |
|
|
|
} else { |
|
|
|
return 'closed'; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._allocateSendBuffer = function () { |
|
|
|
var b = buffers.alloc(1024); |
|
|
|
b.used = 0; |
|
|
@ -351,7 +369,7 @@ Socket.prototype.flush = function () { |
|
|
|
|
|
|
|
if (b == END_OF_FILE) { |
|
|
|
self._shutdown(); |
|
|
|
return false; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
if (b.sent == b.used) { |
|
|
@ -379,19 +397,19 @@ Socket.prototype.flush = function () { |
|
|
|
} |
|
|
|
|
|
|
|
if (bytesWritten === null) { |
|
|
|
// could not flush everything
|
|
|
|
// EAGAIN
|
|
|
|
debug('write EAGAIN'); |
|
|
|
self._writeWatcher.start(); |
|
|
|
assert(self._writeQueueSize > 0); |
|
|
|
return false; |
|
|
|
} |
|
|
|
if (b.isFd) { |
|
|
|
} else if (b.isFd) { |
|
|
|
b.sent = b.used; |
|
|
|
self._writeMessageQueueSize -= 1; |
|
|
|
//debug('sent fd: ' + fdToSend);
|
|
|
|
} else { |
|
|
|
b.sent += bytesWritten; |
|
|
|
self._writeQueueSize -= bytesWritten; |
|
|
|
//debug('bytes sent: ' + b.sent);
|
|
|
|
debug('bytes sent: ' + b.sent); |
|
|
|
} |
|
|
|
} |
|
|
|
if (self._writeWatcher) self._writeWatcher.stop(); |
|
|
@ -453,11 +471,8 @@ Socket.prototype.connect = function () { |
|
|
|
self.type = 'tcp'; |
|
|
|
// TODO dns resolution on arguments[1]
|
|
|
|
var port = arguments[0]; |
|
|
|
var yyy = xxx++; |
|
|
|
lookupDomainName(arguments[1], function (ip) { |
|
|
|
debug('doConnect ' + self.fd + ' yyy=' + yyy); |
|
|
|
doConnect(self, port, ip); |
|
|
|
debug('doConnect done ' + self.fd + ' yyy=' + yyy); |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|