message queue shits
This commit is contained in:
parent
5557dc1750
commit
5d791150b0
1 changed files with 18 additions and 25 deletions
43
src/VM.ts
43
src/VM.ts
|
@ -11,7 +11,7 @@ export default class VM {
|
|||
#socketpath : string;
|
||||
#socket : Socket;
|
||||
#writeLock : Mutex = new Mutex();
|
||||
#fileQueue : Queue<File> = new Queue<File>();
|
||||
#messageQueue : Queue<protocol.ProtocolMessage> = new Queue<protocol.ProtocolMessage>();
|
||||
#nopTimeout : NodeJS.Timeout | null = null;
|
||||
isConnectedToVM : boolean = false;
|
||||
#noNop : boolean = false;
|
||||
|
@ -32,35 +32,30 @@ export default class VM {
|
|||
this.#socket.on('data', (data) => this.#onData(data));
|
||||
this.#events = new EventEmitter();
|
||||
this.#events.on('ack', () => {
|
||||
if (this.#fileQueue.size > 0)
|
||||
this.fileQueueLoop();
|
||||
if (this.#messageQueue.size > 0)
|
||||
this.messageQueueLoop();
|
||||
})
|
||||
}
|
||||
|
||||
UploadFile(file : File) {
|
||||
this.#fileQueue.enqueue(file);
|
||||
if (this.#fileQueue.size === 1)
|
||||
this.fileQueueLoop();
|
||||
this.#enqueueMessage({
|
||||
Operation: protocol.ProtocolOperation.UploadFile,
|
||||
Filename: file.Filename,
|
||||
FileData: file.FileData
|
||||
});
|
||||
}
|
||||
|
||||
fileQueueLoop() {
|
||||
messageQueueLoop() {
|
||||
return new Promise(async (res, rej) => {
|
||||
var file = this.#fileQueue.dequeue();
|
||||
await this.PushFile(file!);
|
||||
var msg = this.#messageQueue.dequeue();
|
||||
var payload = msgpack.encode(msg);
|
||||
await this.#sendMessage(payload);
|
||||
})
|
||||
}
|
||||
|
||||
PushFile(file : File) : Promise<void> {
|
||||
return new Promise(async (res, rej) => {
|
||||
const msg : protocol.ProtocolMessage = {
|
||||
Operation: protocol.ProtocolOperation.UploadFile,
|
||||
Filename: file.Filename,
|
||||
FileData: file.FileData
|
||||
};
|
||||
var payload = msgpack.encode(msg);
|
||||
await this.#sendMessage(payload);
|
||||
res();
|
||||
});
|
||||
#enqueueMessage(msg : protocol.ProtocolMessage) {
|
||||
this.#messageQueue.enqueue(msg);
|
||||
if (this.#messageQueue.size === 1) this.messageQueueLoop();
|
||||
}
|
||||
|
||||
#sendMessage(data : Buffer) : Promise<void> {
|
||||
|
@ -102,7 +97,7 @@ export default class VM {
|
|||
if (this.#nopTimeout) clearInterval(this.#nopTimeout);
|
||||
this.#nopTimeout = setInterval(() => this.#nopTimeoutFunc(), 5000);
|
||||
if (!this.isConnectedToVM) {
|
||||
if (this.#fileQueue.size > 0) this.fileQueueLoop();
|
||||
if (this.#messageQueue.size > 0) this.messageQueueLoop();
|
||||
this.isConnectedToVM = true;
|
||||
this.#noNop = false;
|
||||
}
|
||||
|
@ -115,11 +110,9 @@ export default class VM {
|
|||
|
||||
#nopTimeoutFunc() {
|
||||
if (!this.#noNop) {
|
||||
var payload : protocol.ProtocolMessage = {
|
||||
this.#enqueueMessage({
|
||||
Operation: protocol.ProtocolOperation.NOP
|
||||
};
|
||||
var data = msgpack.encode(payload);
|
||||
this.#sendMessage(data);
|
||||
});
|
||||
this.#noNop = true;
|
||||
} else {
|
||||
this.isConnectedToVM = false;
|
||||
|
|
Loading…
Reference in a new issue