From b02502500126fd57b90a4ebb8bd143e38ed32b46 Mon Sep 17 00:00:00 2001 From: Steveice10 Date: Sun, 31 May 2020 17:15:44 -0700 Subject: [PATCH] Call PacketErrorEvent for more I/O errors. --- .../packetlib/tcp/TcpPacketCodec.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 08e3be9e..43425c48 100644 --- a/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java +++ b/src/main/java/com/github/steveice10/packetlib/tcp/TcpPacketCodec.java @@ -23,8 +23,9 @@ public class TcpPacketCodec extends ByteToMessageCodec { @Override public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) throws Exception { - NetOutput out = new ByteBufNetOutput(buf); try { + NetOutput out = new ByteBufNetOutput(buf); + this.session.getPacketProtocol().getPacketHeader().writePacketId(out, this.session.getPacketProtocol().getOutgoingId(packet)); packet.write(out); } catch(Throwable t) { @@ -38,18 +39,23 @@ public class TcpPacketCodec extends ByteToMessageCodec { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List out) throws Exception { - int initial = buf.readerIndex(); - NetInput in = new ByteBufNetInput(buf); - int id = this.session.getPacketProtocol().getPacketHeader().readPacketId(in); - if(id == -1) { - buf.readerIndex(initial); - return; - } - Packet packet; try { + NetInput in = new ByteBufNetInput(buf); + + int initial = buf.readerIndex(); + int id = this.session.getPacketProtocol().getPacketHeader().readPacketId(in); + if(id == -1) { + buf.readerIndex(initial); + return; + } + packet = this.session.getPacketProtocol().createIncomingPacket(id); packet.read(in); + + if(buf.readableBytes() > 0) { + throw new IllegalStateException("Packet \"" + packet.getClass().getSimpleName() + "\" not fully read."); + } } catch(Throwable t) { PacketErrorEvent e = new PacketErrorEvent(this.session, t); this.session.callEvent(e); @@ -60,10 +66,6 @@ public class TcpPacketCodec extends ByteToMessageCodec { return; } - if(buf.readableBytes() > 0) { - throw new IllegalStateException("Packet \"" + packet.getClass().getSimpleName() + "\" not fully read."); - } - if(packet.isPriority()) { this.session.callEvent(new PacketReceivedEvent(this.session, packet)); }