Skip to content

Commit

Permalink
fix: New web socket code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pazaz committed Dec 13, 2023
1 parent 10782ff commit e097105
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 177 deletions.
153 changes: 153 additions & 0 deletions runetek3-web/src/main/java/jagex2/io/WebClientStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package jagex2.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;

import org.teavm.interop.Async;
import org.teavm.interop.AsyncCallback;
import org.teavm.jso.dom.events.Event;
import org.teavm.jso.dom.events.EventListener;
import org.teavm.jso.dom.events.MessageEvent;
import org.teavm.jso.typedarrays.Int8Array;
import org.teavm.jso.websocket.CloseEvent;
import org.teavm.jso.websocket.WebSocket;

public class WebClientStream {
private String host;
private boolean secure;
private int port;

private WebSocket client;
private Queue<Int8Array> queue = new LinkedList<>();
private int remaining = 0;
private Int8Array buffer = null;
private int offset = 0;

public WebClientStream(String host, int port) {
this.host = host.substring(host.indexOf("//") + 2);
this.secure = host.startsWith("https:");
// tcp on main port, ws on main port + 1, wss on main port + 2
this.port = secure ? port + 2 : port + 1;
}

@Async
public native int connect();
public void connect(AsyncCallback<Integer> callback) {
String protocol = this.secure ? "wss" : "ws";
this.client = WebSocket.create(protocol + "://" + this.host + ":" + this.port, "binary");
this.client.setBinaryType("arraybuffer");

this.client.onMessage(new EventListener<MessageEvent>(){
public void handleEvent(MessageEvent event) {
Int8Array data = Int8Array.create(event.getDataAsArray());
remaining += data.getLength();
queue.add(data);
};
});

this.client.onOpen(new EventListener<MessageEvent>(){
public void handleEvent(MessageEvent event) {
callback.complete(1);
}
});

this.client.onError(new EventListener<Event>(){
public void handleEvent(Event event) {
callback.error(new IOException("WebSocket error"));
}
});
}

public void write(byte[] src, int length, int offset) throws IOException {
if (this.client.getReadyState() != 1) {
throw new IOException("Socket is not able to write");
}

Int8Array data = Int8Array.create(length);

for (int i = 0; i < length; i++) {
data.set(i, src[offset + i]);
}

this.client.send(data);
}

// read 1 byte
public int read() throws IOException {
if (this.client.getReadyState() != 1) {
throw new IOException("Socket is not able to read");
}

if (this.remaining < 1) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}

return this.read();
}

if (this.buffer == null) {
this.buffer = this.queue.poll();
this.offset = 0;
}

int value = this.buffer.get(this.offset) & 0xFF;
this.offset++;
this.remaining--;

if (this.offset == this.buffer.getLength()) {
this.buffer = null;
}

return value;
}

// read n bytes
public int read(byte[] destination, int off, int length) throws IOException {
if (this.client.getReadyState() != 1) {
throw new IOException("Socket is not able to read");
}

if (this.available() < length) {
try {
Thread.sleep(5);
} catch (InterruptedException e) {
}

return this.read(destination, off, length);
}

for (int i = 0; i < length; i++) {
destination[off + i] = (byte) this.read();
}

return length;
}

public void close() {
if (this.client.getReadyState() != 1) {
return;
}

this.client.close();
}

public int available() {
return this.remaining;
}

public void clear() {
if (this.client.getReadyState() == 1) {
this.client.close();
}

this.queue.clear();

this.remaining = 0;
this.buffer = null;
this.offset = 0;
}
}
162 changes: 0 additions & 162 deletions runetek3-web/src/main/java/jagex2/io/WebSocket.java

This file was deleted.

23 changes: 12 additions & 11 deletions webclient/src/main/java/jagex2/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ public final class Client extends GameShell {
public static int updateGame;

@OriginalMember(owner = "client!client", name = "Uf", descriptor = "Lclient!d;")
private WebSocket connection;
private WebClientStream connection;

@OriginalMember(owner = "client!client", name = "Vf", descriptor = "[[B")
private byte[][] sceneMapLocData;
Expand Down Expand Up @@ -7402,7 +7402,7 @@ protected void unload() {
}

@OriginalMember(owner = "client!client", name = "A", descriptor = "(I)Ljava/net/Socket;")
private WebSocket openSocket(@OriginalArg(0) int arg0) throws IOException {
private WebClientStream openSocket(@OriginalArg(0) int arg0) throws IOException {
return Signlink.opensocket(arg0);
}

Expand Down Expand Up @@ -7735,9 +7735,11 @@ private void updateGame() {
this.out.pos = 0;
this.heartbeatTimer = 0;
}
} catch (@Pc(1001) IOException local1001) {
} catch (@Pc(1001) IOException ex) {
ex.printStackTrace();
this.tryReconnect();
} catch (@Pc(1006) Exception local1006) {
} catch (@Pc(1006) Exception ex) {
ex.printStackTrace();
this.logout();
}
}
Expand Down Expand Up @@ -7913,9 +7915,6 @@ private boolean tryMove(@OriginalArg(0) int arg0, @OriginalArg(1) int arg1, @Ori
}
@Pc(882) byte local882 = 0;
this.bfsStepX[local882] = local11;
if (arg5 != 0) {
this.packetType = this.in.g1();
}
local57 = local882 + 1;
this.bfsStepZ[local882] = local39;
local193 = local809 = this.bfsDirection[local11][local39];
Expand Down Expand Up @@ -8553,7 +8552,7 @@ private void tryReconnect() {
this.fontPlain12.drawStringCenter(158, 16777215, "Please wait - attempting to reestablish", 256);
this.areaViewport.draw(11, this.context, 8);
this.flagSceneTileX = 0;
@Pc(60) WebSocket local60 = this.connection;
@Pc(60) WebClientStream local60 = this.connection;
this.ingame = false;
this.login(this.username, this.password, true);
if (!this.ingame) {
Expand Down Expand Up @@ -9439,7 +9438,7 @@ private boolean read() {
this.connection.read(this.in.data, 0, 1);
this.packetType = this.in.data[0] & 0xFF;
if (this.randomIn != null) {
this.packetType = this.packetType - this.randomIn.nextInt() & 0xFF;
this.packetType = (this.packetType - this.randomIn.nextInt()) & 0xFF;
}
this.packetSize = ServerProt.PACKET_LENGTHS[this.packetType];
local15--;
Expand Down Expand Up @@ -10482,9 +10481,11 @@ private boolean read() {
}
Signlink.reporterror("T1 - " + this.packetType + "," + this.packetSize + " - " + this.lastPacketType1 + "," + this.lastPacketType2);
this.logout();
} catch (@Pc(3862) IOException local3862) {
} catch (@Pc(3862) IOException ex) {
ex.printStackTrace();
this.tryReconnect();
} catch (@Pc(3867) Exception local3867) {
} catch (@Pc(3867) Exception ex) {
ex.printStackTrace();
local1264 = "T2 - " + this.packetType + "," + this.lastPacketType1 + "," + this.lastPacketType2 + " - " + this.packetSize + "," + (this.sceneBaseTileX + this.localPlayer.pathTileX[0]) + "," + (this.sceneBaseTileZ + this.localPlayer.pathTileZ[0]) + " - ";
for (local462 = 0; local462 < this.packetSize && local462 < 50; local462++) {
local1264 = local1264 + this.in.data[local462] + ",";
Expand Down
Loading

0 comments on commit e097105

Please sign in to comment.