From 27cdf88b3663b06a1580ca0977c7dfcb1a4465c6 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Wed, 20 May 2020 23:33:27 -0400 Subject: [PATCH] Add BufferedPacket Add PacketProtocol.getOutgoingId(Packet) to get id from BufferedPacket Change TcpPacketCodec.encode to use Packet instance instead of its class --- .../packetlib/packet/BufferedPacket.java | 36 +++++++++++++++++++ .../packetlib/packet/PacketProtocol.java | 25 ++++++++++--- .../packetlib/tcp/TcpPacketCodec.java | 2 +- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/github/steveice10/packetlib/packet/BufferedPacket.java diff --git a/src/main/java/com/github/steveice10/packetlib/packet/BufferedPacket.java b/src/main/java/com/github/steveice10/packetlib/packet/BufferedPacket.java new file mode 100644 index 00000000..66d1ba55 --- /dev/null +++ b/src/main/java/com/github/steveice10/packetlib/packet/BufferedPacket.java @@ -0,0 +1,36 @@ +package com.github.steveice10.packetlib.packet; + +import com.github.steveice10.packetlib.io.NetInput; +import com.github.steveice10.packetlib.io.NetOutput; + +import java.io.EOFException; +import java.io.IOException; + +public class BufferedPacket implements Packet { + private Class packetClass; + private byte[] buf; + + public BufferedPacket(Class packetClass, byte[] buf) { + this.packetClass = packetClass; + this.buf = buf; + } + + public Class getPacketClass() { + return packetClass; + } + + @Override + public void read(NetInput in) throws IOException { + throw new EOFException("BufferedPacket can not be read"); + } + + @Override + public void write(NetOutput out) throws IOException { + out.writeBytes(buf); + } + + @Override + public boolean isPriority() { + return true; + } +} diff --git a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java index f33172cc..7d5464a4 100644 --- a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java +++ b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java @@ -132,15 +132,30 @@ public abstract class PacketProtocol { /** * Gets the registered id of an outgoing packet class. * - * @param packet Class of the packet to get the id for. + * @param packetClass Class of the packet to get the id for. * @return The packet's registered id. * @throws IllegalArgumentException If the packet is not registered. */ - public final int getOutgoingId(Class packet) { - if(!this.outgoing.containsKey(packet) || this.outgoing.get(packet) == null) { - throw new IllegalArgumentException("Unregistered outgoing packet class: " + packet.getName()); + public final int getOutgoingId(Class packetClass) { + if(!this.outgoing.containsKey(packetClass) || this.outgoing.get(packetClass) == null) { + throw new IllegalArgumentException("Unregistered outgoing packet class: " + packetClass.getName()); } - return this.outgoing.get(packet); + return this.outgoing.get(packetClass); + } + + /** + * Gets the registered id of an outgoing {@link Packet} instance. + * + * @param packet Instance of {@link Packet} to get the id for. + * @return The packet's registered id. + * @throws IllegalArgumentException If the packet is not registered. + */ + public final int getOutgoingId(Packet packet) { + if(packet instanceof BufferedPacket) { + return getOutgoingId(((BufferedPacket)packet).getPacketClass()); + } + + return getOutgoingId(packet.getClass()); } } diff --git a/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java b/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java index 88ed3dc3..fcb4bc8a 100644 --- a/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java +++ b/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java @@ -23,7 +23,7 @@ public class TcpPacketCodec extends ByteToMessageCodec { @Override public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) throws Exception { NetOutput out = new ByteBufNetOutput(buf); - this.session.getPacketProtocol().getPacketHeader().writePacketId(out, this.session.getPacketProtocol().getOutgoingId(packet.getClass())); + this.session.getPacketProtocol().getPacketHeader().writePacketId(out, this.session.getPacketProtocol().getOutgoingId(packet)); packet.write(out); }