Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Revert some changes made in 12486a6

Some of the perf improvements from many-writes-fix branch were accidentally
undone in that commit. This puts them back in.
  • Loading branch information...
ry committed Oct 12, 2011
1 parent 16e1d5b commit 25ff181300550005d1d3ca990929d874453872a8
Showing with 42 additions and 48 deletions.
  1. +42 −48 lib/net.js
@@ -59,7 +59,7 @@ exports.connect = exports.createConnection = function(port /* [host], [cb] */) {

/* called when creating new Socket, or when re-using a closed Socket */
function initSocketHandle(self) {
self._writeRequests = [];
self._pendingWriteReqs = 0;

self._flags = 0;
self._connectQueueSize = 0;
@@ -237,7 +237,7 @@ Socket.prototype.destroySoon = function() {
this.writable = false;
this._flags |= FLAG_DESTROY_SOON;

if (this._writeRequests.length == 0) {
if (this._pendingWriteReqs == 0) {
this.destroy();
}
};
@@ -342,29 +342,42 @@ Socket.prototype.setEncoding = function(encoding) {
};


Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
var encoding, fd, cb;
Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return {};
}
if (!this._peername) {
this._peername = this._handle.getpeername();
}
return this._peername;
};


Socket.prototype.__defineGetter__('remoteAddress', function() {
return this._getpeername().address;
});


Socket.prototype.__defineGetter__('remotePort', function() {
return this._getpeername().port;
});


/*
* Arguments data, [encoding], [cb]
*/
Socket.prototype.write = function(data, arg1, arg2) {
var encoding, cb;

// parse arguments
if (typeof arguments[3] == 'function') {
cb = arguments[3];
fd = arguments[2];
encoding = arguments[1];
} else if (typeof arguments[2] == 'function') {
cb = arguments[2];
if (typeof arguments[1] == 'number') {
fd = arguments[1];
if (arg1) {
if (typeof arg1 === 'string') {
encoding = arg1;
cb = arg2;
} else if (typeof arg1 === 'function') {
cb = arg1;
} else {
encoding = arguments[1];
}
} else if (typeof arguments[1] == 'function') {
cb = arguments[1];
} else {
if (typeof arguments[1] == 'number') {
fd = arguments[1];
} else {
encoding = arguments[1];
fd = arguments[2];
throw new Error("bad arg");
}
}

@@ -379,9 +392,9 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
if (this._connecting) {
this._connectQueueSize += data.length;
if (this._connectQueue) {
this._connectQueue.push([data, encoding, fd, cb]);
this._connectQueue.push([data, encoding, cb]);
} else {
this._connectQueue = [[data, encoding, fd, cb]];
this._connectQueue = [[data, encoding, cb]];
}
return false;
}
@@ -395,7 +408,7 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {

writeReq.oncomplete = afterWrite;
writeReq.cb = cb;
this._writeRequests.push(writeReq);
this._pendingWriteReqs++;

return this._handle.writeQueueSize == 0;
};
@@ -410,18 +423,17 @@ function afterWrite(status, handle, req, buffer) {
}
// TODO check status.

var req_ = self._writeRequests.shift();
assert.equal(req, req_);
self._pendingWriteReqs--;

if (self._writeRequests.length == 0) {
if (self._pendingWriteReqs == 0) {
// TODO remove all uses of ondrain - this is not a good hack.
if (self.ondrain) self.ondrain();
self.emit('drain');
}

if (req.cb) req.cb();

if (self._writeRequests.length == 0 && self._flags & FLAG_DESTROY_SOON) {
if (self._pendingWriteReqs == 0 && self._flags & FLAG_DESTROY_SOON) {
self.destroy();
}
}
@@ -691,7 +703,7 @@ Server.prototype.listen = function() {

var port = toPort(arguments[0]);

var TCP = process.binding('tcp_wrap').TCP
var TCP = process.binding('tcp_wrap').TCP;

if (arguments.length == 0 || typeof arguments[0] == 'function') {
// Don't bind(). OS will assign a port with INADDR_ANY.
@@ -730,7 +742,6 @@ Server.prototype.address = function() {
function onconnection(clientHandle) {
var handle = this;
var self = handle.socket;
var peername;

debug('onconnection');

@@ -744,29 +755,12 @@ function onconnection(clientHandle) {
return;
}

// Todo: implement this for unix sockets
if (clientHandle.getpeername) {
peername = clientHandle.getpeername();
if (!peername.address || !peername.port) {
var err = errnoException(errno, 'accept');
clientHandle.close();
self.emit('error', err);
return;
}
}

var socket = new Socket({
handle: clientHandle,
allowHalfOpen: self.allowHalfOpen
});
socket.readable = socket.writable = true;

if (peername) {
socket.remoteAddress = peername.address;
socket.remotePort = peername.port;
// TODO: set family as well
}

socket.resume();

self.connections++;

0 comments on commit 25ff181

Please sign in to comment.
You can’t perform that action at this time.