From 2e0d5526c430d9ff8f31ab91c4407f73ab08f585 Mon Sep 17 00:00:00 2001 From: Dries C Date: Tue, 26 Mar 2024 17:58:11 +0100 Subject: [PATCH] Use QUIC RTT for reporting --- pom.xml | 2 +- .../impl/TransportClientConnection.java | 35 +++++++++++++------ .../integration/QuicTransportServerInfo.java | 1 - 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 1d4e8db..3147ad5 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ io.netty.incubator netty-incubator-codec-native-quic - 0.0.60.Final + 0.0.62.Final linux-x86_64 diff --git a/src/main/java/org/nethergames/proxytransport/impl/TransportClientConnection.java b/src/main/java/org/nethergames/proxytransport/impl/TransportClientConnection.java index 1a5b212..cf0ee20 100644 --- a/src/main/java/org/nethergames/proxytransport/impl/TransportClientConnection.java +++ b/src/main/java/org/nethergames/proxytransport/impl/TransportClientConnection.java @@ -10,6 +10,9 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; +import io.netty.incubator.codec.quic.QuicChannel; +import io.netty.incubator.codec.quic.QuicConnectionPathStats; +import io.netty.util.concurrent.Future; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec; @@ -45,8 +48,8 @@ public class TransportClientConnection extends BedrockClientConnection { private final AtomicBoolean packetSendingLock = new AtomicBoolean(false); // Lock packets from being sent to downstream servers. private final Channel channel; - private long lastPingTimestamp; - private long latency; // Latency in microseconds + private long lastPingTimestamp = -1; + private long latency = 0; // Latency in microseconds private final List> scheduledTasks = new ArrayList<>(); @@ -100,8 +103,10 @@ private void onBedrockBatch(@NonNull BedrockBatchWrapper batch) { wrapper.release(); // release batch.modify(); - this.latency = (System.nanoTime() - this.lastPingTimestamp) / 1000; - this.broadcastPing(); + if (this.lastPingTimestamp != -1) { + this.latency = (System.nanoTime() - this.lastPingTimestamp) / 1000; + this.broadcastPing(); + } } } } @@ -173,13 +178,21 @@ public long getMicroSecondsPing() { public void collectStats() { var connection = getPlayer().getDownstreamConnection(); if (connection instanceof TransportClientConnection && connection.getServerInfo().getServerName().equalsIgnoreCase(getServerInfo().getServerName())) { - NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket(); - packet.setTimestamp(0L); - packet.setFromServer(true); - - sendPacket(packet); - - this.lastPingTimestamp = System.nanoTime(); + if (this.channel instanceof QuicChannel quicChannel) { + quicChannel.collectPathStats(0).addListener((Future quicChannelFuture) -> { + if (quicChannelFuture.isSuccess()) { + this.latency = quicChannelFuture.getNow().rtt() / 1000; // convert to nanoseconds to microseconds + } + }); + } else { + NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket(); + packet.setTimestamp(0L); + packet.setFromServer(true); + + sendPacket(packet); + + this.lastPingTimestamp = System.nanoTime(); + } } } diff --git a/src/main/java/org/nethergames/proxytransport/integration/QuicTransportServerInfo.java b/src/main/java/org/nethergames/proxytransport/integration/QuicTransportServerInfo.java index fba29d0..ff06dc3 100644 --- a/src/main/java/org/nethergames/proxytransport/integration/QuicTransportServerInfo.java +++ b/src/main/java/org/nethergames/proxytransport/integration/QuicTransportServerInfo.java @@ -105,7 +105,6 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { } }) .remoteAddress(address) - .option(QuicChannelOption.QLOG, new QLogConfiguration("/" + this.getServerName() + "-" + System.currentTimeMillis() + ".qlog", this.getServerName(), this.getServerName())) .connect().addListener((Future quicChannelFuture) -> { if (quicChannelFuture.isSuccess()) { logger.debug("Connection to " + address + " for " + this.getServerName() + " server established");