|
@ -12,18 +12,47 @@ var listen = process.listen; |
|
|
var accept = process.accept; |
|
|
var accept = process.accept; |
|
|
var close = process.close; |
|
|
var close = process.close; |
|
|
var shutdown = process.shutdown; |
|
|
var shutdown = process.shutdown; |
|
|
|
|
|
var read = process.read; |
|
|
|
|
|
var write = process.write; |
|
|
|
|
|
var toRead = process.toRead; |
|
|
|
|
|
|
|
|
var Peer = function (peerInfo) { |
|
|
var Peer = function (peerInfo) { |
|
|
process.EventEmitter.call(); |
|
|
process.EventEmitter.call(); |
|
|
|
|
|
|
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
self.fd = peerInfo.fd; |
|
|
process.mixin(self, peerInfo); |
|
|
self.remoteAddress = peerInfo.remoteAddress; |
|
|
|
|
|
self.remotePort = peerInfo.remotePort; |
|
|
// Allocated on demand.
|
|
|
|
|
|
self.recvBuffer = null; |
|
|
|
|
|
|
|
|
self.readWatcher = new process.IOWatcher(function () { |
|
|
self.readWatcher = new process.IOWatcher(function () { |
|
|
debug(self.fd + " readable"); |
|
|
debug("\n" + self.fd + " readable"); |
|
|
|
|
|
|
|
|
|
|
|
// If this is the first recv (recvBuffer doesn't exist) or we've used up
|
|
|
|
|
|
// most of the recvBuffer, allocate a new one.
|
|
|
|
|
|
if (!self.recvBuffer || |
|
|
|
|
|
self.recvBuffer.length - self.recvBufferBytesUsed < 128) { |
|
|
|
|
|
self._allocateNewRecvBuf(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
debug("recvBufferBytesUsed " + self.recvBufferBytesUsed); |
|
|
|
|
|
var bytesRead = read(self.fd, |
|
|
|
|
|
self.recvBuffer, |
|
|
|
|
|
self.recvBufferBytesUsed, |
|
|
|
|
|
self.recvBuffer.length - self.recvBufferBytesUsed); |
|
|
|
|
|
debug("bytesRead " + bytesRead + "\n"); |
|
|
|
|
|
|
|
|
|
|
|
if (bytesRead == 0) { |
|
|
|
|
|
self.readable = false; |
|
|
|
|
|
self.readWatcher.stop(); |
|
|
|
|
|
self.emit("eof"); |
|
|
|
|
|
} else { |
|
|
|
|
|
var slice = self.recvBuffer.slice(self.recvBufferBytesUsed, |
|
|
|
|
|
self.recvBufferBytesUsed + bytesRead); |
|
|
|
|
|
self.recvBufferBytesUsed += bytesRead; |
|
|
|
|
|
self.emit("receive", slice); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
self.readWatcher.set(self.fd, true, false); |
|
|
self.readWatcher.set(self.fd, true, false); |
|
|
self.readWatcher.start(); |
|
|
self.readWatcher.start(); |
|
@ -35,9 +64,36 @@ var Peer = function (peerInfo) { |
|
|
|
|
|
|
|
|
self.readable = true; |
|
|
self.readable = true; |
|
|
self.writable = true; |
|
|
self.writable = true; |
|
|
|
|
|
|
|
|
|
|
|
self._out = []; |
|
|
}; |
|
|
}; |
|
|
process.inherits(Peer, process.EventEmitter); |
|
|
process.inherits(Peer, process.EventEmitter); |
|
|
|
|
|
|
|
|
|
|
|
Peer.prototype._allocateNewRecvBuf = function () { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
|
|
|
|
var newBufferSize = 1024; // TODO make this adjustable from user API
|
|
|
|
|
|
|
|
|
|
|
|
if (toRead) { |
|
|
|
|
|
// Note: only Linux supports toRead().
|
|
|
|
|
|
// Is the extra system call even worth it?
|
|
|
|
|
|
var bytesToRead = toRead(self.fd); |
|
|
|
|
|
if (bytesToRead > 1024) { |
|
|
|
|
|
newBufferSize = 4*1024; |
|
|
|
|
|
} else if (bytesToRead == 0) { |
|
|
|
|
|
// Probably getting an EOF - so let's not allocate so much.
|
|
|
|
|
|
if (self.recvBuffer && |
|
|
|
|
|
self.recvBuffer.length - self.recvBufferBytesUsed > 0) { |
|
|
|
|
|
return; // just recv the eof on the old buf.
|
|
|
|
|
|
} |
|
|
|
|
|
newBufferSize = 128; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.recvBuffer = new process.Buffer(newBufferSize); |
|
|
|
|
|
self.recvBufferBytesUsed = 0; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
Peer.prototype.close = function () { |
|
|
Peer.prototype.close = function () { |
|
|
this.readable = false; |
|
|
this.readable = false; |
|
|
this.writable = false; |
|
|
this.writable = false; |
|
@ -49,8 +105,6 @@ Peer.prototype.close = function () { |
|
|
this.fd = null; |
|
|
this.fd = null; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var Server = function (listener) { |
|
|
var Server = function (listener) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
@ -73,14 +127,26 @@ var Server = function (listener) { |
|
|
}; |
|
|
}; |
|
|
process.inherits(Server, process.EventEmitter); |
|
|
process.inherits(Server, process.EventEmitter); |
|
|
|
|
|
|
|
|
Server.prototype.listen = function (port, host) { |
|
|
Server.prototype.listen = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
if (self.fd) throw new Error("Already running"); |
|
|
if (self.fd) throw new Error("Already running"); |
|
|
|
|
|
|
|
|
self.fd = process.socket("TCP"); |
|
|
if (typeof(arguments[0]) == "string" && arguments.length == 1) { |
|
|
// TODO dns resolution
|
|
|
// the first argument specifies a path
|
|
|
bind(self.fd, port, host); |
|
|
self.fd = process.socket("UNIX"); |
|
|
|
|
|
// TODO unlink sockfile if exists?
|
|
|
|
|
|
// if (lstat(SOCKFILE, &tstat) == 0) {
|
|
|
|
|
|
// assert(S_ISSOCK(tstat.st_mode));
|
|
|
|
|
|
// unlink(SOCKFILE);
|
|
|
|
|
|
// }
|
|
|
|
|
|
bind(self.fd, arguments[0]); |
|
|
|
|
|
} else { |
|
|
|
|
|
// the first argument is the port, the second an IP
|
|
|
|
|
|
self.fd = process.socket("TCP"); |
|
|
|
|
|
// TODO dns resolution on arguments[1]
|
|
|
|
|
|
bind(self.fd, arguments[0], arguments[1]); |
|
|
|
|
|
} |
|
|
listen(self.fd, 128); // TODO configurable backlog
|
|
|
listen(self.fd, 128); // TODO configurable backlog
|
|
|
|
|
|
|
|
|
self.watcher.set(self.fd, true, false); |
|
|
self.watcher.set(self.fd, true, false); |
|
@ -97,7 +163,12 @@ Server.prototype.close = function () { |
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
process.Buffer.prototype.toString = function () { |
|
|
|
|
|
return this.utf8Slice(0, this.length); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
var sys = require("sys"); |
|
|
var sys = require("sys"); |
|
|
|
|
|
|
|
|
var server = new Server(function (peer) { |
|
|
var server = new Server(function (peer) { |
|
|
sys.puts("connection (" + peer.fd + "): " |
|
|
sys.puts("connection (" + peer.fd + "): " |
|
|
+ peer.remoteAddress |
|
|
+ peer.remoteAddress |
|
@ -105,7 +176,11 @@ var server = new Server(function (peer) { |
|
|
+ peer.remotePort |
|
|
+ peer.remotePort |
|
|
); |
|
|
); |
|
|
sys.puts("server fd: " + server.fd); |
|
|
sys.puts("server fd: " + server.fd); |
|
|
peer.close(); |
|
|
|
|
|
|
|
|
peer.addListener("receive", function (b) { |
|
|
|
|
|
sys.puts("recv (" + b.length + "): " + b); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
//server.listen(8000);
|
|
|
server.listen(8000); |
|
|
server.listen(8000); |
|
|
sys.puts("server fd: " + server.fd); |
|
|
sys.puts("server fd: " + server.fd); |
|
|