From 47854dded60b8a33d695fdb0b5278695c65bd326 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Mon, 10 Apr 2023 20:35:39 +0200 Subject: new packet handler --- webAO/client.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/webAO/client.ts b/webAO/client.ts index 100a178..857e4c2 100644 --- a/webAO/client.ts +++ b/webAO/client.ts @@ -105,6 +105,8 @@ class Client extends EventEmitter { _lastTimeICReceived: any; manifest: string[]; viewport: Viewport; + partial_packet: boolean; + temp_packet: String; connect: () => void; loadResources: () => void isLowMemory: () => void @@ -150,6 +152,8 @@ class Client extends EventEmitter { this.sender = sender this.viewport = masterViewport(); this._lastTimeICReceived = new Date(0); + this.partial_packet = false; + this.temp_packet = ""; loadResources isLowMemory } @@ -193,6 +197,7 @@ class Client extends EventEmitter { joinServer() { this.sender.sendServer(`HI#${hdid}#%`); this.sender.sendServer(`ID#webAO#${version}#%`); + if (mode !== "replay") { this.checkUpdater = setInterval(() => this.sender.sendCheck(), 5000); } @@ -230,19 +235,62 @@ class Client extends EventEmitter { const msg = e.data; console.debug(`S: ${msg}`); - const packets = Array(msg.split("%")); + this.handle_server_packet(msg); + + } - packets.forEach(function(data: String){ - const splitPacket = String(data).split('#') - const packetHeader = splitPacket[0]; + /** + * Decode the packet + * @param {MessageEvent} e + */ + handle_server_packet(p_data: string) +{ + let in_data = p_data; + + if (!p_data.endsWith("%")) { + this.partial_packet = true; + this.temp_packet = this.temp_packet + in_data + console.log("Partial packet") + return; + } - packetHandler.has(packetHeader) - ? packetHandler.get(packetHeader)(splitPacket) - : console.warn(`Invalid packet header ${packetHeader}`); - }); - + else { + if (this.partial_packet) { + in_data = this.temp_packet + in_data + this.temp_packet = ""; + this.partial_packet = false; + } + } + + const packet_list = in_data.split("%"); + console.log(packet_list) + + for (var packet of packet_list) { + let f_contents; + // Packet should *always* end with # + if (packet.endsWith("#")) { + f_contents = packet.slice(0, -1).split("#"); + } + // But, if it somehow doesn't, we should still be able to handle it + else { + f_contents = packet.split("#"); + } + // Empty packets are suspicious! + if (f_contents.length == 0) { + console.warn("WARNING: Empty packet received from server, skipping..."); + continue; + } + // Take the first arg as the command + const command = f_contents[0]; + console.log(command) + // The rest is contents of the packet + packetHandler.has(command) + ? packetHandler.get(command)(f_contents) + : console.warn(`Invalid packet header ${command}`); + }; } + /** * Triggered when an network error occurs. * @param {ErrorEvent} e -- cgit From db1b7352f3c7233b98d47b4fd6617fe07cdab618 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Mon, 10 Apr 2023 20:44:07 +0200 Subject: spammy logging --- webAO/client.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/webAO/client.ts b/webAO/client.ts index 566334e..e5c5e2b 100644 --- a/webAO/client.ts +++ b/webAO/client.ts @@ -38,6 +38,7 @@ export const UPDATE_INTERVAL = 60; */ export let oldLoading = false; export const setOldLoading = (val: boolean) => { + console.warn("old loading set to "+val) oldLoading = val } @@ -261,7 +262,6 @@ class Client extends EventEmitter { } const packet_list = in_data.split("%"); - console.log(packet_list) for (var packet of packet_list) { let f_contents; @@ -280,12 +280,14 @@ class Client extends EventEmitter { } // Take the first arg as the command const command = f_contents[0]; - console.log(command) - // The rest is contents of the packet - packetHandler.has(command) - ? packetHandler.get(command)(f_contents) - : console.warn(`Invalid packet header ${command}`); - }; + if(command!=="") + { + // The rest is contents of the packet + packetHandler.has(command) + ? packetHandler.get(command)(f_contents) + : console.warn(`Invalid packet header ${command}`); + }; + } } -- cgit