Skip to content

Commit

Permalink
fix: replace packet parsing recursion with a loop to avoid max call s…
Browse files Browse the repository at this point in the history
…tack issues
  • Loading branch information
Julusian committed Feb 1, 2021
1 parent 7c880a2 commit bb46f5a
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,32 @@ export class CompanionSatelliteClient extends EventEmitter<CompanionSatelliteCli

private _handleReceivedData(data: Buffer): void {
this.receiveBuffer = Buffer.concat([this.receiveBuffer, data])
this.findPackets()
}

while (this.receiveBuffer.length > 0) {
const header = Protocol.readHeader(this.receiveBuffer)

private findPackets(): void {
const header = Protocol.readHeader(this.receiveBuffer)
// not enough data
if (header === false) {
break
}

// not enough data
if (header === false) {
return
}
// out of sync
if (header === -1) {
console.debug('Out of sync, trying to find next valid packet')
// Try to find next start of packet
this.receiveBuffer = this.receiveBuffer.slice(1)

// out of sync
if (header === -1) {
console.debug('Out of sync, trying to find next valid packet')
// Try to find next start of packet
this.receiveBuffer = this.receiveBuffer.slice(1)
// Loop until it is found or we are out of buffer-data
continue
}

// Loop until it is found or we are out of buffer-data
this.findPackets()
return
}
if (this.receiveBuffer.length < header.length + 6) {
// not enough data yet
break
}

if (header.length + 6 <= this.receiveBuffer.length) {
this.parsePacket(header)
this.receiveBuffer = this.receiveBuffer.slice(header.length + 6)

this.findPackets()
}
}

Expand Down

0 comments on commit bb46f5a

Please sign in to comment.