From 53f065b630436d07a590336bdea902f0bb2a54ba Mon Sep 17 00:00:00 2001 From: Elijah Date: Sun, 10 Dec 2023 14:45:23 -0500 Subject: [PATCH] add connection checking --- src/Protocol.ts | 1 + src/VM.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Protocol.ts b/src/Protocol.ts index 5c7601e..9ead95b 100644 --- a/src/Protocol.ts +++ b/src/Protocol.ts @@ -7,4 +7,5 @@ export interface ProtocolMessage { export enum ProtocolOperation { UploadFile = 0, ACK = 1, + NOP = 2, } \ No newline at end of file diff --git a/src/VM.ts b/src/VM.ts index 7568f1e..f02052f 100644 --- a/src/VM.ts +++ b/src/VM.ts @@ -12,6 +12,9 @@ export default class VM { #socket : Socket; #writeLock : Mutex = new Mutex(); #fileQueue : Queue = new Queue(); + #nopTimeout : NodeJS.Timeout | null = null; + isConnectedToVM : boolean = false; + #noNop : boolean = false; connected : boolean = false; #events : EventEmitter; constructor(socketpath : string) { @@ -88,12 +91,31 @@ export default class VM { return; } var msg = msgpack.decode(payload) as protocol.ProtocolMessage; + if (!this.isConnectedToVM) { + if (this.#fileQueue.size > 0) this.fileQueueLoop(); + this.isConnectedToVM = true; + this.#noNop = false; + this.#nopTimeout = setInterval(() => this.#nopTimeoutFunc(), 5000); + } switch (msg.Operation) { case protocol.ProtocolOperation.ACK: this.#events.emit('ack'); break; } } + + #nopTimeoutFunc() { + if (!this.#noNop) { + var payload : protocol.ProtocolMessage = { + Operation: protocol.ProtocolOperation.NOP + }; + var data = msgpack.encode(payload); + this.#sendMessage(data); + this.#noNop = true; + } else { + this.isConnectedToVM = false; + } + } } function sleep(ms : number) : Promise {