|
|
@ -534,21 +534,16 @@ Socket.prototype.write = function(chunk, encoding, cb) { |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._write = function(dataEncoding, cb) { |
|
|
|
assert(Array.isArray(dataEncoding)); |
|
|
|
// assert(Array.isArray(dataEncoding));
|
|
|
|
var data = dataEncoding[0]; |
|
|
|
var encoding = dataEncoding[1] || 'utf8'; |
|
|
|
|
|
|
|
if (this !== process.stderr && this !== process.stdout) |
|
|
|
debug('Socket._write'); |
|
|
|
|
|
|
|
// If we are still connecting, then buffer this for later.
|
|
|
|
// The Writable logic will buffer up any more writes while
|
|
|
|
// waiting for this one to be done.
|
|
|
|
if (this._connecting) { |
|
|
|
debug('_write: waiting for connection'); |
|
|
|
this._pendingWrite = dataEncoding; |
|
|
|
this.once('connect', function() { |
|
|
|
debug('_write: connected now, try again'); |
|
|
|
this._write(dataEncoding, cb); |
|
|
|
}); |
|
|
|
return; |
|
|
@ -558,53 +553,49 @@ Socket.prototype._write = function(dataEncoding, cb) { |
|
|
|
timers.active(this); |
|
|
|
|
|
|
|
if (!this._handle) { |
|
|
|
debug('already destroyed'); |
|
|
|
this._destroy(new Error('This socket is closed.'), cb); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
var writeReq; |
|
|
|
var enc = Buffer.isBuffer(data) ? 'buffer' : encoding; |
|
|
|
var writeReq = createWriteReq(this._handle, data, enc); |
|
|
|
|
|
|
|
if (Buffer.isBuffer(data)) { |
|
|
|
writeReq = this._handle.writeBuffer(data); |
|
|
|
if (!writeReq || typeof writeReq !== 'object') |
|
|
|
return this._destroy(errnoException(errno, 'write'), cb); |
|
|
|
|
|
|
|
} else { |
|
|
|
writeReq.oncomplete = afterWrite; |
|
|
|
this._bytesDispatched += writeReq.bytes; |
|
|
|
|
|
|
|
// If it was entirely flushed, we can write some more right now.
|
|
|
|
// However, if more is left in the queue, then wait until that clears.
|
|
|
|
if (this._handle.writeQueueSize === 0) |
|
|
|
cb(); |
|
|
|
else |
|
|
|
writeReq.cb = cb; |
|
|
|
}; |
|
|
|
|
|
|
|
function createWriteReq(handle, data, encoding) { |
|
|
|
switch (encoding) { |
|
|
|
case 'buffer': |
|
|
|
return handle.writeBuffer(data); |
|
|
|
|
|
|
|
case 'utf8': |
|
|
|
case 'utf-8': |
|
|
|
writeReq = this._handle.writeUtf8String(data); |
|
|
|
break; |
|
|
|
return handle.writeUtf8String(data); |
|
|
|
|
|
|
|
case 'ascii': |
|
|
|
writeReq = this._handle.writeAsciiString(data); |
|
|
|
break; |
|
|
|
return handle.writeAsciiString(data); |
|
|
|
|
|
|
|
case 'ucs2': |
|
|
|
case 'ucs-2': |
|
|
|
case 'utf16le': |
|
|
|
case 'utf-16le': |
|
|
|
writeReq = this._handle.writeUcs2String(data); |
|
|
|
break; |
|
|
|
return handle.writeUcs2String(data); |
|
|
|
|
|
|
|
default: |
|
|
|
writeReq = this._handle.writeBuffer(new Buffer(data, encoding)); |
|
|
|
break; |
|
|
|
} |
|
|
|
return handle.writeBuffer(new Buffer(data, encoding)); |
|
|
|
} |
|
|
|
|
|
|
|
if (!writeReq || typeof writeReq !== 'object') |
|
|
|
return this._destroy(errnoException(errno, 'write'), cb); |
|
|
|
|
|
|
|
writeReq.oncomplete = afterWrite; |
|
|
|
this._bytesDispatched += writeReq.bytes; |
|
|
|
|
|
|
|
// If it was entirely flushed, we can write some more right now.
|
|
|
|
// However, if more is left in the queue, then wait until that clears.
|
|
|
|
if (this._handle.writeQueueSize === 0) |
|
|
|
cb(); |
|
|
|
else |
|
|
|
writeReq.cb = cb; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.__defineGetter__('bytesWritten', function() { |
|
|
|