diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java index fbec886d5..f34f9f96b 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/Session.java @@ -300,8 +300,10 @@ default void send(Packet packet) { void flushSync(); default void switchInboundProtocol(Runnable switcher) { - setAutoRead(false); switcher.run(); + + // We switched to the new inbound state + // we can start reading again setAutoRead(true); } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/Packet.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/Packet.java index 56cd2db7e..5e60ab510 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/Packet.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/packet/Packet.java @@ -8,13 +8,12 @@ public interface Packet { /** - * Gets whether the packet has handling priority. - * If the result is true, the packet will be handled immediately after being - * decoded. + * Gets whether the packet terminates the current protocol state. + * If yes, we disable auto-read if inbound and wait for the code to switch the state. * - * @return Whether the packet has priority. + * @return Whether the packet terminates the current protocol state */ - default boolean isPriority() { + default boolean isTerminal() { return false; } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java index 5ebf7335e..fb354dcf6 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/network/tcp/TcpSession.java @@ -408,10 +408,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { @Override protected void channelRead0(ChannelHandlerContext ctx, Packet packet) { - if (!packet.isPriority() && eventLoop != null) { - eventLoop.execute(() -> this.callPacketReceived(packet)); - } else { - this.callPacketReceived(packet); + if (packet.isTerminal()) { + // Next packets are in a different protocol state, so we must + // disable auto-read to prevent reading wrong packets. + setAutoRead(false); } + + this.callPacketReceived(packet); } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/common/clientbound/ClientboundDisconnectPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/common/clientbound/ClientboundDisconnectPacket.java index cf9b05a19..927dacc25 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/common/clientbound/ClientboundDisconnectPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/common/clientbound/ClientboundDisconnectPacket.java @@ -28,9 +28,4 @@ public ClientboundDisconnectPacket(ByteBuf in, MinecraftCodecHelper helper) { public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeComponent(out, this.reason); } - - @Override - public boolean isPriority() { - return true; - } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundFinishConfigurationPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundFinishConfigurationPacket.java index 3c3afc8bd..f248a38c3 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundFinishConfigurationPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/clientbound/ClientboundFinishConfigurationPacket.java @@ -15,4 +15,9 @@ public ClientboundFinishConfigurationPacket(ByteBuf in, MinecraftCodecHelper hel @Override public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } + + @Override + public boolean isTerminal() { + return true; + } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundFinishConfigurationPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundFinishConfigurationPacket.java index dd573ad94..75147f6c1 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundFinishConfigurationPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/configuration/serverbound/ServerboundFinishConfigurationPacket.java @@ -15,4 +15,9 @@ public ServerboundFinishConfigurationPacket(ByteBuf in, MinecraftCodecHelper hel public void serialize(ByteBuf buf, MinecraftCodecHelper helper) { } + + @Override + public boolean isTerminal() { + return true; + } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/handshake/serverbound/ClientIntentionPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/handshake/serverbound/ClientIntentionPacket.java index 60254a3ac..b1d01b5e5 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/handshake/serverbound/ClientIntentionPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/handshake/serverbound/ClientIntentionPacket.java @@ -34,7 +34,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } @Override - public boolean isPriority() { + public boolean isTerminal() { return true; } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/ClientboundStartConfigurationPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/ClientboundStartConfigurationPacket.java index e6969d373..d7844e6e3 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/ClientboundStartConfigurationPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/clientbound/ClientboundStartConfigurationPacket.java @@ -15,4 +15,9 @@ public ClientboundStartConfigurationPacket(ByteBuf in, MinecraftCodecHelper help public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } + + @Override + public boolean isTerminal() { + return true; + } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/serverbound/ServerboundConfigurationAcknowledgedPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/serverbound/ServerboundConfigurationAcknowledgedPacket.java index 6665a56db..56ec5fdaa 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/serverbound/ServerboundConfigurationAcknowledgedPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/ingame/serverbound/ServerboundConfigurationAcknowledgedPacket.java @@ -15,4 +15,9 @@ public ServerboundConfigurationAcknowledgedPacket(ByteBuf in, MinecraftCodecHelp public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } + + @Override + public boolean isTerminal() { + return true; + } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java index 3d3e528e0..40dd9d251 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundGameProfilePacket.java @@ -32,7 +32,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } @Override - public boolean isPriority() { + public boolean isTerminal() { return true; } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundHelloPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundHelloPacket.java index 144ddaed0..4ae1f0193 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundHelloPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundHelloPacket.java @@ -43,9 +43,4 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeByteArray(out, this.challenge); out.writeBoolean(this.shouldAuthenticate); } - - @Override - public boolean isPriority() { - return true; - } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java index 26a9fba94..33ee3410f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginCompressionPacket.java @@ -21,9 +21,4 @@ public ClientboundLoginCompressionPacket(ByteBuf in, MinecraftCodecHelper helper public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeVarInt(out, this.threshold); } - - @Override - public boolean isPriority() { - return true; - } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java index 444412040..37a1b2911 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/clientbound/ClientboundLoginDisconnectPacket.java @@ -32,9 +32,4 @@ public ClientboundLoginDisconnectPacket(ByteBuf in, MinecraftCodecHelper helper) public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeString(out, DefaultComponentSerializer.get().serialize(reason)); } - - @Override - public boolean isPriority() { - return true; - } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundHelloPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundHelloPacket.java index 0691788ac..141834e76 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundHelloPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundHelloPacket.java @@ -27,9 +27,4 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeString(out, this.username); helper.writeUUID(out, this.profileId); } - - @Override - public boolean isPriority() { - return true; - } } diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundKeyPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundKeyPacket.java index 145411134..05104bdd1 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundKeyPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundKeyPacket.java @@ -45,11 +45,6 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) { helper.writeByteArray(out, this.encryptedChallenge); } - @Override - public boolean isPriority() { - return true; - } - private static byte[] cipherData(int mode, Key key, byte[] data) { try { Cipher cipher = Cipher.getInstance(key.getAlgorithm()); diff --git a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundLoginAcknowledgedPacket.java b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundLoginAcknowledgedPacket.java index 14800c6de..5370be57f 100644 --- a/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundLoginAcknowledgedPacket.java +++ b/protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundLoginAcknowledgedPacket.java @@ -15,4 +15,9 @@ public ServerboundLoginAcknowledgedPacket(ByteBuf in, MinecraftCodecHelper helpe @Override public void serialize(ByteBuf out, MinecraftCodecHelper helper) { } + + @Override + public boolean isTerminal() { + return true; + } }