mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-12 08:58:02 -04:00
packet handler (own thread), buffer fix, moved packets into own packages, disconnect after pong
This commit is contained in:
parent
8928f9b922
commit
a09d2614ad
@ -28,4 +28,9 @@ public class ServerListPing {
|
|||||||
public String getBase64EncodedFavicon() {
|
public String getBase64EncodedFavicon() {
|
||||||
return raw.getString("favicon");
|
return raw.getString("favicon");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMotd() {
|
||||||
|
//ToDo TextComponent handling
|
||||||
|
return raw.getString("description");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,22 @@
|
|||||||
package de.bixilon.minosoft.protocol.network;
|
package de.bixilon.minosoft.protocol.network;
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.packets.serverbound.handshaking.PacketHandshake;
|
import de.bixilon.minosoft.protocol.packets.serverbound.handshaking.PacketHandshake;
|
||||||
import de.bixilon.minosoft.protocol.packets.serverbound.handshaking.PacketStatusPing;
|
import de.bixilon.minosoft.protocol.packets.serverbound.status.PacketStatusPing;
|
||||||
import de.bixilon.minosoft.protocol.packets.serverbound.handshaking.PacketStatusRequest;
|
import de.bixilon.minosoft.protocol.packets.serverbound.status.PacketStatusRequest;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ConnectionState;
|
import de.bixilon.minosoft.protocol.protocol.ConnectionState;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
import de.bixilon.minosoft.util.Util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Connection {
|
public class Connection {
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Network network;
|
private final Network network;
|
||||||
|
private final PacketHandler handler;
|
||||||
|
private final ArrayList<ClientboundPacket> handlingQueue;
|
||||||
private ConnectionState state = ConnectionState.DISCONNECTED;
|
private ConnectionState state = ConnectionState.DISCONNECTED;
|
||||||
|
|
||||||
private boolean onlyPing;
|
private boolean onlyPing;
|
||||||
@ -18,6 +25,18 @@ public class Connection {
|
|||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
network = new Network(this);
|
network = new Network(this);
|
||||||
|
handlingQueue = new ArrayList<>();
|
||||||
|
handler = new PacketHandler(this);
|
||||||
|
Thread handleThread = new Thread(() -> {
|
||||||
|
while (getConnectionState() != ConnectionState.DISCONNECTING) {
|
||||||
|
while (handlingQueue.size() > 0) {
|
||||||
|
handlingQueue.get(0).handle(getHandler());
|
||||||
|
handlingQueue.remove(0);
|
||||||
|
}
|
||||||
|
Util.sleep(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
handleThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,7 +45,6 @@ public class Connection {
|
|||||||
public void ping() {
|
public void ping() {
|
||||||
onlyPing = true;
|
onlyPing = true;
|
||||||
network.connect();
|
network.connect();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,7 +83,7 @@ public class Connection {
|
|||||||
setConnectionState(next);
|
setConnectionState(next);
|
||||||
break;
|
break;
|
||||||
case STATUS:
|
case STATUS:
|
||||||
// send staus request and ping
|
// send status request and ping
|
||||||
network.sendPacket(new PacketStatusRequest());
|
network.sendPacket(new PacketStatusRequest());
|
||||||
network.sendPacket(new PacketStatusPing(0));
|
network.sendPacket(new PacketStatusPing(0));
|
||||||
break;
|
break;
|
||||||
@ -76,4 +94,20 @@ public class Connection {
|
|||||||
//ToDo: static right now
|
//ToDo: static right now
|
||||||
return ProtocolVersion.VERSION_1_7_10;
|
return ProtocolVersion.VERSION_1_7_10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PacketHandler getHandler() {
|
||||||
|
return this.handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handle(ClientboundPacket p) {
|
||||||
|
handlingQueue.add(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOnlyPing() {
|
||||||
|
return onlyPing;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect() {
|
||||||
|
setConnectionState(ConnectionState.DISCONNECTING);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
|
|||||||
import de.bixilon.minosoft.protocol.protocol.ConnectionState;
|
import de.bixilon.minosoft.protocol.protocol.ConnectionState;
|
||||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||||
import de.bixilon.minosoft.protocol.protocol.Protocol;
|
import de.bixilon.minosoft.protocol.protocol.Protocol;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
|
|
||||||
import de.bixilon.minosoft.util.Util;
|
import de.bixilon.minosoft.util.Util;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -60,25 +59,25 @@ public class Network {
|
|||||||
dOut.write(b);
|
dOut.write(b);
|
||||||
dOut.flush();
|
dOut.flush();
|
||||||
binQueue.remove(0);
|
binQueue.remove(0);
|
||||||
System.out.println(String.format("Sent packet (%s)", b[1]));
|
System.out.println(String.format("[OUT] (%s)", b[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// everything sent for now, waiting for data
|
// everything sent for now, waiting for data
|
||||||
|
|
||||||
while (dIn.available() > 0) {
|
while (dIn.available() > 0) {
|
||||||
int numRead = 0;
|
int numRead = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
byte read;
|
byte read;
|
||||||
do {
|
do {
|
||||||
read = dIn.readByte();
|
read = dIn.readByte();
|
||||||
int value = (read & 0b01111111);
|
int value = (read & 0b01111111);
|
||||||
len |= (value << (7 * numRead));
|
len |= (value << (7 * numRead));
|
||||||
|
|
||||||
numRead++;
|
numRead++;
|
||||||
if (numRead > 5) {
|
if (numRead > 5) {
|
||||||
throw new RuntimeException("VarInt is too big");
|
throw new RuntimeException("VarInt is too big");
|
||||||
}
|
}
|
||||||
} while ((read & 0b10000000) != 0);
|
} while ((read & 0b10000000) != 0);
|
||||||
|
|
||||||
|
|
||||||
byte[] in = dIn.readNBytes(len);
|
byte[] in = dIn.readNBytes(len);
|
||||||
@ -89,6 +88,7 @@ public class Network {
|
|||||||
Util.sleep(1);
|
Util.sleep(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
connection.setConnectionState(ConnectionState.DISCONNECTED);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// Could not connect
|
// Could not connect
|
||||||
connection.setConnectionState(ConnectionState.DISCONNECTED);
|
connection.setConnectionState(ConnectionState.DISCONNECTED);
|
||||||
@ -114,17 +114,17 @@ public class Network {
|
|||||||
|
|
||||||
// read data
|
// read data
|
||||||
InPacketBuffer inPacketBuffer = new InPacketBuffer(binQueueIn.get(0));
|
InPacketBuffer inPacketBuffer = new InPacketBuffer(binQueueIn.get(0));
|
||||||
System.out.println("Received packet with command=" + inPacketBuffer.getCommand());
|
|
||||||
Class<? extends ClientboundPacket> clazz = Protocol.getPacketByPacket(connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand()));
|
Class<? extends ClientboundPacket> clazz = Protocol.getPacketByPacket(connection.getVersion().getProtocol().getPacketByCommand(connection.getConnectionState(), inPacketBuffer.getCommand()));
|
||||||
|
|
||||||
if (clazz == null) {
|
if (clazz == null) {
|
||||||
System.out.println("Unknown packet received with command=" + inPacketBuffer.getCommand());
|
System.out.println("[IN] Unknown: " + inPacketBuffer.getCommand());
|
||||||
binQueueIn.remove(0);
|
binQueueIn.remove(0);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
ClientboundPacket packet = clazz.getConstructor().newInstance();
|
ClientboundPacket packet = clazz.getConstructor().newInstance();
|
||||||
packet.read(inPacketBuffer, connection.getVersion());
|
packet.read(inPacketBuffer, connection.getVersion());
|
||||||
|
connection.handle(packet);
|
||||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
// safety first, but will not occur
|
// safety first, but will not occur
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -141,11 +141,6 @@ public class Network {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
|
||||||
connection.setConnectionState(ConnectionState.DISCONNECTING);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendPacket(ServerboundPacket p) {
|
public void sendPacket(ServerboundPacket p) {
|
||||||
queue.add(p);
|
queue.add(p);
|
||||||
|
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets;
|
package de.bixilon.minosoft.protocol.packets;
|
||||||
|
|
||||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
|
||||||
// packet to send to client
|
// packet to send to client
|
||||||
public interface ClientboundPacket extends Packet {
|
public interface ClientboundPacket extends Packet {
|
||||||
void read(InPacketBuffer buffer, ProtocolVersion v);
|
void read(InPacketBuffer buffer, ProtocolVersion v);
|
||||||
|
|
||||||
|
void handle(PacketHandler h);
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,29 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets.clientbound.handshaking;
|
package de.bixilon.minosoft.protocol.packets.clientbound.status;
|
||||||
|
|
||||||
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
|
||||||
public class PacketStatusPong implements ClientboundPacket {
|
public class PacketStatusPong implements ClientboundPacket {
|
||||||
|
Long id;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
public void read(InPacketBuffer buffer, ProtocolVersion v) {
|
||||||
System.out.println("Pong received");
|
this.id = buffer.readLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void log() {
|
public void log() {
|
||||||
// ToDo
|
// ToDo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(PacketHandler h) {
|
||||||
|
h.handle(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getID() {
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,8 +1,9 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets.clientbound.handshaking;
|
package de.bixilon.minosoft.protocol.packets.clientbound.status;
|
||||||
|
|
||||||
import de.bixilon.minosoft.objects.ServerListPing;
|
import de.bixilon.minosoft.objects.ServerListPing;
|
||||||
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
|
||||||
public class PacketStatusResponse implements ClientboundPacket {
|
public class PacketStatusResponse implements ClientboundPacket {
|
||||||
@ -18,4 +19,13 @@ public class PacketStatusResponse implements ClientboundPacket {
|
|||||||
public void log() {
|
public void log() {
|
||||||
// ToDo
|
// ToDo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(PacketHandler h) {
|
||||||
|
h.handle(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServerListPing getResponse() {
|
||||||
|
return this.response;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets.serverbound.handshaking;
|
package de.bixilon.minosoft.protocol.packets.serverbound.status;
|
||||||
|
|
||||||
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
|
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer;
|
import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer;
|
||||||
@ -6,21 +6,21 @@ import de.bixilon.minosoft.protocol.protocol.Packets;
|
|||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
|
||||||
public class PacketStatusPing implements ServerboundPacket {
|
public class PacketStatusPing implements ServerboundPacket {
|
||||||
final Long ms;
|
final Long id;
|
||||||
|
|
||||||
public PacketStatusPing(Long ms) {
|
public PacketStatusPing(Long id) {
|
||||||
this.ms = ms;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PacketStatusPing(int ms) {
|
public PacketStatusPing(int id) {
|
||||||
this.ms = (long) ms;
|
this.id = (long) id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OutPacketBuffer write(ProtocolVersion v) {
|
public OutPacketBuffer write(ProtocolVersion v) {
|
||||||
// no version checking, is the same in all versions (1.7.x - 1.15.2)
|
// no version checking, is the same in all versions (1.7.x - 1.15.2)
|
||||||
OutPacketBuffer buffer = new OutPacketBuffer(v.getPacketCommand(Packets.Serverbound.STATUS_PING));
|
OutPacketBuffer buffer = new OutPacketBuffer(v.getPacketCommand(Packets.Serverbound.STATUS_PING));
|
||||||
buffer.writeLong(ms);
|
buffer.writeLong(id);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,9 @@
|
|||||||
package de.bixilon.minosoft.protocol.packets.serverbound.handshaking;
|
package de.bixilon.minosoft.protocol.packets.serverbound.status;
|
||||||
|
|
||||||
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
|
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.protocol.*;
|
import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.Packets;
|
||||||
|
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||||
|
|
||||||
public class PacketStatusRequest implements ServerboundPacket {
|
public class PacketStatusRequest implements ServerboundPacket {
|
||||||
|
|
@ -4,7 +4,6 @@ import de.bixilon.minosoft.objects.BlockPosition;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@ -39,36 +38,31 @@ public class InByteBuffer {
|
|||||||
public short readShort() {
|
public short readShort() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
||||||
buffer.put(readBytes(Short.BYTES));
|
buffer.put(readBytes(Short.BYTES));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
return buffer.getShort(0);
|
||||||
return buffer.getShort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int readInteger() {
|
public int readInteger() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||||
buffer.put(readBytes(Integer.BYTES));
|
buffer.put(readBytes(Integer.BYTES));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
return buffer.getInt(0);
|
||||||
return buffer.getInt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long readLong() {
|
public Long readLong() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||||
buffer.put(readBytes(Long.BYTES));
|
buffer.put(readBytes(Long.BYTES));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
return buffer.getLong(0);
|
||||||
return buffer.getLong();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Float readFloat() {
|
public Float readFloat() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
||||||
buffer.put(readBytes(Float.BYTES));
|
buffer.put(readBytes(Float.BYTES));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
return buffer.getFloat(0);
|
||||||
return buffer.getFloat();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Double readDouble() {
|
public Double readDouble() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
||||||
buffer.put(readBytes(Double.BYTES));
|
buffer.put(readBytes(Double.BYTES));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
return buffer.getDouble(0);
|
||||||
return buffer.getDouble();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String readString() {
|
public String readString() {
|
||||||
@ -83,7 +77,6 @@ public class InByteBuffer {
|
|||||||
public UUID readUUID() {
|
public UUID readUUID() {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES
|
ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES
|
||||||
buffer.put(readBytes(16));
|
buffer.put(readBytes(16));
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
return new UUID(buffer.getLong(0), buffer.getLong(1));
|
return new UUID(buffer.getLong(0), buffer.getLong(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,14 +4,13 @@ import de.bixilon.minosoft.objects.BlockPosition;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class OutByteBuffer {
|
public class OutByteBuffer {
|
||||||
private List<Byte> bytes = new ArrayList<>();
|
private final List<Byte> bytes = new ArrayList<>();
|
||||||
|
|
||||||
public OutByteBuffer() {
|
public OutByteBuffer() {
|
||||||
}
|
}
|
||||||
@ -37,7 +36,6 @@ public class OutByteBuffer {
|
|||||||
public void writeShort(short s) {
|
public void writeShort(short s) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
|
||||||
buffer.putShort(s);
|
buffer.putShort(s);
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
@ -46,7 +44,6 @@ public class OutByteBuffer {
|
|||||||
public void writeInteger(int i) {
|
public void writeInteger(int i) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
|
||||||
buffer.putInt(i);
|
buffer.putInt(i);
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
@ -55,7 +52,6 @@ public class OutByteBuffer {
|
|||||||
public void writeLong(Long l) {
|
public void writeLong(Long l) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||||
buffer.putLong(l);
|
buffer.putLong(l);
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
@ -64,7 +60,6 @@ public class OutByteBuffer {
|
|||||||
public void writeFloat(Float f) {
|
public void writeFloat(Float f) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
|
||||||
buffer.putFloat(f);
|
buffer.putFloat(f);
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
@ -73,7 +68,6 @@ public class OutByteBuffer {
|
|||||||
public void writeDouble(Double d) {
|
public void writeDouble(Double d) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
|
||||||
buffer.putDouble(d);
|
buffer.putDouble(d);
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
@ -94,7 +88,6 @@ public class OutByteBuffer {
|
|||||||
ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES
|
ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES
|
||||||
buffer.putLong(u.getMostSignificantBits());
|
buffer.putLong(u.getMostSignificantBits());
|
||||||
buffer.putLong(u.getLeastSignificantBits());
|
buffer.putLong(u.getLeastSignificantBits());
|
||||||
buffer.order(ByteOrder.BIG_ENDIAN);
|
|
||||||
for (byte b : buffer.array()) {
|
for (byte b : buffer.array()) {
|
||||||
bytes.add(b);
|
bytes.add(b);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package de.bixilon.minosoft.protocol.protocol;
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.protocol.network.Connection;
|
||||||
|
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
|
||||||
|
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
|
||||||
|
|
||||||
|
public class PacketHandler {
|
||||||
|
Connection connection;
|
||||||
|
|
||||||
|
public PacketHandler(Connection connection) {
|
||||||
|
this.connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handle(PacketStatusResponse pkg) {
|
||||||
|
System.out.println(String.format("Status response received: %s/%s online. MotD: '%s'", pkg.getResponse().getPlayerOnline(), pkg.getResponse().getMaxPlayers(), pkg.getResponse().getMotd()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handle(PacketStatusPong pkg) {
|
||||||
|
System.out.println("Pong: " + pkg.getID());
|
||||||
|
if (connection.isOnlyPing()) {
|
||||||
|
// pong arrived, closing connection
|
||||||
|
connection.disconnect();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package de.bixilon.minosoft.protocol.protocol;
|
package de.bixilon.minosoft.protocol.protocol;
|
||||||
|
|
||||||
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
|
||||||
import de.bixilon.minosoft.protocol.packets.clientbound.handshaking.PacketStatusPong;
|
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
|
||||||
import de.bixilon.minosoft.protocol.packets.clientbound.handshaking.PacketStatusResponse;
|
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user