Skip to content

Commit

Permalink
api.ts send queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Die4Ever committed May 7, 2024
1 parent 262b498 commit 2e9f929
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
68 changes: 64 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class APIConnection
onErrorCallback: (hadError: any) => void;
onDataCallback: (hadError: any) => void;
buffer:string = "";
awaitingResponse:boolean = false;
responseTimeout: number = null;
sendQueue = [];

constructor(name:string, port:number, callback:CallableFunction, errorCallback?:CallableFunction, connectCallback?:CallableFunction) {
this.name = name;
Expand All @@ -31,17 +34,62 @@ class APIConnection
this.connect();
}

send(data:Object) {
send(data:Object, awaitResponse:boolean) {
try {
let r: string = JSON.stringify(data) + '\0';
info(this.name+' sending:', r.length, r);
this.end(r);

if(this.awaitingResponse) {
info(this.name+' queuing:', r.length, r);
this.sendQueue.push(r);
this.sendQueue.push(awaitResponse);
} else {
info(this.name+' sending:', r.length, r);
if(awaitResponse) {
this._setAwaitResponse();
}
this.end(r);
}
} catch(e) {
printException('error sending '+this.name, e);
}
}

destroy() {
private _setAwaitResponse() {
this.awaitingResponse = true;
this.responseTimeout = context.setTimeout(function() {
this.responseTimeout = null;
this._sendNext();
}, 30000);
}

private _sendNext() {
try {
if(this.responseTimeout) {
context.clearTimeout(this.responseTimeout);
}
this.awaitingResponse = false;

let r:string = this.sendQueue.shift();
let awaitResponse:boolean = this.sendQueue.shift();
if(!r) return;
info(this.name+' _sendNext() sending:', r.length, r);

if(awaitResponse) {
this._setAwaitResponse();
}

this.end(r);
} catch(e) {
printException('error in _sendNext() '+this.name, e);
}
}

private _checkSendQueue() {
if(this.awaitingResponse) return;
this._sendNext();
}

private destroy() {
if(!this.sock) return;

info(this.name+'.destroy()');
Expand Down Expand Up @@ -105,6 +153,7 @@ class APIConnection
private onData(message: string) {
let data: Object = null;
let resp: Object = null;
let success: boolean = false;

let packets = message.split('\0');

Expand All @@ -114,6 +163,7 @@ class APIConnection

for(let i=0; i<packets.length; i++) {
let message = packets[i];
resp = null;
if(message.length == 0) continue;

try {
Expand All @@ -133,10 +183,19 @@ class APIConnection

try {
info(this.name+" received data: ", data);
success = true;
resp = this.callback(data);
} catch(e) {
printException('error handling '+this.name+' request: ' + message, e);
}

if(resp) {
this.send(resp, false);
}
}

if(success) {
this._sendNext();
}
}

Expand Down Expand Up @@ -205,6 +264,7 @@ class APIConnection
if(self.connectCallback) {
self.connectCallback();
}
this._checkSendQueue();
}
self.good = true;
});
Expand Down
15 changes: 8 additions & 7 deletions src/modules/archipelagoConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ function archipelago_select_message(type: string, message?: any){
}
}
// if (requested_games){//request any remaining games
// let games = requested_games;
// let games = requested_games;
// context.setTimeout(() => {connection.send({cmd: "GetDataPackage", games: games}); archipelago_games_requested += games.length;}, timeout);
// }
}
// else{
// connection.send({cmd: "GetDataPackage", games: archipelago_settings.multiworld_games});
// connection.send({cmd: "GetDataPackage", games: archipelago_settings.multiworld_games});
// archipelago_games_requested += archipelago_settings.multiworld_games.length;
// }
break;
Expand All @@ -122,6 +122,7 @@ function archipelago_select_message(type: string, message?: any){
}

function ac_req(data) {//This is what we do when we receive a data packet
// TODO: come up with a better name for this function, and also move some of these big cases into their own functions
var Archipelago = GetModule("RCTRArchipelago") as RCTRArchipelago;
var archipelagoPlayers: playerTuple[] = [];
switch(data.cmd){
Expand Down Expand Up @@ -348,12 +349,12 @@ function ac_req(data) {//This is what we do when we receive a data packet
}
else{
var player_color = "{PALELAVENDER}" + source;
var message_choice = [player_color + " {RED}is bad at video games!", player_color + " {RED}just got Windows Vista'd.",
player_color + " {RED}should have upgraded to Linux.", player_color + " {RED}has met their maker!",
'{RED}"What is death anyways?"\n{PALELAVENDER}-' + player_color,
var message_choice = [player_color + " {RED}is bad at video games!", player_color + " {RED}just got Windows Vista'd.",
player_color + " {RED}should have upgraded to Linux.", player_color + " {RED}has met their maker!",
'{RED}"What is death anyways?"\n{PALELAVENDER}-' + player_color,
"{RED}If it makes you feel better, at least it's " + player_color + "{RED}'s fault and not yours.", player_color + " {RED}is an airsick lowlander!",
player_color + "{RED} missed 100% of the shots they didn't take.", "{RED}It was " + player_color + "{RED}'s controller, I swear!",
player_color + "{RED} was not the imposter.", player_color + "{RED} rolled a natural 1.",
player_color + "{RED} was not the imposter.", player_color + "{RED} rolled a natural 1.",
player_color + "{RED} should not have tried stealing the kings flocks from Ammon!", player_color + "{RED} started a land war in Asia!"];
var death_message = message_choice[Math.floor(Math.random() * message_choice.length)];
archipelago_print_message(death_message);
Expand Down Expand Up @@ -395,7 +396,7 @@ function ac_req(data) {//This is what we do when we receive a data packet
console.log("Instert bee movie here");
context.setTimeout(() => {archipelago_send_message("LocationScouts");}, 3000)
}

}
break;
case "SetReply"://Handles hints
Expand Down

0 comments on commit 2e9f929

Please sign in to comment.