Skip to content

Commit 4cd08b7

Browse files
Use interval for keepalive timer, and fix the invalid dummy packet we were sending.
1 parent efd4fd2 commit 4cd08b7

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/consts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { combineRgb } from '@companion-module/base'
22

33
export const msgDelay = 5
4-
export const keepAliveTimeOut = 30000
4+
export const keepAliveTime = 30000
55

66
export const DLE = 0x10
77
export const STX = 0x02
@@ -165,4 +165,4 @@ export function getCommandName(value) {
165165
}
166166
}
167167
return 'Unknown Command'
168-
}
168+
}

src/keepalive.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
import { keepAliveTimeOut } from './consts.js'
1+
import { keepAliveTime } from './consts.js'
22

33
export function startKeepAliveTimer() {
44
if (this.keepAliveTimer) {
5-
clearTimeout(this.keepAliveTimer)
5+
clearInterval(this.keepAliveTimer)
66
}
7-
this.keepAliveTimer = setTimeout(() => {
7+
this.keepAliveTimer = setInterval(() => {
88
this.keepAlive()
9-
}, keepAliveTimeOut)
9+
}, keepAliveTime)
1010
}
1111

1212
export function stopKeepAliveTimer() {
1313
if (this.keepAliveTimer) {
14-
clearTimeout(this.keepAliveTimer)
14+
clearInterval(this.keepAliveTimer)
1515
// biome-ignore lint/performance/noDelete: not really a performance issue
1616
delete this.keepAliveTimer
1717
}
1818
}
1919

2020
export function keepAlive() {
2121
if (this.socket?.isConnected) {
22-
//Send dummy message
23-
this.sendMessage([0x00])
22+
// Send dummy message if the queue is empty
23+
if (this.queue.size === 0) {
24+
this.sendMessage([])
25+
}
2426
}
25-
this.startKeepAliveTimer()
2627
}

src/tcp.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,21 @@ export function hasCommand(cmdCode) {
9191
*/
9292
export function sendMessage(message) {
9393
const msg = message instanceof Buffer ? message : Buffer.from(message)
94-
if (msg.length < 1) {
95-
this.log('warn', 'Empty or invalid message!')
96-
return
97-
}
9894

99-
// check that the command is implemented in the router
100-
const cmdCode = msg[0]
101-
102-
if (
103-
cmdCode !== 97 &&
104-
cmdCode !== 0 &&
105-
this.config.supported_commands_on_connect === true &&
106-
this.commands.length > 0
107-
) {
108-
if (this.commands.indexOf(cmdCode) === -1) {
109-
this.log('warn', `Command code ${cmdCode} is not implemented by this hardware`)
110-
return
95+
if (msg.length > 0) {
96+
// check that the command is implemented in the router
97+
const cmdCode = msg[0]
98+
99+
if (
100+
cmdCode !== 97 &&
101+
cmdCode !== 0 &&
102+
this.config.supported_commands_on_connect === true &&
103+
this.commands.length > 0
104+
) {
105+
if (this.commands.indexOf(cmdCode) === -1) {
106+
this.log('warn', `Command code ${cmdCode} is not implemented by this hardware`)
107+
return
108+
}
111109
}
112110
}
113111

@@ -148,7 +146,6 @@ export function sendMessage(message) {
148146
this.queue.add(async () => {
149147
if (this.socket?.isConnected) {
150148
this.socket.send(packetBuffer)
151-
this.startKeepAliveTimer()
152149

153150
this.addAckCallback(() => {
154151
// Retry sending the command if it fails
@@ -170,6 +167,8 @@ export function init_tcp() {
170167
delete this.socket
171168
}
172169

170+
this.stopKeepAliveTimer()
171+
173172
if (this.config.host) {
174173
this.socket = new TCPHelper(this.config.host, this.config.port)
175174

@@ -183,6 +182,10 @@ export function init_tcp() {
183182
this.stopKeepAliveTimer()
184183
})
185184

185+
this.socket.on('close', () => {
186+
this.stopKeepAliveTimer()
187+
})
188+
186189
this.socket.on('connect', () => {
187190
console.log(`Connected to ${this.config.host}:${this.config.port}`)
188191
this.ackCallbacks = []

0 commit comments

Comments
 (0)