2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const assert = require('assert');
|
2015-05-29 19:35:43 +02:00
|
|
|
const Buffer = require('buffer').Buffer;
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
2015-09-17 00:45:29 +02:00
|
|
|
const EventEmitter = require('events');
|
2015-01-21 17:36:59 +01:00
|
|
|
const constants = require('constants');
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const UDP = process.binding('udp_wrap').UDP;
|
|
|
|
const SendWrap = process.binding('udp_wrap').SendWrap;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const BIND_STATE_UNBOUND = 0;
|
|
|
|
const BIND_STATE_BINDING = 1;
|
|
|
|
const BIND_STATE_BOUND = 2;
|
2012-12-31 17:42:54 +01:00
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
// lazily loaded
|
2013-01-22 23:52:20 +01:00
|
|
|
var cluster = null;
|
2011-08-20 03:47:40 +02:00
|
|
|
var dns = null;
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const errnoException = util._errnoException;
|
|
|
|
const exceptionWithHostPort = util._exceptionWithHostPort;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
|
|
|
function lookup(address, family, callback) {
|
|
|
|
if (!dns)
|
|
|
|
dns = require('dns');
|
|
|
|
|
|
|
|
return dns.lookup(address, family, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function lookup4(address, callback) {
|
|
|
|
return lookup(address || '0.0.0.0', 4, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function lookup6(address, callback) {
|
|
|
|
return lookup(address || '::0', 6, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function newHandle(type) {
|
|
|
|
if (type == 'udp4') {
|
2016-01-29 05:08:57 +01:00
|
|
|
const handle = new UDP();
|
2011-08-20 03:47:40 +02:00
|
|
|
handle.lookup = lookup4;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'udp6') {
|
2016-01-29 05:08:57 +01:00
|
|
|
const handle = new UDP();
|
2011-08-20 03:47:40 +02:00
|
|
|
handle.lookup = lookup6;
|
|
|
|
handle.bind = handle.bind6;
|
|
|
|
handle.send = handle.send6;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'unix_dgram')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new Error('"unix_dgram" type sockets are not supported any more');
|
2011-08-20 03:47:40 +02:00
|
|
|
|
|
|
|
throw new Error('Bad socket type specified. Valid types are: udp4, udp6');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-25 21:24:41 +02:00
|
|
|
exports._createSocketHandle = function(address, port, addressType, fd, flags) {
|
2013-01-22 23:52:20 +01:00
|
|
|
// Opening an existing fd is not supported for UDP handles.
|
2015-01-29 02:05:53 +01:00
|
|
|
assert(typeof fd !== 'number' || fd < 0);
|
2013-01-22 23:52:20 +01:00
|
|
|
|
|
|
|
var handle = newHandle(addressType);
|
|
|
|
|
|
|
|
if (port || address) {
|
2015-08-25 21:24:41 +02:00
|
|
|
var err = handle.bind(address, port || 0, flags);
|
2013-07-18 23:18:50 +02:00
|
|
|
if (err) {
|
2013-01-22 23:52:20 +01:00
|
|
|
handle.close();
|
2013-07-18 23:18:50 +02:00
|
|
|
return err;
|
2013-01-22 23:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
function Socket(type, listener) {
|
2015-09-17 00:45:29 +02:00
|
|
|
EventEmitter.call(this);
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2014-04-09 01:04:53 +02:00
|
|
|
if (typeof type === 'object') {
|
|
|
|
var options = type;
|
|
|
|
type = options.type;
|
|
|
|
}
|
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
var handle = newHandle(type);
|
2012-04-27 04:37:27 +02:00
|
|
|
handle.owner = this;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
|
|
|
this._handle = handle;
|
|
|
|
this._receiving = false;
|
2012-12-31 17:42:54 +01:00
|
|
|
this._bindState = BIND_STATE_UNBOUND;
|
2011-08-20 03:47:40 +02:00
|
|
|
this.type = type;
|
2011-08-24 02:42:49 +02:00
|
|
|
this.fd = null; // compatibility hack
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2014-04-09 01:04:53 +02:00
|
|
|
// If true - UV_UDP_REUSEADDR flag will be set
|
|
|
|
this._reuseAddr = options && options.reuseAddr;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof listener === 'function')
|
2011-08-20 03:47:40 +02:00
|
|
|
this.on('message', listener);
|
|
|
|
}
|
2015-09-17 00:45:29 +02:00
|
|
|
util.inherits(Socket, EventEmitter);
|
2011-08-20 03:47:40 +02:00
|
|
|
exports.Socket = Socket;
|
|
|
|
|
|
|
|
|
|
|
|
exports.createSocket = function(type, listener) {
|
|
|
|
return new Socket(type, listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-22 23:52:20 +01:00
|
|
|
function startListening(socket) {
|
|
|
|
socket._handle.onmessage = onMessage;
|
|
|
|
// Todo: handle errors
|
|
|
|
socket._handle.recvStart();
|
|
|
|
socket._receiving = true;
|
|
|
|
socket._bindState = BIND_STATE_BOUND;
|
|
|
|
socket.fd = -42; // compatibility hack
|
|
|
|
|
|
|
|
socket.emit('listening');
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:31:24 +01:00
|
|
|
function replaceHandle(self, newHandle) {
|
2013-01-22 23:52:20 +01:00
|
|
|
|
2013-02-27 19:31:24 +01:00
|
|
|
// Set up the handle that we got from master.
|
|
|
|
newHandle.lookup = self._handle.lookup;
|
|
|
|
newHandle.bind = self._handle.bind;
|
|
|
|
newHandle.send = self._handle.send;
|
|
|
|
newHandle.owner = self;
|
|
|
|
|
|
|
|
// Replace the existing handle by the handle we got from master.
|
|
|
|
self._handle.close();
|
|
|
|
self._handle = newHandle;
|
|
|
|
}
|
|
|
|
|
2016-01-10 23:52:13 +01:00
|
|
|
Socket.prototype.bind = function(port_ /*, address, callback*/) {
|
2011-08-20 03:47:40 +02:00
|
|
|
var self = this;
|
2016-01-10 23:52:13 +01:00
|
|
|
let port = port_;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
|
|
|
self._healthCheck();
|
|
|
|
|
2013-01-22 23:52:20 +01:00
|
|
|
if (this._bindState != BIND_STATE_UNBOUND)
|
|
|
|
throw new Error('Socket is already bound');
|
|
|
|
|
|
|
|
this._bindState = BIND_STATE_BINDING;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof arguments[arguments.length - 1] === 'function')
|
2013-02-27 19:31:24 +01:00
|
|
|
self.once('listening', arguments[arguments.length - 1]);
|
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
if (port instanceof UDP) {
|
|
|
|
replaceHandle(self, port);
|
2013-02-27 19:31:24 +01:00
|
|
|
startListening(self);
|
2014-12-29 21:03:24 +01:00
|
|
|
return self;
|
2013-02-27 19:31:24 +01:00
|
|
|
}
|
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
var address;
|
|
|
|
var exclusive;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (port !== null && typeof port === 'object') {
|
2014-08-22 22:51:53 +02:00
|
|
|
address = port.address || '';
|
|
|
|
exclusive = !!port.exclusive;
|
|
|
|
port = port.port;
|
|
|
|
} else {
|
2015-01-29 02:05:53 +01:00
|
|
|
address = typeof arguments[1] === 'function' ? '' : arguments[1];
|
2014-08-22 22:51:53 +02:00
|
|
|
exclusive = false;
|
|
|
|
}
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
// resolve address first
|
|
|
|
self._handle.lookup(address, function(err, ip) {
|
|
|
|
if (err) {
|
2013-01-22 23:52:20 +01:00
|
|
|
self._bindState = BIND_STATE_UNBOUND;
|
2012-07-09 17:53:48 +02:00
|
|
|
self.emit('error', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-22 23:52:20 +01:00
|
|
|
if (!cluster)
|
|
|
|
cluster = require('cluster');
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2015-08-25 21:24:41 +02:00
|
|
|
var flags = 0;
|
|
|
|
if (self._reuseAddr)
|
|
|
|
flags |= constants.UV_UDP_REUSEADDR;
|
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
if (cluster.isWorker && !exclusive) {
|
2015-08-25 21:24:41 +02:00
|
|
|
function onHandle(err, handle) {
|
2013-07-28 12:19:34 +02:00
|
|
|
if (err) {
|
2015-01-08 20:14:44 +01:00
|
|
|
var ex = exceptionWithHostPort(err, 'bind', ip, port);
|
|
|
|
self.emit('error', ex);
|
2013-07-28 12:19:34 +02:00
|
|
|
self._bindState = BIND_STATE_UNBOUND;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-22 23:52:20 +01:00
|
|
|
if (!self._handle)
|
|
|
|
// handle has been closed in the mean time.
|
|
|
|
return handle.close();
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2013-02-27 19:31:24 +01:00
|
|
|
replaceHandle(self, handle);
|
2013-01-22 23:52:20 +01:00
|
|
|
startListening(self);
|
2015-08-25 21:24:41 +02:00
|
|
|
}
|
|
|
|
cluster._getServer(self, {
|
|
|
|
address: ip,
|
|
|
|
port: port,
|
|
|
|
addressType: self.type,
|
|
|
|
fd: -1,
|
|
|
|
flags: flags
|
|
|
|
}, onHandle);
|
2013-01-22 23:52:20 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!self._handle)
|
|
|
|
return; // handle has been closed in the mean time
|
|
|
|
|
2016-01-29 05:08:57 +01:00
|
|
|
const err = self._handle.bind(ip, port || 0, flags);
|
2013-07-18 23:18:50 +02:00
|
|
|
if (err) {
|
2015-01-08 20:14:44 +01:00
|
|
|
var ex = exceptionWithHostPort(err, 'bind', ip, port);
|
|
|
|
self.emit('error', ex);
|
2013-01-22 23:52:20 +01:00
|
|
|
self._bindState = BIND_STATE_UNBOUND;
|
|
|
|
// Todo: close?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
startListening(self);
|
|
|
|
}
|
|
|
|
});
|
2014-12-29 21:03:24 +01:00
|
|
|
|
|
|
|
return self;
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// thin wrapper around `send`, here for compatibility with dgram_legacy.js
|
|
|
|
Socket.prototype.sendto = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
address,
|
|
|
|
callback) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof offset !== 'number' || typeof length !== 'number')
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new Error('Send takes "offset" and "length" as args 2 and 3');
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof address !== 'string')
|
2011-08-20 03:47:40 +02:00
|
|
|
throw new Error(this.type + ' sockets must send to port, address');
|
|
|
|
|
|
|
|
this.send(buffer, offset, length, port, address, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
function sliceBuffer(buffer, offset, length) {
|
|
|
|
if (typeof buffer === 'string')
|
|
|
|
buffer = new Buffer(buffer);
|
|
|
|
else if (!(buffer instanceof Buffer))
|
|
|
|
throw new TypeError('First argument must be a buffer or string');
|
|
|
|
|
|
|
|
offset = offset >>> 0;
|
|
|
|
length = length >>> 0;
|
|
|
|
|
|
|
|
return buffer.slice(offset, offset + length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function fixBuffer(buffer) {
|
|
|
|
for (var i = 0, l = buffer.length; i < l; i++) {
|
|
|
|
var buf = buffer[i];
|
|
|
|
if (typeof buf === 'string')
|
|
|
|
buffer[i] = new Buffer(buf);
|
|
|
|
else if (!(buf instanceof Buffer))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function enqueue(self, toEnqueue) {
|
|
|
|
// If the send queue hasn't been initialized yet, do it, and install an
|
|
|
|
// event handler that flushes the send queue after binding is done.
|
|
|
|
if (!self._sendQueue) {
|
|
|
|
self._sendQueue = [];
|
|
|
|
self.once('listening', function() {
|
|
|
|
// Flush the send queue.
|
|
|
|
for (var i = 0; i < this._sendQueue.length; i++)
|
|
|
|
this.send.apply(self, this._sendQueue[i]);
|
|
|
|
this._sendQueue = undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
self._sendQueue.push(toEnqueue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-24 09:12:59 +01:00
|
|
|
// valid combinations
|
|
|
|
// send(buffer, offset, length, port, address, callback)
|
|
|
|
// send(buffer, offset, length, port, address)
|
|
|
|
// send(buffer, offset, length, port)
|
|
|
|
// send(bufferOrList, port, address, callback)
|
|
|
|
// send(bufferOrList, port, address)
|
|
|
|
// send(bufferOrList, port)
|
2011-08-20 03:47:40 +02:00
|
|
|
Socket.prototype.send = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
address,
|
|
|
|
callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2016-02-24 09:12:59 +01:00
|
|
|
if (address || (port && typeof port !== 'function')) {
|
2016-01-29 14:18:27 +01:00
|
|
|
buffer = sliceBuffer(buffer, offset, length);
|
|
|
|
} else {
|
|
|
|
callback = port;
|
|
|
|
port = offset;
|
|
|
|
address = length;
|
|
|
|
}
|
2012-02-23 01:51:27 +01:00
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
if (!Array.isArray(buffer)) {
|
|
|
|
if (typeof buffer === 'string') {
|
|
|
|
buffer = [ new Buffer(buffer) ];
|
|
|
|
} else if (!(buffer instanceof Buffer)) {
|
|
|
|
throw new TypeError('First argument must be a buffer or a string');
|
|
|
|
} else {
|
|
|
|
buffer = [ buffer ];
|
|
|
|
}
|
|
|
|
} else if (!fixBuffer(buffer)) {
|
|
|
|
throw new TypeError('Buffer list arguments must be buffers or strings');
|
|
|
|
}
|
2013-08-09 02:33:40 +02:00
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
port = port >>> 0;
|
|
|
|
if (port === 0 || port > 65535)
|
2013-08-09 02:33:40 +02:00
|
|
|
throw new RangeError('Port should be > 0 and < 65536');
|
2012-02-23 01:51:27 +01:00
|
|
|
|
2013-08-09 04:48:10 +02:00
|
|
|
// Normalize callback so it's either a function or undefined but not anything
|
|
|
|
// else.
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof callback !== 'function')
|
2013-08-09 04:48:10 +02:00
|
|
|
callback = undefined;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
|
|
|
self._healthCheck();
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2012-12-31 17:42:54 +01:00
|
|
|
if (self._bindState == BIND_STATE_UNBOUND)
|
dgram: implicit binds should be exclusive
Server sockets should be shared by default, and client sockets should be
exclusive by default. For net/TCP, this is how it is, for dgram/UDP, its
a little less clear what a client socket is, but a socket that is
auto-bound during a dgram.send() is not usefully shared among cluster
workers, any more than an outgoing TCP connection would be usefully
shared.
Since implicit binds become exclusive, implicit/client dgram sockets can
now be used with cluster on Windows. Before, neither explicit nor
implicitly bound sockets could be used, causing dgram to be completely
unsupported with cluster on Windows. After this change, they become half
supported.
PR: https://github.com/iojs/io.js/pull/325
PR: https://github.com/joyent/node/pull/8643
Reviewed-by: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-by: Bert Belder <bertbelder@gmail.com>
2015-01-10 04:25:07 +01:00
|
|
|
self.bind({port: 0, exclusive: true}, null);
|
2012-12-31 17:42:54 +01:00
|
|
|
|
2013-01-28 22:19:02 +01:00
|
|
|
// If the socket hasn't been bound yet, push the outbound packet onto the
|
|
|
|
// send queue and send after binding is complete.
|
2012-12-31 17:42:54 +01:00
|
|
|
if (self._bindState != BIND_STATE_BOUND) {
|
2016-01-29 14:18:27 +01:00
|
|
|
enqueue(self, [buffer, port, address, callback]);
|
2012-07-09 17:53:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
self._handle.lookup(address, function afterDns(ex, ip) {
|
|
|
|
doSend(ex, self, ip, buffer, address, port, callback);
|
2011-08-20 03:47:40 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-29 14:18:27 +01:00
|
|
|
function doSend(ex, self, ip, buffer, address, port, callback) {
|
|
|
|
if (ex) {
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('error', ex);
|
|
|
|
return;
|
|
|
|
} else if (!self._handle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var req = new SendWrap();
|
|
|
|
req.buffer = buffer; // Keep reference alive.
|
|
|
|
req.address = address;
|
|
|
|
req.port = port;
|
|
|
|
if (callback) {
|
|
|
|
req.callback = callback;
|
|
|
|
req.oncomplete = afterSend;
|
|
|
|
}
|
|
|
|
var err = self._handle.send(req,
|
|
|
|
buffer,
|
|
|
|
buffer.length,
|
|
|
|
port,
|
|
|
|
ip,
|
|
|
|
!!callback);
|
|
|
|
if (err && callback) {
|
|
|
|
// don't emit as error, dgram_legacy.js compatibility
|
2016-01-29 05:08:57 +01:00
|
|
|
const ex = exceptionWithHostPort(err, 'send', address, port);
|
2016-01-29 14:18:27 +01:00
|
|
|
process.nextTick(callback, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterSend(err, sent) {
|
2015-01-08 20:14:44 +01:00
|
|
|
if (err) {
|
|
|
|
err = exceptionWithHostPort(err, 'send', this.address, this.port);
|
|
|
|
}
|
2016-01-29 14:18:27 +01:00
|
|
|
this.callback(err, sent);
|
2011-08-20 03:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-30 06:30:03 +01:00
|
|
|
Socket.prototype.close = function(callback) {
|
2015-01-26 18:28:18 +01:00
|
|
|
if (typeof callback === 'function')
|
2014-12-30 06:30:03 +01:00
|
|
|
this.on('close', callback);
|
2011-08-20 03:47:40 +02:00
|
|
|
this._healthCheck();
|
|
|
|
this._stopReceiving();
|
|
|
|
this._handle.close();
|
|
|
|
this._handle = null;
|
2014-12-30 06:20:54 +01:00
|
|
|
var self = this;
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(socketCloseNT, self);
|
2014-12-29 21:12:30 +01:00
|
|
|
|
|
|
|
return this;
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
function socketCloseNT(self) {
|
|
|
|
self.emit('close');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
Socket.prototype.address = function() {
|
|
|
|
this._healthCheck();
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
var out = {};
|
|
|
|
var err = this._handle.getsockname(out);
|
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'getsockname');
|
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
return out;
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setBroadcast = function(arg) {
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.setBroadcast(arg ? 1 : 0);
|
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setBroadcast');
|
2011-10-27 16:48:38 +02:00
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setTTL = function(arg) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof arg !== 'number') {
|
2012-01-23 23:52:08 +01:00
|
|
|
throw new TypeError('Argument must be a number');
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.setTTL(arg);
|
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setTTL');
|
2012-01-23 23:52:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg;
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setMulticastTTL = function(arg) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof arg !== 'number') {
|
2012-01-23 21:09:56 +01:00
|
|
|
throw new TypeError('Argument must be a number');
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.setMulticastTTL(arg);
|
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setMulticastTTL');
|
2011-11-22 22:04:40 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 21:09:56 +01:00
|
|
|
return arg;
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setMulticastLoopback = function(arg) {
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.setMulticastLoopback(arg ? 1 : 0);
|
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setMulticastLoopback');
|
2012-01-23 23:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg; // 0.4 compatibility
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.addMembership = function(multicastAddress,
|
2011-10-12 19:55:15 +02:00
|
|
|
interfaceAddress) {
|
|
|
|
this._healthCheck();
|
|
|
|
|
|
|
|
if (!multicastAddress) {
|
|
|
|
throw new Error('multicast address must be specified');
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.addMembership(multicastAddress, interfaceAddress);
|
|
|
|
if (err) {
|
2014-08-13 06:03:25 +02:00
|
|
|
throw errnoException(err, 'addMembership');
|
2012-01-23 21:36:48 +01:00
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.dropMembership = function(multicastAddress,
|
2011-10-12 19:55:15 +02:00
|
|
|
interfaceAddress) {
|
|
|
|
this._healthCheck();
|
|
|
|
|
|
|
|
if (!multicastAddress) {
|
|
|
|
throw new Error('multicast address must be specified');
|
|
|
|
}
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = this._handle.dropMembership(multicastAddress, interfaceAddress);
|
|
|
|
if (err) {
|
2014-08-13 06:03:25 +02:00
|
|
|
throw errnoException(err, 'dropMembership');
|
2012-01-23 21:36:48 +01:00
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._healthCheck = function() {
|
|
|
|
if (!this._handle)
|
|
|
|
throw new Error('Not running'); // error message from dgram_legacy.js
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype._stopReceiving = function() {
|
|
|
|
if (!this._receiving)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._handle.recvStop();
|
|
|
|
this._receiving = false;
|
2011-08-24 02:42:49 +02:00
|
|
|
this.fd = null; // compatibility hack
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
function onMessage(nread, handle, buf, rinfo) {
|
2012-04-27 04:37:27 +02:00
|
|
|
var self = handle.owner;
|
2013-07-18 23:18:50 +02:00
|
|
|
if (nread < 0) {
|
|
|
|
return self.emit('error', errnoException(nread, 'recvmsg'));
|
2013-02-28 17:50:14 +01:00
|
|
|
}
|
2013-06-21 20:35:29 +02:00
|
|
|
rinfo.size = buf.length; // compatibility
|
|
|
|
self.emit('message', buf, rinfo);
|
2011-08-20 03:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-13 03:26:04 +02:00
|
|
|
Socket.prototype.ref = function() {
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.ref();
|
2015-05-22 18:35:57 +02:00
|
|
|
|
|
|
|
return this;
|
2012-07-13 03:26:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.unref = function() {
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.unref();
|
2015-05-22 18:35:57 +02:00
|
|
|
|
|
|
|
return this;
|
2012-07-13 03:26:04 +02:00
|
|
|
};
|