chunk autism

This commit is contained in:
Elijah 2023-12-10 15:11:11 -05:00
parent 064cf15a35
commit 238ae23d77

View file

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