mirror of
https://github.com/FabricMC/fabric.git
synced 2025-03-24 22:11:18 -04:00
Copy untyped payload buffer on read/write (#3450)
* copy untyped payload buffer on read/write * use the actual handler on junit tests
This commit is contained in:
parent
4944b5a5ee
commit
95a8ac5184
3 changed files with 36 additions and 12 deletions
fabric-networking-api-v1/src
main/java/net/fabricmc/fabric/impl/networking/payload
test/java/net/fabricmc/fabric/test/networking/unit
testmod/java/net/fabricmc/fabric/test/networking/play
|
@ -21,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketType;
|
||||
|
||||
public record UntypedPayload(Identifier id, PacketByteBuf buffer) implements ResolvedPayload {
|
||||
|
@ -29,8 +30,9 @@ public record UntypedPayload(Identifier id, PacketByteBuf buffer) implements Res
|
|||
if (type == null) {
|
||||
return this;
|
||||
} else {
|
||||
TypedPayload typed = new TypedPayload(type.read(buffer));
|
||||
int dangling = buffer.readableBytes();
|
||||
PacketByteBuf copy = PacketByteBufs.copy(buffer);
|
||||
TypedPayload typed = new TypedPayload(type.read(copy));
|
||||
int dangling = copy.readableBytes();
|
||||
|
||||
if (dangling > 0) {
|
||||
throw new IllegalStateException("Found " + dangling + " extra bytes when reading packet " + id);
|
||||
|
@ -42,6 +44,11 @@ public record UntypedPayload(Identifier id, PacketByteBuf buffer) implements Res
|
|||
|
||||
@Override
|
||||
public void write(PacketByteBuf buf) {
|
||||
buf.writeBytes(buffer);
|
||||
buf.writeBytes(buffer.copy());
|
||||
}
|
||||
|
||||
@Override
|
||||
public PacketByteBuf buffer() {
|
||||
return PacketByteBufs.copy(buffer);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,9 +47,11 @@ import net.minecraft.network.packet.CustomPayload;
|
|||
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
|
||||
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.api.networking.v1.ServerConfigurationNetworking;
|
||||
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
|
||||
import net.fabricmc.fabric.impl.networking.CommonPacketHandler;
|
||||
import net.fabricmc.fabric.impl.networking.CommonPacketsImpl;
|
||||
|
@ -58,7 +60,6 @@ import net.fabricmc.fabric.impl.networking.CommonVersionPayload;
|
|||
import net.fabricmc.fabric.impl.networking.client.ClientConfigurationNetworkAddon;
|
||||
import net.fabricmc.fabric.impl.networking.client.ClientNetworkingImpl;
|
||||
import net.fabricmc.fabric.impl.networking.payload.ResolvablePayload;
|
||||
import net.fabricmc.fabric.impl.networking.payload.UntypedPayload;
|
||||
import net.fabricmc.fabric.impl.networking.server.ServerConfigurationNetworkAddon;
|
||||
import net.fabricmc.fabric.impl.networking.server.ServerNetworkingImpl;
|
||||
|
||||
|
@ -108,7 +109,9 @@ public class CommonPacketTests {
|
|||
PacketByteBuf buf = PacketByteBufs.create();
|
||||
buf.writeIntArray(new int[]{1, 2, 3});
|
||||
|
||||
packetHandler.internal().receive(null, clientNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
// The actual handler doesn't copy the buffer
|
||||
ClientConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ClientConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, clientNetworkHandler, buf, packetSender);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
@ -132,7 +135,8 @@ public class CommonPacketTests {
|
|||
buf.writeIntArray(new int[]{2, 3}); // We only support version 1
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> {
|
||||
packetHandler.internal().receive(null, clientNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ClientConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ClientConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, clientNetworkHandler, buf, packetSender);
|
||||
});
|
||||
|
||||
// Assert the entire packet was read
|
||||
|
@ -149,7 +153,8 @@ public class CommonPacketTests {
|
|||
PacketByteBuf buf = PacketByteBufs.create();
|
||||
buf.writeIntArray(new int[]{1, 2, 3});
|
||||
|
||||
packetHandler.internal().receive(null, serverNetworkHandler, new UntypedPayload(null, buf), null);
|
||||
ServerConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ServerConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, serverNetworkHandler, buf, null);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
@ -167,7 +172,8 @@ public class CommonPacketTests {
|
|||
buf.writeIntArray(new int[]{3}); // Server only supports version 1
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> {
|
||||
packetHandler.internal().receive(null, serverNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ServerConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ServerConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, serverNetworkHandler, buf, null);
|
||||
});
|
||||
|
||||
// Assert the entire packet was read
|
||||
|
@ -188,7 +194,8 @@ public class CommonPacketTests {
|
|||
buf.writeString("play"); // Target phase
|
||||
buf.writeCollection(List.of(new Identifier("fabric", "test")), PacketByteBuf::writeIdentifier);
|
||||
|
||||
packetHandler.internal().receive(null, clientNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ClientConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ClientConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, clientNetworkHandler, buf, packetSender);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
@ -217,7 +224,8 @@ public class CommonPacketTests {
|
|||
buf.writeString("configuration"); // Target phase
|
||||
buf.writeCollection(List.of(new Identifier("fabric", "test")), PacketByteBuf::writeIdentifier);
|
||||
|
||||
packetHandler.internal().receive(null, clientNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ClientConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ClientConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, clientNetworkHandler, buf, packetSender);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
@ -245,7 +253,8 @@ public class CommonPacketTests {
|
|||
buf.writeString("play"); // Target phase
|
||||
buf.writeCollection(List.of(new Identifier("fabric", "test")), PacketByteBuf::writeIdentifier);
|
||||
|
||||
packetHandler.internal().receive(null, serverNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ServerConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ServerConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, serverNetworkHandler, buf, null);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
@ -266,7 +275,8 @@ public class CommonPacketTests {
|
|||
buf.writeString("configuration"); // Target phase
|
||||
buf.writeCollection(List.of(new Identifier("fabric", "test")), PacketByteBuf::writeIdentifier);
|
||||
|
||||
packetHandler.internal().receive(null, serverNetworkHandler, new UntypedPayload(null, buf), packetSender);
|
||||
ServerConfigurationNetworking.ConfigurationChannelHandler actualHandler = (ServerConfigurationNetworking.ConfigurationChannelHandler) packetHandler.actual();
|
||||
actualHandler.receive(null, serverNetworkHandler, buf, null);
|
||||
|
||||
// Assert the entire packet was read
|
||||
assertEquals(0, buf.readableBytes());
|
||||
|
|
|
@ -78,6 +78,13 @@ public final class NetworkingPlayPacketTest implements ModInitializer {
|
|||
ctx.getSource().getPlayer().networkHandler.sendPacket(new CustomPayloadS2CPacket(buf));
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}))
|
||||
.then(literal("repeat").executes(ctx -> {
|
||||
PacketByteBuf buf = PacketByteBufs.create();
|
||||
buf.writeText(Text.literal("repeat"));
|
||||
ServerPlayNetworking.send(ctx.getSource().getPlayer(), TEST_CHANNEL, buf);
|
||||
ServerPlayNetworking.send(ctx.getSource().getPlayer(), TEST_CHANNEL, buf);
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}))
|
||||
.then(literal("bundled").executes(ctx -> {
|
||||
PacketByteBuf buf1 = PacketByteBufs.create();
|
||||
buf1.writeText(Text.literal("bundled #1"));
|
||||
|
|
Loading…
Add table
Reference in a new issue