Fix multiplayer

Should work with actual alpha servers now
This commit is contained in:
PeytonPlayz595 2024-10-16 12:39:44 -04:00
parent 3018e41862
commit d3142f1adf
5 changed files with 209 additions and 85 deletions

View File

@ -0,0 +1,22 @@
package net.PeytonPlayz585.network;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
public class ByteBufferDirectInputStream extends InputStream {
private ByteBuffer buf;
public ByteBufferDirectInputStream(ByteBuffer b) {
this.buf = b;
}
@Override
public int read() throws IOException {
return buf.remaining() > 0 ? ((int) buf.get() & 0xFF) : -1;
}
@Override
public int available() {
return buf.remaining();
}
}

View File

@ -0,0 +1,40 @@
package net.PeytonPlayz585.network;
import net.PeytonPlayz585.opengl.GL11;
import java.io.IOException;
import java.io.OutputStream;
public class Socket {
public Socket(String ip) throws IOException {
if(!GL11.EaglerAdapterImpl2.startConnection(ip)) {
throw new IOException("Failed to connect to '" + ip + "'!");
}
}
public void close() {
if(!GL11.EaglerAdapterImpl2.connectionOpen()) {
return;
}
GL11.EaglerAdapterImpl2.endConnection();
}
public boolean open() {
return GL11.EaglerAdapterImpl2.connectionOpen();
}
public void write(byte[] data) {
if(open()) {
GL11.EaglerAdapterImpl2.writePacket(data);
}
}
public byte[] read() {
if(open()) {
return GL11.EaglerAdapterImpl2.readPacket();
}
return null;
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package net.PeytonPlayz585.network;
import java.io.IOException;
/**
* Thrown to indicate that there is an error creating or accessing a Socket.
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class SocketException extends IOException {
private static final long serialVersionUID = -5935874303556886934L;
/**
* Constructs a new {@code SocketException} with the
* specified detail message.
*
* @param msg the detail message.
*/
public SocketException(String msg) {
super(msg);
}
/**
* Constructs a new {@code SocketException} with no detail message.
*/
public SocketException() {
}
@Override
public String toString() {
String s = "java.net.SocketException";
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
}

View File

@ -21,7 +21,8 @@ public class NetClientHandler extends NetHandler {
public void processReadPackets() {
if(!this.disconnected) {
NetClientHandler.this.netManager.readPacket();
this.netManager.readPacket();
this.netManager.processReadPackets();
}
}

View File

@ -5,18 +5,20 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import net.PeytonPlayz585.opengl.GL11;
import net.PeytonPlayz585.network.ByteBufferDirectInputStream;
import net.PeytonPlayz585.network.Socket;
public class NetworkManager {
private Object sendQueueLock = new Object();
private Socket field_12258_e;
private boolean isRunning = true;
private List readPackets = Collections.synchronizedList(new ArrayList());
private List dataPackets = Collections.synchronizedList(new ArrayList());
private List chunkDataPackets = Collections.synchronizedList(new ArrayList());
private NetHandler netHandler;
@ -28,6 +30,7 @@ public class NetworkManager {
private int chunkDataSendCounter = 0;
public NetworkManager(String var4, NetHandler var3) throws IOException {
this.netHandler = var3;
String uri = null;
System.out.println(uri);
@ -41,9 +44,8 @@ public class NetworkManager {
}else {
throw new IOException("Invalid URI Protocol!");
}
if(!GL11.EaglerAdapterImpl2.startConnection(var4)) {
throw new IOException("Websocket to " + uri + " failed!");
}
this.field_12258_e = new Socket(var4);
}
public void addToSendQueue(Packet var1) {
@ -56,13 +58,13 @@ public class NetworkManager {
} else {
this.dataPackets.add(var1);
}
this.sendPacket();
}
}
}
private ByteArrayOutputStream sendBuffer;
ByteArrayOutputStream os;
private void sendPacket() {
try {
boolean var1 = true;
@ -76,12 +78,14 @@ public class NetworkManager {
this.sendQueueByteLength -= var2.getPacketSize() + 1;
}
sendBuffer = new ByteArrayOutputStream();
DataOutputStream yee = new DataOutputStream(sendBuffer);
Packet.writePacket(var2, yee);
yee.flush();
GL11.EaglerAdapterImpl2.writePacket(sendBuffer.toByteArray());
sendBuffer.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream socketOutputStream = new DataOutputStream(baos);
Packet.writePacket(var2, socketOutputStream);
baos.flush();
socketOutputStream.flush();
this.field_12258_e.write(baos.toByteArray());
baos.flush();
socketOutputStream.flush();
}
if((var1 || this.chunkDataSendCounter-- <= 0) && !this.chunkDataPackets.isEmpty()) {
@ -92,12 +96,14 @@ public class NetworkManager {
this.sendQueueByteLength -= var2.getPacketSize() + 1;
}
sendBuffer = new ByteArrayOutputStream();
DataOutputStream yee = new DataOutputStream(sendBuffer);
Packet.writePacket(var2, yee);
yee.flush();
GL11.EaglerAdapterImpl2.writePacket(sendBuffer.toByteArray());
sendBuffer.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream socketOutputStream = new DataOutputStream(baos);
Packet.writePacket(var2, socketOutputStream);
baos.flush();
socketOutputStream.flush();
this.field_12258_e.write(baos.toByteArray());
baos.flush();
socketOutputStream.flush();
this.chunkDataSendCounter = 50;
}
@ -117,22 +123,20 @@ public class NetworkManager {
private LinkedList<ByteBuffer> readChunks = new LinkedList();
public void readPacket() {
if(this.sendQueueByteLength > 1048576) {
this.networkShutdown("Send buffer overflow");
}
readChunks.clear();
if(oldChunkBuffer != null) {
readChunks.add(oldChunkBuffer);
}
byte[] packet;
while((packet = GL11.EaglerAdapterImpl2.readPacket()) != null) {
readChunks.add(ByteBuffer.wrap(packet));
if(this.field_12258_e.open()) {
byte[] packet;
while((packet = this.field_12258_e.read()) != null) {
readChunks.add(ByteBuffer.wrap(packet));
}
}
if(!readChunks.isEmpty()) {
this.timeSinceLastRead = 0;
int cap = 0;
for(ByteBuffer b : readChunks) {
cap += b.limit();
@ -145,24 +149,19 @@ public class NetworkManager {
stream.flip();
DataInputStream packetStream = new DataInputStream(new ByteBufferDirectInputStream(stream));
int var1 = 100;
while(stream.hasRemaining() && var1-- > 0) {
while(stream.hasRemaining()) {
stream.mark();
try {
Packet pkt = Packet.readPacket(packetStream);
if(pkt == null) {
this.networkShutdown("End of Stream");
Packet var1 = Packet.readPacket(packetStream);
if(var1 != null) {
this.readPackets.add(var1);
} else {
this.networkShutdown("End of stream");
}
pkt.processPacket(this.netHandler);
} catch (EOFException e) {
} catch(EOFException e) {
stream.reset();
break;
} catch (IOException e) {
continue;
} catch(ArrayIndexOutOfBoundsException e) {
continue;
} catch(NullPointerException e) {
continue;
} catch(Exception e) {
continue;
} catch(Throwable t) {
@ -175,19 +174,6 @@ public class NetworkManager {
}else {
oldChunkBuffer = null;
}
}
if(this.timeSinceLastRead++ == 1200) {
this.networkShutdown("Timed out");
}
if(!isConnectionOpen() && !this.isTerminating) {
this.networkShutdown("Lost connection!");
}
if(this.isTerminating && this.readChunks.isEmpty()) {
this.netHandler.handleErrorMessage(this.terminationReason);
}
}
@ -201,42 +187,55 @@ public class NetworkManager {
this.isTerminating = true;
this.terminationReason = var1;
this.isRunning = false;
if(GL11.EaglerAdapterImpl2.connectionOpen()) {
GL11.EaglerAdapterImpl2.endConnection();
try {
this.field_12258_e.close();
this.field_12258_e = null;
} catch (Throwable var3) {
}
}
}
public void processReadPackets() {
if(this.sendQueueByteLength > 1048576) {
this.networkShutdown("Send buffer overflow");
}
if(this.readPackets.isEmpty()) {
if(this.timeSinceLastRead++ == 1200) {
this.networkShutdown("Timed out");
}
} else {
this.timeSinceLastRead = 0;
}
int var1 = 100;
while(!this.readPackets.isEmpty() && var1-- >= 0) {
Packet var2 = (Packet)this.readPackets.remove(0);
var2.processPacket(this.netHandler);
}
if(this.isTerminating && this.readPackets.isEmpty()) {
this.netHandler.handleErrorMessage(this.terminationReason);
}
}
static boolean isRunning(NetworkManager var0) {
return var0.isRunning;
}
static boolean isConnectionOpen() {
return GL11.EaglerAdapterImpl2.connectionOpen();
}
static boolean isServerTerminating(NetworkManager var0) {
return var0.isServerTerminating;
}
static void readNetworkPacket(NetworkManager var0) {
var0.readPacket();
}
static void sendNetworkPacket(NetworkManager var0) {
var0.sendPacket();
}
private static class ByteBufferDirectInputStream extends InputStream {
private ByteBuffer buf;
private ByteBufferDirectInputStream(ByteBuffer b) {
this.buf = b;
}
@Override
public int read() throws IOException {
return buf.remaining() > 0 ? ((int)buf.get() & 0xFF) : -1;
}
@Override
public int available() {
return buf.remaining();
}
}
}