From 0b756d95f7581a0e4fc2c2181a066be38c01f199 Mon Sep 17 00:00:00 2001 From: Fallen_Breath Date: Sun, 2 Jun 2024 01:55:12 +0800 Subject: [PATCH] optimized `connectionSimulatedDelay` impl, make it add delay for both read / write --- .../ChannelInboundDelayer.java | 38 ++++++++++++++++++ .../ChannelOutboundDelayer.java | 39 +++++++++++++++++++ ...ChannelDelayer.java => DelayedRunner.java} | 27 +++++-------- ...entConnection_ChannelInitializerMixin.java | 6 ++- .../assets/tweakermore/lang/en_us.yml | 2 +- .../assets/tweakermore/lang/zh_cn.yml | 2 +- 6 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelInboundDelayer.java create mode 100644 src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelOutboundDelayer.java rename src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/{ChannelDelayer.java => DelayedRunner.java} (62%) diff --git a/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelInboundDelayer.java b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelInboundDelayer.java new file mode 100644 index 00000000..be3e57e7 --- /dev/null +++ b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelInboundDelayer.java @@ -0,0 +1,38 @@ +/* + * This file is part of the TweakerMore project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * TweakerMore is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * TweakerMore is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with TweakerMore. If not, see . + */ + +package me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +public class ChannelInboundDelayer extends ChannelInboundHandlerAdapter +{ + private final DelayedRunner delayedRunner = new DelayedRunner(); + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) + { + this.delayedRunner.delayedRun( + delay -> delay / 2, + () -> ctx.fireChannelRead(msg) + ); + } +} diff --git a/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelOutboundDelayer.java b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelOutboundDelayer.java new file mode 100644 index 00000000..9417770e --- /dev/null +++ b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelOutboundDelayer.java @@ -0,0 +1,39 @@ +/* + * This file is part of the TweakerMore project, licensed under the + * GNU Lesser General Public License v3.0 + * + * Copyright (C) 2023 Fallen_Breath and contributors + * + * TweakerMore is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * TweakerMore is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with TweakerMore. If not, see . + */ + +package me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; + +public class ChannelOutboundDelayer extends ChannelOutboundHandlerAdapter +{ + private final DelayedRunner delayedRunner = new DelayedRunner(); + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) + { + this.delayedRunner.delayedRun( + delay -> delay - delay / 2, + () -> ctx.write(msg, promise) + ); + } +} diff --git a/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelDelayer.java b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/DelayedRunner.java similarity index 62% rename from src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelDelayer.java rename to src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/DelayedRunner.java index 69edb530..4d2f4097 100644 --- a/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/ChannelDelayer.java +++ b/src/main/java/me/fallenbreath/tweakermore/impl/mc_tweaks/connectionSimulatedDelay/DelayedRunner.java @@ -2,7 +2,7 @@ * This file is part of the TweakerMore project, licensed under the * GNU Lesser General Public License v3.0 * - * Copyright (C) 2023 Fallen_Breath and contributors + * Copyright (C) 2024 Fallen_Breath and contributors * * TweakerMore is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -21,39 +21,32 @@ package me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay; import com.google.common.collect.Queues; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.HashedWheelTimer; -import io.netty.util.Timeout; import io.netty.util.Timer; import me.fallenbreath.tweakermore.config.TweakerMoreConfigs; import java.util.Queue; import java.util.concurrent.TimeUnit; +import java.util.function.Function; -public class ChannelDelayer extends ChannelInboundHandlerAdapter +class DelayedRunner { - private static final Timer TIMER = new HashedWheelTimer(); + private final Timer timer = new HashedWheelTimer(10, TimeUnit.MILLISECONDS); private final Queue queue = Queues.newConcurrentLinkedQueue(); - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) + public void delayedRun(Function delayTransformer, Runnable task) { - Runnable task = () -> ctx.fireChannelRead(msg); - if (TweakerMoreConfigs.CONNECTION_SIMULATED_DELAY.isModified() || !this.queue.isEmpty()) + int optionValue = TweakerMoreConfigs.CONNECTION_SIMULATED_DELAY.getIntegerValue(); + int delayMs = delayTransformer.apply(optionValue); + + if ((delayMs > 0 && TweakerMoreConfigs.CONNECTION_SIMULATED_DELAY.isModified()) || !this.queue.isEmpty()) { this.queue.add(task); - int delayMs = TweakerMoreConfigs.CONNECTION_SIMULATED_DELAY.getIntegerValue(); - TIMER.newTimeout(this::actualChannelRead, delayMs, TimeUnit.MILLISECONDS); + this.timer.newTimeout(t -> this.queue.remove().run(), delayMs, TimeUnit.MILLISECONDS); } else { task.run(); } } - - private void actualChannelRead(Timeout timeout) - { - this.queue.remove().run(); - } } diff --git a/src/main/java/me/fallenbreath/tweakermore/mixins/tweaks/mc_tweaks/connectionSimulatedDelay/ClientConnection_ChannelInitializerMixin.java b/src/main/java/me/fallenbreath/tweakermore/mixins/tweaks/mc_tweaks/connectionSimulatedDelay/ClientConnection_ChannelInitializerMixin.java index 229dc60d..7765af54 100644 --- a/src/main/java/me/fallenbreath/tweakermore/mixins/tweaks/mc_tweaks/connectionSimulatedDelay/ClientConnection_ChannelInitializerMixin.java +++ b/src/main/java/me/fallenbreath/tweakermore/mixins/tweaks/mc_tweaks/connectionSimulatedDelay/ClientConnection_ChannelInitializerMixin.java @@ -21,7 +21,8 @@ package me.fallenbreath.tweakermore.mixins.tweaks.mc_tweaks.connectionSimulatedDelay; import io.netty.channel.Channel; -import me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay.ChannelDelayer; +import me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay.ChannelInboundDelayer; +import me.fallenbreath.tweakermore.impl.mc_tweaks.connectionSimulatedDelay.ChannelOutboundDelayer; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -36,6 +37,7 @@ public abstract class ClientConnection_ChannelInitializerMixin @Inject(method = "initChannel(Lio/netty/channel/Channel;)V", at = @At("HEAD")) private void connectionSimulatedDelay(Channel channel, CallbackInfo ci) { - channel.pipeline().addLast(new ChannelDelayer()); + channel.pipeline().addLast(new ChannelInboundDelayer()); + channel.pipeline().addLast(new ChannelOutboundDelayer()); } } diff --git a/src/main/resources/assets/tweakermore/lang/en_us.yml b/src/main/resources/assets/tweakermore/lang/en_us.yml index aac2cc3f..a7d39a39 100644 --- a/src/main/resources/assets/tweakermore/lang/en_us.yml +++ b/src/main/resources/assets/tweakermore/lang/en_us.yml @@ -418,7 +418,7 @@ tweakermore: .: connectionSimulatedDelay comment: |- Client network delay simulator. Enabled when the value is greater than 0 - Adds given delay (in milliseconds) before any packet processing + Adds half of given delay (in milliseconds) before packet read / write, so the total delay increases with the given value Basically it stably adds your ping to the server with the given value daytimeOverride: .: daytimeOverride diff --git a/src/main/resources/assets/tweakermore/lang/zh_cn.yml b/src/main/resources/assets/tweakermore/lang/zh_cn.yml index 3b6de01c..517dbd5e 100644 --- a/src/main/resources/assets/tweakermore/lang/zh_cn.yml +++ b/src/main/resources/assets/tweakermore/lang/zh_cn.yml @@ -418,7 +418,7 @@ tweakermore: .: 网络连接延迟模拟 comment: |- 客户端网络延迟模拟器,于给定值大于0时启用 - 在任何数据包处理前插入给定的延迟 (单位毫秒) + 在任何数据包的发送/接受前,延迟给定毫秒的一半,因此总延迟增加将为给定的值 可以认为它会稳定地将你的网络延迟ping值增加给定的值 daytimeOverride: .: 覆盖世界时间