node/lib/net.js

914 строки
23 KiB
JavaScript
Исходник Обычный вид История

var sys = require("sys");
var fs = require("fs");
var debugLevel = 0;
2009-12-17 11:31:10 +03:00
if ('NODE_DEBUG' in process.ENV) debugLevel = 1;
function debug (x) {
if (debugLevel > 0) {
2009-12-17 11:31:10 +03:00
process.stdio.writeError(x + '\n');
}
}
2009-12-17 11:31:10 +03:00
2010-03-09 22:59:42 +03:00
var Buffer = process.Buffer;
var IOWatcher = process.IOWatcher;
2009-12-18 16:52:02 +03:00
var assert = process.assert;
2009-12-17 11:31:10 +03:00
var socket = process.socket;
var bind = process.bind;
var connect = process.connect;
var listen = process.listen;
var accept = process.accept;
var close = process.close;
var shutdown = process.shutdown;
var read = process.read;
var recvMsg = process.recvMsg;
var sendFD = process.sendFD;
2009-12-17 11:31:10 +03:00
var write = process.write;
var toRead = process.toRead;
2009-12-30 21:43:47 +03:00
var setNoDelay = process.setNoDelay;
2009-12-17 11:31:10 +03:00
var socketError = process.socketError;
2009-12-18 16:52:02 +03:00
var getsockname = process.getsockname;
var getaddrinfo = process.getaddrinfo;
var needsLookup = process.needsLookup;
2010-02-08 18:36:40 +03:00
var errnoException = process.errnoException;
2009-12-17 11:31:10 +03:00
var EINPROGRESS = process.EINPROGRESS;
var ENOENT = process.ENOENT;
var END_OF_FILE = 0;
// IDLE TIMEOUTS
//
// Because often many sockets will have the same idle timeout we will not
// use one timeout watcher per socket. It is too much overhead. Instead
// we'll use a single watcher for all sockets with the same timeout value
// and a linked list. This technique is described in the libev manual:
// http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
var timeout = new (function () {
// Object containing all lists, timers
// key = time in milliseconds
// value = list
var lists = {};
// show the most idle socket
function peek (list) {
if (list._idlePrev == list) return null;
return list._idlePrev;
}
// remove the most idle socket from the list
function shift (list) {
var first = list._idlePrev;
remove(first);
return first;
}
// remove a socket from its list
function remove (socket) {
socket._idleNext._idlePrev = socket._idlePrev;
socket._idlePrev._idleNext = socket._idleNext;
}
// remove a socket from its list and place at the end.
function append (list, socket) {
remove(socket);
socket._idleNext = list._idleNext;
socket._idleNext._idlePrev = socket;
socket._idlePrev = list
list._idleNext = socket;
}
function normalize (msecs) {
if (!msecs || msecs <= 0) return 0;
// round up to one sec
if (msecs < 1000) return 1000;
// round down to nearest second.
return msecs - (msecs % 1000);
}
// the main function - creates lists on demand and the watchers associated
// with them.
function insert (socket, msecs) {
socket._idleStart = process.now;
socket._idleTimeout = msecs;
if (!msecs) return;
var list;
if (lists[msecs]) {
list = lists[msecs];
} else {
list = new process.Timer();
list._idleNext = list;
list._idlePrev = list;
lists[msecs] = list;
list.callback = function () {
sys.puts('timeout callback ' + msecs);
// TODO - don't stop and start the watcher all the time.
// just set its repeat
var now = process.now;
var first;
while (first = peek(list)) {
var diff = now - first._idleStart;
if (diff < msecs) {
list.again(msecs - diff);
sys.puts(msecs + ' list wait');
return;
} else {
remove(first);
assert(first != peek(list));
first.emit('timeout');
first.forceClose(new Error('idle timeout'));
}
}
sys.puts(msecs + ' list empty');
assert(list._idleNext == list); // list is empty
list.stop();
};
}
if (list._idleNext == list) {
// if empty (re)start the timer
list.again(msecs);
}
append(list, socket);
assert(list._idleNext != list); // list is not empty
}
var unenroll = this.unenroll = function (socket) {
if (socket._idleNext) {
socket._idleNext._idlePrev = socket._idlePrev;
socket._idlePrev._idleNext = socket._idleNext;
var list = lists[socket._idleTimeout];
// if empty then stop the watcher
//sys.puts('unenroll');
if (list && list._idlePrev == list) {
//sys.puts('unenroll: list empty');
list.stop();
}
}
};
// Does not start the time, just sets up the members needed.
this.enroll = function (socket, msecs) {
// if this socket was already in a list somewhere
// then we should unenroll it from that
if (socket._idleNext) unenroll(socket);
socket._idleTimeout = msecs;
socket._idleNext = socket;
socket._idlePrev = socket;
};
// call this whenever the socket is active (not idle)
// it will reset its timeout.
this.active = function (socket) {
var msecs = socket._idleTimeout;
if (msecs) {
var list = lists[msecs];
if (socket._idleNext == socket) {
insert(socket, msecs);
} else {
// inline append
socket._idleStart = process.now;
socket._idleNext._idlePrev = socket._idlePrev;
socket._idlePrev._idleNext = socket._idleNext;
socket._idleNext = list._idleNext;
socket._idleNext._idlePrev = socket;
socket._idlePrev = list
list._idleNext = socket;
}
}
};
})();
// This is a free list to avoid creating so many of the same object.
function FreeList (name, max, constructor) {
this.name = name;
this.constructor = constructor;
this.max = max;
this.list = [];
}
FreeList.prototype.alloc = function () {
2010-03-10 03:27:49 +03:00
//debug("alloc " + this.name + " " + this.list.length);
return this.list.length ? this.list.shift()
: this.constructor.apply(this, arguments);
};
FreeList.prototype.free = function (obj) {
2010-03-10 03:27:49 +03:00
//debug("free " + this.name + " " + this.list.length);
if (this.list.length < this.max) {
this.list.push(obj);
}
};
var ioWatchers = new FreeList("iowatcher", 100, function () {
return new IOWatcher();
});
2010-01-29 06:13:13 +03:00
var nb = 0;
var buffers = new FreeList("buffer", 100, function (l) {
return new Buffer(l);
});
// Allocated on demand.
var recvBuffer = null;
function allocRecvBuffer () {
2010-03-09 22:59:42 +03:00
recvBuffer = new Buffer(40*1024);
recvBuffer.used = 0;
}
2009-12-17 11:31:10 +03:00
function _doFlush () {
var socket = this.socket;
// Socket becomes writeable on connect() but don't flush if there's
// nothing actually to write
if ((socket._writeQueueSize == 0) && (socket._writeMessageQueueSize == 0)) {
return;
}
if (socket.flush()) {
assert(socket._writeQueueSize == 0);
assert(socket._writeMessageQueueSize == 0);
if (socket._events && socket._events['drain']) socket.emit("drain");
if (socket.ondrain) socket.ondrain(); // Optimization
}
}
2010-02-26 23:13:49 +03:00
function initSocket (self) {
self._readWatcher = ioWatchers.alloc();
self._readWatcher.callback = function () {
2009-12-16 10:35:09 +03:00
// If this is the first recv (recvBuffer doesn't exist) or we've used up
// most of the recvBuffer, allocate a new one.
if (recvBuffer) {
if (recvBuffer.length - recvBuffer.used < 128) {
// discard the old recvBuffer. Can't add to the free list because
// users might have refernces to slices on it.
recvBuffer = null;
allocRecvBuffer();
}
} else {
allocRecvBuffer();
2009-12-16 10:35:09 +03:00
}
2010-03-10 03:27:49 +03:00
//debug('recvBuffer.used ' + recvBuffer.used);
var bytesRead;
2010-02-06 06:47:16 +03:00
try {
if (self.type == "unix") {
bytesRead = recvMsg(self.fd,
recvBuffer,
recvBuffer.used,
recvBuffer.length - recvBuffer.used);
2010-03-10 03:27:49 +03:00
//debug('recvMsg.fd ' + recvMsg.fd);
2010-02-06 06:47:16 +03:00
if (recvMsg.fd) {
self.emit('fd', recvMsg.fd);
}
} else {
bytesRead = read(self.fd,
recvBuffer,
recvBuffer.used,
recvBuffer.length - recvBuffer.used);
}
2010-02-06 06:47:16 +03:00
} catch (e) {
self.forceClose(e);
return;
}
2010-03-10 03:27:49 +03:00
//debug('bytesRead ' + bytesRead + '\n');
2009-12-16 10:35:09 +03:00
if (!recvMsg.fd && bytesRead == 0) {
2009-12-16 10:35:09 +03:00
self.readable = false;
self._readWatcher.stop();
if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
if (!self.writable) self.forceClose();
} else if (bytesRead > 0) {
timeout.active(self);
var start = recvBuffer.used;
var end = recvBuffer.used + bytesRead;
2010-03-10 03:27:49 +03:00
if (!self._encoding) {
if (self._events && self._events['data']) {
// emit a slice
self.emit('data', recvBuffer.slice(start, end));
}
// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(recvBuffer, start, end);
} else {
// TODO remove me - we should only output Buffer
var string;
switch (self._encoding) {
case 'utf8':
string = recvBuffer.utf8Slice(start, end);
break;
case 'ascii':
string = recvBuffer.asciiSlice(start, end);
break;
default:
throw new Error('Unsupported encoding ' + self._encoding + '. Use Buffer');
}
self.emit('data', string);
}
recvBuffer.used += bytesRead;
2009-12-16 10:35:09 +03:00
}
};
2009-12-18 16:52:02 +03:00
self.readable = false;
2010-03-09 22:59:42 +03:00
self._writeQueue = []; // queue of buffers that need to be written to socket
// XXX use link list?
2010-03-09 22:59:42 +03:00
self._writeQueueSize = 0; // in bytes, not to be confused with _writeQueue.length!
self._writeMessageQueueSize = 0; // number of messages remaining to be sent
self._writeWatcher = ioWatchers.alloc();
self._writeWatcher.socket = self;
self._writeWatcher.callback = _doFlush;
2009-12-17 11:31:10 +03:00
self.writable = false;
2010-02-26 23:13:49 +03:00
}
2010-03-13 00:04:33 +03:00
function Socket (fd) {
2010-02-26 23:13:49 +03:00
process.EventEmitter.call(this);
2010-03-16 01:11:40 +03:00
this.fd = null;
if (parseInt(fd) >= 0) {
2010-02-26 23:13:49 +03:00
initSocket(this);
2009-12-17 11:31:10 +03:00
2010-03-13 00:04:33 +03:00
this.fd = fd;
2010-02-26 23:13:49 +03:00
this.readable = true;
2009-12-18 16:52:02 +03:00
2010-02-26 23:13:49 +03:00
this._writeWatcher.set(this.fd, false, true);
this.writable = true;
2009-12-17 11:31:10 +03:00
}
};
sys.inherits(Socket, process.EventEmitter);
exports.Socket = Socket;
2009-12-17 11:31:10 +03:00
2009-12-18 16:52:02 +03:00
exports.createConnection = function (port, host) {
var s = new Socket();
2009-12-18 16:52:02 +03:00
s.connect(port, host);
return s;
};
var readyStateMessage;
Object.defineProperty(Socket.prototype, 'readyState', {
get: function () {
if (!readyStateMessage) {
readyStateMessage = 'readyState is depricated. Use stream.readable or stream.writable';
sys.error(readyStateMessage);
}
if (this.readable && this.writable) {
return 'open';
} else if (this.readable && !this.writable){
return 'readOnly';
} else if (!this.readable && this.writable){
return 'writeOnly';
} else {
return 'closed';
}
}
});
Socket.prototype._allocateSendBuffer = function () {
var b = buffers.alloc(1024);
2009-12-16 15:50:28 +03:00
b.used = 0;
b.sent = 0;
b.isMsg = false;
2010-03-09 22:59:42 +03:00
this._writeQueue.push(b);
2009-12-16 15:50:28 +03:00
return b;
};
2009-12-17 11:31:10 +03:00
2010-03-09 22:59:42 +03:00
Socket.prototype._writeString = function (data, encoding) {
2009-12-16 15:50:28 +03:00
var self = this;
if (!self.writable) throw new Error('Socket is not writable');
2009-12-17 11:31:10 +03:00
var buffer;
2010-03-10 03:27:49 +03:00
2010-03-09 22:59:42 +03:00
if (self._writeQueue.length == 0) {
2009-12-17 11:31:10 +03:00
buffer = self._allocateSendBuffer();
} else {
2010-03-09 22:59:42 +03:00
buffer = self.__writeQueueLast();
2010-01-29 06:13:13 +03:00
if (buffer.used == buffer.length) {
buffer = self._allocateSendBuffer();
2009-12-17 11:31:10 +03:00
}
}
2009-12-16 15:50:28 +03:00
2009-12-17 11:31:10 +03:00
encoding = encoding || 'ascii'; // default to ascii since it's faster
2009-12-16 15:50:28 +03:00
2009-12-17 11:31:10 +03:00
var charsWritten;
2009-12-18 16:52:02 +03:00
var bytesWritten;
2009-12-16 15:50:28 +03:00
// The special encoding "fd" means that data is an integer FD and we want
// to pass the FD on the socket with sendmsg()
if (encoding == "fd") {
buffer.isFd = true;
// TODO is this OK -- does it guarantee that the fd is the only thing in the buffer?
charsWritten = buffer.asciiWrite(data, buffer.used, buffer.length - buffer.used);
bytesWritten = charsWritten;
} else if (encoding.toLowerCase() == 'utf8') {
buffer.isFd = false;
2010-03-10 03:27:49 +03:00
charsWritten = buffer.utf8Write(data, buffer.used);
2010-03-09 22:59:42 +03:00
bytesWritten = Buffer.utf8ByteLength(data.slice(0, charsWritten));
2009-12-17 11:31:10 +03:00
} else {
// ascii
buffer.isFd = false;
2010-03-10 03:27:49 +03:00
charsWritten = buffer.asciiWrite(data, buffer.used);
2009-12-18 16:52:02 +03:00
bytesWritten = charsWritten;
2009-12-17 11:31:10 +03:00
}
2009-12-18 16:52:02 +03:00
buffer.used += bytesWritten;
if (buffer.isFd) {
2010-03-09 22:59:42 +03:00
self._writeMessageQueueSize += 1;
} else {
2010-03-09 22:59:42 +03:00
self._writeQueueSize += bytesWritten;
}
2009-12-16 15:50:28 +03:00
2010-03-10 03:27:49 +03:00
//debug('charsWritten ' + charsWritten);
//debug('buffer.used ' + buffer.used);
2009-12-16 15:50:28 +03:00
2009-12-17 11:31:10 +03:00
// If we didn't finish, then recurse with the rest of the string.
if (charsWritten < data.length) {
2010-03-10 03:27:49 +03:00
//debug('recursive write');
2010-03-09 22:59:42 +03:00
self._writeString(data.slice(charsWritten), encoding);
2009-12-17 11:31:10 +03:00
}
};
2010-03-09 22:59:42 +03:00
Socket.prototype.__writeQueueLast = function () {
return this._writeQueue.length > 0 ? this._writeQueue[this._writeQueue.length-1]
: null;
};
2010-03-09 22:59:42 +03:00
Socket.prototype.send = function () {
throw new Error('send renamed to write');
};
2010-03-10 03:27:49 +03:00
Socket.prototype.setEncoding = function (enc) {
// TODO check values, error out on bad, and deprecation message?
this._encoding = enc.toLowerCase();
};
2010-03-09 22:59:42 +03:00
2009-12-18 16:52:02 +03:00
// Returns true if all the data was flushed to socket. Returns false if
// something was queued. If data was queued, then the "drain" event will
// signal when it has been finally flushed to socket.
2010-03-09 22:59:42 +03:00
Socket.prototype.write = function (data, encoding) {
2009-12-17 11:31:10 +03:00
var self = this;
if (!self.writable) throw new Error('Socket is not writable');
2010-03-09 22:59:42 +03:00
if (self.__writeQueueLast() == END_OF_FILE) {
throw new Error('socket.close() called already; cannot write.');
}
2009-12-17 11:31:10 +03:00
if (typeof(data) == 'string') {
2010-03-09 22:59:42 +03:00
self._writeString(data, encoding);
2009-12-16 15:50:28 +03:00
} else {
2010-03-09 22:59:42 +03:00
// data is a Buffer
// walk through the _writeQueue, find the first empty buffer
2010-02-02 05:19:08 +03:00
//var inserted = false;
2009-12-16 15:50:28 +03:00
data.sent = 0;
data.used = data.length;
2010-03-09 22:59:42 +03:00
self._writeQueue.push(data);
self._writeQueueSize += data.used;
2009-12-16 15:50:28 +03:00
}
2009-12-18 16:52:02 +03:00
return this.flush();
2009-12-16 15:50:28 +03:00
};
// Sends a file descriptor over a unix socket
Socket.prototype.sendFD = function(socketToPass) {
var self = this;
if (!self.writable) throw new Error('Socket is not writable');
2010-03-09 22:59:42 +03:00
if (self.__writeQueueLast() == END_OF_FILE) {
throw new Error('socket.close() called already; cannot write.');
}
if (self.type != "unix") {
throw new Error('FD passing only available on unix sockets');
}
if (! socketToPass instanceof Socket) {
throw new Error('Provided arg is not a socket');
}
2010-03-09 22:59:42 +03:00
return self.write(socketToPass.fd.toString(), "fd");
};
2009-12-17 11:31:10 +03:00
// Flushes the write buffer out.
// Returns true if the entire buffer was flushed.
Socket.prototype.flush = function () {
2009-12-16 15:50:28 +03:00
var self = this;
2009-12-18 16:52:02 +03:00
2009-12-16 15:50:28 +03:00
var bytesWritten;
2010-03-09 22:59:42 +03:00
while (self._writeQueue.length) {
if (!self.writable) throw new Error('Socket is not writable');
2010-03-09 22:59:42 +03:00
var b = self._writeQueue[0];
2009-12-16 15:50:28 +03:00
if (b == END_OF_FILE) {
self._shutdown();
return true;
}
2009-12-16 15:50:28 +03:00
if (b.sent == b.used) {
2010-01-29 06:13:13 +03:00
// shift!
2010-03-09 22:59:42 +03:00
self._writeQueue.shift();
buffers.free(b);
2009-12-16 15:50:28 +03:00
continue;
}
var fdToSend = null;
2010-02-06 06:47:16 +03:00
try {
if (b.isFd) {
fdToSend = parseInt(b.asciiSlice(b.sent, b.used - b.sent));
2010-03-09 22:59:42 +03:00
bytesWritten = writeFD(self.fd, fdToSend);
2010-02-06 06:47:16 +03:00
} else {
bytesWritten = write(self.fd,
b,
b.sent,
b.used - b.sent);
}
} catch (e) {
self.forceClose(e);
return false;
}
2010-02-06 06:47:16 +03:00
timeout.active(self);
2009-12-16 15:50:28 +03:00
if (bytesWritten === null) {
// EAGAIN
debug('write EAGAIN');
self._writeWatcher.start();
2010-03-09 22:59:42 +03:00
assert(self._writeQueueSize > 0);
2009-12-16 15:50:28 +03:00
return false;
} else if (b.isFd) {
b.sent = b.used;
2010-03-09 22:59:42 +03:00
self._writeMessageQueueSize -= 1;
2010-03-10 03:27:49 +03:00
//debug('sent fd: ' + fdToSend);
} else {
b.sent += bytesWritten;
2010-03-09 22:59:42 +03:00
self._writeQueueSize -= bytesWritten;
debug('bytes sent: ' + b.sent);
}
2009-12-16 15:50:28 +03:00
}
if (self._writeWatcher) self._writeWatcher.stop();
2009-12-16 15:50:28 +03:00
return true;
2009-12-16 10:35:09 +03:00
};
2010-03-10 03:27:49 +03:00
function doConnect (socket, port, host) {
try {
connect(socket.fd, port, host);
} catch (e) {
socket.forceClose(e);
}
// Don't start the read watcher until connection is established
socket._readWatcher.set(socket.fd, true, false);
// How to connect on POSIX: Wait for fd to become writable, then call
// socketError() if there isn't an error, we're connected. AFAIK this a
// platform independent way determining when a non-blocking connection
// is established, but I have only seen it documented in the Linux
// Manual Page connect(2) under the error code EINPROGRESS.
socket._writeWatcher.set(socket.fd, false, true);
socket._writeWatcher.start();
socket._writeWatcher.callback = function () {
var errno = socketError(socket.fd);
if (errno == 0) {
// connection established
2010-03-16 01:11:40 +03:00
socket.resume();
2010-03-10 03:27:49 +03:00
socket.readable = true;
socket.writable = true;
socket._writeWatcher.callback = socket._doFlush;
socket.emit('connect');
} else if (errno != EINPROGRESS) {
socket.forceClose(errnoException(errno, 'connect'));
}
};
}
// var stream = new Socket();
2009-12-17 11:31:10 +03:00
// stream.connect(80) - TCP connect to port 80 on the localhost
// stream.connect(80, 'nodejs.org') - TCP connect to port 80 on nodejs.org
// stream.connect('/tmp/socket') - UNIX connect to socket specified by path
Socket.prototype.connect = function () {
2009-12-17 11:31:10 +03:00
var self = this;
2010-03-13 05:39:02 +03:00
initSocket(self);
if (self.fd) throw new Error('Socket already opened');
2010-03-13 05:39:02 +03:00
if (!self._readWatcher) throw new Error('No readWatcher');
timeout.active(socket);
2009-12-17 11:31:10 +03:00
2010-03-13 05:39:02 +03:00
var port = parseInt(arguments[0]);
if (port >= 0) {
2009-12-28 19:01:49 +03:00
self.fd = socket('tcp');
2010-03-10 03:27:49 +03:00
debug('new fd = ' + self.fd);
2009-12-28 19:01:49 +03:00
self.type = 'tcp';
2009-12-17 11:31:10 +03:00
// TODO dns resolution on arguments[1]
var port = arguments[0];
lookupDomainName(arguments[1], function (ip) {
2010-03-10 03:27:49 +03:00
doConnect(self, port, ip);
});
2010-03-13 05:39:02 +03:00
} else {
self.fd = socket('unix');
self.type = 'unix';
// TODO check if sockfile exists?
doConnect(self, arguments[0]);
2009-12-17 11:31:10 +03:00
}
};
2009-12-30 11:57:55 +03:00
Socket.prototype.address = function () {
return getsockname(this.fd);
};
2009-12-30 21:43:47 +03:00
Socket.prototype.setNoDelay = function (v) {
if (this.type == 'tcp') setNoDelay(this.fd, v);
2009-12-30 21:43:47 +03:00
};
Socket.prototype.setTimeout = function (msecs) {
timeout.enroll(this, msecs);
};
2010-03-10 03:27:49 +03:00
Socket.prototype.pause = function () {
this._readWatcher.stop();
};
2010-03-10 03:27:49 +03:00
Socket.prototype.resume = function () {
2010-03-16 01:11:40 +03:00
if (this.fd === null) throw new Error('Cannot resume() closed Socket.');
2010-03-10 03:27:49 +03:00
this._readWatcher.set(this.fd, true, false);
this._readWatcher.start();
};
Socket.prototype.forceClose = function (exception) {
// recvBuffer is shared between sockets, so don't need to free it here.
2010-03-10 03:27:49 +03:00
var self = this;
2009-12-17 11:31:10 +03:00
var b;
2010-03-09 22:59:42 +03:00
while (this._writeQueue.length) {
b = this._writeQueue.shift();
if (b instanceof Buffer) buffers.free(b);
}
if (this._writeWatcher) {
this._writeWatcher.stop();
ioWatchers.free(this._writeWatcher);
this._writeWatcher = null;
}
if (this._readWatcher) {
this._readWatcher.stop();
ioWatchers.free(this._readWatcher);
this._readWatcher = null;
}
timeout.unenroll(this);
if (this.fd) {
2009-12-17 11:31:10 +03:00
close(this.fd);
2010-03-10 03:27:49 +03:00
debug('close ' + this.fd);
2009-12-17 11:31:10 +03:00
this.fd = null;
2010-03-10 03:27:49 +03:00
process.nextTick(function () {
if (exception) self.emit('error', exception);
self.emit('close', exception ? true : false);
});
2009-12-17 11:31:10 +03:00
}
};
2009-12-17 11:31:10 +03:00
Socket.prototype._shutdown = function () {
2009-12-18 16:52:02 +03:00
if (this.writable) {
2009-12-17 11:31:10 +03:00
this.writable = false;
2010-02-06 06:47:16 +03:00
try {
shutdown(this.fd, 'write')
} catch (e) {
this.forceClose(e);
return;
}
if (!this.readable) this.forceClose();
2009-12-18 16:52:02 +03:00
}
};
Socket.prototype.close = function () {
if (this.writable) {
2010-03-09 22:59:42 +03:00
if (this.__writeQueueLast() != END_OF_FILE) {
this._writeQueue.push(END_OF_FILE);
this.flush();
2009-12-18 16:52:02 +03:00
}
}
2009-12-17 11:31:10 +03:00
};
function Server (listener) {
2010-01-27 05:36:24 +03:00
process.EventEmitter.call(this);
var self = this;
if (listener) {
2009-12-17 11:31:10 +03:00
self.addListener('connection', listener);
}
self.watcher = new IOWatcher();
self.watcher.host = self;
2010-03-13 00:04:33 +03:00
self.watcher.callback = function () {
while (self.fd) {
var peerInfo = accept(self.fd);
if (!peerInfo) return;
2010-03-16 01:11:40 +03:00
2010-03-13 00:04:33 +03:00
var s = new Socket(peerInfo.fd);
s.remoteAddress = peerInfo.remoteAddress;
s.remotePort = peerInfo.remotePort;
s.type = self.type;
s.server = self;
2010-03-16 01:11:40 +03:00
s.resume();
2010-03-13 00:04:33 +03:00
self.emit('connection', s);
2010-03-10 03:27:49 +03:00
// The 'connect' event probably should be removed for server-side
// sockets. It's redundent.
2010-03-13 00:04:33 +03:00
s.emit('connect');
}
};
}
sys.inherits(Server, process.EventEmitter);
2009-12-17 11:31:10 +03:00
exports.Server = Server;
2009-12-18 16:52:02 +03:00
exports.createServer = function (listener) {
return new Server(listener);
};
/* This function does both an ipv4 and ipv6 look up.
* It first tries the ipv4 look up, if that fails, then it does the ipv6.
*/
function lookupDomainName (dn, callback) {
if (!needsLookup(dn)) {
2010-03-10 03:27:49 +03:00
// Always wait until the next tick this is so people can do
//
// server.listen(8000);
// server.addListener('listening', fn);
//
// Marginally slower, but a lot fewer WTFs.
process.nextTick(function () { callback(dn); })
} else {
debug("getaddrinfo 4 " + dn);
getaddrinfo(dn, 4, function (r4) {
if (r4 instanceof Error) throw r4;
if (r4.length > 0) {
debug("getaddrinfo 4 found " + r4);
callback(r4[0]);
} else {
debug("getaddrinfo 6 " + dn);
getaddrinfo(dn, 6, function (r6) {
if (r6 instanceof Error) throw r6;
if (r6.length < 0) {
throw new Error("No address associated with hostname " + dn);
}
debug("getaddrinfo 6 found " + r6);
callback(r6[0]);
});
}
});
}
}
2009-12-18 16:52:02 +03:00
2009-12-28 19:01:49 +03:00
// Listen on a UNIX socket
// server.listen("/tmp/socket");
//
// Listen on port 8000, accept connections from INADDR_ANY.
// server.listen(8000);
//
// Listen on port 8000, accept connections to "192.168.1.2"
// server.listen(8000, "192.168.1.2");
2009-12-16 10:35:09 +03:00
Server.prototype.listen = function () {
var self = this;
2009-12-17 11:31:10 +03:00
if (self.fd) throw new Error('Server already opened');
function doListen () {
listen(self.fd, 128);
self.watcher.set(self.fd, true, false);
self.watcher.start();
self.emit("listening");
}
if (typeof(arguments[0]) == 'string') {
2009-12-16 10:35:09 +03:00
// the first argument specifies a path
2009-12-28 19:01:49 +03:00
self.fd = socket('unix');
self.type = 'unix';
var path = arguments[0];
self.path = path;
// unlink sockfile if it exists
2010-03-10 03:27:49 +03:00
fs.stat(path, function (err, r) {
if (err) {
if (err.errno == ENOENT) {
bind(self.fd, path);
doListen();
} else {
throw r;
}
} else {
if (!r.isFile()) {
throw new Error("Non-file exists at " + path);
} else {
2010-03-10 03:27:49 +03:00
fs.unlink(path, function (err) {
if (err) {
throw err;
} else {
bind(self.fd, path);
doListen();
}
});
}
}
});
} else if (!arguments[0]) {
// Don't bind(). OS will assign a port with INADDR_ANY.
// The port can be found with server.address()
2009-12-28 19:01:49 +03:00
self.fd = socket('tcp');
self.type = 'tcp';
doListen();
2009-12-16 10:35:09 +03:00
} else {
// the first argument is the port, the second an IP
2009-12-28 19:01:49 +03:00
self.fd = socket('tcp');
self.type = 'tcp';
var port = arguments[0];
lookupDomainName(arguments[1], function (ip) {
bind(self.fd, port, ip);
doListen();
});
2009-12-16 10:35:09 +03:00
}
};
2009-12-17 11:31:10 +03:00
2009-12-30 11:57:55 +03:00
Server.prototype.address = function () {
return getsockname(this.fd);
2009-12-18 16:52:02 +03:00
};
Server.prototype.close = function () {
var self = this;
if (!self.fd) throw new Error('Not running');
self.watcher.stop();
close(self.fd);
self.fd = null;
if (self.type === "unix") {
2010-03-10 03:27:49 +03:00
fs.unlink(self.path, function () {
self.emit("close");
});
} else {
self.emit("close");
}
};