Skip to content

Commit

Permalink
optimized connectionSimulatedDelay impl, make it add delay for both…
Browse files Browse the repository at this point in the history
… read / write
  • Loading branch information
Fallen-Breath committed Jun 1, 2024
1 parent c6e0ac0 commit 0b756d9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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)
);
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Runnable> queue = Queues.newConcurrentLinkedQueue();

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
public void delayedRun(Function<Integer, Integer> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/assets/tweakermore/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/assets/tweakermore/lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ tweakermore:
.: 网络连接延迟模拟
comment: |-
客户端网络延迟模拟器,于给定值大于0时启用
在任何数据包处理前插入给定的延迟 (单位毫秒)
在任何数据包的发送/接受前,延迟给定毫秒的一半,因此总延迟增加将为给定的值
可以认为它会稳定地将你的网络延迟ping值增加给定的值
daytimeOverride:
.: 覆盖世界时间
Expand Down

0 comments on commit 0b756d9

Please sign in to comment.