Browse Source

Optimize: Use callbacks instead of events in net2

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
979f5889d5
  1. 9
      lib/http2.js
  2. 32
      lib/net.js

9
lib/http2.js

@ -387,12 +387,11 @@ function connectionListener (socket) {
// we need to keep track of the order they were sent.
var responses = [];
socket.addListener('dataLite', function (d, start, end) {
socket.ondata = function (d, start, end) {
parser.execute(d, start, end - start);
});
};
// is this really needed?
socket.addListener('end', function () {
socket.onend = function () {
parser.finish();
// unref the parser for easy gc
freeParser(parser);
@ -402,7 +401,7 @@ function connectionListener (socket) {
} else {
responses[responses.length-1].closeOnFinish = true;
}
});
};
parser.socket = socket;
// The following callback is issued after the headers have been read on a

32
lib/net.js

@ -1,3 +1,4 @@
var sys = require("./sys");
var debugLevel = 0;
if ('NODE_DEBUG' in process.ENV) debugLevel = 1;
function debug (x) {
@ -118,19 +119,23 @@ function Socket (peerInfo) {
if (!recvMsg.fd && bytesRead == 0) {
self.readable = false;
self._readWatcher.stop();
self.emit('end');
if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
if (!self.writable) self.forceClose();
} else if (bytesRead > 0) {
var start = recvBuffer.used;
var end = recvBuffer.used + bytesRead;
if (self.listeners('data').length) {
if (self._events && self._events['data']) {
// emit a slice
self.emit('data', recvBuffer.slice(start, end));
}
if (self.listeners('dataLite').length) {
// emit the original buffer with end points.
self.emit('dataLite', recvBuffer, start, end);
}
// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(recvBuffer, start, end);
recvBuffer.used += bytesRead;
}
};
@ -141,15 +146,17 @@ function Socket (peerInfo) {
self.sendQueueSize = 0; // in bytes, not to be confused with sendQueue.length!
self.sendMessageQueueSize = 0; // number of messages remaining to be sent
self._doFlush = function () {
/* Socket becomes writeable on connect() but don't flush if there's
* nothing actually to write */
// Socket becomes writeable on connect() but don't flush if there's
// nothing actually to write
if ((self.sendQueueSize == 0) && (self.sendMessageQueueSize == 0)) {
return;
}
if (self.flush()) {
assert(self.sendQueueSize == 0);
assert(self.sendMessageQueueSize == 0);
self.emit("drain");
if (self._events && self._events['drain']) self.emit("drain");
if (self.ondrain) self.ondrain(); // Optimization
}
};
self._writeWatcher = ioWatchers.alloc();
@ -302,7 +309,8 @@ Socket.prototype.sendFD = function(socketToPass) {
};
// Flushes the write buffer out. Emits "drain" if the buffer is empty.
// Flushes the write buffer out.
// Returns true if the entire buffer was flushed.
Socket.prototype.flush = function () {
var self = this;
@ -314,7 +322,7 @@ Socket.prototype.flush = function () {
if (b == END_OF_FILE) {
self._shutdown();
break;
return false;
}
if (b.sent == b.used) {
@ -338,7 +346,7 @@ Socket.prototype.flush = function () {
}
} catch (e) {
self.forceClose(e);
return;
return false;
}
if (bytesWritten === null) {

Loading…
Cancel
Save