mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
net: Move createWriteReq to separate function
This commit is contained in:
parent
9785ab6057
commit
aec2f733f9
61
lib/net.js
61
lib/net.js
@ -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,39 +553,12 @@ 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;
|
||||
|
||||
if (Buffer.isBuffer(data)) {
|
||||
writeReq = this._handle.writeBuffer(data);
|
||||
|
||||
} else {
|
||||
switch (encoding) {
|
||||
case 'utf8':
|
||||
case 'utf-8':
|
||||
writeReq = this._handle.writeUtf8String(data);
|
||||
break;
|
||||
|
||||
case 'ascii':
|
||||
writeReq = this._handle.writeAsciiString(data);
|
||||
break;
|
||||
|
||||
case 'ucs2':
|
||||
case 'ucs-2':
|
||||
case 'utf16le':
|
||||
case 'utf-16le':
|
||||
writeReq = this._handle.writeUcs2String(data);
|
||||
break;
|
||||
|
||||
default:
|
||||
writeReq = this._handle.writeBuffer(new Buffer(data, encoding));
|
||||
break;
|
||||
}
|
||||
}
|
||||
var enc = Buffer.isBuffer(data) ? 'buffer' : encoding;
|
||||
var writeReq = createWriteReq(this._handle, data, enc);
|
||||
|
||||
if (!writeReq || typeof writeReq !== 'object')
|
||||
return this._destroy(errnoException(errno, 'write'), cb);
|
||||
@ -606,6 +574,29 @@ Socket.prototype._write = function(dataEncoding, cb) {
|
||||
writeReq.cb = cb;
|
||||
};
|
||||
|
||||
function createWriteReq(handle, data, encoding) {
|
||||
switch (encoding) {
|
||||
case 'buffer':
|
||||
return handle.writeBuffer(data);
|
||||
|
||||
case 'utf8':
|
||||
case 'utf-8':
|
||||
return handle.writeUtf8String(data);
|
||||
|
||||
case 'ascii':
|
||||
return handle.writeAsciiString(data);
|
||||
|
||||
case 'ucs2':
|
||||
case 'ucs-2':
|
||||
case 'utf16le':
|
||||
case 'utf-16le':
|
||||
return handle.writeUcs2String(data);
|
||||
|
||||
default:
|
||||
return handle.writeBuffer(new Buffer(data, encoding));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Socket.prototype.__defineGetter__('bytesWritten', function() {
|
||||
var bytes = this._bytesDispatched,
|
||||
|
Loading…
Reference in New Issue
Block a user