|
@ -16,15 +16,18 @@ var read = process.read; |
|
|
var write = process.write; |
|
|
var write = process.write; |
|
|
var toRead = process.toRead; |
|
|
var toRead = process.toRead; |
|
|
|
|
|
|
|
|
var Peer = function (peerInfo) { |
|
|
var Stream = function (peerInfo) { |
|
|
process.EventEmitter.call(); |
|
|
process.EventEmitter.call(); |
|
|
|
|
|
|
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
process.mixin(self, peerInfo); |
|
|
self.fd = peerInfo.fd; |
|
|
|
|
|
self.remoteAddress = peerInfo.remoteAddress; |
|
|
|
|
|
self.remotePort = peerInfo.remotePort; |
|
|
|
|
|
|
|
|
// Allocated on demand.
|
|
|
// Allocated on demand.
|
|
|
self.recvBuffer = null; |
|
|
self.recvBuffer = null; |
|
|
|
|
|
self.sendQueue = []; |
|
|
|
|
|
|
|
|
self.readWatcher = new process.IOWatcher(function () { |
|
|
self.readWatcher = new process.IOWatcher(function () { |
|
|
debug("\n" + self.fd + " readable"); |
|
|
debug("\n" + self.fd + " readable"); |
|
@ -32,15 +35,15 @@ var Peer = function (peerInfo) { |
|
|
// If this is the first recv (recvBuffer doesn't exist) or we've used up
|
|
|
// If this is the first recv (recvBuffer doesn't exist) or we've used up
|
|
|
// most of the recvBuffer, allocate a new one.
|
|
|
// most of the recvBuffer, allocate a new one.
|
|
|
if (!self.recvBuffer || |
|
|
if (!self.recvBuffer || |
|
|
self.recvBuffer.length - self.recvBufferBytesUsed < 128) { |
|
|
self.recvBuffer.length - self.recvBuffer.used < 128) { |
|
|
self._allocateNewRecvBuf(); |
|
|
self._allocateNewRecvBuf(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
debug("recvBufferBytesUsed " + self.recvBufferBytesUsed); |
|
|
debug("recvBuffer.used " + self.recvBuffer.used); |
|
|
var bytesRead = read(self.fd, |
|
|
var bytesRead = read(self.fd, |
|
|
self.recvBuffer, |
|
|
self.recvBuffer, |
|
|
self.recvBufferBytesUsed, |
|
|
self.recvBuffer.used, |
|
|
self.recvBuffer.length - self.recvBufferBytesUsed); |
|
|
self.recvBuffer.length - self.recvBuffer.used); |
|
|
debug("bytesRead " + bytesRead + "\n"); |
|
|
debug("bytesRead " + bytesRead + "\n"); |
|
|
|
|
|
|
|
|
if (bytesRead == 0) { |
|
|
if (bytesRead == 0) { |
|
@ -48,9 +51,9 @@ var Peer = function (peerInfo) { |
|
|
self.readWatcher.stop(); |
|
|
self.readWatcher.stop(); |
|
|
self.emit("eof"); |
|
|
self.emit("eof"); |
|
|
} else { |
|
|
} else { |
|
|
var slice = self.recvBuffer.slice(self.recvBufferBytesUsed, |
|
|
var slice = self.recvBuffer.slice(self.recvBuffer.used, |
|
|
self.recvBufferBytesUsed + bytesRead); |
|
|
self.recvBuffer.used + bytesRead); |
|
|
self.recvBufferBytesUsed += bytesRead; |
|
|
self.recvBuffer.used += bytesRead; |
|
|
self.emit("receive", slice); |
|
|
self.emit("receive", slice); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
@ -58,18 +61,16 @@ var Peer = function (peerInfo) { |
|
|
self.readWatcher.start(); |
|
|
self.readWatcher.start(); |
|
|
|
|
|
|
|
|
self.writeWatcher = new process.IOWatcher(function () { |
|
|
self.writeWatcher = new process.IOWatcher(function () { |
|
|
debug(self.fd + " writable"); |
|
|
self.flush(); |
|
|
}); |
|
|
}); |
|
|
self.writeWatcher.set(self.fd, false, true); |
|
|
self.writeWatcher.set(self.fd, false, true); |
|
|
|
|
|
|
|
|
self.readable = true; |
|
|
self.readable = true; |
|
|
self.writable = true; |
|
|
self.writable = true; |
|
|
|
|
|
|
|
|
self._out = []; |
|
|
|
|
|
}; |
|
|
}; |
|
|
process.inherits(Peer, process.EventEmitter); |
|
|
process.inherits(Stream, process.EventEmitter); |
|
|
|
|
|
|
|
|
Peer.prototype._allocateNewRecvBuf = function () { |
|
|
Stream.prototype._allocateNewRecvBuf = function () { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var newBufferSize = 1024; // TODO make this adjustable from user API
|
|
|
var newBufferSize = 1024; // TODO make this adjustable from user API
|
|
@ -83,7 +84,7 @@ Peer.prototype._allocateNewRecvBuf = function () { |
|
|
} else if (bytesToRead == 0) { |
|
|
} else if (bytesToRead == 0) { |
|
|
// Probably getting an EOF - so let's not allocate so much.
|
|
|
// Probably getting an EOF - so let's not allocate so much.
|
|
|
if (self.recvBuffer && |
|
|
if (self.recvBuffer && |
|
|
self.recvBuffer.length - self.recvBufferBytesUsed > 0) { |
|
|
self.recvBuffer.length - self.recvBuffer.used > 0) { |
|
|
return; // just recv the eof on the old buf.
|
|
|
return; // just recv the eof on the old buf.
|
|
|
} |
|
|
} |
|
|
newBufferSize = 128; |
|
|
newBufferSize = 128; |
|
@ -91,10 +92,115 @@ Peer.prototype._allocateNewRecvBuf = function () { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
self.recvBuffer = new process.Buffer(newBufferSize); |
|
|
self.recvBuffer = new process.Buffer(newBufferSize); |
|
|
self.recvBufferBytesUsed = 0; |
|
|
self.recvBuffer.used = 0; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Stream.prototype._allocateSendBuffer = function () { |
|
|
|
|
|
var b = new process.Buffer(1024); |
|
|
|
|
|
b.used = 0; |
|
|
|
|
|
b.sent = 0; |
|
|
|
|
|
this.sendQueue.push(b); |
|
|
|
|
|
return b; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Stream.prototype.send = function (data, encoding) { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
if (typeof(data) == "string") { |
|
|
|
|
|
var buffer; |
|
|
|
|
|
if (self.sendQueue.length == 0) { |
|
|
|
|
|
buffer = self._allocateSendBuffer(); |
|
|
|
|
|
} else { |
|
|
|
|
|
// walk through the sendQueue, find the first empty buffer
|
|
|
|
|
|
for (var i = 0; i < self.sendQueue.length; i++) { |
|
|
|
|
|
if (self.sendQueue[i].used == 0) { |
|
|
|
|
|
buffer = self.sendQueue[i]; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// if we didn't find one, take the last
|
|
|
|
|
|
if (!buffer) { |
|
|
|
|
|
buffer = self.sendQueue[self.sendQueue.length-1]; |
|
|
|
|
|
// if last buffer is empty
|
|
|
|
|
|
if (buffer.length == buffer.used) buffer = self._allocateSendBuffer(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
encoding = encoding || "ascii"; // default to ascii since it's faster
|
|
|
|
|
|
|
|
|
|
|
|
var charsWritten; |
|
|
|
|
|
|
|
|
|
|
|
if (encoding.toLowerCase() == "utf8") { |
|
|
|
|
|
charsWritten = buffer.utf8Write(data, |
|
|
|
|
|
buffer.used, |
|
|
|
|
|
buffer.length - buffer.used); |
|
|
|
|
|
buffer.used += process.Buffer.utf8Length(data.slice(0, charsWritten)); |
|
|
|
|
|
} else { |
|
|
|
|
|
// ascii
|
|
|
|
|
|
charsWritten = buffer.asciiWrite(data, |
|
|
|
|
|
buffer.used, |
|
|
|
|
|
buffer.length - buffer.used); |
|
|
|
|
|
buffer.used += charsWritten; |
|
|
|
|
|
debug("ascii charsWritten " + charsWritten); |
|
|
|
|
|
debug("ascii buffer.used " + buffer.used); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we didn't finish, then recurse with the rest of the string.
|
|
|
|
|
|
if (charsWritten < data.length) { |
|
|
|
|
|
debug("recursive send"); |
|
|
|
|
|
self.send(data.slice(charsWritten), encoding); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// data is a process.Buffer
|
|
|
|
|
|
|
|
|
|
|
|
// walk through the sendQueue, find the first empty buffer
|
|
|
|
|
|
var inserted = false; |
|
|
|
|
|
data.sent = 0; |
|
|
|
|
|
data.used = data.length; |
|
|
|
|
|
for (var i = 0; i < self.sendQueue.length; i++) { |
|
|
|
|
|
if (self.sendQueue[i].used == 0) { |
|
|
|
|
|
// if found, insert the data there
|
|
|
|
|
|
self.sendQueue.splice(i, 0, data); |
|
|
|
|
|
inserted = true; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!inserted) self.sendQueue.push(data); |
|
|
|
|
|
} |
|
|
|
|
|
this.flush(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// returns true if flushed without getting EAGAIN
|
|
|
|
|
|
// false if it got EAGAIN
|
|
|
|
|
|
Stream.prototype.flush = function () { |
|
|
|
|
|
var self = this; |
|
|
|
|
|
var bytesWritten; |
|
|
|
|
|
while (self.sendQueue.length > 0) { |
|
|
|
|
|
var b = self.sendQueue[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (b.sent == b.used) { |
|
|
|
|
|
// this can be improved - save the buffer for later?
|
|
|
|
|
|
self.sendQueue.shift() |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bytesWritten = write(self.fd, |
|
|
|
|
|
b, |
|
|
|
|
|
b.sent, |
|
|
|
|
|
b.used - b.sent); |
|
|
|
|
|
if (bytesWritten === null) { |
|
|
|
|
|
this.writeWatcher.start(); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
b.sent += bytesWritten; |
|
|
|
|
|
debug("bytes sent: " + b.sent); |
|
|
|
|
|
} |
|
|
|
|
|
this.writeWatcher.stop(); |
|
|
|
|
|
return true; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Peer.prototype.close = function () { |
|
|
Stream.prototype.close = function () { |
|
|
this.readable = false; |
|
|
this.readable = false; |
|
|
this.writable = false; |
|
|
this.writable = false; |
|
|
|
|
|
|
|
@ -113,14 +219,11 @@ var Server = function (listener) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
self.watcher = new process.IOWatcher(function (readable, writeable) { |
|
|
self.watcher = new process.IOWatcher(function (readable, writeable) { |
|
|
debug("readable " + readable); |
|
|
|
|
|
debug("writable " + writeable); |
|
|
|
|
|
while (self.fd) { |
|
|
while (self.fd) { |
|
|
debug("accept from " + self.fd); |
|
|
|
|
|
var peerInfo = accept(self.fd); |
|
|
var peerInfo = accept(self.fd); |
|
|
debug("accept: " + JSON.stringify(peerInfo)); |
|
|
debug("accept: " + JSON.stringify(peerInfo)); |
|
|
if (!peerInfo) return; |
|
|
if (!peerInfo) return; |
|
|
var peer = new Peer(peerInfo); |
|
|
var peer = new Stream(peerInfo); |
|
|
self.emit("connection", peer); |
|
|
self.emit("connection", peer); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
@ -132,6 +235,7 @@ Server.prototype.listen = function () { |
|
|
|
|
|
|
|
|
if (self.fd) throw new Error("Already running"); |
|
|
if (self.fd) throw new Error("Already running"); |
|
|
|
|
|
|
|
|
|
|
|
var backlogIndex; |
|
|
if (typeof(arguments[0]) == "string" && arguments.length == 1) { |
|
|
if (typeof(arguments[0]) == "string" && arguments.length == 1) { |
|
|
// the first argument specifies a path
|
|
|
// the first argument specifies a path
|
|
|
self.fd = process.socket("UNIX"); |
|
|
self.fd = process.socket("UNIX"); |
|
@ -141,13 +245,15 @@ Server.prototype.listen = function () { |
|
|
// unlink(SOCKFILE);
|
|
|
// unlink(SOCKFILE);
|
|
|
// }
|
|
|
// }
|
|
|
bind(self.fd, arguments[0]); |
|
|
bind(self.fd, arguments[0]); |
|
|
|
|
|
backlogIndex = 1; |
|
|
} else { |
|
|
} else { |
|
|
// the first argument is the port, the second an IP
|
|
|
// the first argument is the port, the second an IP
|
|
|
self.fd = process.socket("TCP"); |
|
|
self.fd = process.socket("TCP"); |
|
|
// TODO dns resolution on arguments[1]
|
|
|
// TODO dns resolution on arguments[1]
|
|
|
bind(self.fd, arguments[0], arguments[1]); |
|
|
bind(self.fd, arguments[0], arguments[1]); |
|
|
|
|
|
backlogIndex = typeof(arguments[1]) == "string" ? 2 : 1; |
|
|
} |
|
|
} |
|
|
listen(self.fd, 128); // TODO configurable backlog
|
|
|
listen(self.fd, arguments[backlogIndex] ? arguments[backlogIndex] : 128); |
|
|
|
|
|
|
|
|
self.watcher.set(self.fd, true, false); |
|
|
self.watcher.set(self.fd, true, false); |
|
|
self.watcher.start(); |
|
|
self.watcher.start(); |
|
@ -179,6 +285,7 @@ var server = new Server(function (peer) { |
|
|
|
|
|
|
|
|
peer.addListener("receive", function (b) { |
|
|
peer.addListener("receive", function (b) { |
|
|
sys.puts("recv (" + b.length + "): " + b); |
|
|
sys.puts("recv (" + b.length + "): " + b); |
|
|
|
|
|
peer.send("pong\r\n"); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
//server.listen(8000);
|
|
|
//server.listen(8000);
|
|
|