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
TobiasGrether authored and dries-c committed Mar 5, 2024
1 parent dcfb59f commit 4e3b69d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.nethergames.proxytransport</groupId>
<artifactId>proxy-transport</artifactId>
<version>2.0.5-SNAPSHOT</version>
<version>2.0.5</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.waterdog.waterdogpe.network.protocol.ProtocolCodecs;
import dev.waterdog.waterdogpe.plugin.Plugin;
import io.netty.incubator.codec.quic.Quic;
import org.nethergames.proxytransport.integration.QuicTransportServerInfo;
import org.nethergames.proxytransport.integration.TcpTransportServerInfo;
import org.nethergames.proxytransport.utils.CodecUpdater;
Expand All @@ -11,12 +12,16 @@ public class ProxyTransport extends Plugin {
@Override
public void onStartup() {
ProtocolCodecs.addUpdater(new CodecUpdater());
getLogger().info("Registered architecture: {} | {}", System.getProperty("os.name"), System.getProperty("os.arch"));

getLogger().info("System Properties: {}", System.getProperties().toString());
try {
Class.forName("io.netty.incubator.codec.quic.QuicheNativeStaticallyReferencedJniMethods");
Quic.ensureAvailability();
getLogger().info("QUIC is supported");
} catch (ClassNotFoundException e) {
} catch (Exception e) {
getLogger().info("QUIC is not supported / shading failed");
getLogger().error(e);
}

getLogger().info("ProxyTransport was started.");
Expand Down
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,40 +163,30 @@ public void enableEncryption(SecretKey secretKey) {

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

public long getMicroSecondsPing() {
return this.latency;
}

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.setResponseTimestamp(this.latency);
latencyPacket.setRequestTimestamp(getPlayer().getPing());
latencyPacket.setResponseTimestamp(this.getPing());

sendPacket(latencyPacket);
}
Expand Down

0 comments on commit 4e3b69d

Please sign in to comment.