Skip to content

Commit

Permalink
Fix unhandled packets disconnecting the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Sep 18, 2023
1 parent daef22a commit d6ddf77
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ protected void receiveRegistration(boolean register, PacketByteBuf buf) {
* @return true if the packet has been handled
*/
public boolean handle(PacketByteBufPayload payload) {
try {
return this.handle(payload.id(), payload.data());
} finally {
payload.data().release();
}
return this.handle(payload.id(), payload.data());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public void onServerReady() {
* @return true if the packet has been handled
*/
public boolean handle(PacketByteBufPayload payload) {
try {
return this.handle(payload.id(), payload.data());
} finally {
payload.data().release();
}
return this.handle(payload.id(), payload.data());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ protected void registerPendingChannels(ChannelInfoHolder holder, NetworkState st
}

// always supposed to handle async!
protected boolean handle(Identifier channelName, PacketByteBuf originalBuf) {
protected boolean handle(Identifier channelName, PacketByteBuf buf) {
this.logger.debug("Handling inbound packet from channel with name \"{}\"", channelName);

// Handle reserved packets
if (NetworkingImpl.REGISTER_CHANNEL.equals(channelName)) {
this.receiveRegistration(true, PacketByteBufs.slice(originalBuf));
this.receiveRegistration(true, buf);
return true;
}

if (NetworkingImpl.UNREGISTER_CHANNEL.equals(channelName)) {
this.receiveRegistration(false, PacketByteBufs.slice(originalBuf));
this.receiveRegistration(false, buf);
return true;
}

Expand All @@ -92,8 +92,6 @@ protected boolean handle(Identifier channelName, PacketByteBuf originalBuf) {
return false;
}

PacketByteBuf buf = PacketByteBufs.slice(originalBuf);

try {
this.receive(handler, buf);
} catch (Throwable ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@

public final class NetworkingPlayPacketTest implements ModInitializer {
public static final Identifier TEST_CHANNEL = NetworkingTestmods.id("test_channel");
private static final Identifier UNKNOWN_TEST_CHANNEL = NetworkingTestmods.id("unknown_test_channel");

public static void sendToTestChannel(ServerPlayerEntity player, String stuff) {
ServerPlayNetworking.getSender(player).sendPacket(new OverlayPacket(Text.literal(stuff)), future -> {
NetworkingTestmods.LOGGER.info("Sent custom payload packet in {}", TEST_CHANNEL);
});
}

private static void sendToUnknownChannel(ServerPlayerEntity player) {
ServerPlayNetworking.getSender(player).sendPacket(UNKNOWN_TEST_CHANNEL, PacketByteBufs.create());
}

public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
NetworkingTestmods.LOGGER.info("Registering test command");

Expand All @@ -61,6 +66,10 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
sendToTestChannel(ctx.getSource().getPlayer(), stuff);
return Command.SINGLE_SUCCESS;
}))
.then(literal("unknown").executes(ctx -> {
sendToUnknownChannel(ctx.getSource().getPlayer());
return Command.SINGLE_SUCCESS;
}))
.then(literal("bundled").executes(ctx -> {
PacketByteBuf buf1 = PacketByteBufs.create();
buf1.writeText(Text.literal("bundled #1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,36 @@

package net.fabricmc.fabric.test.networking.client.play;

import com.mojang.brigadier.Command;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.Identifier;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.fabricmc.fabric.test.networking.NetworkingTestmods;
import net.fabricmc.fabric.test.networking.play.NetworkingPlayPacketTest;

public final class NetworkingPlayPacketClientTest implements ClientModInitializer, ClientPlayNetworking.PlayPacketHandler<NetworkingPlayPacketTest.OverlayPacket> {
private static final Identifier UNKNOWN_TEST_CHANNEL = NetworkingTestmods.id("unknown_test_channel");

@Override
public void onInitializeClient() {
ClientPlayConnectionEvents.INIT.register((handler, client) -> ClientPlayNetworking.registerReceiver(NetworkingPlayPacketTest.OverlayPacket.PACKET_TYPE, this));

ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(
ClientCommandManager.literal("clientnetworktestcommand")
.then(ClientCommandManager.literal("unknown").executes(context -> {
ClientPlayNetworking.send(UNKNOWN_TEST_CHANNEL, PacketByteBufs.create());
return Command.SINGLE_SUCCESS;
}
))));
}

@Override
Expand Down

0 comments on commit d6ddf77

Please sign in to comment.