|
|
@ -179,6 +179,8 @@ function initSocket(self) { |
|
|
|
self._writeQueueEncoding = []; |
|
|
|
self._writeQueueFD = []; |
|
|
|
self._writeQueueCallbacks = []; |
|
|
|
// Number of charactes (which approx. equals number of bytes)
|
|
|
|
self.bufferSize = 0; |
|
|
|
|
|
|
|
self._writeWatcher = ioWatchers.alloc(); |
|
|
|
self._writeWatcher.socket = self; |
|
|
@ -192,6 +194,7 @@ function Socket(options) { |
|
|
|
if (!(this instanceof Socket)) return new Socket(arguments[0], arguments[1]); |
|
|
|
stream.Stream.call(this); |
|
|
|
|
|
|
|
this.bufferSize = 0; |
|
|
|
this.fd = null; |
|
|
|
this.type = null; |
|
|
|
this.allowHalfOpen = false; |
|
|
@ -270,6 +273,8 @@ Object.defineProperty(Socket.prototype, 'readyState', { |
|
|
|
Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) { |
|
|
|
var encoding, fd, cb; |
|
|
|
|
|
|
|
assert(this.bufferSize >= 0); |
|
|
|
|
|
|
|
// parse arguments
|
|
|
|
if (typeof arguments[1] == 'string') { |
|
|
|
encoding = arguments[1]; |
|
|
@ -296,6 +301,7 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) { |
|
|
|
|
|
|
|
if (this._connecting || (this._writeQueue && this._writeQueue.length)) { |
|
|
|
if (!this._writeQueue) { |
|
|
|
this.bufferSize = 0; |
|
|
|
this._writeQueue = []; |
|
|
|
this._writeQueueEncoding = []; |
|
|
|
this._writeQueueFD = []; |
|
|
@ -309,6 +315,8 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) { |
|
|
|
|
|
|
|
var last = this._writeQueue.length - 1; |
|
|
|
|
|
|
|
this.bufferSize += data.length; |
|
|
|
|
|
|
|
if (typeof data == 'string' && |
|
|
|
this._writeQueue.length && |
|
|
|
typeof this._writeQueue[last] === 'string' && |
|
|
@ -403,6 +411,8 @@ Socket.prototype._writeOut = function(data, encoding, fd, cb) { |
|
|
|
// (data.length - charsWritten) +
|
|
|
|
// ' bytes into the pool\n');
|
|
|
|
// Unshift whatever didn't fit onto the buffer
|
|
|
|
assert(data.length > charsWritten); |
|
|
|
this.bufferSize += data.length - charsWritten; |
|
|
|
this._writeQueue.unshift(data.slice(charsWritten)); |
|
|
|
this._writeQueueEncoding.unshift(encoding); |
|
|
|
this._writeQueueCallbacks.unshift(cb); |
|
|
@ -451,6 +461,7 @@ Socket.prototype._writeOut = function(data, encoding, fd, cb) { |
|
|
|
//if (!this._writeQueue) initWriteSocket(this);
|
|
|
|
|
|
|
|
// data should be the next thing to write.
|
|
|
|
this.bufferSize += leftOver.length; |
|
|
|
this._writeQueue.unshift(leftOver); |
|
|
|
this._writeQueueEncoding.unshift(null); |
|
|
|
this._writeQueueCallbacks.unshift(cb); |
|
|
@ -478,6 +489,9 @@ Socket.prototype.flush = function() { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
// Only decrement if it's not the END_OF_FILE object...
|
|
|
|
this.bufferSize -= data.length; |
|
|
|
|
|
|
|
var flushed = this._writeOut(data, encoding, fd, cb); |
|
|
|
if (!flushed) return false; |
|
|
|
} |
|
|
@ -731,7 +745,12 @@ Socket.prototype.destroy = function(exception) { |
|
|
|
|
|
|
|
// TODO would like to set _writeQueue to null to avoid extra object alloc,
|
|
|
|
// but lots of code assumes this._writeQueue is always an array.
|
|
|
|
assert(this.bufferSize >= 0); |
|
|
|
this._writeQueue = []; |
|
|
|
this._writeQueueEncoding = []; |
|
|
|
this._writeQueueCallbacks = []; |
|
|
|
this._writeQueueFD = []; |
|
|
|
this.bufferSize = 0; |
|
|
|
|
|
|
|
this.readable = this.writable = false; |
|
|
|
|
|
|
|