From acab40399fc46800b40b65128f4efce568d34ca3 Mon Sep 17 00:00:00 2001 From: PeytonPlayz595 <106421860+PeytonPlayz595@users.noreply.github.com> Date: Mon, 18 Dec 2023 17:25:02 -0500 Subject: [PATCH] Rewrite WebSockets because idk how they work --- .../adapter/EaglerAdapterImpl2.java | 164 ++++-------------- 1 file changed, 33 insertions(+), 131 deletions(-) diff --git a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java index 10dbe67..e2813e8 100644 --- a/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java +++ b/src/teavm/java/net/lax1dude/eaglercraft/adapter/EaglerAdapterImpl2.java @@ -1200,124 +1200,24 @@ public class EaglerAdapterImpl2 { @JSBody(params = { "name", "cvs" }, script = "var a=document.createElement(\"a\");a.href=cvs.toDataURL(\"image/png\");a.download=name;a.click();") private static native void saveScreenshot(String name, HTMLCanvasElement cvs); + + private static WebSocket webSocket; + private static boolean isConnected = false; + public static java.util.Queue receivedBuffers = new java.util.ArrayDeque(); + + public static boolean startConnection(String serverUrl) { + webSocket = WebSocket.create(serverUrl); - public static enum RateLimit { - NONE, FAILED, BLOCKED, FAILED_POSSIBLY_LOCKED, LOCKED, NOW_LOCKED; - } - - private static final Set rateLimitedAddresses = new HashSet(); - private static final Set blockedAddresses = new HashSet(); - - private static WebSocket sock = null; - private static boolean sockIsConnecting = false; - private static boolean sockIsConnected = false; - private static boolean sockIsAlive = false; - private static RateLimit rateLimitStatus = null; - private static String currentSockURI = null; - public static java.util.Queue receivedBuffers = new java.util.ArrayDeque(); - - public static final RateLimit getRateLimitStatus() { - RateLimit l = rateLimitStatus; - rateLimitStatus = null; - return l; - } - public static final void logRateLimit(String addr, RateLimit l) { - if(l == RateLimit.BLOCKED) { - blockedAddresses.add(addr); - }else { - rateLimitedAddresses.add(addr); - } - } - public static final RateLimit checkRateLimitHistory(String addr) { - if(blockedAddresses.contains(addr)) { - return RateLimit.LOCKED; - }else if(rateLimitedAddresses.contains(addr)) { - return RateLimit.BLOCKED; - }else { - return RateLimit.NONE; - } - } - - @Async - public static native String connectWebSocket(String sockURI); - - private static void connectWebSocket(String sockURI, final AsyncCallback cb) { - sockIsConnecting = true; - sockIsConnected = false; - sockIsAlive = false; - rateLimitStatus = null; - currentSockURI = sockURI; - try { - sock = WebSocket.create(sockURI); - } catch(Throwable t) { - sockIsConnecting = false; - sockIsAlive = false; - return; - } - sock.setBinaryType("arraybuffer"); - sock.onOpen(new EventListener() { + webSocket.onOpen(new EventListener() { + @Override + public void handleEvent(MessageEvent evt) { + isConnected = true; + } + }); + + webSocket.onMessage(new EventListener() { @Override public void handleEvent(MessageEvent evt) { - sockIsConnecting = false; - sockIsAlive = false; - sockIsConnected = true; - receivedBuffers.clear(); - cb.complete("okay"); - } - }); - sock.onClose(new EventListener() { - @Override - public void handleEvent(CloseEvent evt) { - sock = null; - if(sockIsConnecting) { - if(rateLimitStatus == null) { - if(blockedAddresses.contains(currentSockURI)) { - rateLimitStatus = RateLimit.LOCKED; - }else if(rateLimitedAddresses.contains(currentSockURI)) { - rateLimitStatus = RateLimit.FAILED_POSSIBLY_LOCKED; - }else { - rateLimitStatus = RateLimit.FAILED; - } - } - }else if(!sockIsAlive) { - if(rateLimitStatus == null) { - if(blockedAddresses.contains(currentSockURI)) { - rateLimitStatus = RateLimit.LOCKED; - }else if(rateLimitedAddresses.contains(currentSockURI)) { - rateLimitStatus = RateLimit.BLOCKED; - } - } - } - boolean b = sockIsConnecting; - sockIsConnecting = false; - sockIsConnected = false; - sockIsAlive = false; - if(b) cb.complete("fail"); - } - }); - sock.onMessage(new EventListener() { - @Override - public void handleEvent(MessageEvent evt) { - sockIsAlive = true; - if(isString(evt.getData())) { - String stat = evt.getDataAsString(); - if(stat.equalsIgnoreCase("BLOCKED")) { - if(rateLimitStatus == null) { - rateLimitStatus = RateLimit.BLOCKED; - } - rateLimitedAddresses.add(currentSockURI); - }else if(stat.equalsIgnoreCase("LOCKED")) { - if(rateLimitStatus == null) { - rateLimitStatus = RateLimit.NOW_LOCKED; - } - rateLimitedAddresses.add(currentSockURI); - blockedAddresses.add(currentSockURI); - } - sockIsConnecting = false; - sockIsConnected = false; - sock.close(); - return; - } Uint8Array a = Uint8Array.create(evt.getDataAsArray()); byte[] b = new byte[a.getByteLength()]; for(int i = 0; i < b.length; ++i) { @@ -1327,33 +1227,35 @@ public class EaglerAdapterImpl2 { receivedBuffers.add(buffer); } }); + + webSocket.onClose(new EventListener() { + @Override + public void handleEvent(CloseEvent event) { + isConnected = false; + } + }); + + return isConnected; + } + + public static void endConnection() { + webSocket.close(); } - public static final boolean startConnection(String uri) { - String res = connectWebSocket(uri); - return "fail".equals(res) ? false : true; - } - public static final void endConnection() { - if(sock == null || sock.getReadyState() == 3) { - sockIsConnecting = false; - } - if(sock != null && !sockIsConnecting) sock.close(); - } public static final boolean connectionOpen() { - if(sock == null || sock.getReadyState() == 3) { - sockIsConnecting = false; - } - return sock != null && !sockIsConnecting && sock.getReadyState() != 3; + return isConnected; } + @JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);") private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer); public static final void writePacket(byte[] packet) { - if(sock != null && !sockIsConnecting) { + if(webSocket != null && isConnected) { Uint8Array arr = Uint8Array.create(packet.length); arr.set(packet); - nativeBinarySend(sock, arr.getBuffer()); + nativeBinarySend(webSocket, arr.getBuffer()); } } + public static final byte[] loadLocalStorage(String key) { String s = win.getLocalStorage().getItem("_eaglercraft_beta."+key); if(s != null) {