Skip to content

Commit

Permalink
Network refactoring (neoforged#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans authored Dec 31, 2023
1 parent 9ae1def commit bd3a222
Show file tree
Hide file tree
Showing 174 changed files with 6,391 additions and 5,253 deletions.
10 changes: 1 addition & 9 deletions patches/net/minecraft/client/Minecraft.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,10 @@
try {
this.updateScreenAndTick(p_91321_);
if (this.level != null) {
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.level.LevelEvent.Unload(this.level));
+ net.neoforged.neoforge.common.NeoForge.EVENT_BUS.post(new net.neoforged.neoforge.event.level.LevelEvent.Unload(this.level));
if (integratedserver != null) {
this.profiler.push("waitForServer");

@@ -2176,6 +_,7 @@

this.gui.onDisconnected();
this.isLocalServer = false;
+ net.neoforged.neoforge.client.ClientHooks.handleClientLevelClosing(this.level);
}

this.level = null;
@@ -2324,6 +_,7 @@

private void pickBlock() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
--- a/net/minecraft/client/multiplayer/ClientCommonPacketListenerImpl.java
+++ b/net/minecraft/client/multiplayer/ClientCommonPacketListenerImpl.java
@@ -101,12 +_,12 @@
@@ -96,17 +_,17 @@
public void handleCustomPayload(ClientboundCustomPayloadPacket p_295727_) {
CustomPacketPayload custompacketpayload = p_295727_.payload();
if (!(custompacketpayload instanceof DiscardedPayload)) {
- PacketUtils.ensureRunningOnSameThread(p_295727_, this, this.minecraft);
if (custompacketpayload instanceof BrandPayload brandpayload) {
+ PacketUtils.ensureRunningOnSameThread(p_295727_, this, this.minecraft); //Neo: We move this here to ensure that only vanilla packets are handled on the main thread.
this.serverBrand = brandpayload.brand();
this.telemetryManager.onServerBrandReceived(brandpayload.brand());
} else {
Expand All @@ -15,3 +21,40 @@

protected abstract RegistryAccess.Frozen registryAccess();

@@ -189,6 +_,10 @@
}

public void send(Packet<?> p_295097_) {
+ if (!net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().canSendPacket(p_295097_, this)) {
+ return;
+ }
+
this.connection.send(p_295097_);
}

@@ -196,6 +_,9 @@
public void onDisconnect(Component p_295485_) {
this.telemetryManager.onDisconnect();
this.minecraft.disconnect(this.createDisconnectScreen(p_295485_));
+ if (!this.connection.isMemoryConnection()) {
+ net.neoforged.neoforge.registries.RegistryManager.revertToFrozen();
+ }
LOGGER.warn("Client disconnected with reason: {}", p_295485_.getString());
}

@@ -315,5 +_,15 @@
@OnlyIn(Dist.CLIENT)
static record PendingRequest(UUID id, URL url, String hash) {
}
+ }
+
+ @Override
+ public Connection getConnection() {
+ return connection;
+ }
+
+ @Override
+ public Minecraft getMinecraft() {
+ return minecraft;
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,81 @@
--- a/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java
+++ b/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java
@@ -45,11 +_,15 @@
@@ -26,6 +_,9 @@
private final GameProfile localGameProfile;
private RegistryAccess.Frozen receivedRegistries;
private FeatureFlagSet enabledFeatures;
+ private boolean isModdedConnection = false;
+ private boolean isVanillaConnection = true;
+ private java.util.Map<net.minecraft.resources.ResourceLocation, net.minecraft.network.chat.Component> failureReasons = new java.util.HashMap<>();

public ClientConfigurationPacketListenerImpl(Minecraft p_295262_, Connection p_296339_, CommonListenerCookie p_294706_) {
super(p_295262_, p_296339_, p_294706_);
@@ -45,7 +_,30 @@
}

@Override
- protected void handleCustomPayload(CustomPacketPayload p_295411_) {
- this.handleUnknownCustomPayload(p_295411_);
+ protected void handleCustomPayload(net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket p_295727_, CustomPacketPayload p_295411_) {
+ this.handleUnknownCustomPayload(p_295727_, p_295411_);
}

- private void handleUnknownCustomPayload(CustomPacketPayload p_296412_) {
+ private void handleUnknownCustomPayload(net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket p_295727_, CustomPacketPayload p_296412_) {
+ if (p_296412_ instanceof net.neoforged.neoforge.network.custom.payload.SimplePayload simplePayload && net.neoforged.neoforge.network.NetworkHooks.onCustomPayload(p_295727_, simplePayload, connection)) {
+ if (p_295411_ instanceof net.neoforged.neoforge.network.payload.ModdedNetworkQueryPayload) {
+ this.isModdedConnection = true;
+ net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onNetworkQuery(this);
+ return;
+ }
+ if (p_295411_ instanceof net.neoforged.neoforge.network.payload.ModdedNetworkPayload moddedNetworkPayload) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onModdedNetworkConnectionEstablished(this, moddedNetworkPayload.configuration(), moddedNetworkPayload.play());
+ return;
+ }
+ if (p_295411_ instanceof net.neoforged.neoforge.network.payload.ModdedNetworkSetupFailedPayload setupFailedPayload) {
+ failureReasons = setupFailedPayload.failureReasons();
+ }
+ if (!this.isModdedConnection && p_295411_ instanceof net.minecraft.network.protocol.common.custom.BrandPayload) {
+ this.isVanillaConnection = true;
+ if (!net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onVanillaNetworkConnectionEstablished(this)) {
+ return;
+ }
+ }
+ if (this.isModdedConnection) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onModdedPacketAtClient(this, p_295727_);
+ return;
+ }
+
LOGGER.warn("Unknown custom packet payload: {}", p_296412_.id());
this.handleUnknownCustomPayload(p_295411_);
}

@@ -93,6 +_,7 @@
);
this.connection.resumeInboundAfterProtocolChange();
this.connection.send(new ServerboundFinishConfigurationPacket());
+ net.neoforged.neoforge.network.NetworkHooks.handleClientLoginSuccess(this.connection);
@@ -69,6 +_,11 @@
@Override
public void handleEnabledFeatures(ClientboundUpdateEnabledFeaturesPacket p_294410_) {
this.enabledFeatures = FeatureFlags.REGISTRY.fromNames(p_294410_.features());
+ //Fallback detection layer for vanilla servers
+ if (!this.isModdedConnection) {
+ this.isVanillaConnection = true;
+ net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onVanillaNetworkConnectionEstablished(this);
+ }
}

@Override
@@ -87,7 +_,8 @@
this.enabledFeatures,
this.serverBrand,
this.serverData,
- this.postDisconnectScreen
+ this.postDisconnectScreen,
+ this.isModdedConnection
)
)
);
@@ -104,5 +_,14 @@
public void onDisconnect(Component p_314649_) {
super.onDisconnect(p_314649_);
this.minecraft.clearDownloadedResourcePacks();
+ }
+
+ @Override
+ protected net.minecraft.client.gui.screens.Screen createDisconnectScreen(net.minecraft.network.chat.Component p_296470_) {
+ final net.minecraft.client.gui.screens.Screen superScreen = super.createDisconnectScreen(p_296470_);
+ if (failureReasons.isEmpty())
+ return superScreen;
+
+ return new net.neoforged.neoforge.client.gui.ModMismatchDisconnectedScreen(superScreen, p_296470_, failureReasons);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
--- a/net/minecraft/client/multiplayer/ClientPacketListener.java
+++ b/net/minecraft/client/multiplayer/ClientPacketListener.java
@@ -340,6 +_,7 @@
private MessageSignatureCache messageSignatureCache = MessageSignatureCache.createDefault();
private final ChunkBatchSizeCalculator chunkBatchSizeCalculator = new ChunkBatchSizeCalculator();
private final PingDebugMonitor pingDebugMonitor;
+ private final boolean isModdedConnection;
@Nullable
private LevelLoadStatusManager levelLoadStatusManager;
private boolean seenInsecureChatWarning = false;
@@ -353,6 +_,7 @@
this.advancements = new ClientAdvancements(p_253924_, this.telemetryManager);
this.suggestionsProvider = new ClientSuggestionProvider(this, p_253924_);
this.pingDebugMonitor = new PingDebugMonitor(this, p_253924_.getDebugOverlay().getPingLogger());
+ this.isModdedConnection = p_295121_.isModdedConnection();
}

public ClientSuggestionProvider getSuggestionsProvider() {
@@ -416,6 +_,7 @@

this.minecraft.debugRenderer.clear();
Expand All @@ -8,14 +24,16 @@
this.minecraft.player.setId(p_105030_.playerId());
this.level.addEntity(this.minecraft.player);
this.minecraft.player.input = new KeyboardInput(this.minecraft.options);
@@ -429,6 +_,7 @@
this.minecraft.player.setPortalCooldown(commonplayerspawninfo.portalCooldown());
this.minecraft.gameMode.setLocalMode(commonplayerspawninfo.gameType(), commonplayerspawninfo.previousGameType());
this.minecraft.options.setServerRenderDistance(p_105030_.chunkRadius());
+ net.neoforged.neoforge.network.NetworkHooks.sendMCRegistryPackets(connection, net.neoforged.neoforge.network.PlayNetworkDirection.PLAY_TO_SERVER);
this.chatSession = null;
this.lastSeenMessages = new LastSeenMessagesTracker(20);
this.messageSignatureCache = MessageSignatureCache.createDefault();
@@ -801,7 +_,8 @@
this.enabledFeatures,
this.serverBrand,
this.serverData,
- this.postDisconnectScreen
+ this.postDisconnectScreen,
+ this.isModdedConnection
)
)
);
@@ -1143,6 +_,7 @@
}

Expand Down Expand Up @@ -65,33 +83,28 @@
}

@Override
@@ -1916,7 +_,7 @@
@@ -1916,7 +_,11 @@
}

@Override
- public void handleCustomPayload(CustomPacketPayload p_295851_) {
+ public void handleCustomPayload(net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket p_295727_, CustomPacketPayload p_295851_) {
+ if (this.isModdedConnection && net.neoforged.neoforge.network.registration.NetworkRegistry.getInstance().onModdedPacketAtClient(this, p_295727_)) {
+ return;
+ }
+ PacketUtils.ensureRunningOnSameThread(p_295727_, this, this.minecraft); //Neo: We move this here to ensure that only vanilla packets are handled on the main thread.
if (p_295851_ instanceof PathfindingDebugPayload pathfindingdebugpayload) {
this.minecraft
.debugRenderer
@@ -1985,11 +_,15 @@
} else if (p_295851_ instanceof BreezeDebugPayload breezedebugpayload) {
this.minecraft.debugRenderer.breezeDebugRenderer.add(breezedebugpayload.breezeInfo());
} else {
- this.handleUnknownCustomPayload(p_295851_);
+ this.handleUnknownCustomPayload(p_295727_, p_295851_);
}
}
@@ -2267,7 +_,7 @@
public void handleBundlePacket(ClientboundBundlePacket p_265195_) {
PacketUtils.ensureRunningOnSameThread(p_265195_, this, this.minecraft);

- private void handleUnknownCustomPayload(CustomPacketPayload p_294389_) {
+ private void handleUnknownCustomPayload(net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket p_295727_, CustomPacketPayload p_294389_) {
+ if (p_294389_ instanceof net.neoforged.neoforge.network.custom.payload.SimplePayload simplePayload && net.neoforged.neoforge.network.NetworkHooks.onCustomPayload(p_295727_, simplePayload, connection)) {
+ return;
+ }
+
LOGGER.warn("Unknown custom packet payload: {}", p_294389_.id());
- for(Packet<ClientGamePacketListener> packet : p_265195_.subPackets()) {
+ for(Packet<? super ClientGamePacketListener> packet : p_265195_.subPackets()) {
packet.handle(this);
}
}

@@ -2389,6 +_,8 @@
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- a/net/minecraft/client/multiplayer/CommonListenerCookie.java
+++ b/net/minecraft/client/multiplayer/CommonListenerCookie.java
@@ -17,6 +_,15 @@
FeatureFlagSet enabledFeatures,
@Nullable String serverBrand,
@Nullable ServerData serverData,
- @Nullable Screen postDisconnectScreen
+ @Nullable Screen postDisconnectScreen,
+ boolean isModdedConnection
) {
+
+ /**
+ * @deprecated Use {@link #CommonListenerCookie(GameProfile, WorldSessionTelemetryManager, RegistryAccess.Frozen, FeatureFlagSet, String, ServerData, Screen, boolean)} instead, to indicate whether the connection is modded.
+ */
+ @Deprecated
+ public CommonListenerCookie(GameProfile localGameProfile, WorldSessionTelemetryManager telemetryManager, RegistryAccess.Frozen receivedRegistries, FeatureFlagSet enabledFeatures, @Nullable String serverBrand, @Nullable ServerData serverData, @Nullable Screen postDisconnectScreen) {
+ this(localGameProfile, telemetryManager, receivedRegistries, enabledFeatures, serverBrand, serverData, postDisconnectScreen, false);
+ }
}

This file was deleted.

37 changes: 14 additions & 23 deletions patches/net/minecraft/network/Connection.java.patch
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
--- a/net/minecraft/network/Connection.java
+++ b/net/minecraft/network/Connection.java
@@ -97,6 +_,7 @@
private volatile Component delayedDisconnect;
@Nullable
BandwidthDebugMonitor bandwidthDebugMonitor;
+ private java.util.function.Consumer<Connection> activationHandler;

public Connection(PacketFlow p_129482_) {
this.receiving = p_129482_;
@@ -107,6 +_,7 @@
super.channelActive(p_129525_);
this.channel = p_129525_.channel();
this.address = this.channel.remoteAddress();
+ if (activationHandler != null) activationHandler.accept(this);
@@ -110,6 +_,7 @@
if (this.delayedDisconnect != null) {
this.disconnect(this.delayedDisconnect);
}
+ net.neoforged.neoforge.network.connection.ConnectionUtils.setConnection(p_129525_, this);
}

public static void setInitialProtocolAttributes(Channel p_294962_) {
@@ -120,6 +_,7 @@
@Override
public void channelInactive(ChannelHandlerContext p_129527_) {
this.disconnect(Component.translatable("disconnect.endOfStream"));
+ net.neoforged.neoforge.network.connection.ConnectionUtils.removeConnection(p_129527_);
}

@Override
@@ -380,7 +_,7 @@
if (this.address == null) {
return "local";
Expand All @@ -25,23 +25,14 @@
}
}

@@ -419,6 +_,8 @@
@@ -419,6 +_,7 @@
}

public static ChannelFuture connect(InetSocketAddress p_290034_, boolean p_290035_, final Connection p_290031_) {
+ net.neoforged.neoforge.network.DualStackUtils.checkIPv6(p_290034_.getAddress());
+ p_290031_.activationHandler = net.neoforged.neoforge.network.NetworkHooks::registerClientLoginChannel;
Class<? extends SocketChannel> oclass;
EventLoopGroup eventloopgroup;
if (Epoll.isAvailable() && p_290035_) {
@@ -475,6 +_,7 @@

public static Connection connectToLocalServer(SocketAddress p_129494_) {
final Connection connection = new Connection(PacketFlow.CLIENTBOUND);
+ connection.activationHandler = net.neoforged.neoforge.network.NetworkHooks::registerClientLoginChannel;
new Bootstrap().group(LOCAL_WORKER_GROUP.get()).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel p_129557_) {
@@ -569,6 +_,14 @@

public float getAverageSentPackets() {
Expand Down
Loading

0 comments on commit bd3a222

Please sign in to comment.