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');
|
2016-05-02 19:27:12 +02:00
|
|
|
const UV_UDP_REUSEADDR = process.binding('constants').os.UV_UDP_REUSEADDR;
|
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) {
|
2016-02-29 19:47:14 +01:00
|
|
|
return lookup(address || '127.0.0.1', 4, callback);
|
2011-08-20 03:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function lookup6(address, callback) {
|
2016-02-29 19:47:14 +01:00
|
|
|
return lookup(address || '::1', 6, callback);
|
2011-08-20 03:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function newHandle(type) {
|
2016-08-08 11:32:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-08 11:32:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-08-08 11:32:50 +02:00
|
|
|
if (type !== null && typeof type === 'object') {
|
2014-04-09 01:04:53 +02:00
|
|
|
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*/) {
|
|
|
|
let port = port_;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
this._healthCheck();
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2016-08-08 11:32:50 +02:00
|
|
|
if (this._bindState !== BIND_STATE_UNBOUND)
|
2013-01-22 23:52:20 +01:00
|
|
|
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')
|
2017-02-08 18:18:07 +01:00
|
|
|
this.once('listening', arguments[arguments.length - 1]);
|
2013-02-27 19:31:24 +01:00
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
if (port instanceof UDP) {
|
2017-02-08 18:18:07 +01:00
|
|
|
replaceHandle(this, port);
|
|
|
|
startListening(this);
|
|
|
|
return this;
|
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
|
|
|
|
2016-02-29 19:47:14 +01:00
|
|
|
// defaulting address for bind to all interfaces
|
2017-02-08 18:18:07 +01:00
|
|
|
if (!address && this._handle.lookup === lookup4) {
|
2016-02-29 19:47:14 +01:00
|
|
|
address = '0.0.0.0';
|
2017-02-08 18:18:07 +01:00
|
|
|
} else if (!address && this._handle.lookup === lookup6) {
|
2016-02-29 19:47:14 +01:00
|
|
|
address = '::';
|
|
|
|
}
|
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
// resolve address first
|
2017-02-08 18:18:07 +01:00
|
|
|
this._handle.lookup(address, (err, ip) => {
|
2011-08-20 03:47:40 +02:00
|
|
|
if (err) {
|
2017-02-08 18:18:07 +01:00
|
|
|
this._bindState = BIND_STATE_UNBOUND;
|
|
|
|
this.emit('error', err);
|
2012-07-09 17:53:48 +02:00
|
|
|
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;
|
2017-02-08 18:18:07 +01:00
|
|
|
if (this._reuseAddr)
|
2016-05-02 19:27:12 +02:00
|
|
|
flags |= UV_UDP_REUSEADDR;
|
2015-08-25 21:24:41 +02:00
|
|
|
|
2014-08-22 22:51:53 +02:00
|
|
|
if (cluster.isWorker && !exclusive) {
|
2017-02-08 18:18:07 +01:00
|
|
|
const 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);
|
2017-02-08 18:18:07 +01:00
|
|
|
this.emit('error', ex);
|
|
|
|
this._bindState = BIND_STATE_UNBOUND;
|
2013-07-28 12:19:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
if (!this._handle)
|
2013-01-22 23:52:20 +01:00
|
|
|
// handle has been closed in the mean time.
|
|
|
|
return handle.close();
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
replaceHandle(this, handle);
|
|
|
|
startListening(this);
|
|
|
|
};
|
|
|
|
cluster._getServer(this, {
|
2015-08-25 21:24:41 +02:00
|
|
|
address: ip,
|
|
|
|
port: port,
|
2017-02-08 18:18:07 +01:00
|
|
|
addressType: this.type,
|
2015-08-25 21:24:41 +02:00
|
|
|
fd: -1,
|
|
|
|
flags: flags
|
|
|
|
}, onHandle);
|
2013-01-22 23:52:20 +01:00
|
|
|
} else {
|
2017-02-08 18:18:07 +01:00
|
|
|
if (!this._handle)
|
2013-01-22 23:52:20 +01:00
|
|
|
return; // handle has been closed in the mean time
|
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
const err = this._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);
|
2017-02-08 18:18:07 +01:00
|
|
|
this.emit('error', ex);
|
|
|
|
this._bindState = BIND_STATE_UNBOUND;
|
2013-01-22 23:52:20 +01:00
|
|
|
// Todo: close?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
startListening(this);
|
2013-01-22 23:52:20 +01:00
|
|
|
}
|
|
|
|
});
|
2014-12-29 21:03:24 +01:00
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
return this;
|
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')
|
2016-01-26 00:00:06 +01:00
|
|
|
buffer = Buffer.from(buffer);
|
2016-01-29 14:18:27 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-17 12:36:54 +02:00
|
|
|
function fixBufferList(list) {
|
|
|
|
const newlist = new Array(list.length);
|
|
|
|
|
|
|
|
for (var i = 0, l = list.length; i < l; i++) {
|
|
|
|
var buf = list[i];
|
2016-01-29 14:18:27 +01:00
|
|
|
if (typeof buf === 'string')
|
2016-05-17 12:36:54 +02:00
|
|
|
newlist[i] = Buffer.from(buf);
|
2016-01-29 14:18:27 +01:00
|
|
|
else if (!(buf instanceof Buffer))
|
2016-05-17 12:36:54 +02:00
|
|
|
return null;
|
|
|
|
else
|
|
|
|
newlist[i] = buf;
|
2016-01-29 14:18:27 +01:00
|
|
|
}
|
|
|
|
|
2016-05-17 12:36:54 +02:00
|
|
|
return newlist;
|
2016-01-29 14:18:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
2016-05-30 18:29:27 +02:00
|
|
|
if (!self._queue) {
|
|
|
|
self._queue = [];
|
2017-01-27 15:54:31 +01:00
|
|
|
self.once('error', onListenError);
|
|
|
|
self.once('listening', onListenSuccess);
|
2016-01-29 14:18:27 +01:00
|
|
|
}
|
2016-05-30 18:29:27 +02:00
|
|
|
self._queue.push(toEnqueue);
|
2016-01-29 14:18:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-27 15:54:31 +01:00
|
|
|
function onListenSuccess() {
|
|
|
|
this.removeListener('error', onListenError);
|
|
|
|
clearQueue.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onListenError(err) {
|
|
|
|
this.removeListener('listening', onListenSuccess);
|
|
|
|
this._queue = undefined;
|
|
|
|
this.emit('error', new Error('Unable to send data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-30 18:29:27 +02:00
|
|
|
function clearQueue() {
|
|
|
|
const queue = this._queue;
|
|
|
|
this._queue = undefined;
|
|
|
|
|
|
|
|
// Flush the send queue.
|
|
|
|
for (var i = 0; i < queue.length; i++)
|
|
|
|
queue[i]();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2016-05-17 12:36:54 +02:00
|
|
|
let list;
|
2011-08-20 03:47:40 +02:00
|
|
|
|
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') {
|
2016-05-17 12:36:54 +02:00
|
|
|
list = [ Buffer.from(buffer) ];
|
2016-01-29 14:18:27 +01:00
|
|
|
} else if (!(buffer instanceof Buffer)) {
|
|
|
|
throw new TypeError('First argument must be a buffer or a string');
|
|
|
|
} else {
|
2016-05-17 12:36:54 +02:00
|
|
|
list = [ buffer ];
|
2016-01-29 14:18:27 +01:00
|
|
|
}
|
2016-05-17 12:36:54 +02:00
|
|
|
} else if (!(list = fixBufferList(buffer))) {
|
2016-01-29 14:18:27 +01:00
|
|
|
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
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
this._healthCheck();
|
2012-07-09 17:53:48 +02:00
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
if (this._bindState === BIND_STATE_UNBOUND)
|
|
|
|
this.bind({port: 0, exclusive: true}, null);
|
2012-12-31 17:42:54 +01:00
|
|
|
|
2016-05-17 12:36:54 +02:00
|
|
|
if (list.length === 0)
|
2016-09-23 22:55:53 +02:00
|
|
|
list.push(Buffer.alloc(0));
|
2016-05-17 12:36:54 +02: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.
|
2017-02-08 18:18:07 +01:00
|
|
|
if (this._bindState !== BIND_STATE_BOUND) {
|
|
|
|
enqueue(this, this.send.bind(this, list, port, address, callback));
|
2012-07-09 17:53:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-08-20 03:47:40 +02:00
|
|
|
|
2017-02-08 18:18:07 +01:00
|
|
|
const afterDns = (ex, ip) => {
|
|
|
|
doSend(ex, this, ip, list, address, port, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._handle.lookup(address, afterDns);
|
2011-08-20 03:47:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-17 12:36:54 +02:00
|
|
|
function doSend(ex, self, ip, list, address, port, callback) {
|
2016-01-29 14:18:27 +01:00
|
|
|
if (ex) {
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
callback(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('error', ex);
|
|
|
|
return;
|
|
|
|
} else if (!self._handle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var req = new SendWrap();
|
2016-05-17 12:36:54 +02:00
|
|
|
req.list = list; // Keep reference alive.
|
2016-01-29 14:18:27 +01:00
|
|
|
req.address = address;
|
|
|
|
req.port = port;
|
|
|
|
if (callback) {
|
|
|
|
req.callback = callback;
|
|
|
|
req.oncomplete = afterSend;
|
|
|
|
}
|
|
|
|
var err = self._handle.send(req,
|
2016-05-17 12:36:54 +02:00
|
|
|
list,
|
|
|
|
list.length,
|
2016-01-29 14:18:27 +01:00
|
|
|
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-03-28 03:08:29 +02:00
|
|
|
} else {
|
|
|
|
err = null;
|
2015-01-08 20:14:44 +01:00
|
|
|
}
|
2016-03-28 03:08:29 +02:00
|
|
|
|
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);
|
2016-05-30 18:29:27 +02:00
|
|
|
|
|
|
|
if (this._queue) {
|
|
|
|
this._queue.push(this.close.bind(this));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-08-20 03:47:40 +02:00
|
|
|
this._healthCheck();
|
|
|
|
this._stopReceiving();
|
|
|
|
this._handle.close();
|
|
|
|
this._handle = null;
|
2016-02-15 05:53:17 +01:00
|
|
|
process.nextTick(socketCloseNT, this);
|
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
|
|
|
};
|