forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.js
161 lines (128 loc) · 4.11 KB
/
udp.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <[email protected]>, Håkon Nessjøen <[email protected]>
*
* This program is free software.
* You should have received a copy of the MIT licence as well as the Bitfocus
* Individual Contributor License Agreement for companion along with
* this program.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*
*/
var debug = require('debug')('lib/udp')
var UDP = require('dgram')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var udp_sockets = []
const STATUS_UNKNOWN = null
const STATUS_OK = 0
const STATUS_WARNING = 1
const STATUS_ERROR = 2
// Private function
function new_status(self, status, message) {
if (self.status != status) {
self.status = status
self.emit('status_change', status, message)
}
}
function udp(host, port, options) {
var self = this
EventEmitter.call(this)
self.destroyed = false
self.status = undefined
self.host = host
self.port = port
self.options = options || {}
self.bound = false
self.pending_memberships = []
self.socket = UDP.createSocket('udp4')
debug('new udp instance for sending to ' + host)
if (self.options.bind_port || self.options.bind_ip) {
try {
self.socket.bind(self.options.bind_port || 0, self.options.bind_ip)
} catch (e) {
debug('Error binding to ip/port: ' + self.options.bind_ip + ':' + self.options.bind_port)
new_status(self, STATUS_ERROR, 'Unable to bind to port ' + self.options.bind_port)
}
}
if (self.options.broadcast) {
self.socket.setBroadcast(true)
}
if (self.options.ttl !== undefined) {
self.socket.setTTL(self.options.ttl)
}
if (self.options.multicast_ttl !== undefined) {
self.socket.setMulticastTTL(self.options.multicast_ttl)
}
self.socket.on('error', function (error) {
// status levels: null = unknown, 0 = ok, 1 = warning, 2 = error
new_status(self, STATUS_ERROR, error.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
})
self.socket.on('listening', function () {
self.bound = true
if (self.pending_memberships.length) {
while (self.pending_memberships.length > 0) {
self.socket.addMembership(member.shift())
}
}
if (self.options.multicast_interface) {
self.socket.setMulticastInterface(self.options.multicast_interface)
}
new_status(self, STATUS_OK)
self.emit.apply(self, ['listening'].concat(Array.from(arguments)))
})
self.socket.on('message', self.emit.bind(self, 'data'))
udp_sockets.push(self.socket)
debug(udp_sockets.length + ' UDP sockets in use (+1)')
setTimeout(() => {
if (!self.destroyed && !self.listenerCount('error')) {
// The socket is active and has no listeners. Log an error for the module devs!
console.error(`Danger: UDP socket for ${self.host}:${self.port} is missing an error handler!`)
}
}, 5000)
return self
}
util.inherits(udp, EventEmitter)
udp.prototype.send = function (message, cb) {
var self = this
debug('sending ' + (message !== undefined ? message.length : 'undefined') + ' bytes to', self.host, self.port)
self.socket.send(message, self.port, self.host, function (error) {
if (error) {
new_status(self, STATUS_ERROR, error.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
if (typeof cb == 'function') {
cb(error)
}
return
}
new_status(self, STATUS_OK)
if (typeof cb == 'function') {
cb()
}
})
}
udp.prototype.addMembership = function (member) {
if (!self.bound) {
self.pending_memberships.push(member)
} else {
self.socket.addMembership(member)
}
}
udp.prototype.destroy = function () {
var self = this
self.destroyed = true
if (udp_sockets.indexOf(self.socket) !== -1) {
udp_sockets.splice(udp_sockets.indexOf(self.socket), 1)
debug(udp_sockets.length + ' UDP sockets in use (-1)')
}
self.socket.removeAllListeners()
self.removeAllListeners()
self.socket.close()
}
exports = module.exports = udp