Optimize online count and broadcast methods

This commit is contained in:
md_5 2013-05-25 17:01:39 +10:00
parent 80c22027de
commit 775ffdc998
7 changed files with 46 additions and 11 deletions

View File

@ -254,4 +254,20 @@ public abstract class ProxyServer
* @return the server's {@link AsyncHttpClient} instance * @return the server's {@link AsyncHttpClient} instance
*/ */
public abstract AsyncHttpClient getHttpClient(); 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);
} }

View File

@ -54,6 +54,7 @@ import net.md_5.bungee.command.*;
import net.md_5.bungee.config.YamlConfig; import net.md_5.bungee.config.YamlConfig;
import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.packet.DefinedPacket; 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.packet.PacketFAPluginMessage;
import net.md_5.bungee.scheduler.BungeeThreadPool; import net.md_5.bungee.scheduler.BungeeThreadPool;
import net.md_5.bungee.util.CaseInsensitiveMap; import net.md_5.bungee.util.CaseInsensitiveMap;
@ -365,10 +366,23 @@ public class BungeeCord extends ProxyServer
} }
@Override @Override
@SuppressWarnings("unchecked") // TODO: Abstract more @SuppressWarnings("unchecked")
public Collection<ProxiedPlayer> getPlayers() public Collection<ProxiedPlayer> 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 @Override
@ -453,6 +467,13 @@ public class BungeeCord extends ProxyServer
return ConsoleCommandSender.getInstance(); return ConsoleCommandSender.getInstance();
} }
@Override
public void broadcast(String message)
{
getConsole().sendMessage( message );
broadcast( new Packet3Chat( message ) );
}
public void addConnection(UserConnection con) public void addConnection(UserConnection con)
{ {
connectionLock.writeLock().lock(); connectionLock.writeLock().lock();

View File

@ -76,7 +76,7 @@ public class Metrics extends Thread
data.append( encode( "guid" ) ).append( '=' ).append( encode( BungeeCord.getInstance().config.getUuid() ) ); data.append( encode( "guid" ) ).append( '=' ).append( encode( BungeeCord.getInstance().config.getUuid() ) );
encodeDataPair( data, "version", ProxyServer.getInstance().getVersion() ); encodeDataPair( data, "version", ProxyServer.getInstance().getVersion() );
encodeDataPair( data, "server", "0" ); 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 ) ); encodeDataPair( data, "revision", String.valueOf( REVISION ) );
// If we're pinging, append it // If we're pinging, append it

View File

@ -39,10 +39,8 @@ public class CommandAlert extends Command
} }
String message = builder.substring( 0, builder.length() - 1 ); String message = builder.substring( 0, builder.length() - 1 );
for ( ProxiedPlayer player : ProxyServer.getInstance().getPlayers() )
{ ProxyServer.getInstance().broadcast( message );
player.sendMessage( message );
}
ProxyServer.getInstance().getConsole().sendMessage( message ); ProxyServer.getInstance().getConsole().sendMessage( message );
} }
} }

View File

@ -58,6 +58,6 @@ public class CommandList extends Command
sender.sendMessage( message.toString() ); 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() );
} }
} }

View File

@ -268,7 +268,7 @@ public class DownstreamBridge extends PacketHandler
out.writeUTF( "PlayerCount" ); out.writeUTF( "PlayerCount" );
if ( target.equals( "ALL" ) ) if ( target.equals( "ALL" ) )
{ {
out.writeInt( bungee.getPlayers().size() ); out.writeInt( bungee.getOnlineCount() );
} else } else
{ {
ServerInfo server = bungee.getServerInfo( target ); ServerInfo server = bungee.getServerInfo( target );

View File

@ -96,7 +96,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
public void handle(PacketFEPing ping) throws Exception public void handle(PacketFEPing ping) throws Exception
{ {
ServerPing response = new ServerPing( bungee.getProtocolVersion(), bungee.getGameVersion(), 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(); 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(); int limit = BungeeCord.getInstance().config.getPlayerLimit();
if ( limit > 0 && bungee.getPlayers().size() > limit ) if ( limit > 0 && bungee.getOnlineCount() > limit )
{ {
disconnect( bungee.getTranslation( "proxy_full" ) ); disconnect( bungee.getTranslation( "proxy_full" ) );
return; return;