Rewrite WebSockets because idk how they work

This commit is contained in:
PeytonPlayz595 2023-12-18 17:25:02 -05:00
parent f4bb2bc8ce
commit acab40399f

View File

@ -1201,123 +1201,23 @@ public class EaglerAdapterImpl2 {
@JSBody(params = { "name", "cvs" }, script = "var a=document.createElement(\"a\");a.href=cvs.toDataURL(\"image/png\");a.download=name;a.click();") @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 native void saveScreenshot(String name, HTMLCanvasElement cvs);
public static enum RateLimit { private static WebSocket webSocket;
NONE, FAILED, BLOCKED, FAILED_POSSIBLY_LOCKED, LOCKED, NOW_LOCKED; private static boolean isConnected = false;
}
private static final Set<String> rateLimitedAddresses = new HashSet<String>();
private static final Set<String> blockedAddresses = new HashSet<String>();
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<ByteBuffer> receivedBuffers = new java.util.ArrayDeque<ByteBuffer>(); public static java.util.Queue<ByteBuffer> receivedBuffers = new java.util.ArrayDeque<ByteBuffer>();
public static final RateLimit getRateLimitStatus() { public static boolean startConnection(String serverUrl) {
RateLimit l = rateLimitStatus; webSocket = WebSocket.create(serverUrl);
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 webSocket.onOpen(new EventListener<MessageEvent>() {
public static native String connectWebSocket(String sockURI);
private static void connectWebSocket(String sockURI, final AsyncCallback<String> 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<MessageEvent>() {
@Override @Override
public void handleEvent(MessageEvent evt) { public void handleEvent(MessageEvent evt) {
sockIsConnecting = false; isConnected = true;
sockIsAlive = false;
sockIsConnected = true;
receivedBuffers.clear();
cb.complete("okay");
} }
}); });
sock.onClose(new EventListener<CloseEvent>() {
@Override webSocket.onMessage(new EventListener<MessageEvent>() {
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<MessageEvent>() {
@Override @Override
public void handleEvent(MessageEvent evt) { 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()); Uint8Array a = Uint8Array.create(evt.getDataAsArray());
byte[] b = new byte[a.getByteLength()]; byte[] b = new byte[a.getByteLength()];
for(int i = 0; i < b.length; ++i) { for(int i = 0; i < b.length; ++i) {
@ -1327,33 +1227,35 @@ public class EaglerAdapterImpl2 {
receivedBuffers.add(buffer); receivedBuffers.add(buffer);
} }
}); });
webSocket.onClose(new EventListener<CloseEvent>() {
@Override
public void handleEvent(CloseEvent event) {
isConnected = false;
}
});
return isConnected;
} }
public static final boolean startConnection(String uri) { public static void endConnection() {
String res = connectWebSocket(uri); webSocket.close();
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() { public static final boolean connectionOpen() {
if(sock == null || sock.getReadyState() == 3) { return isConnected;
sockIsConnecting = false;
}
return sock != null && !sockIsConnecting && sock.getReadyState() != 3;
} }
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);") @JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")
private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer); private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer);
public static final void writePacket(byte[] packet) { public static final void writePacket(byte[] packet) {
if(sock != null && !sockIsConnecting) { if(webSocket != null && isConnected) {
Uint8Array arr = Uint8Array.create(packet.length); Uint8Array arr = Uint8Array.create(packet.length);
arr.set(packet); arr.set(packet);
nativeBinarySend(sock, arr.getBuffer()); nativeBinarySend(webSocket, arr.getBuffer());
} }
} }
public static final byte[] loadLocalStorage(String key) { public static final byte[] loadLocalStorage(String key) {
String s = win.getLocalStorage().getItem("_eaglercraft_beta."+key); String s = win.getLocalStorage().getItem("_eaglercraft_beta."+key);
if(s != null) { if(s != null) {