WebSockets

This commit is contained in:
PeytonPlayz595 2023-12-18 13:50:13 -05:00
parent 8a34075f2c
commit 8eb92b2c47
4 changed files with 232 additions and 136 deletions

View File

@ -20,7 +20,6 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.channels.SocketChannel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -286,15 +285,11 @@ public class MinecraftServer implements Runnable {
try {
NetworkHandler var3 = var10.networkHandler;
try {
if(var3.out.position() > 0) {
var3.out.flip();
var3.channel.write(var3.out);
var3.write(var3.out);
var3.out.compact();
}
} catch (IOException var6) {
;
}
if(var10.b-- <= 0) {
try {
@ -332,64 +327,16 @@ public class MinecraftServer implements Runnable {
}
}
try {
BindTo var13 = this.bindTo;
SocketChannel var14;
while((var14 = var13.serverChannel.accept()) != null) {
try {
var14.configureBlocking(false);
NetworkHandler var2 = new NetworkHandler(var14);
NetworkHandler var2 = new NetworkHandler(8080);
var13.c.add(var2);
NetworkHandler var3 = var2;
MinecraftServer var4 = var13.server;
if(var13.server.playerManager3.containsPlayer(var2.address)) {
var2.send(PacketType.DISCONNECT, new Object[]{"You\'re banned!"});
logger.info(var2.address + " tried to connect, but is banned.");
var4.b(var2);
} else {
int var5 = 0;
Iterator var6 = var4.n.iterator();
while(var6.hasNext()) {
if(((NetworkManager)var6.next()).networkHandler.address.equals(var3.address)) {
++var5;
}
}
if(var5 >= var4.maxConnections) {
var3.send(PacketType.DISCONNECT, new Object[]{"Too many connection!"});
logger.info(var3.address + " tried to connect, but is already connected " + var5 + " times.");
var4.b(var3);
} else {
int var24;
if((var24 = var4.e()) < 0) {
var3.send(PacketType.DISCONNECT, new Object[]{"The server is full!"});
logger.info(var3.address + " tried to connect, but failed because the server was full.");
var4.b(var3);
} else {
NetworkManager var16 = new NetworkManager(var4, var3, var24);
logger.info(var16 + " connected");
var4.m.put(var3, var16);
var4.n.add(var16);
if(var16.playerID >= 0) {
var4.networkManager[var16.playerID] = var16;
}
}
}
}
} catch (IOException var10) {
var14.close();
throw var10;
}
}
for(int var17 = 0; var17 < var13.c.size(); ++var17) {
NetworkHandler var15 = (NetworkHandler)var13.c.get(var17);
try {
NetworkHandler var20 = var15;
var15.channel.read(var15.in);
var15.read(var15.in);
int var19 = 0;
while(var20.in.position() > 0 && var19++ != 100) {
@ -422,7 +369,7 @@ public class MinecraftServer implements Runnable {
if(var20.out.position() > 0) {
var20.out.flip();
var20.channel.write(var20.out);
var20.write(var20.out);
var20.out.compact();
}
} catch (Exception var9) {
@ -443,10 +390,6 @@ public class MinecraftServer implements Runnable {
var8.printStackTrace();
}
}
} catch (IOException var11) {
throw new RuntimeException("IOException while ticking socketserver", var11);
}
}
public final void a(NetworkManager var1, String var2) {

View File

@ -2,41 +2,31 @@ package com.mojang.net;
import com.mojang.minecraft.net.PacketType;
import com.mojang.minecraft.server.NetworkManager;
import java.net.Socket;
import java.net.SocketException;
import net.io.DummyLogger;
import net.io.WebSocketNetworkManager;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import org.eclipse.jetty.util.log.Log;
public final class NetworkHandler {
public volatile boolean connected;
public SocketChannel channel;
public ByteBuffer in = ByteBuffer.allocate(1048576);
public ByteBuffer out = ByteBuffer.allocate(1048576);
public NetworkManager networkManager;
private Socket socket;
private boolean h = false;
public String address;
private byte[] stringBytes = new byte[64];
public NetworkHandler(SocketChannel var1) {
public NetworkHandler(int port) {
Log.setLog(new DummyLogger());
try {
this.channel = var1;
this.channel.configureBlocking(false);
System.currentTimeMillis();
this.socket = this.channel.socket();
this.connected = true;
this.in.clear();
this.out.clear();
this.socket.setTcpNoDelay(true);
this.socket.setTrafficClass(24);
this.socket.setKeepAlive(false);
this.socket.setReuseAddress(false);
this.socket.setSoTimeout(100);
this.address = this.socket.getInetAddress().toString();
} catch(Exception e) {
WebSocketNetworkManager.startServer(port);
} catch (Exception e) {
e.printStackTrace();
}
}
@ -45,7 +35,7 @@ public final class NetworkHandler {
try {
if(this.out.position() > 0) {
this.out.flip();
this.channel.write(this.out);
this.write(this.out);
this.out.compact();
}
} catch (Exception var2) {
@ -55,13 +45,10 @@ public final class NetworkHandler {
this.connected = false;
try {
this.channel.close();
WebSocketNetworkManager.stopServer();
} catch (Exception var1) {
;
}
this.socket = null;
this.channel = null;
}
public final void send(PacketType var1, Object ... var2) {
@ -155,4 +142,12 @@ public final class NetworkHandler {
}
}
}
public void read(ByteBuffer buf) {
WebSocketNetworkManager.read(buf);
}
public void write(ByteBuffer buf) {
WebSocketNetworkManager.write(buf);
}
}

View File

@ -0,0 +1,39 @@
package net.io;
import org.eclipse.jetty.util.log.Logger;
public class DummyLogger implements Logger {
public String getName() {
return "DummyLogger";
}
public void warn(String msg, Object... args) {}
public void warn(Throwable thrown) {}
public void warn(String msg, Throwable thrown) {}
public void info(String msg, Object... args) {}
public void info(Throwable thrown) {}
public void info(String msg, Throwable thrown) {}
public boolean isDebugEnabled() {return false; }
public void setDebugEnabled(boolean enabled) {}
public void debug(String msg, Object... args) {}
public void debug(Throwable thrown) {}
public void debug(String msg, Throwable thrown) {}
public Logger getLogger(String name) {return this; }
public void ignore(Throwable ignored) {}
public void debug(String msg, long value) {}
}

View File

@ -0,0 +1,119 @@
package net.io;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
@WebSocket
public class WebSocketNetworkManager extends WebSocketServlet {
private static Set<Session> sessions = new HashSet<Session>();
public static int port;
private static Queue<ByteBuffer> receivedBuffers = new ArrayDeque<ByteBuffer>();
private static ByteBuffer writeBuffer;
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(WebSocketNetworkManager.class);
}
@OnWebSocketConnect
public void onConnect(Session session) {
sessions.add(session);
}
@OnWebSocketMessage
public void onMessage(byte[] data, int offset, int length) {
ByteBuffer buffer = ByteBuffer.wrap(data, offset, length);
receivedBuffers.add(buffer);
}
@OnWebSocketClose
public void onClose(Session session, int statusCode, String reason) {
sessions.remove(session);
}
@OnWebSocketError
public void onError(Session session, Throwable throwable) {
System.out.println(throwable.getMessage());
}
public static void startServer(int port) throws Exception {
WebSocketNetworkManager.port = port;
org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(port);
WebSocketHandler wsHandler = new WebSocketHandler() {
@Override
public void configure(WebSocketServletFactory factory) {
factory.setCreator(new WebSocketCreator() {
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
return new WebSocketNetworkManager();
}
});
}
};
server.setHandler(wsHandler);
server.start();
}
public static void stopServer() {
for (Session session : sessions) {
session.close();
}
}
public static void read(ByteBuffer buf) {
int bytesRead = 0;
while (bytesRead < buf.capacity()) {
if (!receivedBuffers.isEmpty()) {
ByteBuffer receivedBuffer = receivedBuffers.peek();
int remainingBytes = buf.capacity() - bytesRead;
int bytesToRead = Math.min(receivedBuffer.remaining(), remainingBytes);
receivedBuffer.get(buf.array(), bytesRead, bytesToRead);
bytesRead += bytesToRead;
if (receivedBuffer.remaining() == 0) {
receivedBuffers.poll();
}
} else {
break;
}
}
}
public static void write(ByteBuffer buf) {
if (writeBuffer == null) {
writeBuffer = ByteBuffer.allocate(buf.capacity());
}
int bytesToWrite = Math.min(buf.remaining(), writeBuffer.remaining());
writeBuffer.put(buf.array(), buf.position(), bytesToWrite);
buf.position(buf.position() + bytesToWrite);
if (writeBuffer.remaining() == 0 || buf.remaining() == 0) {
writeBuffer.flip();
byte[] data = new byte[writeBuffer.remaining()];
writeBuffer.get(data);
for(Session session : sessions) {
try {
session.getRemote().sendBytes(ByteBuffer.wrap(data));
} catch (IOException e) {
e.printStackTrace();
}
}
writeBuffer.clear();
}
if (buf.remaining() > 0) {
write(buf);
}
}
}