mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 12:20:11 +00:00
Handle modded packets on the network thread (#8703)
* Handle modded packets on the network thread - On the server we simply need to remove the call to ensureRunningOnSameThread. - On the client side, we now handle the packet at the very start of the call. We make sure we're running from a network thread to prevent calling the handling code twice. While this does mean we no longer call .release(), in practice this doesn't cause any leaks as ClientboundCustomPayloadPacket releases for us. * Clarify behaviour a little in the documentation * Javadoc formatting * Add a helper method for handling packets on the main thread Also rename the network thread one. Should make it clearer the expected behaviour of the two, and make it clearer there's a potentially breaking change. * Add back consumer() methods Also document EventNetworkChannel, to clarify the thread behaviour there. * Add since = "1.19" to deprecated annotations
This commit is contained in:
1 parent
b70d99489b
commit
c7c2478cb0
5 files changed
+118
-26
No files matched your search
@@ -26,7 +26,7 @@ class NetworkInitialization {
|
||||
loginIndex(HandshakeMessages.LoginIndexedMessage::getLoginIndex, HandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(HandshakeMessages.C2SAcknowledge::decode).
|
||||
encoder(HandshakeMessages.C2SAcknowledge::encode).
|
||||
consumer(HandshakeHandler.indexFirst(HandshakeHandler::handleClientAck)).
|
||||
consumerNetworkThread(HandshakeHandler.indexFirst(HandshakeHandler::handleClientAck)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.S2CModData.class, 5, NetworkDirection.LOGIN_TO_CLIENT).
|
||||
@@ -35,7 +35,7 @@ class NetworkInitialization {
|
||||
encoder(HandshakeMessages.S2CModData::encode).
|
||||
markAsLoginPacket().
|
||||
noResponse().
|
||||
consumer(HandshakeHandler.biConsumerFor(HandshakeHandler::handleModData)).
|
||||
consumerNetworkThread(HandshakeHandler.biConsumerFor(HandshakeHandler::handleModData)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.S2CModList.class, 1, NetworkDirection.LOGIN_TO_CLIENT).
|
||||
@@ -43,14 +43,14 @@ class NetworkInitialization {
|
||||
decoder(HandshakeMessages.S2CModList::decode).
|
||||
encoder(HandshakeMessages.S2CModList::encode).
|
||||
markAsLoginPacket().
|
||||
consumer(HandshakeHandler.biConsumerFor(HandshakeHandler::handleServerModListOnClient)).
|
||||
consumerNetworkThread(HandshakeHandler.biConsumerFor(HandshakeHandler::handleServerModListOnClient)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.C2SModListReply.class, 2, NetworkDirection.LOGIN_TO_SERVER).
|
||||
loginIndex(HandshakeMessages.LoginIndexedMessage::getLoginIndex, HandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(HandshakeMessages.C2SModListReply::decode).
|
||||
encoder(HandshakeMessages.C2SModListReply::encode).
|
||||
consumer(HandshakeHandler.indexFirst(HandshakeHandler::handleClientModListOnServer)).
|
||||
consumerNetworkThread(HandshakeHandler.indexFirst(HandshakeHandler::handleClientModListOnServer)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.S2CRegistry.class, 3, NetworkDirection.LOGIN_TO_CLIENT).
|
||||
@@ -58,7 +58,7 @@ class NetworkInitialization {
|
||||
decoder(HandshakeMessages.S2CRegistry::decode).
|
||||
encoder(HandshakeMessages.S2CRegistry::encode).
|
||||
buildLoginPacketList(RegistryManager::generateRegistryPackets). //TODO: Make this non-static, and store a cache on the client.
|
||||
consumer(HandshakeHandler.biConsumerFor(HandshakeHandler::handleRegistryMessage)).
|
||||
consumerNetworkThread(HandshakeHandler.biConsumerFor(HandshakeHandler::handleRegistryMessage)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.S2CConfigData.class, 4, NetworkDirection.LOGIN_TO_CLIENT).
|
||||
@@ -66,14 +66,14 @@ class NetworkInitialization {
|
||||
decoder(HandshakeMessages.S2CConfigData::decode).
|
||||
encoder(HandshakeMessages.S2CConfigData::encode).
|
||||
buildLoginPacketList(ConfigSync.INSTANCE::syncConfigs).
|
||||
consumer(HandshakeHandler.biConsumerFor(HandshakeHandler::handleConfigSync)).
|
||||
consumerNetworkThread(HandshakeHandler.biConsumerFor(HandshakeHandler::handleConfigSync)).
|
||||
add();
|
||||
|
||||
handshakeChannel.messageBuilder(HandshakeMessages.S2CChannelMismatchData.class, 6, NetworkDirection.LOGIN_TO_CLIENT).
|
||||
loginIndex(HandshakeMessages.LoginIndexedMessage::getLoginIndex, HandshakeMessages.LoginIndexedMessage::setLoginIndex).
|
||||
decoder(HandshakeMessages.S2CChannelMismatchData::decode).
|
||||
encoder(HandshakeMessages.S2CChannelMismatchData::encode).
|
||||
consumer(HandshakeHandler.biConsumerFor(HandshakeHandler::handleModMismatchData)).
|
||||
consumerNetworkThread(HandshakeHandler.biConsumerFor(HandshakeHandler::handleModMismatchData)).
|
||||
add();
|
||||
|
||||
return handshakeChannel;
|
||||
@@ -90,13 +90,13 @@ class NetworkInitialization {
|
||||
playChannel.messageBuilder(PlayMessages.SpawnEntity.class, 0).
|
||||
decoder(PlayMessages.SpawnEntity::decode).
|
||||
encoder(PlayMessages.SpawnEntity::encode).
|
||||
consumer(PlayMessages.SpawnEntity::handle).
|
||||
consumerNetworkThread(PlayMessages.SpawnEntity::handle).
|
||||
add();
|
||||
|
||||
playChannel.messageBuilder(PlayMessages.OpenContainer.class,1).
|
||||
decoder(PlayMessages.OpenContainer::decode).
|
||||
encoder(PlayMessages.OpenContainer::encode).
|
||||
consumer(PlayMessages.OpenContainer::handle).
|
||||
consumerNetworkThread(PlayMessages.OpenContainer::handle).
|
||||
add();
|
||||
|
||||
return playChannel;
|
||||
|
||||
@@ -6,11 +6,25 @@
|
||||
package net.minecraftforge.network.event;
|
||||
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.NetworkInstance;
|
||||
import net.minecraftforge.network.NetworkRegistry;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* An event-bus like object on which {@link NetworkEvent}s are posted.
|
||||
*
|
||||
* These events are fired from the network thread, and so should not interact with most game state by default.
|
||||
* {@link NetworkEvent.Context#enqueueWork(Runnable)} can be used to handle the message on the main server or client
|
||||
* thread.
|
||||
*
|
||||
* @see NetworkRegistry#newEventChannel(ResourceLocation, Supplier, Predicate, Predicate)
|
||||
* @see NetworkRegistry.ChannelBuilder#newEventChannel(ResourceLocation, Supplier, Predicate, Predicate)
|
||||
*/
|
||||
public class EventNetworkChannel
|
||||
{
|
||||
private final NetworkInstance instance;
|
||||
|
||||
@@ -8,25 +8,16 @@ package net.minecraftforge.network.simple;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.NetworkInstance;
|
||||
import net.minecraftforge.network.PacketDistributor;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
public class SimpleChannel
|
||||
@@ -181,11 +172,34 @@ public class SimpleChannel
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message encoder, which writes this message to a {@link FriendlyByteBuf}.
|
||||
* <p>
|
||||
* The encoder is called <em>immediately</em> {@linkplain #send(PacketDistributor.PacketTarget, Object) when the
|
||||
* packet is sent}. This means encoding typically occurs on the main server/client thread rather than on the
|
||||
* network thread.
|
||||
* <p>
|
||||
* However, this behaviour should not be relied on, and the encoder should try to be thread-safe and not
|
||||
* interact with the current game state.
|
||||
*
|
||||
* @param encoder The message encoder.
|
||||
* @return This message builder, for chaining.
|
||||
*/
|
||||
public MessageBuilder<MSG> encoder(BiConsumer<MSG, FriendlyByteBuf> encoder) {
|
||||
this.encoder = encoder;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message decoder, which reads the message from a {@link FriendlyByteBuf}.
|
||||
* <p>
|
||||
* The decoder is called when the message is received on the network thread. The decoder should not attempt to
|
||||
* access or mutate any game state, deferring that until the {@linkplain #consumer(ToBooleanBiFunction) the
|
||||
* message is handled}.
|
||||
*
|
||||
* @param decoder The message decoder.
|
||||
* @return The message builder, for chaining.
|
||||
*/
|
||||
public MessageBuilder<MSG> decoder(Function<FriendlyByteBuf, MSG> decoder) {
|
||||
this.decoder = decoder;
|
||||
return this;
|
||||
@@ -223,11 +237,56 @@ public class SimpleChannel
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message consumer, which is called once a message has been decoded.
|
||||
* @param consumer The message consumer.
|
||||
* @return The message builder, for chaining.
|
||||
* @deprecated Use {@link #consumerMainThread(BiConsumer)} or {@link #consumerNetworkThread(BiConsumer)}.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.19")
|
||||
public MessageBuilder<MSG> consumer(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) {
|
||||
consumerMainThread(consumer);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message consumer, which is called once a message has been decoded. This accepts the decoded message
|
||||
* object and the message's context.
|
||||
* <p>
|
||||
* The consumer is called on the network thread, and so should not interact with most game state by default.
|
||||
* {@link NetworkEvent.Context#enqueueWork(Runnable)} can be used to handle the message on the main server or
|
||||
* client thread. Alternatively one can use {@link #consumerMainThread(BiConsumer)} to run the handler on the
|
||||
* main thread.
|
||||
*
|
||||
* @param consumer The message consumer.
|
||||
* @return The message builder, for chaining.
|
||||
* @see #consumerMainThread(BiConsumer)
|
||||
*/
|
||||
public MessageBuilder<MSG> consumerNetworkThread(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) {
|
||||
this.consumer = consumer;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message consumer, which is called once a message has been decoded. This accepts the decoded message
|
||||
* object and the message's context.
|
||||
* <p>
|
||||
* Unlike {@link #consumerNetworkThread(BiConsumer)}, the consumer is called on the main thread, and so can
|
||||
* interact with most game state by default.
|
||||
*
|
||||
* @param consumer The message consumer.
|
||||
* @return The message builder, for chaining.
|
||||
* @see #consumerNetworkThread(BiConsumer)
|
||||
*/
|
||||
public MessageBuilder<MSG> consumerMainThread(BiConsumer<MSG, Supplier<NetworkEvent.Context>> consumer) {
|
||||
this.consumer = (msg, context) -> {
|
||||
var ctx = context.get();
|
||||
ctx.enqueueWork(() -> consumer.accept(msg, context));
|
||||
ctx.setPacketHandled(true);
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
public interface ToBooleanBiFunction<T, U> {
|
||||
boolean applyAsBool(T first, U second);
|
||||
}
|
||||
@@ -236,8 +295,9 @@ public class SimpleChannel
|
||||
* Function returning a boolean "packet handled" indication, for simpler channel building.
|
||||
* @param handler a handler
|
||||
* @return this
|
||||
* @see #consumerNetworkThread(BiConsumer)
|
||||
*/
|
||||
public MessageBuilder<MSG> consumer(ToBooleanBiFunction<MSG, Supplier<NetworkEvent.Context>> handler) {
|
||||
public MessageBuilder<MSG> consumerNetworkThread(ToBooleanBiFunction<MSG, Supplier<NetworkEvent.Context>> handler) {
|
||||
this.consumer = (msg, ctx) -> {
|
||||
boolean handled = handler.applyAsBool(msg, ctx);
|
||||
ctx.get().setPacketHandled(handled);
|
||||
@@ -245,6 +305,18 @@ public class SimpleChannel
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message consumer, which is called once a message has been decoded.
|
||||
* @param handler The message consumer.
|
||||
* @return The message builder, for chaining.
|
||||
* @deprecated Use {@link #consumerMainThread(BiConsumer)} or {@link #consumerNetworkThread(BiConsumer)}.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.19")
|
||||
public MessageBuilder<MSG> consumer(ToBooleanBiFunction<MSG, Supplier<NetworkEvent.Context>> handler) {
|
||||
consumerMainThread(handler::applyAsBool);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void add() {
|
||||
final IndexedMessageCodec.MessageHandler<MSG> message = this.channel.registerMessage(this.id, this.type, this.encoder, this.decoder, this.consumer, this.networkDirection);
|
||||
if (this.loginIndexSetter != null) {
|
||||
|
||||
Reference in new issue
Block a user