Browse Source

net: Move createWriteReq to separate function

v0.9.5-release
isaacs 12 years ago
parent
commit
aec2f733f9
  1. 57
      lib/net.js

57
lib/net.js

@ -534,21 +534,16 @@ Socket.prototype.write = function(chunk, encoding, cb) {
Socket.prototype._write = function(dataEncoding, cb) { Socket.prototype._write = function(dataEncoding, cb) {
assert(Array.isArray(dataEncoding)); // assert(Array.isArray(dataEncoding));
var data = dataEncoding[0]; var data = dataEncoding[0];
var encoding = dataEncoding[1] || 'utf8'; 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. // If we are still connecting, then buffer this for later.
// The Writable logic will buffer up any more writes while // The Writable logic will buffer up any more writes while
// waiting for this one to be done. // waiting for this one to be done.
if (this._connecting) { if (this._connecting) {
debug('_write: waiting for connection');
this._pendingWrite = dataEncoding; this._pendingWrite = dataEncoding;
this.once('connect', function() { this.once('connect', function() {
debug('_write: connected now, try again');
this._write(dataEncoding, cb); this._write(dataEncoding, cb);
}); });
return; return;
@ -558,54 +553,50 @@ Socket.prototype._write = function(dataEncoding, cb) {
timers.active(this); timers.active(this);
if (!this._handle) { if (!this._handle) {
debug('already destroyed');
this._destroy(new Error('This socket is closed.'), cb); this._destroy(new Error('This socket is closed.'), cb);
return false; return false;
} }
var writeReq; var enc = Buffer.isBuffer(data) ? 'buffer' : encoding;
var writeReq = createWriteReq(this._handle, data, enc);
if (Buffer.isBuffer(data)) { if (!writeReq || typeof writeReq !== 'object')
writeReq = this._handle.writeBuffer(data); 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) { switch (encoding) {
case 'buffer':
return handle.writeBuffer(data);
case 'utf8': case 'utf8':
case 'utf-8': case 'utf-8':
writeReq = this._handle.writeUtf8String(data); return handle.writeUtf8String(data);
break;
case 'ascii': case 'ascii':
writeReq = this._handle.writeAsciiString(data); return handle.writeAsciiString(data);
break;
case 'ucs2': case 'ucs2':
case 'ucs-2': case 'ucs-2':
case 'utf16le': case 'utf16le':
case 'utf-16le': case 'utf-16le':
writeReq = this._handle.writeUcs2String(data); return handle.writeUcs2String(data);
break;
default: default:
writeReq = this._handle.writeBuffer(new Buffer(data, encoding)); return handle.writeBuffer(new Buffer(data, encoding));
break;
} }
} }
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() { Socket.prototype.__defineGetter__('bytesWritten', function() {
var bytes = this._bytesDispatched, var bytes = this._bytesDispatched,

Loading…
Cancel
Save