Skip to content

Commit

Permalink
fix: handle slow networks better
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Apr 20, 2022
1 parent 447e3e6 commit 683c91d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeviceDrawProps, DeviceRegisterProps } from './device-types/api.js'
import { DEFAULT_PORT } from './lib.js'

const PING_UNACKED_LIMIT = 5 // Arbitrary number
const PING_IDLE_TIMEOUT = 500 // Pings are allowed to be late if another packet has been received recently
const PING_INTERVAL = 100
const RECONNECT_DELAY = 1000

Expand Down Expand Up @@ -62,6 +63,7 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit

private _pingInterval: NodeJS.Timer | undefined
private _pingUnackedCount = 0
private _lastReceivedAt = 0
private _connected = false
private _connectionActive = false // True when connected/connecting/reconnecting
private _retryConnectTimeout: NodeJS.Timer | undefined = undefined
Expand Down Expand Up @@ -97,6 +99,7 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit
this.emit('disconnected')
}
this._connected = false
this.receiveBuffer = ''

if (this._pingInterval) {
clearInterval(this._pingInterval)
Expand All @@ -121,6 +124,7 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit

this._connected = true
this._pingUnackedCount = 0
this.receiveBuffer = ''

if (!this._pingInterval) {
this._pingInterval = setInterval(() => this.sendPing(), PING_INTERVAL)
Expand All @@ -143,7 +147,7 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit

private sendPing(): void {
if (this._connected && this.socket) {
if (this._pingUnackedCount > PING_UNACKED_LIMIT) {
if (this._pingUnackedCount > PING_UNACKED_LIMIT && this._lastReceivedAt <= Date.now() - PING_IDLE_TIMEOUT) {
// Ping was never acked, so it looks like a timeout
this.emit('log', 'ping timeout')
try {
Expand Down Expand Up @@ -204,16 +208,17 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit
}

private _handleReceivedData(data: Buffer): void {
this._lastReceivedAt = Date.now()
this.receiveBuffer += data.toString()

let i = -1
let offset = 0
while ((i = this.receiveBuffer.indexOf('\n', offset)) !== -1) {
const line = this.receiveBuffer.substr(offset, i - offset)
const line = this.receiveBuffer.substring(offset, i)
offset = i + 1
this.handleCommand(line.toString().replace(/\r/, ''))
}
this.receiveBuffer = this.receiveBuffer.substr(offset)
this.receiveBuffer = this.receiveBuffer.substring(offset)
}

private handleCommand(line: string): void {
Expand All @@ -222,6 +227,8 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit
const body = i === -1 ? '' : line.slice(i + 1)
const params = parseLineParameters(body)

console.log(line)

switch (cmd.toUpperCase()) {
case 'PING':
this.socket?.write(`PONG ${body}\n`)
Expand Down

1 comment on commit 683c91d

@amcchord
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!

Please sign in to comment.