Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled packets disconnecting the client. #3324

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now correct, it now matches the server network addons.

}
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
Loading