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');
|
|
|
|
const events = require('events');
|
|
|
|
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') {
|
2015-04-28 19:46:14 +02:00
|
|
|
var handle = new UDP();
|
2011-08-20 03:47:40 +02:00
|
|
|
handle.lookup = lookup4;
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == 'udp6') {
|
2015-04-28 19:46:14 +02:00
|
|
|
var 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')
|
|
|
|
throw new Error('unix_dgram sockets are not supported any more.');
|
|
|
|
|
|
|
|
throw new Error('Bad socket type specified. Valid types are: udp4, udp6');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-22 23:52:20 +01:00
|
|
|
exports._createSocketHandle = function(address, port, addressType, fd) {
|
|
|
|
// 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) {
|
2013-07-18 23:18:50 +02:00
|
|
|
var err = handle.bind(address, port || 0, 0);
|
|
|
|
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) {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
util.inherits(Socket, events.EventEmitter);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
Socket.prototype.bind = function(port /*, address, callback*/) {
|
2011-08-20 03:47:40 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
if (cluster.isWorker && !exclusive) {
|
2013-07-28 12:19:34 +02:00
|
|
|
cluster._getServer(self, ip, port, self.type, -1, function(err, handle) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!self._handle)
|
|
|
|
return; // handle has been closed in the mean time
|
|
|
|
|
2014-04-09 01:04:53 +02:00
|
|
|
var flags = 0;
|
|
|
|
if (self._reuseAddr)
|
|
|
|
flags |= constants.UV_UDP_REUSEADDR;
|
|
|
|
|
|
|
|
var 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')
|
2011-08-20 03:47:40 +02:00
|
|
|
throw new Error('send takes offset and length as args 2 and 3');
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.send = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
address,
|
|
|
|
callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof buffer === 'string')
|
2013-10-28 22:58:37 +01:00
|
|
|
buffer = new Buffer(buffer);
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (!(buffer instanceof Buffer))
|
2013-10-28 22:58:37 +01:00
|
|
|
throw new TypeError('First argument must be a buffer or string.');
|
2013-01-08 04:27:34 +01:00
|
|
|
|
2013-08-09 02:33:40 +02:00
|
|
|
offset = offset | 0;
|
|
|
|
if (offset < 0)
|
|
|
|
throw new RangeError('Offset should be >= 0');
|
|
|
|
|
2014-07-02 15:24:17 +02:00
|
|
|
if ((length == 0 && offset > buffer.length) ||
|
|
|
|
(length > 0 && offset >= buffer.length))
|
2013-08-09 02:33:40 +02:00
|
|
|
throw new RangeError('Offset into buffer too large');
|
|
|
|
|
|
|
|
// Sending a zero-length datagram is kind of pointless but it _is_
|
|
|
|
// allowed, hence check that length >= 0 rather than > 0.
|
|
|
|
length = length | 0;
|
|
|
|
if (length < 0)
|
|
|
|
throw new RangeError('Length should be >= 0');
|
2012-02-23 01:51:27 +01:00
|
|
|
|
|
|
|
if (offset + length > buffer.length)
|
2013-08-09 02:33:40 +02:00
|
|
|
throw new RangeError('Offset + length beyond buffer length');
|
|
|
|
|
|
|
|
port = port | 0;
|
|
|
|
if (port <= 0 || port > 65535)
|
|
|
|
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) {
|
2013-01-28 22:19:02 +01:00
|
|
|
// 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 < self._sendQueue.length; i++)
|
|
|
|
self.send.apply(self, self._sendQueue[i]);
|
|
|
|
self._sendQueue = undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
self._sendQueue.push([buffer, offset, length, port, address, callback]);
|
2012-07-09 17:53:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2013-07-18 23:18:50 +02:00
|
|
|
self._handle.lookup(address, function(ex, ip) {
|
|
|
|
if (ex) {
|
|
|
|
if (callback) callback(ex);
|
|
|
|
self.emit('error', ex);
|
2015-04-28 19:46:14 +02:00
|
|
|
} else if (self._handle) {
|
2014-12-09 05:29:47 +01:00
|
|
|
var req = new SendWrap();
|
|
|
|
req.buffer = buffer; // Keep reference alive.
|
|
|
|
req.length = length;
|
2015-01-08 20:14:44 +01:00
|
|
|
req.address = address;
|
|
|
|
req.port = port;
|
2013-08-09 04:48:10 +02:00
|
|
|
if (callback) {
|
|
|
|
req.callback = callback;
|
|
|
|
req.oncomplete = afterSend;
|
|
|
|
}
|
|
|
|
var err = self._handle.send(req,
|
|
|
|
buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
ip,
|
|
|
|
!!callback);
|
|
|
|
if (err && callback) {
|
2011-08-20 03:47:40 +02:00
|
|
|
// don't emit as error, dgram_legacy.js compatibility
|
2015-05-04 20:40:25 +02:00
|
|
|
var ex = exceptionWithHostPort(err, 'send', address, port);
|
|
|
|
process.nextTick(callback, ex);
|
2011-08-20 03:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-27 00:43:23 +02:00
|
|
|
function afterSend(err) {
|
2015-01-08 20:14:44 +01:00
|
|
|
if (err) {
|
|
|
|
err = exceptionWithHostPort(err, 'send', this.address, this.port);
|
|
|
|
}
|
2015-05-27 09:43:02 +02:00
|
|
|
this.callback(err, this.length);
|
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
|
|
|
};
|