forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.js
194 lines (154 loc) · 4.63 KB
/
tcp.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <william@bitfocus.io>, Håkon Nessjøen <haakon@bitfocus.io>
*
* 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/tcp')
var net = require('net')
var util = require('util')
var EventEmitter = require('events').EventEmitter
var tcp_sockets = []
const STATUS_UNKNOWN = null
const STATUS_OK = 0
const STATUS_WARNING = 1
const STATUS_ERROR = 2
// Private functions
function new_status(self, status, message) {
if (self.status != status) {
self.status = status
self.emit('status_change', status, message)
}
}
function tcp_reconnect(self) {
delete self.try_timer
new_status(self, STATUS_WARNING, 'Connecting')
self.failed_attempts++
debug('Reconnecting to ' + self.host + ':' + self.port + ', retry ' + self.failed_attempts)
self.connect()
}
function tcp(host, port, options) {
var self = this
EventEmitter.call(this)
self.status = undefined
self.host = host
self.port = port
self.options = options || {}
self.trying = false
self.try_timer = undefined
self.failed_attempts = 0
self.connected = false
if (self.options.reconnect_interval === undefined) {
self.options.reconnect_interval = 2000
}
if (self.options.reconnect === undefined) {
self.options.reconnect = true
}
self.socket = new net.Socket()
self.socket.setKeepAlive(true)
self.socket.setNoDelay(true)
debug('new tcp instance for sending to ' + host)
self.socket.on('error', function (err) {
self.trying = false
self.connected = false
if (self.options.reconnect) {
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
self.try_timer = setTimeout(tcp_reconnect, self.options.reconnect_interval, self)
}
// status levels: null = unknown, 0 = ok, 1 = warning, 2 = error
debug('error', err.message)
new_status(self, STATUS_ERROR, err.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
})
self.socket.on('connect', function () {
self.failed_attempts = 0
self.connected = true
self.trying = false
new_status(self, STATUS_OK)
self.emit('connect', self.socket)
})
self.socket.on('end', function () {
debug('Disconnected')
self.connected = false
new_status(self, STATUS_ERROR, 'Disconnected')
if (!self.trying && self.options.reconnect) {
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
self.try_timer = setTimeout(tcp_reconnect, self.options.reconnect_interval, self)
}
self.emit('end')
})
self.socket.on('data', self.emit.bind(self, 'data'))
self.socket.on('drain', self.emit.bind(self, 'drain'))
tcp_sockets.push(self.socket)
debug(tcp_sockets.length + ' TCP sockets in use (+1)')
// Let caller install event handlers first
setImmediate(self.connect.bind(self))
return self
}
util.inherits(tcp, EventEmitter)
tcp.prototype.connect = function () {
var self = this
if (!self.trying) {
self.trying = true
self.socket.connect(self.port, self.host)
}
}
tcp.prototype.write = tcp.prototype.send = function (message, cb) {
var self = this
if (self.connected) {
debug('sending ' + (message !== undefined ? message.length : 'undefined') + ' bytes to', self.host, self.port)
try {
self.socket.write(message, 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
}
if (typeof cb == 'function') {
cb()
}
})
} catch (error) {
// Unhandeled socket error
new_status(self, STATUS_ERROR, error.message)
self.emit.apply(self, ['error'].concat(Array.from(arguments)))
self.connected = false
cb(error)
}
return true
} else {
debug('Tried to send, but not connected')
return false
}
}
tcp.prototype.destroy = function () {
var self = this
if (self.try_timer !== undefined) {
clearTimeout(self.try_timer)
}
if (tcp_sockets.indexOf(self.socket) !== -1) {
tcp_sockets.splice(tcp_sockets.indexOf(self.socket), 1)
debug(tcp_sockets.length + ' TCP sockets in use (-1)')
}
self.socket.removeAllListeners()
self.removeAllListeners()
self.socket.destroy()
}
exports = module.exports = tcp