diff --git a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java index 06a0afd5..86461043 100644 --- a/proxy/src/main/java/net/md_5/bungee/ServerConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/ServerConnection.java @@ -3,6 +3,9 @@ package net.md_5.bungee; import com.google.common.base.Preconditions; import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.util.LinkedList; +import java.util.Queue; +import lombok.Data; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; @@ -26,8 +29,7 @@ public class ServerConnection implements Server @Getter private final boolean forgeServer = false; @Getter - @Setter - private long sentPingId = -1; + private final Queue keepAlives = new LinkedList<>(); private final Unsafe unsafe = new Unsafe() { @@ -87,4 +89,12 @@ public class ServerConnection implements Server { return unsafe; } + + @Data + public static class KeepAliveData + { + + private final long id; + private final long time; + } } diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 3d7e9043..afcae3a2 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -93,9 +93,6 @@ public final class UserConnection implements ProxiedPlayer /*========================================================================*/ @Getter @Setter - private long sentPingTime; - @Getter - @Setter private int ping = 100; @Getter @Setter diff --git a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java index b66712d2..e53a3ff9 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/DownstreamBridge.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import lombok.RequiredArgsConstructor; import net.md_5.bungee.ServerConnection; +import net.md_5.bungee.ServerConnection.KeepAliveData; import net.md_5.bungee.UserConnection; import net.md_5.bungee.Util; import net.md_5.bungee.api.ProxyServer; @@ -124,8 +125,10 @@ public class DownstreamBridge extends PacketHandler @Override public void handle(KeepAlive alive) throws Exception { - server.setSentPingId( alive.getRandomId() ); - con.setSentPingTime( System.currentTimeMillis() ); + if ( server.getKeepAlives().size() < bungee.getConfig().getTimeout() / 50 ) // Allow a theoretical maximum of 1 keepalive per tick + { + server.getKeepAlives().add( new KeepAliveData( alive.getRandomId(), System.currentTimeMillis() ) ); + } } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index 540cf213..6b888c85 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import net.md_5.bungee.BungeeCord; +import net.md_5.bungee.ServerConnection.KeepAliveData; import net.md_5.bungee.UserConnection; import net.md_5.bungee.Util; import net.md_5.bungee.api.ProxyServer; @@ -121,9 +122,11 @@ public class UpstreamBridge extends PacketHandler @Override public void handle(KeepAlive alive) throws Exception { - if ( alive.getRandomId() == con.getServer().getSentPingId() ) + KeepAliveData keepAliveData = con.getServer().getKeepAlives().poll(); + + if ( keepAliveData != null && alive.getRandomId() == keepAliveData.getId() ) { - int newPing = (int) ( System.currentTimeMillis() - con.getSentPingTime() ); + int newPing = (int) ( System.currentTimeMillis() - keepAliveData.getTime() ); con.getTabListHandler().onPingChange( newPing ); con.setPing( newPing ); } else