add connection checking

This commit is contained in:
Elijah 2023-12-10 14:45:23 -05:00
parent 5cd5ba57c2
commit 53f065b630
2 changed files with 23 additions and 0 deletions

View file

@ -7,4 +7,5 @@ export interface ProtocolMessage {
export enum ProtocolOperation { export enum ProtocolOperation {
UploadFile = 0, UploadFile = 0,
ACK = 1, ACK = 1,
NOP = 2,
} }

View file

@ -12,6 +12,9 @@ export default class VM {
#socket : Socket; #socket : Socket;
#writeLock : Mutex = new Mutex(); #writeLock : Mutex = new Mutex();
#fileQueue : Queue<File> = new Queue<File>(); #fileQueue : Queue<File> = new Queue<File>();
#nopTimeout : NodeJS.Timeout | null = null;
isConnectedToVM : boolean = false;
#noNop : boolean = false;
connected : boolean = false; connected : boolean = false;
#events : EventEmitter; #events : EventEmitter;
constructor(socketpath : string) { constructor(socketpath : string) {
@ -88,12 +91,31 @@ export default class VM {
return; return;
} }
var msg = msgpack.decode(payload) as protocol.ProtocolMessage; 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) { switch (msg.Operation) {
case protocol.ProtocolOperation.ACK: case protocol.ProtocolOperation.ACK:
this.#events.emit('ack'); this.#events.emit('ack');
break; 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<void> { function sleep(ms : number) : Promise<void> {