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();")
private static native void saveScreenshot(String name, HTMLCanvasElement cvs);
public static enum RateLimit {
NONE, FAILED, BLOCKED, FAILED_POSSIBLY_LOCKED, LOCKED, NOW_LOCKED;
}
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;
private static WebSocket webSocket;
private static boolean isConnected = false;
public static java.util.Queue<ByteBuffer> receivedBuffers = new java.util.ArrayDeque<ByteBuffer>();
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;
}
}
public static boolean startConnection(String serverUrl) {
webSocket = WebSocket.create(serverUrl);
@Async
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>() {
webSocket.onOpen(new EventListener<MessageEvent>() {
@Override
public void handleEvent(MessageEvent evt) {
sockIsConnecting = false;
sockIsAlive = false;
sockIsConnected = true;
receivedBuffers.clear();
cb.complete("okay");
isConnected = true;
}
});
sock.onClose(new EventListener<CloseEvent>() {
@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<MessageEvent>() {
webSocket.onMessage(new EventListener<MessageEvent>() {
@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<CloseEvent>() {
@Override
public void handleEvent(CloseEvent event) {
isConnected = false;
}
});
return isConnected;
}
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 void endConnection() {
webSocket.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) {