Skip to content

Commit

Permalink
Use QUIC RTT for reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Mar 27, 2024
1 parent c78de57 commit d8e6433
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-codec-native-quic</artifactId>
<version>0.0.60.Final</version>
<version>0.0.62.Final</version>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.QuicConnectionPathStats;
import io.netty.incubator.codec.quic.QuicStreamChannel;
import io.netty.util.concurrent.Future;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
Expand Down Expand Up @@ -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<ScheduledFuture<?>> scheduledTasks = new ArrayList<>();

Expand Down Expand Up @@ -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();
}
}
}
}
Expand Down Expand Up @@ -173,13 +178,22 @@ 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 QuicStreamChannel quicChannel) {
quicChannel.parent().collectPathStats(0).addListener((Future<QuicConnectionPathStats> quicChannelFuture) -> {
if (quicChannelFuture.isSuccess()) {
this.latency = quicChannelFuture.getNow().rtt() / 1000; // convert to nanoseconds to microseconds
this.broadcastPing();
}
});
} else {
NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket();
packet.setTimestamp(0L);
packet.setFromServer(true);

sendPacket(packet);

this.lastPingTimestamp = System.nanoTime();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuicChannel> quicChannelFuture) -> {
if (quicChannelFuture.isSuccess()) {
logger.debug("Connection to " + address + " for " + this.getServerName() + " server established");
Expand Down

0 comments on commit d8e6433

Please sign in to comment.