This commit is contained in:
PeytonPlayz595 2023-12-19 12:39:30 -05:00
parent d1542af8b9
commit f5a560d102
4 changed files with 149 additions and 136 deletions

View File

@ -236,7 +236,7 @@ public class MinecraftServer implements Runnable {
if(var3.out.position() > 0) {
var3.out.flip();
var3.write(var3.out);
var3.channel.write(var3.out);
var3.out.compact();
}
@ -287,7 +287,7 @@ public class MinecraftServer implements Runnable {
try {
NetworkHandler var20 = var15;
var15.read(var15.in);
var15.channel.read(var15.in);
int var19 = 0;
while(var20.in.position() > 0 && var19++ != 100) {
@ -320,7 +320,7 @@ public class MinecraftServer implements Runnable {
if(var20.out.position() > 0) {
var20.out.flip();
var20.write(var20.out);
var20.channel.write(var20.out);
var20.out.compact();
}
} catch (Exception var9) {

View File

@ -4,7 +4,7 @@ import com.mojang.minecraft.net.PacketType;
import com.mojang.minecraft.server.NetworkManager;
import net.io.DummyLogger;
import net.io.WebSocketNetworkManager;
import net.io.WebSocketChannel;
import java.nio.ByteBuffer;
import java.util.Arrays;
@ -21,13 +21,13 @@ public final class NetworkHandler {
public String address;
private byte[] stringBytes = new byte[64];
public static boolean gay = false;
public WebSocketChannel channel;
public NetworkHandler(int port) {
gay = true;
Log.setLog(new DummyLogger());
try {
WebSocketNetworkManager.startServer(port);
Log.setLog(new DummyLogger());
channel = new WebSocketChannel(port);
} catch (Exception e) {
e.printStackTrace();
throw new Error(e.getLocalizedMessage());
@ -38,7 +38,7 @@ public final class NetworkHandler {
try {
if(this.out.position() > 0) {
this.out.flip();
this.write(this.out);
this.channel.write(this.out);
this.out.compact();
}
} catch (Exception var2) {
@ -48,7 +48,7 @@ public final class NetworkHandler {
this.connected = false;
try {
WebSocketNetworkManager.stopServer();
channel.stopServer();
} catch (Exception var1) {
;
}
@ -145,12 +145,4 @@ 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,140 @@
package net.io;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
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 WebSocketChannel {
private static Set<Session> sessions;
private final Queue<ByteBuffer> writeQueue;
private final ByteBuffer readBuffer;
private boolean reading;
private boolean isConnected = false;
public WebSocketChannel(int port) {
writeQueue = new LinkedList<ByteBuffer>();
readBuffer = ByteBuffer.allocate(1024);
reading = false;
sessions = new HashSet<Session>();
writeQueue.clear();
try {
this.startServer(port);
isConnected = true;
} catch (Exception e) {
isConnected = false;
e.printStackTrace();
}
}
@OnWebSocketConnect
private void onConnect(Session session) {
sessions.add(session);
}
@OnWebSocketMessage
private void onMessage(byte[] data, int offset, int length) {
for (byte b : data) {
readBuffer.put(b);
}
if (reading) {
readBuffer.flip();
reading = false;
}
}
@OnWebSocketClose
private void onClose(Session session, int statusCode, String reason) {
sessions.remove(session);
}
@OnWebSocketError
private void onError(Session session, Throwable throwable) {
System.err.println(throwable.getMessage());
}
private void startServer(int port) throws Exception {
org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(port);
org.eclipse.jetty.servlet.ServletContextHandler context = new org.eclipse.jetty.servlet.ServletContextHandler();
context.setContextPath("");
context.addServlet(MyWebSocketServlet.class, "");
server.setHandler(context);
server.start();
server.join();
}
public void stopServer() {
for (Session session : sessions) {
session.close();
}
}
public boolean connectionOpen() {
return isConnected;
}
public int read(ByteBuffer buffer) {
if (reading) {
return 0;
}
if (readBuffer.hasRemaining()) {
int bytesToRead = Math.min(buffer.remaining(), readBuffer.remaining());
for (int i = 0; i < bytesToRead; i++) {
buffer.put(readBuffer.get());
}
return bytesToRead;
} else {
readBuffer.clear();
reading = true;
return 0;
}
}
public int write(ByteBuffer buffer) {
if (!isConnected) {
return 0;
}
int bytesToWrite = 0;
for (Session session : sessions) {
if (session.isOpen()) {
int sessionBytesToWrite = Math.min(buffer.remaining(), session.getPolicy().getMaxTextMessageBufferSize());
if (sessionBytesToWrite > 0) {
byte[] data = new byte[sessionBytesToWrite];
buffer.get(data);
try {
session.getRemote().sendBytes(ByteBuffer.wrap(data));
} catch (IOException e) {
e.printStackTrace();
}
}
bytesToWrite += sessionBytesToWrite;
}
}
if (buffer.hasRemaining()) {
writeQueue.offer(buffer);
}
return bytesToWrite;
}
private static class MyWebSocketServlet extends WebSocketServlet {
private static final long serialVersionUID = 1L;
@Override
public void configure(WebSocketServletFactory factory) {
factory.register(WebSocketChannel.class);
}
}
}

View File

@ -1,119 +0,0 @@
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(Session session, byte[] data, int offset, int length) {
ByteBuffer buffer = ByteBuffer.wrap(data);
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);
}
}
}