Skip to content

Commit

Permalink
Merge branch 'refs/heads/improve-protocol-state' into translatable-me…
Browse files Browse the repository at this point in the history
…ssages

# Conflicts:
#	protocol/src/main/java/org/geysermc/mcprotocollib/protocol/packet/login/serverbound/ServerboundKeyPacket.java
  • Loading branch information
AlexProgrammerDE committed Jun 17, 2024
2 parents ae3c065 + cfd3447 commit 6dfa453
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public ClientboundFinishConfigurationPacket(ByteBuf in, MinecraftCodecHelper hel
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public ServerboundFinishConfigurationPacket(ByteBuf in, MinecraftCodecHelper hel

public void serialize(ByteBuf buf, MinecraftCodecHelper helper) {
}

@Override
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isPriority() {
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public ClientboundStartConfigurationPacket(ByteBuf in, MinecraftCodecHelper help

public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public ServerboundConfigurationAcknowledgedPacket(ByteBuf in, MinecraftCodecHelp

public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isPriority() {
public boolean isTerminal() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public ServerboundLoginAcknowledgedPacket(ByteBuf in, MinecraftCodecHelper helpe
@Override
public void serialize(ByteBuf out, MinecraftCodecHelper helper) {
}

@Override
public boolean isTerminal() {
return true;
}
}

0 comments on commit 6dfa453

Please sign in to comment.