Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add optional features: One domain per incoming data event, catchall Socket listener #2269

Closed
wants to merge 4 commits into from

3 participants

@thejh

Hello,
this patch adds two features:

  • Socket.addGenericListener allows you to listen to data, write, connect and close events of all sockets
  • if Socket.eventDomains is set, you get one domain per incoming data event

Yes, if you use these features, you'll probably get some performance impact, but it also allows you to do cool stuff like a network traffic viewer that can show you which events belong together: http://www.youtube.com/watch?v=4iSDwAb8XEA

@Nodejs-Jenkins
Collaborator

Can one of the admins verify this patch?

@trevnorris trevnorris closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 12 deletions.
  1. +46 −12 lib/net.js
View
58 lib/net.js
@@ -115,6 +115,11 @@ function Socket(options) {
stream.Stream.call(this);
+ if (Socket.genericEmitter != null) {
+ this.on('connect', Socket.genericEmitter.emit.bind(Socket.genericEmitter, 'connect', this));
+ this.on('close', Socket.genericEmitter.emit.bind(Socket.genericEmitter, 'close', this));
+ }
+
if (typeof options == 'number') {
// Legacy interface.
// Must support legacy interface. NPM depends on it.
@@ -143,6 +148,14 @@ exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.
+Socket.addGenericListener = function(event, listener) {
+ if (Socket.genericEmitter == null) {
+ Socket.genericEmitter = new events.EventEmitter;
+ }
+ Socket.genericEmitter.on(event, listener);
+};
+
+
Socket.prototype.listen = function() {
var self = this;
self.on('connection', arguments[0]);
@@ -333,22 +346,39 @@ function onread(buffer, offset, length) {
if (buffer) {
// Emit 'data' event.
- if (self._decoder) {
- // Emit a string.
- var string = self._decoder.write(buffer.slice(offset, end));
- if (string.length) self.emit('data', string);
+ var data = buffer.slice(offset, end);
+ var eventId;
+
+ if (Socket.eventDomains) {
+ eventId = Math.random();
+ var domain = domains.create(null, emitEvent);
+ // FIXME ugly, ugly, ugly hack
+ process.nextTick(function(){});
+ domain.eventId = eventId;
+ domain.isNetDataEventDomain = true;
} else {
- // Emit a slice. Attempt to avoid slicing the buffer if no one is
- // listening for 'data'.
- if (self._events && self._events['data']) {
- self.emit('data', buffer.slice(offset, end));
- }
+ emitEvent()
}
- self.bytesRead += length;
+ function emitEvent() {
+ if (Socket.genericEmitter != null) {
+ Socket.genericEmitter.emit('data', self, data, eventId);
+ }
- // Optimization: emit the original buffer with end points
- if (self.ondata) self.ondata(buffer, offset, end);
+ if (self._decoder) {
+ // Emit a string.
+ var string = self._decoder.write(data);
+ if (string.length) self.emit('data', string);
+ } else {
+ // Emit a slice.
+ self.emit('data', data);
+ }
+
+ self.bytesRead += length;
+
+ // Optimization: emit the original buffer with end points
+ if (self.ondata) self.ondata(buffer, offset, end);
+ }
} else if (errno == 'EOF') {
// EOF
@@ -445,6 +475,10 @@ Socket.prototype.write = function(data, arg1, arg2) {
Socket.prototype._write = function(data, encoding, cb) {
timers.active(this);
+ if (Socket.genericEmitter != null) {
+ Socket.genericEmitter.emit('write', this, data);
+ }
+
// `encoding` is unused right now, `data` is always a buffer.
var writeReq = this._handle.write(data);
Something went wrong with that request. Please try again.