node/lib/net.js

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

2011-03-10 11:54:52 +03:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2010-12-02 05:07:20 +03:00
var util = require('util');
var events = require('events');
var stream = require('stream');
2011-01-24 21:55:30 +03:00
var timers = require('timers');
2010-03-16 02:44:50 +03:00
var kMinPoolSpace = 128;
2010-12-02 05:07:20 +03:00
var kPoolSize = 40 * 1024;
2010-12-02 05:07:20 +03:00
var debug;
if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
2011-02-09 21:23:26 +03:00
debug = function(x) { console.error('NET:', x); };
2010-12-02 05:07:20 +03:00
} else {
debug = function() { };
}
2010-12-02 05:07:20 +03:00
2010-03-16 02:44:50 +03:00
var binding = process.binding('net');
2010-03-19 22:08:57 +03:00
// Note about Buffer interface:
// I'm attempting to do the simplest possible interface to abstracting raw
// memory allocation. This might turn out to be too simple - it seems that
// I always use a buffer.used member to keep track of how much I've filled.
// Perhaps giving the Buffer a file-like interface with a head (which would
// represent buffer.used) that can be seeked around would be easier. I'm not
// yet convinced that every use-case can be fit into that abstraction, so
// waiting to implement it until I get more experience with this.
var FreeList = require('freelist').FreeList;
2010-03-19 22:08:57 +03:00
2010-12-02 05:07:20 +03:00
var IOWatcher = process.binding('io_watcher').IOWatcher;
var constants = process.binding('constants');
2011-01-28 03:59:28 +03:00
var assert = require('assert').ok;
2010-12-02 05:07:20 +03:00
var socket = binding.socket;
var bind = binding.bind;
var connect = binding.connect;
var listen = binding.listen;
var accept = binding.accept;
var close = binding.close;
var shutdown = binding.shutdown;
var read = binding.read;
var write = binding.write;
var toRead = binding.toRead;
var setNoDelay = binding.setNoDelay;
var setKeepAlive = binding.setKeepAlive;
2010-03-16 02:44:50 +03:00
var socketError = binding.socketError;
var getsockname = binding.getsockname;
var errnoException = binding.errnoException;
2010-12-02 05:07:20 +03:00
var sendMsg = binding.sendMsg;
var recvMsg = binding.recvMsg;
2011-01-18 02:27:08 +03:00
var EINPROGRESS = constants.EINPROGRESS || constants.WSAEINPROGRESS;
2010-12-02 05:07:20 +03:00
var ENOENT = constants.ENOENT;
var EMFILE = constants.EMFILE;
2010-03-28 00:07:43 +03:00
var END_OF_FILE = 42;
2010-12-02 05:07:20 +03:00
var ioWatchers = new FreeList('iowatcher', 100, function() {
return new IOWatcher();
});
exports.isIP = binding.isIP;
2010-12-02 05:07:20 +03:00
exports.isIPv4 = function(input) {
if (binding.isIP(input) === 4) {
return true;
}
return false;
};
2010-12-02 05:07:20 +03:00
exports.isIPv6 = function(input) {
if (binding.isIP(input) === 6) {
return true;
}
return false;
};
// Allocated on demand.
2010-03-20 09:09:16 +03:00
var pool = null;
2010-12-02 05:07:20 +03:00
function allocNewPool() {
pool = new Buffer(kPoolSize);
2010-03-20 09:09:16 +03:00
pool.used = 0;
}
2009-12-17 11:31:10 +03:00
2010-04-03 03:11:53 +04:00
var emptyBuffer = null;
2010-12-02 05:07:20 +03:00
function allocEmptyBuffer() {
emptyBuffer = new Buffer(1);
emptyBuffer.sent = 0;
emptyBuffer.length = 0;
2010-04-03 03:11:53 +04:00
}
2010-12-02 05:07:20 +03:00
function setImplmentationMethods(self) {
function noData(buf, off, len) {
return !buf ||
(off != undefined && off >= buf.length) ||
(len == 0);
};
if (self.type == 'unix') {
2010-12-02 05:07:20 +03:00
self._writeImpl = function(buf, off, len, fd, flags) {
// Detect and disallow zero-byte writes wth an attached file
// descriptor. This is an implementation limitation of sendmsg(2).
if (fd && noData(buf, off, len)) {
throw new Error('File descriptors can only be written with data');
}
return sendMsg(self.fd, buf, off, len, fd, flags);
};
2010-12-02 05:07:20 +03:00
self._readImpl = function(buf, off, len) {
var bytesRead = recvMsg(self.fd, buf, off, len);
// Do not emit this in the same stack, otherwise we risk corrupting our
// buffer pool which is full of read data, but has not had had its
// pointers updated just yet.
//
// Save off recvMsg.fd in a closure so that, when we emit it later, we're
// emitting the same value that we see now. Otherwise, we can end up
// calling emit() after recvMsg() has been called again and end up
// emitting null (or another FD).
2011-03-11 19:35:02 +03:00
if (typeof recvMsg.fd === 'number') {
2010-10-21 16:36:00 +04:00
var fd = recvMsg.fd;
2010-12-02 05:07:20 +03:00
process.nextTick(function() {
2010-10-21 16:36:00 +04:00
self.emit('fd', fd);
});
}
return bytesRead;
};
} else {
2010-12-02 05:07:20 +03:00
self._writeImpl = function(buf, off, len, fd, flags) {
// XXX: TLS support requires that 0-byte writes get processed
// by the kernel for some reason. Otherwise, we'd just
// fast-path return here.
// Drop 'fd' and 'flags' as these are not supported by the write(2)
// system call
return write(self.fd, buf, off, len);
};
2010-12-02 05:07:20 +03:00
self._readImpl = function(buf, off, len) {
return read(self.fd, buf, off, len);
};
}
2010-12-02 05:07:20 +03:00
self._shutdownImpl = function() {
shutdown(self.fd, 'write');
};
2010-12-02 05:07:20 +03:00
}
2010-12-02 05:07:20 +03:00
function onReadable(readable, writable) {
assert(this.socket);
var socket = this.socket;
2010-10-29 00:02:51 +04:00
socket._onReadable();
}
2010-03-10 03:27:49 +03:00
2010-12-02 05:07:20 +03:00
function onWritable(readable, writable) {
assert(this.socket);
var socket = this.socket;
if (socket._connecting) {
2010-11-24 05:30:52 +03:00
assert(socket.writable);
socket._onConnect();
} else {
socket._onWritable();
}
}
2011-01-05 22:53:56 +03:00
function initSocket(self) {
self._readWatcher = ioWatchers.alloc();
self._readWatcher.socket = self;
self._readWatcher.callback = onReadable;
self.readable = self.destroyed = false;
// Queue of buffers and string that need to be written to socket.
self._writeQueue = [];
self._writeQueueEncoding = [];
self._writeQueueFD = [];
2010-12-16 02:47:02 +03:00
self._writeQueueCallbacks = [];
2011-01-31 21:41:52 +03:00
// Number of charactes (which approx. equals number of bytes)
self.bufferSize = 0;
self._writeWatcher = ioWatchers.alloc();
self._writeWatcher.socket = self;
self._writeWatcher.callback = onWritable;
2009-12-17 11:31:10 +03:00
self.writable = false;
2010-02-26 23:13:49 +03:00
}
2011-01-05 22:53:56 +03:00
// Deprecated API: Socket(fd, type)
// New API: Socket({ fd: 10, type: 'unix', allowHalfOpen: true })
function Socket(options) {
if (!(this instanceof Socket)) return new Socket(arguments[0], arguments[1]);
2010-10-11 04:21:36 +04:00
stream.Stream.call(this);
2011-01-31 21:41:52 +03:00
this.bufferSize = 0;
2010-03-16 01:11:40 +03:00
this.fd = null;
this.type = null;
this.allowHalfOpen = false;
2010-12-02 05:07:20 +03:00
if (typeof options == 'object') {
this.fd = options.fd !== undefined ? parseInt(options.fd, 10) : null;
this.type = options.type || null;
this.allowHalfOpen = options.allowHalfOpen || false;
2010-12-02 05:07:20 +03:00
} else if (typeof options == 'number') {
this.fd = arguments[0];
this.type = arguments[1] || null;
}
2010-03-16 01:11:40 +03:00
if (parseInt(this.fd, 10) >= 0) {
this.open(this.fd, this.type);
} else {
setImplmentationMethods(this);
2009-12-17 11:31:10 +03:00
}
2010-12-02 05:07:20 +03:00
}
2011-01-05 22:53:56 +03:00
util.inherits(Socket, stream.Stream);
exports.Socket = Socket;
2009-12-17 11:31:10 +03:00
2011-01-05 22:53:56 +03:00
// Legacy naming.
exports.Stream = Socket;
2010-09-28 22:14:24 +04:00
2011-01-05 22:53:56 +03:00
Socket.prototype._onTimeout = function() {
2010-10-26 22:56:32 +04:00
this.emit('timeout');
};
2011-01-05 22:53:56 +03:00
Socket.prototype.open = function(fd, type) {
initSocket(this);
2010-03-18 00:00:17 +03:00
this.fd = fd;
this.type = type || getFdType(fd)
2010-03-18 00:00:17 +03:00
this.readable = true;
setImplmentationMethods(this);
2010-03-18 00:00:17 +03:00
this._writeWatcher.set(this.fd, false, true);
this.writable = true;
2010-10-07 07:05:23 +04:00
};
2010-03-18 00:00:17 +03:00
2010-12-02 05:07:20 +03:00
exports.createConnection = function(port, host) {
2011-01-05 22:53:56 +03:00
var s = new Socket();
2009-12-18 16:52:02 +03:00
s.connect(port, host);
return s;
};
2011-01-05 22:53:56 +03:00
Object.defineProperty(Socket.prototype, 'readyState', {
2010-12-02 05:07:20 +03:00
get: function() {
if (this._connecting) {
2010-03-19 00:33:42 +03:00
return 'opening';
} else if (this.readable && this.writable) {
2011-03-11 19:35:02 +03:00
assert(typeof this.fd === 'number');
return 'open';
2010-12-02 05:07:20 +03:00
} else if (this.readable && !this.writable) {
2011-03-11 19:35:02 +03:00
assert(typeof this.fd === 'number');
return 'readOnly';
2010-12-02 05:07:20 +03:00
} else if (!this.readable && this.writable) {
2011-03-11 19:35:02 +03:00
assert(typeof this.fd === 'number');
return 'writeOnly';
} else {
2011-03-11 19:35:02 +03:00
assert(typeof this.fd !== 'number');
return 'closed';
}
}
});
// Returns true if all the data was flushed to socket. Returns false if
2010-12-02 05:07:20 +03:00
// something was queued. If data was queued, then the 'drain' event will
// signal when it has been finally flushed to socket.
2011-01-05 22:53:56 +03:00
Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
var encoding, fd, cb;
2011-01-31 21:41:52 +03:00
assert(this.bufferSize >= 0);
// parse arguments
if (typeof arguments[1] == 'string') {
encoding = arguments[1];
if (typeof arguments[2] == 'number') {
fd = arguments[2];
cb = arguments[3];
} else {
cb = arguments[2];
}
} else if (typeof arguments[1] == 'number') {
fd = arguments[1];
cb = arguments[2];
} else if (typeof arguments[2] == 'number') {
// This case is to support old calls when the encoding argument
// was not optional: s.write(buf, undefined, pipeFDs[1])
encoding = arguments[1];
fd = arguments[2];
cb = arguments[3];
} else {
cb = arguments[1];
}
// TODO - actually use cb
if (this._connecting || (this._writeQueue && this._writeQueue.length)) {
if (!this._writeQueue) {
2011-01-31 21:41:52 +03:00
this.bufferSize = 0;
this._writeQueue = [];
this._writeQueueEncoding = [];
this._writeQueueFD = [];
2010-12-16 02:47:02 +03:00
this._writeQueueCallbacks = [];
}
// Slow. There is already a write queue, so let's append to it.
if (this._writeQueueLast() === END_OF_FILE) {
2011-01-05 22:53:56 +03:00
throw new Error('Socket.end() called already; cannot write.');
}
2010-12-02 05:07:20 +03:00
var last = this._writeQueue.length - 1;
2011-01-31 21:41:52 +03:00
this.bufferSize += data.length;
if (typeof data == 'string' &&
this._writeQueue.length &&
2010-12-02 05:07:20 +03:00
typeof this._writeQueue[last] === 'string' &&
this._writeQueueEncoding[last] === encoding) {
// optimization - concat onto last
2010-12-02 05:07:20 +03:00
this._writeQueue[last] += data;
2010-12-16 02:47:02 +03:00
if (cb) {
if (!this._writeQueueCallbacks[last]) {
this._writeQueueCallbacks[last] = cb;
} else {
// awful
2011-01-07 03:06:27 +03:00
this._writeQueueCallbacks[last] = function() {
2010-12-16 02:47:02 +03:00
this._writeQueueCallbacks[last]();
cb();
};
}
}
2010-11-12 22:47:31 +03:00
} else {
this._writeQueue.push(data);
this._writeQueueEncoding.push(encoding);
2010-12-16 02:47:02 +03:00
this._writeQueueCallbacks.push(cb);
2010-11-12 22:47:31 +03:00
}
if (fd != undefined) {
this._writeQueueFD.push(fd);
}
this._onBufferChange();
2011-02-10 05:50:26 +03:00
DTRACE_NET_SOCKET_WRITE(this, 0);
return false;
} else {
// Fast.
// The most common case. There is no write queue. Just push the data
// directly to the socket.
2010-12-16 02:47:02 +03:00
return this._writeOut(data, encoding, fd, cb);
2010-11-12 22:47:31 +03:00
}
};
// Directly writes the data to socket.
//
// Steps:
// 1. If it's a string, write it to the `pool`. (If not space remains
// on the pool make a new one.)
// 2. Write data to socket. Return true if flushed.
// 3. Slice out remaining
// 4. Unshift remaining onto _writeQueue. Return false.
2011-01-05 22:53:56 +03:00
Socket.prototype._writeOut = function(data, encoding, fd, cb) {
if (!this.writable) {
2011-01-05 22:53:56 +03:00
throw new Error('Socket is not writable');
2010-11-12 22:24:30 +03:00
}
var buffer, off, len;
var bytesWritten, charsWritten;
var queuedData = false;
if (typeof data != 'string') {
// 'data' is a buffer, ignore 'encoding'
buffer = data;
off = 0;
len = data.length;
} else {
assert(typeof data == 'string');
if (!pool || pool.length - pool.used < kMinPoolSpace) {
pool = null;
allocNewPool();
}
if (!encoding || encoding == 'utf8' || encoding == 'utf-8') {
// default to utf8
bytesWritten = pool.write(data, 'utf8', pool.used);
charsWritten = Buffer._charsWritten;
} else {
bytesWritten = pool.write(data, encoding, pool.used);
charsWritten = bytesWritten;
}
if (encoding && data.length > 0) {
assert(bytesWritten > 0);
}
buffer = pool;
len = bytesWritten;
off = pool.used;
pool.used += bytesWritten;
debug('wrote ' + bytesWritten + ' bytes to pool');
if (charsWritten != data.length) {
2010-12-02 05:07:20 +03:00
// debug('couldn't fit ' +
// (data.length - charsWritten) +
// ' bytes into the pool\n');
// Unshift whatever didn't fit onto the buffer
2011-01-31 21:41:52 +03:00
assert(data.length > charsWritten);
this.bufferSize += data.length - charsWritten;
this._writeQueue.unshift(data.slice(charsWritten));
this._writeQueueEncoding.unshift(encoding);
2010-12-16 02:47:02 +03:00
this._writeQueueCallbacks.unshift(cb);
this._writeWatcher.start();
this._onBufferChange();
queuedData = true;
}
2010-03-28 00:07:43 +03:00
}
try {
bytesWritten = this._writeImpl(buffer, off, len, fd, 0);
2011-02-10 05:50:26 +03:00
DTRACE_NET_SOCKET_WRITE(this, bytesWritten);
} catch (e) {
this.destroy(e);
return false;
}
2011-02-09 21:23:26 +03:00
debug('wrote ' + bytesWritten + ' bytes to socket.')
debug('[fd, off, len] = ' + JSON.stringify([this.fd, off, len]));
timers.active(this);
if (bytesWritten == len) {
// awesome. sent to buffer.
if (buffer === pool) {
// If we're just writing from the pool then we can make a little
// optimization and save the space.
buffer.used -= len;
}
if (queuedData) {
return false;
} else {
2010-12-16 02:47:02 +03:00
if (cb) cb();
return true;
}
}
// Didn't write the entire thing to buffer.
// Need to wait for the socket to become available before trying again.
this._writeWatcher.start();
// Slice out the data left.
var leftOver = buffer.slice(off + bytesWritten, off + len);
leftOver.used = leftOver.length; // used the whole thing...
// util.error('data.used = ' + data.used);
2011-01-05 22:53:56 +03:00
//if (!this._writeQueue) initWriteSocket(this);
2009-12-16 15:50:28 +03:00
// data should be the next thing to write.
2011-01-31 21:41:52 +03:00
this.bufferSize += leftOver.length;
this._writeQueue.unshift(leftOver);
this._writeQueueEncoding.unshift(null);
2010-12-16 02:47:02 +03:00
this._writeQueueCallbacks.unshift(cb);
this._onBufferChange();
2010-02-06 06:47:16 +03:00
// If didn't successfully write any bytes, enqueue our fd and try again
if (!bytesWritten) {
this._writeQueueFD.unshift(fd);
}
return false;
};
Socket.prototype._onBufferChange = function() {
// Put DTrace hooks here.
;
};
// Flushes the write buffer out.
// Returns true if the entire buffer was flushed.
2011-01-05 22:53:56 +03:00
Socket.prototype.flush = function() {
while (this._writeQueue && this._writeQueue.length) {
var data = this._writeQueue.shift();
var encoding = this._writeQueueEncoding.shift();
2010-12-16 02:47:02 +03:00
var cb = this._writeQueueCallbacks.shift();
var fd = this._writeQueueFD.shift();
if (data === END_OF_FILE) {
this._shutdown();
return true;
}
2011-01-31 21:41:52 +03:00
// Only decrement if it's not the END_OF_FILE object...
this.bufferSize -= data.length;
this._onBufferChange();
2011-01-31 21:41:52 +03:00
2010-12-16 02:47:02 +03:00
var flushed = this._writeOut(data, encoding, fd, cb);
if (!flushed) return false;
}
if (this._writeWatcher) this._writeWatcher.stop();
return true;
};
2011-01-05 22:53:56 +03:00
Socket.prototype._writeQueueLast = function() {
2010-12-02 05:07:20 +03:00
return this._writeQueue.length > 0 ?
this._writeQueue[this._writeQueue.length - 1] : null;
2010-03-28 00:07:43 +03:00
};
2011-01-05 22:53:56 +03:00
Socket.prototype.setEncoding = function(encoding) {
2010-12-02 05:07:20 +03:00
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder(encoding);
2009-12-16 10:35:09 +03:00
};
2010-12-02 05:07:20 +03:00
function doConnect(socket, port, host) {
if (socket.destroyed) return;
timers.active(socket);
2010-03-10 03:27:49 +03:00
try {
connect(socket.fd, port, host);
} catch (e) {
socket.destroy(e);
2010-03-31 18:59:09 +04:00
return;
2010-03-10 03:27:49 +03:00
}
debug('connecting to ' + host + ' : ' + port);
2010-03-10 03:27:49 +03:00
// 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.
2010-03-10 03:27:49 +03:00
socket._writeWatcher.set(socket.fd, false, true);
socket._writeWatcher.start();
}
2010-12-02 05:07:20 +03:00
function toPort(x) { return (x = Number(x)) >= 0 ? x : false; }
2011-01-05 22:53:56 +03:00
Socket.prototype._onConnect = function() {
var errno = socketError(this.fd);
if (errno == 0) {
// connection established
this._connecting = false;
this.resume();
2010-11-24 05:30:52 +03:00
assert(this.writable);
this.readable = this.writable = true;
try {
this.emit('connect');
} catch (e) {
this.destroy(e);
return;
2010-03-10 03:27:49 +03:00
}
if (this._writeQueue && this._writeQueue.length) {
// Flush this in case any writes are queued up while connecting.
this._onWritable();
}
} else if (errno != EINPROGRESS) {
this.destroy(errnoException(errno, 'connect'));
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype._onWritable = function() {
// Socket becomes writable on connect() but don't flush if there's
// nothing actually to write
if (this.flush()) {
2010-12-02 05:07:20 +03:00
if (this._events && this._events['drain']) this.emit('drain');
if (this.ondrain) this.ondrain(); // Optimization
if (this.__destroyOnDrain) this.destroy();
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype._onReadable = function() {
var self = this;
// If this is the first recv (pool doesn't exist) or we've used up
// most of the pool, allocate a new one.
if (!pool || pool.length - pool.used < kMinPoolSpace) {
// discard the old pool. Can't add to the free list because
// users might have refernces to slices on it.
pool = null;
allocNewPool();
}
//debug('pool.used ' + pool.used);
var bytesRead;
try {
bytesRead = self._readImpl(pool,
pool.used,
2010-10-29 00:02:51 +04:00
pool.length - pool.used);
2011-02-10 05:50:26 +03:00
DTRACE_NET_SOCKET_READ(this, bytesRead);
} catch (e) {
if (e.code == 'ECONNRESET') {
self.destroy();
} else {
self.destroy(e);
}
return;
}
// Note that some _readImpl() implementations return -1 bytes
// read as an indication not to do any processing on the result
// (but not an error).
if (bytesRead === 0) {
self.readable = false;
self._readWatcher.stop();
if (!self.writable) self.destroy();
// Note: 'close' not emitted until nextTick.
if (!self.allowHalfOpen) self.end();
if (self._events && self._events['end']) self.emit('end');
if (self.onend) self.onend();
} else if (bytesRead > 0) {
timers.active(self);
var start = pool.used;
var end = pool.used + bytesRead;
pool.used += bytesRead;
2011-02-09 21:23:26 +03:00
debug('socket ' + self.fd + ' received ' + bytesRead + ' bytes');
if (self._decoder) {
// emit String
var string = self._decoder.write(pool.slice(start, end));
if (string.length) self.emit('data', string);
} else {
// emit buffer
if (self._events && self._events['data']) {
// emit a slice
self.emit('data', pool.slice(start, end));
}
}
// Optimization: emit the original buffer with end points
if (self.ondata) self.ondata(pool, start, end);
}
};
2010-03-10 03:27:49 +03:00
2011-01-05 22:53:56 +03:00
// var socket = new Socket();
// socket.connect(80) - TCP connect to port 80 on the localhost
// socket.connect(80, 'nodejs.org') - TCP connect to port 80 on nodejs.org
// socket.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;
2011-01-05 22:53:56 +03:00
initSocket(self);
2011-03-11 19:35:02 +03:00
if (typeof self.fd === 'number') throw new Error('Socket already opened');
2010-03-13 05:39:02 +03:00
if (!self._readWatcher) throw new Error('No readWatcher');
2010-04-12 00:46:24 +04:00
timers.active(this);
2009-12-17 11:31:10 +03:00
self._connecting = true; // set false in doConnect
self.writable = true;
2010-03-13 05:39:02 +03:00
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') {
self.addListener('connect', lastArg);
}
2010-10-07 07:05:23 +04:00
var port = toPort(arguments[0]);
if (port === false) {
// UNIX
self.fd = socket('unix');
self.type = 'unix';
setImplmentationMethods(this);
doConnect(self, arguments[0]);
} else {
// TCP
2010-12-02 05:07:20 +03:00
require('dns').lookup(arguments[1], function(err, ip, addressType) {
if (err) {
self.emit('error', err);
} else {
timers.active(self);
2010-04-08 03:05:07 +04:00
self.type = addressType == 4 ? 'tcp4' : 'tcp6';
self.fd = socket(self.type);
self.remoteAddress = ip;
self.remotePort = port;
doConnect(self, port, ip);
}
});
2009-12-17 11:31:10 +03:00
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype.address = function() {
2009-12-30 11:57:55 +03:00
return getsockname(this.fd);
};
2011-01-05 22:53:56 +03:00
Socket.prototype.setNoDelay = function(v) {
2010-12-02 05:07:20 +03:00
if ((this.type == 'tcp4') || (this.type == 'tcp6')) {
setNoDelay(this.fd, v);
}
2009-12-30 21:43:47 +03:00
};
2011-01-05 22:53:56 +03:00
Socket.prototype.setKeepAlive = function(enable, time) {
2010-12-02 05:07:20 +03:00
if ((this.type == 'tcp4') || (this.type == 'tcp6')) {
var secondDelay = Math.ceil(time / 1000);
setKeepAlive(this.fd, enable, secondDelay);
}
};
2009-12-30 21:43:47 +03:00
Socket.prototype.setTimeout = function(msecs, callback) {
if (msecs > 0) {
timers.enroll(this, msecs);
2011-03-11 19:35:02 +03:00
if (typeof this.fd === 'number') { timers.active(this); }
if (callback) {
this.once('timeout', callback);
}
} else if (msecs === 0) {
timers.unenroll(this);
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype.pause = function() {
if (this._readWatcher) this._readWatcher.stop();
2010-03-10 03:27:49 +03:00
};
2011-01-05 22:53:56 +03:00
Socket.prototype.resume = function() {
2011-03-11 19:35:02 +03:00
if (typeof this.fd !== 'number') {
throw new Error('Cannot resume() closed Socket.');
}
if (this._readWatcher) {
this._readWatcher.stop();
this._readWatcher.set(this.fd, true, false);
this._readWatcher.start();
}
2010-03-10 03:27:49 +03:00
};
2011-01-05 22:53:56 +03:00
Socket.prototype.destroySoon = function() {
if (this.flush()) {
this.destroy();
} else {
this.__destroyOnDrain = true;
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype.destroy = function(exception) {
2010-03-20 09:09:16 +03:00
// pool 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
2011-02-05 02:14:58 +03:00
debug('destroy ' + this.fd);
// TODO would like to set _writeQueue to null to avoid extra object alloc,
// but lots of code assumes this._writeQueue is always an array.
2011-01-31 21:41:52 +03:00
assert(this.bufferSize >= 0);
this._writeQueue = [];
2011-01-31 21:41:52 +03:00
this._writeQueueEncoding = [];
this._writeQueueCallbacks = [];
this._writeQueueFD = [];
this.bufferSize = 0;
this.readable = this.writable = false;
if (this._writeWatcher) {
this._writeWatcher.stop();
this._writeWatcher.socket = null;
ioWatchers.free(this._writeWatcher);
this._writeWatcher = null;
}
if (this._readWatcher) {
this._readWatcher.stop();
this._readWatcher.socket = null;
ioWatchers.free(this._readWatcher);
this._readWatcher = null;
}
timers.unenroll(this);
if (this.server && !this.destroyed) {
this.server.connections--;
}
2010-03-18 00:00:17 +03:00
// FIXME Bug when this.fd == 0
2011-03-11 19:35:02 +03:00
if (typeof this.fd === 'number') {
2011-02-05 02:14:58 +03:00
debug('close ' + this.fd);
close(this.fd);
2009-12-17 11:31:10 +03:00
this.fd = null;
2010-12-02 05:07:20 +03:00
process.nextTick(function() {
2010-03-10 03:27:49 +03:00
if (exception) self.emit('error', exception);
self.emit('close', exception ? true : false);
});
2009-12-17 11:31:10 +03:00
}
this.destroyed = true;
};
2009-12-17 11:31:10 +03:00
2011-01-05 22:53:56 +03:00
Socket.prototype._shutdown = function() {
if (!this.writable) {
throw new Error('The connection is not writable');
} else {
2010-11-24 05:30:52 +03:00
// readable and writable
this.writable = false;
if (this.readable) {
2010-02-06 06:47:16 +03:00
try {
this._shutdownImpl();
} catch (e) {
if (e.code == 'ENOTCONN') {
// Allowed.
this.destroy();
} else {
this.destroy(e);
}
}
} else {
// writable but not readable
this.destroy();
2010-02-06 06:47:16 +03:00
}
2009-12-18 16:52:02 +03:00
}
};
2011-01-05 22:53:56 +03:00
Socket.prototype.end = function(data, encoding) {
if (this.writable) {
if (this._writeQueueLast() !== END_OF_FILE) {
DTRACE_NET_STREAM_END(this);
if (data) this.write(data, encoding);
this._writeQueue.push(END_OF_FILE);
if (!this._connecting) {
this.flush();
}
}
}
2009-12-17 11:31:10 +03:00
};
2010-12-02 05:07:20 +03:00
function Server(/* [ options, ] listener */) {
if (!(this instanceof Server)) return new Server(arguments[0], arguments[1]);
2010-03-16 02:44:50 +03:00
events.EventEmitter.call(this);
var self = this;
var options = {};
2010-12-02 05:07:20 +03:00
if (typeof arguments[0] == 'object') {
options = arguments[0];
}
2010-10-27 23:08:47 +04:00
// listener: find the last argument that is a function
for (var l = arguments.length - 1; l >= 0; l--) {
2010-12-02 05:07:20 +03:00
if (typeof arguments[l] == 'function') {
self.addListener('connection', arguments[l]);
}
if (arguments[l] !== undefined) break;
}
self.connections = 0;
self.allowHalfOpen = options.allowHalfOpen || false;
self.watcher = new IOWatcher();
self.watcher.host = self;
2010-12-02 05:07:20 +03:00
self.watcher.callback = function() {
// Just in case we don't have a dummy fd.
getDummyFD();
if (self._pauseTimer) {
2010-10-27 23:08:47 +04:00
// Somehow the watcher got started again. Need to wait until
// the timer finishes.
self.watcher.stop();
}
while (typeof self.fd === 'number') {
2010-04-09 21:44:08 +04:00
try {
var peerInfo = accept(self.fd);
} catch (e) {
2010-10-27 23:08:47 +04:00
if (e.errno != EMFILE) throw e;
// Gracefully reject pending clients by freeing up a file
// descriptor.
rescueEMFILE(function() {
self._rejectPending();
});
2010-10-27 23:08:47 +04:00
return;
2010-04-09 21:44:08 +04:00
}
if (!peerInfo) return;
2010-03-16 01:11:40 +03:00
if (self.maxConnections && self.connections >= self.maxConnections) {
// Close the connection we just had
close(peerInfo.fd);
// Reject all other pending connectins.
self._rejectPending();
return;
}
self.connections++;
2010-12-02 05:07:20 +03:00
var options = { fd: peerInfo.fd,
type: self.type,
allowHalfOpen: self.allowHalfOpen };
2011-01-05 22:53:56 +03:00
var s = new Socket(options);
s.remoteAddress = peerInfo.address;
s.remotePort = peerInfo.port;
2010-03-13 00:04:33 +03:00
s.type = self.type;
s.server = self;
2010-03-16 01:11:40 +03:00
s.resume();
DTRACE_NET_SERVER_CONNECTION(s);
2010-03-13 00:04:33 +03:00
self.emit('connection', s);
}
};
}
util.inherits(Server, events.EventEmitter);
2009-12-17 11:31:10 +03:00
exports.Server = Server;
2010-12-02 05:07:20 +03:00
exports.createServer = function() {
return new Server(arguments[0], arguments[1]);
2009-12-18 16:52:02 +03:00
};
// Just stop trying to accepting connections for a while.
// Useful for throttling against DoS attacks.
2010-12-02 05:07:20 +03:00
Server.prototype.pause = function(msecs) {
// We're already paused.
if (this._pauseTimer) return;
var self = this;
msecs = msecs || 1000;
this.watcher.stop();
// Wait a second before accepting more.
2010-12-02 05:07:20 +03:00
this._pauseTimer = setTimeout(function() {
// Our fd should still be there. If someone calls server.close() then
// the pauseTimer should be cleared.
assert(parseInt(self.fd) >= 0);
self._pauseTimer = null;
self.watcher.start();
}, msecs);
};
2010-12-02 05:07:20 +03:00
Server.prototype._rejectPending = function() {
var self = this;
var acceptCount = 0;
// Accept and close the waiting clients one at a time.
// Single threaded programming ftw.
while (true) {
2010-12-05 03:11:57 +03:00
var peerInfo = accept(this.fd);
if (!peerInfo) return;
close(peerInfo.fd);
// Don't become DoS'd by incoming requests
if (++acceptCount > 50) {
this.pause();
return;
}
}
};
2009-12-28 19:01:49 +03:00
// Listen on a UNIX socket
2010-12-02 05:07:20 +03:00
// server.listen('/tmp/socket');
2009-12-28 19:01:49 +03:00
//
// Listen on port 8000, accept connections from INADDR_ANY.
// server.listen(8000);
//
2010-12-02 05:07:20 +03:00
// Listen on port 8000, accept connections to '192.168.1.2'
// server.listen(8000, '192.168.1.2');
Server.prototype.listen = function() {
var self = this;
2011-03-11 19:35:02 +03:00
if (typeof self.fd === 'number') throw new Error('Server already opened');
var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') {
self.addListener('listening', lastArg);
}
var port = toPort(arguments[0]);
if (arguments.length == 0 || typeof arguments[0] == 'function') {
// Don't bind(). OS will assign a port with INADDR_ANY.
// The port can be found with server.address()
self.type = 'tcp4';
self.fd = socket(self.type);
self._doListen(port);
} else if (port === false) {
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-12-02 05:07:20 +03:00
require('fs').stat(path, function(err, r) {
2010-03-10 03:27:49 +03:00
if (err) {
if (err.errno == ENOENT) {
2010-10-23 22:22:24 +04:00
self._doListen(path);
} else {
throw r;
}
} else {
if (!r.isSocket()) {
2010-12-02 05:07:20 +03:00
throw new Error('Non-socket exists at ' + path);
} else {
2010-12-02 05:07:20 +03:00
require('fs').unlink(path, function(err) {
2010-10-23 22:22:24 +04:00
if (err) throw err;
self._doListen(path);
});
}
}
});
2009-12-16 10:35:09 +03:00
} else {
// the first argument is the port, the second an IP
2010-12-02 05:07:20 +03:00
require('dns').lookup(arguments[1], function(err, ip, addressType) {
if (err) {
self.emit('error', err);
} else {
2010-04-08 03:05:07 +04:00
self.type = addressType == 4 ? 'tcp4' : 'tcp6';
self.fd = socket(self.type);
2010-10-23 22:22:24 +04:00
self._doListen(port, ip);
}
});
2009-12-16 10:35:09 +03:00
}
};
2010-12-02 05:07:20 +03:00
Server.prototype.listenFD = function(fd, type) {
2011-03-11 19:35:02 +03:00
if (typeof this.fd === 'number') {
throw new Error('Server already opened');
}
this.fd = fd;
this.type = type || null;
this._startWatcher();
};
2010-12-02 05:07:20 +03:00
Server.prototype._startWatcher = function() {
this.watcher.set(this.fd, true, false);
this.watcher.start();
2010-12-02 05:07:20 +03:00
this.emit('listening');
};
2010-12-02 05:07:20 +03:00
Server.prototype._doListen = function() {
2010-10-23 22:22:24 +04:00
var self = this;
// Ensure we have a dummy fd for EMFILE conditions.
getDummyFD();
try {
2010-10-23 22:22:24 +04:00
bind(self.fd, arguments[0], arguments[1]);
} catch (err) {
self.close();
2010-10-23 22:22:24 +04:00
self.emit('error', err);
return;
}
2010-10-23 22:22:24 +04:00
// Need to the listening in the nextTick so that people potentially have
// time to register 'listening' listeners.
2010-12-02 05:07:20 +03:00
process.nextTick(function() {
2010-10-23 22:22:24 +04:00
// It could be that server.close() was called between the time the
// original listen command was issued and this. Bail if that's the case.
// See test/simple/test-net-eaddrinuse.js
2011-03-11 19:35:02 +03:00
if (typeof self.fd !== 'number') return;
2010-10-23 22:22:24 +04:00
try {
listen(self.fd, self._backlog || 128);
} catch (err) {
self.close();
2010-10-23 22:22:24 +04:00
self.emit('error', err);
return;
}
self._startWatcher();
});
2010-12-02 05:07:20 +03:00
};
2009-12-17 11:31:10 +03:00
2010-12-02 05:07:20 +03:00
Server.prototype.address = function() {
2009-12-30 11:57:55 +03:00
return getsockname(this.fd);
2009-12-18 16:52:02 +03:00
};
2010-12-02 05:07:20 +03:00
Server.prototype.close = function() {
var self = this;
if (typeof self.fd !== 'number') throw new Error('Not running');
self.watcher.stop();
close(self.fd);
self.fd = null;
if (self._pauseTimer) {
clearTimeout(self._pauseTimer);
self._pauseTimer = null;
}
2010-12-02 05:07:20 +03:00
if (self.type === 'unix') {
require('fs').unlink(self.path, function() {
self.emit('close');
});
} else {
2010-12-02 05:07:20 +03:00
self.emit('close');
}
};
function getFdType(fd) {
var type = 'file';
var family;
try {
type = binding.getsocktype(fd);
family = binding.getsockfamily(fd);
if (family == "AF_UNIX") {
type = "unix";
} else if (type == "SOCK_STREAM" && family == "AF_INET") {
type = "tcp";
} else if (type == "SOCK_STREAM" && family == "AF_INET6") {
type = "tcp6";
} else if (type == "SOCK_DGRAM" && family == "AF_INET") {
type = "udp";
} else if (type == "SOCK_DGRAM" && family == "AF_INET6") {
type = "udp6";
}
} catch (e) {
if (e.code == 'ENOTSOCK') {
type = 'file';
} else {
throw e;
}
}
return type;
}
exports.getFdType = getFdType;
var dummyFD = null;
var lastEMFILEWarning = 0;
// Ensures to have at least one free file-descriptor free.
// callback should only use 1 file descriptor and close it before end of call
function rescueEMFILE(callback) {
// Output a warning, but only at most every 5 seconds.
var now = new Date();
if (now - lastEMFILEWarning > 5000) {
console.error('(node) Hit max file limit. Increase "ulimit -n"');
lastEMFILEWarning = now;
}
if (dummyFD) {
close(dummyFD);
dummyFD = null;
callback();
getDummyFD();
}
}
function getDummyFD() {
if (!dummyFD) {
try {
2010-12-02 05:07:20 +03:00
dummyFD = socket('tcp');
} catch (e) {
dummyFD = null;
}
}
}