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;
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) {