Make sure bad packet data is skipped when errors are suppressed.

This commit is contained in:
Steveice10 2020-05-31 17:23:22 -07:00
parent b025025001
commit 8dbe3d081c

View file

@ -23,12 +23,17 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
@Override
public void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf buf) throws Exception {
int initial = buf.writerIndex();
try {
NetOutput out = new ByteBufNetOutput(buf);
this.session.getPacketProtocol().getPacketHeader().writePacketId(out, this.session.getPacketProtocol().getOutgoingId(packet));
packet.write(out);
} catch(Throwable t) {
// Reset writer index to make sure incomplete data is not written out.
buf.writerIndex(initial);
PacketErrorEvent e = new PacketErrorEvent(this.session, t);
this.session.callEvent(e);
if(!e.shouldSuppress()) {
@ -39,11 +44,12 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
int initial = buf.readerIndex();
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);
@ -57,6 +63,9 @@ public class TcpPacketCodec extends ByteToMessageCodec<Packet> {
throw new IllegalStateException("Packet \"" + packet.getClass().getSimpleName() + "\" not fully read.");
}
} catch(Throwable t) {
// Advance buffer to end to make sure remaining data in this packet is skipped.
buf.readerIndex(buf.readerIndex() + buf.readableBytes());
PacketErrorEvent e = new PacketErrorEvent(this.session, t);
this.session.callEvent(e);
if(!e.shouldSuppress()) {