forked from wankdanker/node-datagram-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (76 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var udp = require('dgram')
, pipe = require('stream').prototype.pipe
, nodeVersion = process.version.replace('v','').split(/\./gi).map(function (t) { return parseInt(t, 10) });
module.exports = UdpStream;
function UdpStream (options, cb) {
var options = options || {};
var address = options.address || '0.0.0.0';
var port = options.port || 12345;
var bindingPort = options.bindingPort || port;
var unicast = options.unicast || null;
var broadcast = options.broadcast || null;
var multicast = options.multicast || null;
var multicastTTL = options.multicastTTL || 1;
var destination = unicast || multicast || broadcast;
var loopback = options.loopback || false;
var reuseAddr = (options.reuseAddr === false) ? false : true;
var socket;
if (nodeVersion[0] === 0 && nodeVersion[1] < 12) {
//node v0.10 does not support passing an object to dgram.createSocket
//not sure if v0.11 does, but assuming it does not.
socket = udp.createSocket('udp4');
}
else {
socket = udp.createSocket({type: 'udp4', reuseAddr: reuseAddr });
}
socket.write = function (message) {
if (typeof message === "string") {
message = new Buffer(message, "utf8");
}
socket.send(message, 0, message.length, port, destination);
return true;
};
socket.end = function () {
setImmediate(function () {
socket.close();
});
};
socket.pause = function () {
socket.paused = true;
return this;
};
socket.resume = function () {
socket.paused = false;
return this;
};
socket.on('message', function (msg, rinfo) {
msg.rinfo = rinfo;
socket.emit('data', msg);
});
socket.on('error', startupErrorListener);
socket.bind(bindingPort, address);
socket.on('listening', function () {
socket.removeListener('error', startupErrorListener);
if (multicast) {
//set up for multicast
try {
socket.addMembership(multicast);
socket.setMulticastTTL(multicastTTL);
socket.setMulticastLoopback(loopback ? true : false);
}
catch (err) {
socket.emit('error', err);
return cb && cb(err);
}
}
else if (broadcast) {
socket.setBroadcast(true);
}
return cb && cb();
});
socket.pipe = pipe;
return socket;
function startupErrorListener(err) {
return cb && cb(err);
}
}