#2710: Store queue of pending keepalives

This allows the server to send them at a rate greater than the client reply time.
This commit is contained in:
polo1k 2020-01-24 13:13:01 +11:00 committed by md_5
parent 3f6aa0336c
commit eeb374798b
4 changed files with 22 additions and 9 deletions

View File

@ -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<KeepAliveData> 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;
}
}

View File

@ -93,9 +93,6 @@ public final class UserConnection implements ProxiedPlayer
/*========================================================================*/
@Getter
@Setter
private long sentPingTime;
@Getter
@Setter
private int ping = 100;
@Getter
@Setter

View File

@ -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

View File

@ -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