I hate race conditions
This commit is contained in:
@@ -46,50 +46,52 @@ export default class QMPClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async onData(data : Buffer) {
|
private async onData(data : Buffer) {
|
||||||
let msgraw = data.toString();
|
let msgraw = data.toString().split("\n");
|
||||||
let msg;
|
for(var message in msgraw) {
|
||||||
|
let msg;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = JSON.parse(msgraw);
|
msg = JSON.parse(msgraw[message]);
|
||||||
} catch {
|
} catch {
|
||||||
this.emit("qmpreturn", '');
|
this.emit("qmpreturn", "");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.QMP !== undefined) {
|
|
||||||
if (this.sentConnected)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await this.execute({ execute: "qmp_capabilities" });
|
|
||||||
|
|
||||||
this.emit('connected');
|
|
||||||
this.sentConnected = true;
|
|
||||||
}
|
|
||||||
if (msg.return !== undefined)
|
|
||||||
this.emit("qmpreturn", msg.return);
|
|
||||||
else if(msg.event !== undefined) {
|
|
||||||
switch(msg.event) {
|
|
||||||
case "STOP":
|
|
||||||
{
|
|
||||||
log("INFO", "The VM was shut down, restarting...");
|
|
||||||
this.reboot();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "RESET":
|
|
||||||
{
|
|
||||||
log("INFO", "QEMU reset event occured");
|
|
||||||
this.resume();
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
default: {
|
|
||||||
this.emit("qmpreturn", '');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}else
|
|
||||||
// for now just return an empty string.
|
if (msg.QMP !== undefined) {
|
||||||
// This is a giant hack but avoids a deadlock
|
if (this.sentConnected)
|
||||||
this.emit("qmpreturn", '');
|
return;
|
||||||
|
|
||||||
|
await this.execute({ execute: "qmp_capabilities" });
|
||||||
|
|
||||||
|
this.emit('connected');
|
||||||
|
this.sentConnected = true;
|
||||||
|
}
|
||||||
|
if (msg.return !== undefined)
|
||||||
|
this.emit("qmpreturn", msg.return);
|
||||||
|
else if(msg.event !== undefined) {
|
||||||
|
switch(msg.event) {
|
||||||
|
case "STOP":
|
||||||
|
{
|
||||||
|
log("INFO", "The VM was shut down, restarting...");
|
||||||
|
this.reboot();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "RESET":
|
||||||
|
{
|
||||||
|
log("INFO", "QEMU reset event occured");
|
||||||
|
this.resume();
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
default: {
|
||||||
|
this.emit("qmpreturn", "");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
// for now just return an empty string.
|
||||||
|
// This is a giant hack but avoids a deadlock
|
||||||
|
this.emit("qmpreturn", "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private onClose() {
|
private onClose() {
|
||||||
|
|||||||
Reference in New Issue
Block a user