Skip to content

Commit

Permalink
Switch back to miliseconds & use NetworkStackLatencyPacket again for …
Browse files Browse the repository at this point in the history
…ping
  • Loading branch information
dries-c committed Mar 5, 2024
1 parent 203461a commit 461d39a
Showing 1 changed file with 7 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.incubator.codec.quic.QuicChannel;
import io.netty.incubator.codec.quic.QuicConnectionStats;
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 @@ -168,39 +163,25 @@ public void enableEncryption(SecretKey secretKey) {

@Override
public long getPing() {
return latency;
return this.latency / 1000;
}

public void collectStats() {
var connection = getPlayer().getDownstreamConnection();
if (connection instanceof TransportClientConnection && connection.getServerInfo().getServerName().equalsIgnoreCase(getServerInfo().getServerName())) {
if (this.channel instanceof NioSocketChannel) {
NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket();
packet.setTimestamp(0L);
packet.setFromServer(true);
NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket();
packet.setTimestamp(0L);
packet.setFromServer(true);

sendPacket(packet);
sendPacket(packet);

this.lastPingTimestamp = System.nanoTime();
} else if (this.channel instanceof EpollSocketChannel epollChannel) {
this.latency = epollChannel.tcpInfo().rtt() / 2;
this.broadcastPing();
} else if (this.channel instanceof QuicChannel quicChannel) {
quicChannel.collectStats().addListener((Future<QuicConnectionStats> future) -> {
if (future.isSuccess()) {
QuicConnectionStats stats = future.getNow();

this.latency = stats.recv();
this.broadcastPing();
}
});
}
this.lastPingTimestamp = System.nanoTime();
}
}

private void broadcastPing() {
TickSyncPacket latencyPacket = new TickSyncPacket();
latencyPacket.setRequestTimestamp(getPlayer().getPing() * 1000);
latencyPacket.setRequestTimestamp(getPlayer().getPing());
latencyPacket.setResponseTimestamp(this.latency);

sendPacket(latencyPacket);
Expand Down

0 comments on commit 461d39a

Please sign in to comment.