Refactor permissions to be lock free, and clean up duplicate references

This commit is contained in:
md_5 2013-04-28 10:37:18 +10:00
parent ebff48ff86
commit 824bdc5491
3 changed files with 18 additions and 22 deletions

View File

@ -55,7 +55,7 @@ public class ServerConnector extends PacketHandler
out.writeInt( user.getAddress().getPort() ); out.writeInt( user.getAddress().getPort() );
channel.write( new PacketFAPluginMessage( "BungeeCord", out.toByteArray() ) ); channel.write( new PacketFAPluginMessage( "BungeeCord", out.toByteArray() ) );
channel.write( user.handshake ); channel.write( user.getPendingConnection().getHandshake() );
channel.write( PacketCDClientStatus.CLIENT_LOGIN ); channel.write( PacketCDClientStatus.CLIENT_LOGIN );
} }

View File

@ -12,7 +12,6 @@ import java.net.InetSocketAddress;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level; import java.util.logging.Level;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -23,15 +22,20 @@ import lombok.Synchronized;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PermissionCheckEvent; import net.md_5.bungee.api.event.PermissionCheckEvent;
import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.scoreboard.Scoreboard; import net.md_5.bungee.api.scoreboard.Scoreboard;
import net.md_5.bungee.netty.HandlerBoss; import net.md_5.bungee.connection.InitialHandler;
import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.packet.*; import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.Packet3Chat;
import net.md_5.bungee.packet.Packet9Respawn;
import net.md_5.bungee.packet.PacketCCSettings;
import net.md_5.bungee.packet.PacketFAPluginMessage;
import net.md_5.bungee.packet.PacketFFKick;
@RequiredArgsConstructor @RequiredArgsConstructor
public final class UserConnection implements ProxiedPlayer public final class UserConnection implements ProxiedPlayer
@ -41,9 +45,7 @@ public final class UserConnection implements ProxiedPlayer
private final ProxyServer bungee; private final ProxyServer bungee;
public final ChannelWrapper ch; public final ChannelWrapper ch;
@Getter @Getter
private final PendingConnection pendingConnection; private final InitialHandler pendingConnection;
public final Packet2Handshake handshake;
final List<PacketFAPluginMessage> loginMessages;
/*========================================================================*/ /*========================================================================*/
@Getter @Getter
@Setter(AccessLevel.PACKAGE) @Setter(AccessLevel.PACKAGE)
@ -61,10 +63,10 @@ public final class UserConnection implements ProxiedPlayer
@Getter @Getter
@Setter @Setter
private int ping = 1000; private int ping = 1000;
// Permissions /*========================================================================*/
private final Collection<String> playerGroups = new HashSet<>(); private final Collection<String> groups = new HashSet<>();
private final Collection<String> permissions = new HashSet<>(); private final Collection<String> permissions = new HashSet<>();
private final Object permMutex = new Object(); /*========================================================================*/
@Getter @Getter
private final Object switchMutex = new Object(); private final Object switchMutex = new Object();
public PacketCCSettings settings; public PacketCCSettings settings;
@ -228,16 +230,15 @@ public final class UserConnection implements ProxiedPlayer
@Synchronized("permMutex") @Synchronized("permMutex")
public Collection<String> getGroups() public Collection<String> getGroups()
{ {
return Collections.unmodifiableCollection( playerGroups ); return Collections.unmodifiableCollection( groups );
} }
@Override @Override
@Synchronized("permMutex")
public void addGroups(String... groups) public void addGroups(String... groups)
{ {
for ( String group : groups ) for ( String group : groups )
{ {
playerGroups.add( group ); this.groups.add( group );
for ( String permission : bungee.getConfigurationAdapter().getPermissions( group ) ) for ( String permission : bungee.getConfigurationAdapter().getPermissions( group ) )
{ {
setPermission( permission, true ); setPermission( permission, true );
@ -246,12 +247,11 @@ public final class UserConnection implements ProxiedPlayer
} }
@Override @Override
@Synchronized("permMutex")
public void removeGroups(String... groups) public void removeGroups(String... groups)
{ {
for ( String group : groups ) for ( String group : groups )
{ {
playerGroups.remove( group ); this.groups.remove( group );
for ( String permission : bungee.getConfigurationAdapter().getPermissions( group ) ) for ( String permission : bungee.getConfigurationAdapter().getPermissions( group ) )
{ {
setPermission( permission, false ); setPermission( permission, false );
@ -260,14 +260,12 @@ public final class UserConnection implements ProxiedPlayer
} }
@Override @Override
@Synchronized("permMutex")
public boolean hasPermission(String permission) public boolean hasPermission(String permission)
{ {
return bungee.getPluginManager().callEvent( new PermissionCheckEvent( this, permission, permissions.contains( permission ) ) ).hasPermission(); return bungee.getPluginManager().callEvent( new PermissionCheckEvent( this, permission, permissions.contains( permission ) ) ).hasPermission();
} }
@Override @Override
@Synchronized("permMutex")
public void setPermission(String permission, boolean value) public void setPermission(String permission, boolean value)
{ {
if ( value ) if ( value )

View File

@ -32,8 +32,6 @@ import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.netty.CipherCodec; import net.md_5.bungee.netty.CipherCodec;
import net.md_5.bungee.netty.HandlerBoss; import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.PacketDecoder;
import net.md_5.bungee.packet.Packet1Login;
import net.md_5.bungee.packet.Packet2Handshake; import net.md_5.bungee.packet.Packet2Handshake;
import net.md_5.bungee.packet.PacketCDClientStatus; import net.md_5.bungee.packet.PacketCDClientStatus;
import net.md_5.bungee.packet.PacketFAPluginMessage; import net.md_5.bungee.packet.PacketFAPluginMessage;
@ -42,7 +40,6 @@ import net.md_5.bungee.packet.PacketFDEncryptionRequest;
import net.md_5.bungee.packet.PacketFEPing; import net.md_5.bungee.packet.PacketFEPing;
import net.md_5.bungee.packet.PacketFFKick; import net.md_5.bungee.packet.PacketFFKick;
import net.md_5.bungee.packet.PacketHandler; import net.md_5.bungee.packet.PacketHandler;
import net.md_5.bungee.protocol.PacketDefinitions;
@RequiredArgsConstructor @RequiredArgsConstructor
public class InitialHandler extends PacketHandler implements PendingConnection public class InitialHandler extends PacketHandler implements PendingConnection
@ -52,6 +49,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
private ChannelWrapper ch; private ChannelWrapper ch;
@Getter @Getter
private final ListenerInfo listener; private final ListenerInfo listener;
@Getter
private Packet2Handshake handshake; private Packet2Handshake handshake;
private PacketFDEncryptionRequest request; private PacketFDEncryptionRequest request;
private List<PacketFAPluginMessage> loginMessages = new ArrayList<>(); private List<PacketFAPluginMessage> loginMessages = new ArrayList<>();
@ -212,7 +210,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{ {
Preconditions.checkState( thisState == State.LOGIN, "Not expecting LOGIN" ); Preconditions.checkState( thisState == State.LOGIN, "Not expecting LOGIN" );
UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this, handshake, loginMessages ); UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this );
userCon.init(); userCon.init();
bungee.getPluginManager().callEvent( new PostLoginEvent( userCon ) ); bungee.getPluginManager().callEvent( new PostLoginEvent( userCon ) );