From 775ffdc998ed6e9170bbd07deaab2aaae537b759 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sat, 25 May 2013 17:01:39 +1000 Subject: [PATCH] Optimize online count and broadcast methods --- .../java/net/md_5/bungee/api/ProxyServer.java | 16 ++++++++++++ .../main/java/net/md_5/bungee/BungeeCord.java | 25 +++++++++++++++++-- .../main/java/net/md_5/bungee/Metrics.java | 2 +- .../net/md_5/bungee/command/CommandAlert.java | 6 ++--- .../net/md_5/bungee/command/CommandList.java | 2 +- .../bungee/connection/DownstreamBridge.java | 2 +- .../bungee/connection/InitialHandler.java | 4 +-- 7 files changed, 46 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java index 4da11dbf..09902527 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyServer.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyServer.java @@ -254,4 +254,20 @@ public abstract class ProxyServer * @return the server's {@link AsyncHttpClient} instance */ public abstract AsyncHttpClient getHttpClient(); + + /** + * Get the current number of connected users. The default implementation is + * more efficient than {@link #getPlayers()} as it does not take a lock or + * make a copy. + * + * @return the current number of connected players + */ + public abstract int getOnlineCount(); + + /** + * Send the specified message to the console and all connected players. + * + * @param message the message to broadcast + */ + public abstract void broadcast(String message); } diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 936d7750..6db66e84 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -54,6 +54,7 @@ import net.md_5.bungee.command.*; import net.md_5.bungee.config.YamlConfig; import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.packet.DefinedPacket; +import net.md_5.bungee.packet.Packet3Chat; import net.md_5.bungee.packet.PacketFAPluginMessage; import net.md_5.bungee.scheduler.BungeeThreadPool; import net.md_5.bungee.util.CaseInsensitiveMap; @@ -365,10 +366,23 @@ public class BungeeCord extends ProxyServer } @Override - @SuppressWarnings("unchecked") // TODO: Abstract more + @SuppressWarnings("unchecked") public Collection getPlayers() { - return (Collection) connections.values(); + connectionLock.readLock().lock(); + try + { + return (Collection) new HashSet<>( connections.values() ); + } finally + { + connectionLock.readLock().unlock(); + } + } + + @Override + public int getOnlineCount() + { + return connections.size(); } @Override @@ -453,6 +467,13 @@ public class BungeeCord extends ProxyServer return ConsoleCommandSender.getInstance(); } + @Override + public void broadcast(String message) + { + getConsole().sendMessage( message ); + broadcast( new Packet3Chat( message ) ); + } + public void addConnection(UserConnection con) { connectionLock.writeLock().lock(); diff --git a/proxy/src/main/java/net/md_5/bungee/Metrics.java b/proxy/src/main/java/net/md_5/bungee/Metrics.java index c3d103e7..0e012e20 100644 --- a/proxy/src/main/java/net/md_5/bungee/Metrics.java +++ b/proxy/src/main/java/net/md_5/bungee/Metrics.java @@ -76,7 +76,7 @@ public class Metrics extends Thread data.append( encode( "guid" ) ).append( '=' ).append( encode( BungeeCord.getInstance().config.getUuid() ) ); encodeDataPair( data, "version", ProxyServer.getInstance().getVersion() ); encodeDataPair( data, "server", "0" ); - encodeDataPair( data, "players", Integer.toString( ProxyServer.getInstance().getPlayers().size() ) ); + encodeDataPair( data, "players", Integer.toString( ProxyServer.getInstance().getOnlineCount() ) ); encodeDataPair( data, "revision", String.valueOf( REVISION ) ); // If we're pinging, append it diff --git a/proxy/src/main/java/net/md_5/bungee/command/CommandAlert.java b/proxy/src/main/java/net/md_5/bungee/command/CommandAlert.java index 391045fa..a1903fb7 100644 --- a/proxy/src/main/java/net/md_5/bungee/command/CommandAlert.java +++ b/proxy/src/main/java/net/md_5/bungee/command/CommandAlert.java @@ -39,10 +39,8 @@ public class CommandAlert extends Command } String message = builder.substring( 0, builder.length() - 1 ); - for ( ProxiedPlayer player : ProxyServer.getInstance().getPlayers() ) - { - player.sendMessage( message ); - } + + ProxyServer.getInstance().broadcast( message ); ProxyServer.getInstance().getConsole().sendMessage( message ); } } diff --git a/proxy/src/main/java/net/md_5/bungee/command/CommandList.java b/proxy/src/main/java/net/md_5/bungee/command/CommandList.java index 4344d9c5..6f331e7e 100644 --- a/proxy/src/main/java/net/md_5/bungee/command/CommandList.java +++ b/proxy/src/main/java/net/md_5/bungee/command/CommandList.java @@ -58,6 +58,6 @@ public class CommandList extends Command sender.sendMessage( message.toString() ); } - sender.sendMessage( ProxyServer.getInstance().getTranslation( "total_players" ) + ProxyServer.getInstance().getPlayers().size() ); + sender.sendMessage( ProxyServer.getInstance().getTranslation( "total_players" ) + ProxyServer.getInstance().getOnlineCount() ); } } 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 05773dc4..1379dd2c 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 @@ -268,7 +268,7 @@ public class DownstreamBridge extends PacketHandler out.writeUTF( "PlayerCount" ); if ( target.equals( "ALL" ) ) { - out.writeInt( bungee.getPlayers().size() ); + out.writeInt( bungee.getOnlineCount() ); } else { ServerInfo server = bungee.getServerInfo( target ); diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index e004c7cf..6ddac09f 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -96,7 +96,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection public void handle(PacketFEPing ping) throws Exception { ServerPing response = new ServerPing( bungee.getProtocolVersion(), bungee.getGameVersion(), - listener.getMotd(), bungee.getPlayers().size(), listener.getMaxPlayers() ); + listener.getMotd(), bungee.getOnlineCount(), listener.getMaxPlayers() ); response = bungee.getPluginManager().callEvent( new ProxyPingEvent( this, response ) ).getResponse(); @@ -130,7 +130,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection } int limit = BungeeCord.getInstance().config.getPlayerLimit(); - if ( limit > 0 && bungee.getPlayers().size() > limit ) + if ( limit > 0 && bungee.getOnlineCount() > limit ) { disconnect( bungee.getTranslation( "proxy_full" ) ); return;