diff --git a/src/VM.ts b/src/VM.ts index f8e7455..2fab739 100644 --- a/src/VM.ts +++ b/src/VM.ts @@ -17,10 +17,13 @@ export default class VM { #noNop : boolean = false; connected : boolean = false; #events : EventEmitter; + + #currentChunkSize : number | null = null; + #currentChunk : Buffer | null = null; + #currentChunkOffset : number = 0; constructor(socketpath : string) { this.#socketpath = socketpath; this.#socket = new Socket(); - this.#socket.setEncoding("binary"); this.#socket.connect(socketpath); this.#socket.on('connect', () => { this.connected = true; @@ -84,13 +87,18 @@ export default class VM { } #onData(data : Buffer) { - var payload = data.subarray(4); - var header = data.readUInt32LE(0); - if (header !== payload.length) { - log("WARN", `Received message with invalid length header ${header}`); + if (this.#currentChunkSize === null) { + this.#currentChunkSize = data.readUInt32LE(0); + this.#currentChunk = Buffer.alloc(this.#currentChunkSize); + data.copy(this.#currentChunk, 0, 4); + this.#currentChunkOffset = data.length - 4; + return; + } else if (this.#currentChunkSize > this.#currentChunk!.length) { + data.copy(this.#currentChunk!, this.#currentChunkOffset); + this.#currentChunkOffset += data.length; return; } - var msg = msgpack.decode(payload) as protocol.ProtocolMessage; + var msg = msgpack.decode(this.#currentChunk!) as protocol.ProtocolMessage; if (this.#nopTimeout) clearInterval(this.#nopTimeout); this.#nopTimeout = setInterval(() => this.#nopTimeoutFunc(), 5000); if (!this.isConnectedToVM) {