-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: satellite ws implementation #3101
- Loading branch information
Showing
7 changed files
with
147 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import type { Registry } from '../Registry.js' | |
import { ServiceSatelliteApi } from './SatelliteApi.js' | ||
|
||
/** | ||
* Class providing the Satellite/Remote Surface api. | ||
* Class providing the Satellite/Remote Surface api over tcp. | ||
* | ||
* @author Håkon Nessjøen <[email protected]> | ||
* @author Keith Rocheck <[email protected]> | ||
|
@@ -24,15 +24,15 @@ import { ServiceSatelliteApi } from './SatelliteApi.js' | |
* develop commercial activities involving the Companion software without | ||
* disclosing the source code of your own applications. | ||
*/ | ||
export class ServiceSatellite extends ServiceBase { | ||
export class ServiceSatelliteTcp extends ServiceBase { | ||
readonly #api: ServiceSatelliteApi | ||
|
||
server: net.Server | undefined = undefined | ||
#server: net.Server | undefined = undefined | ||
|
||
readonly #clients = new Set<Socket>() | ||
|
||
constructor(registry: Registry) { | ||
super(registry, 'Service/Satellite', null, null) | ||
super(registry, 'Service/SatelliteTcp', null, null) | ||
|
||
this.#api = new ServiceSatelliteApi(registry) | ||
|
||
|
@@ -42,9 +42,9 @@ export class ServiceSatellite extends ServiceBase { | |
} | ||
|
||
listen() { | ||
this.server = net.createServer((socket) => { | ||
this.#server = net.createServer((socket) => { | ||
const name = socket.remoteAddress + ':' + socket.remotePort | ||
const socketLogger = LogController.createLogger(`Service/Satellite/${name}`) | ||
const socketLogger = LogController.createLogger(`Service/SatelliteTcp/${name}`) | ||
|
||
this.#clients.add(socket) | ||
|
||
|
@@ -75,23 +75,23 @@ export class ServiceSatellite extends ServiceBase { | |
|
||
socket.on('close', doCleanup) | ||
|
||
socket.on('data', processMessage) | ||
socket.on('data', (data) => processMessage(data.toString())) | ||
}) | ||
this.server.on('error', (e) => { | ||
this.#server.on('error', (e) => { | ||
this.logger.debug(`listen-socket error: ${e}`) | ||
}) | ||
|
||
try { | ||
this.server.listen(this.port) | ||
this.#server.listen(this.port) | ||
} catch (e) { | ||
this.logger.debug(`ERROR opening port this.port for companion satellite devices`) | ||
this.logger.debug(`ERROR opening tcp port ${this.port} for companion satellite devices`) | ||
} | ||
} | ||
|
||
close(): void { | ||
if (this.server) { | ||
this.server.close() | ||
this.server = undefined | ||
if (this.#server) { | ||
this.#server.close() | ||
this.#server = undefined | ||
} | ||
|
||
// Disconnect all clients | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { ServiceBase } from './Base.js' | ||
import LogController from '../Log/Controller.js' | ||
import type { Registry } from '../Registry.js' | ||
import { ServiceSatelliteApi } from './SatelliteApi.js' | ||
import { WebSocketServer } from 'ws' | ||
|
||
/** | ||
* Class providing the Satellite/Remote Surface api over websockets. | ||
* | ||
* @author Håkon Nessjøen <[email protected]> | ||
* @author Keith Rocheck <[email protected]> | ||
* @author William Viker <[email protected]> | ||
* @author Julian Waller <[email protected]> | ||
* @since 2.2.0 | ||
* @copyright 2022 Bitfocus AS | ||
* @license | ||
* 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. | ||
*/ | ||
export class ServiceSatelliteWebsocket extends ServiceBase { | ||
readonly #api: ServiceSatelliteApi | ||
|
||
#server: WebSocketServer | undefined = undefined | ||
|
||
constructor(registry: Registry) { | ||
super(registry, 'Service/SatelliteWebsocket', null, null) | ||
|
||
this.#api = new ServiceSatelliteApi(registry) | ||
|
||
this.port = 16623 | ||
|
||
this.init() | ||
} | ||
|
||
listen() { | ||
if (this.#server === undefined) { | ||
try { | ||
this.#server = new WebSocketServer({ | ||
port: this.port, | ||
}) | ||
|
||
this.#server.on('error', (e) => { | ||
this.logger.debug(`listen-socket error: ${e}`) | ||
}) | ||
|
||
this.#server.on('connection', (socket) => { | ||
// @ts-expect-error This works but isn't in the types.. TODO: verify this | ||
const name = socket.remoteAddress + ':' + socket.remotePort | ||
const socketLogger = LogController.createLogger(`Service/SatelliteWs/${name}`) | ||
|
||
let lastReceived = Date.now() | ||
|
||
// socket.setTimeout(5000) | ||
socket.on('error', (e) => { | ||
socketLogger.silly('socket error:', e) | ||
}) | ||
|
||
const { processMessage, cleanupDevices } = this.#api.initSocket(socketLogger, { | ||
// @ts-expect-error The property exists but not in the types | ||
remoteAddress: socket.remoteAddress, | ||
destroy: () => socket.terminate(), | ||
write: (data) => socket.send(data), | ||
}) | ||
|
||
const timeoutCheck = setInterval(() => { | ||
if (lastReceived < Date.now() - 5000) { | ||
socketLogger.debug('socket timeout') | ||
socket.terminate() | ||
doCleanup() | ||
} | ||
}, 3000) | ||
|
||
const doCleanup = () => { | ||
const count = cleanupDevices() | ||
socketLogger.info(`connection closed with ${count} connected surfaces`) | ||
|
||
socket.removeAllListeners('data') | ||
socket.removeAllListeners('close') | ||
|
||
clearInterval(timeoutCheck) | ||
} | ||
|
||
socket.on('close', doCleanup) | ||
|
||
socket.on('message', (data) => { | ||
lastReceived = Date.now() | ||
|
||
processMessage(data.toString()) | ||
}) | ||
}) | ||
} catch (e) { | ||
this.logger.debug(`ERROR opening ws port ${this.port} for companion satellite devices`) | ||
} | ||
} | ||
} | ||
|
||
close(): void { | ||
if (this.#server) { | ||
this.logger.info('Shutting down') | ||
|
||
for (const client of this.#server.clients) { | ||
client.terminate() | ||
} | ||
|
||
this.#server.close() | ||
this.#server = undefined | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters