Skip to content

Commit

Permalink
Improve ping collection: QUIC ping is not working yet, netty/netty-in…
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Mar 2, 2024
1 parent 0ada167 commit ce00c07
Showing 1 changed file with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
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 @@ -56,7 +61,7 @@ public TransportClientConnection(ProxiedPlayer player, ServerInfo serverInfo, Ch
this.channel = channel;
this.channel.closeFuture().addListener(future -> cleanActiveChannels());

scheduledTasks.add(channel.eventLoop().scheduleAtFixedRate(this::sendAcknowledge, PING_CYCLE_TIME, PING_CYCLE_TIME, TimeUnit.SECONDS));
scheduledTasks.add(channel.eventLoop().scheduleAtFixedRate(this::collectStats, PING_CYCLE_TIME, PING_CYCLE_TIME, TimeUnit.SECONDS));
scheduledTasks.add(channel.eventLoop().scheduleAtFixedRate(() -> packetSendingLimit.set(0), 1, 1, TimeUnit.SECONDS));
}

Expand Down Expand Up @@ -100,7 +105,8 @@ private void onBedrockBatch(@NonNull BedrockBatchWrapper batch) {
wrapper.release(); // release
batch.modify();

receiveAcknowledge();
this.latency = (System.currentTimeMillis() - this.lastPingTimestamp) / 2;
this.broadcastPing();
}
}
}
Expand Down Expand Up @@ -165,25 +171,37 @@ public long getPing() {
return latency;
}

public void sendAcknowledge() {
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);

lastPingTimestamp = System.currentTimeMillis();
if (this.channel instanceof NioSocketChannel) {
NetworkStackLatencyPacket packet = new NetworkStackLatencyPacket();
packet.setTimestamp(0L);
packet.setFromServer(true);

sendPacket(packet);

this.lastPingTimestamp = System.currentTimeMillis();
} 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();
}
});
}
}
}

private void receiveAcknowledge() {
latency = (System.currentTimeMillis() - lastPingTimestamp) / 2;

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

sendPacket(latencyPacket);
}
Expand Down

0 comments on commit ce00c07

Please sign in to comment.