From 5dbe6aa8084182af3d60c5c3b05fb1382baec936 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 11 Jan 2019 17:46:33 -0500 Subject: [PATCH] Rename connection field in ConnectedPlayer. This sounds stupid, but YourKit really casts its net wide when it tries to instrument usages of JDBC. Velocity doesn't use JDBC, and yet if you run Velocity with YourKit (even without profiling active) you pay a significant cost. --- .../velocitypowered/proxy/VelocityServer.java | 2 +- .../backend/BackendPlaySessionHandler.java | 11 ++-- .../backend/LoginSessionHandler.java | 2 +- .../backend/VelocityServerConnection.java | 6 +- .../client/ClientPlaySessionHandler.java | 22 +++---- .../connection/client/ConnectedPlayer.java | 60 +++++++++---------- .../LegacyForgeHandshakeBackendPhase.java | 2 +- .../LegacyForgeHandshakeClientPhase.java | 2 +- 8 files changed, 53 insertions(+), 54 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index e7c23283b..d41c11c9c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -422,7 +422,7 @@ public class VelocityServer implements ProxyServer { Preconditions.checkNotNull(component, "component"); Chat chat = Chat.createClientbound(component); for (ConnectedPlayer player : connectionsByUuid.values()) { - player.getConnection().write(chat); + player.getMinecraftConnection().write(chat); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 4ac2cfc8a..354933773 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -36,7 +36,8 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { this.server = server; this.serverConn = serverConn; - MinecraftSessionHandler psh = serverConn.getPlayer().getConnection().getSessionHandler(); + MinecraftSessionHandler psh = serverConn.getPlayer().getMinecraftConnection() + .getSessionHandler(); if (!(psh instanceof ClientPlaySessionHandler)) { throw new IllegalStateException( "Initializing BackendPlaySessionHandler with no backing client play session handler!"); @@ -97,7 +98,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { if (PluginMessageUtil.isMcBrand(packet)) { PluginMessage rewritten = PluginMessageUtil.rewriteMinecraftBrand(packet, server.getVersion()); - serverConn.getPlayer().getConnection().write(rewritten); + serverConn.getPlayer().getMinecraftConnection().write(rewritten); return true; } @@ -111,7 +112,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { return false; } - MinecraftConnection clientConn = serverConn.getPlayer().getConnection(); + MinecraftConnection clientConn = serverConn.getPlayer().getMinecraftConnection(); PluginMessageEvent event = new PluginMessageEvent(serverConn, serverConn.getPlayer(), id, packet.getData()); server.getEventManager().fire(event) @@ -156,12 +157,12 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { @Override public void handleGeneric(MinecraftPacket packet) { - serverConn.getPlayer().getConnection().write(packet); + serverConn.getPlayer().getMinecraftConnection().write(packet); } @Override public void handleUnknown(ByteBuf buf) { - serverConn.getPlayer().getConnection().write(buf.retain()); + serverConn.getPlayer().getMinecraftConnection().write(buf.retain()); } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java index 977d1a95c..117d1c15d 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/LoginSessionHandler.java @@ -114,7 +114,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { VelocityServerConnection existingConnection = serverConn.getPlayer().getConnectedServer(); if (existingConnection == null) { // Strap on the play session handler - serverConn.getPlayer().getConnection() + serverConn.getPlayer().getMinecraftConnection() .setSessionHandler(new ClientPlaySessionHandler(server, serverConn.getPlayer())); } else { // For Legacy Forge diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index d2b973a46..31b1869a0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -10,7 +10,6 @@ import static com.velocitypowered.proxy.network.Connections.MINECRAFT_ENCODER; import static com.velocitypowered.proxy.network.Connections.READ_TIMEOUT; import com.google.common.base.Preconditions; -import com.google.common.base.VerifyException; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.api.proxy.ServerConnection; @@ -22,7 +21,6 @@ import com.velocitypowered.proxy.connection.ConnectionTypes; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; @@ -139,13 +137,13 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, PlayerInfoForwarding forwardingMode = server.getConfiguration().getPlayerInfoForwardingMode(); // Initiate the handshake. - ProtocolVersion protocolVersion = proxyPlayer.getConnection().getNextProtocolVersion(); + ProtocolVersion protocolVersion = proxyPlayer.getMinecraftConnection().getNextProtocolVersion(); Handshake handshake = new Handshake(); handshake.setNextStatus(StateRegistry.LOGIN_ID); handshake.setProtocolVersion(protocolVersion); if (forwardingMode == PlayerInfoForwarding.LEGACY) { handshake.setServerAddress(createLegacyForwardingAddress()); - } else if (proxyPlayer.getConnection().getType() == ConnectionTypes.LEGACY_FORGE) { + } else if (proxyPlayer.getMinecraftConnection().getType() == ConnectionTypes.LEGACY_FORGE) { handshake.setServerAddress(handshake.getServerAddress() + HANDSHAKE_HOSTNAME_TOKEN); } else { handshake.setServerAddress(registeredServer.getServerInfo().getAddress().getHostString()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 6b2f31d12..bf6524fe7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -68,7 +68,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { public void activated() { PluginMessage register = PluginMessageUtil.constructChannelsPacket(player.getProtocolVersion(), server.getChannelRegistrar().getChannelsForProtocol(player.getProtocolVersion())); - player.getConnection().write(register); + player.getMinecraftConnection().write(register); } @Override @@ -165,7 +165,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { resp.setLength(longestLength); resp.getOffers().addAll(offers); - player.getConnection().write(resp); + player.getMinecraftConnection().write(resp); return true; } } @@ -288,7 +288,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { public void writabilityChanged() { VelocityServerConnection serverConn = player.getConnectedServer(); if (serverConn != null) { - boolean writable = player.getConnection().getChannel().isWritable(); + boolean writable = player.getMinecraftConnection().getChannel().isWritable(); MinecraftConnection smc = serverConn.getConnection(); if (smc != null) { smc.getChannel().config().setAutoRead(writable); @@ -316,7 +316,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { if (!spawned) { // Nothing special to do with regards to spawning the player spawned = true; - player.getConnection().delayedWrite(joinGame); + player.getMinecraftConnection().delayedWrite(joinGame); // Required for Legacy Forge player.getPhase().onFirstJoin(player); @@ -336,12 +336,12 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { // Most notably, by having the client accept the join game packet, we can work around the need // to perform entity ID rewrites, eliminating potential issues from rewriting packets and // improving compatibility with mods. - player.getConnection().delayedWrite(joinGame); + player.getMinecraftConnection().delayedWrite(joinGame); int tempDim = joinGame.getDimension() == 0 ? -1 : 0; - player.getConnection().delayedWrite( + player.getMinecraftConnection().delayedWrite( new Respawn(tempDim, joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType())); - player.getConnection().delayedWrite( + player.getMinecraftConnection().delayedWrite( new Respawn(joinGame.getDimension(), joinGame.getDifficulty(), joinGame.getGamemode(), joinGame.getLevelType())); } @@ -352,7 +352,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { BossBar deletePacket = new BossBar(); deletePacket.setUuid(serverBossBar); deletePacket.setAction(BossBar.REMOVE); - player.getConnection().delayedWrite(deletePacket); + player.getMinecraftConnection().delayedWrite(deletePacket); } serverBossBars.clear(); @@ -375,11 +375,11 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } // Clear any title from the previous server. - player.getConnection() + player.getMinecraftConnection() .delayedWrite(TitlePacket.resetForProtocolVersion(player.getProtocolVersion())); // Flush everything - player.getConnection().flush(); + player.getMinecraftConnection().flush(); serverMc.flush(); serverConn.completeJoin(); } @@ -417,7 +417,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { } } - player.getConnection().write(response); + player.getMinecraftConnection().write(response); } /** diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index ea4134b8e..9265be3e4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -70,7 +70,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private static final Logger logger = LogManager.getLogger(ConnectedPlayer.class); - private final MinecraftConnection connection; + private final MinecraftConnection minecraftConnection; private final @Nullable InetSocketAddress virtualHost; private GameProfile profile; private PermissionFunction permissionFunction; @@ -87,15 +87,15 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @MonotonicNonNull private List serversToTry = null; - ConnectedPlayer(VelocityServer server, GameProfile profile, MinecraftConnection connection, - @Nullable InetSocketAddress virtualHost) { + ConnectedPlayer(VelocityServer server, GameProfile profile, + MinecraftConnection minecraftConnection, @Nullable InetSocketAddress virtualHost) { this.server = server; - this.tabList = new VelocityTabList(connection); + this.tabList = new VelocityTabList(minecraftConnection); this.profile = profile; - this.connection = connection; + this.minecraftConnection = minecraftConnection; this.virtualHost = virtualHost; this.permissionFunction = PermissionFunction.ALWAYS_UNDEFINED; - this.connectionPhase = connection.getType().getInitialClientPhase(); + this.connectionPhase = minecraftConnection.getType().getInitialClientPhase(); } @Override @@ -117,8 +117,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return profile; } - public MinecraftConnection getConnection() { - return connection; + public MinecraftConnection getMinecraftConnection() { + return minecraftConnection; } @Override @@ -151,7 +151,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @Override public InetSocketAddress getRemoteAddress() { - return (InetSocketAddress) connection.getRemoteAddress(); + return (InetSocketAddress) minecraftConnection.getRemoteAddress(); } @Override @@ -165,12 +165,12 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @Override public boolean isActive() { - return connection.getChannel().isActive(); + return minecraftConnection.getChannel().isActive(); } @Override public ProtocolVersion getProtocolVersion() { - return connection.getProtocolVersion(); + return minecraftConnection.getProtocolVersion(); } @Override @@ -186,7 +186,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { TitlePacket pkt = new TitlePacket(); pkt.setAction(TitlePacket.SET_ACTION_BAR); pkt.setComponent(ComponentSerializers.JSON.serialize(component)); - connection.write(pkt); + minecraftConnection.write(pkt); return; } else { // Due to issues with action bar packets, we'll need to convert the text message into a @@ -202,7 +202,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { Chat chat = new Chat(); chat.setType(pos); chat.setMessage(json); - connection.write(chat); + minecraftConnection.write(chat); } @Override @@ -238,23 +238,23 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { @Override public void disconnect(Component reason) { logger.info("{} has disconnected: {}", this, ComponentSerializers.LEGACY.serialize(reason)); - connection.closeWith(Disconnect.create(reason)); + minecraftConnection.closeWith(Disconnect.create(reason)); } @Override public void sendTitle(Title title) { Preconditions.checkNotNull(title, "title"); + ProtocolVersion protocolVersion = minecraftConnection.getProtocolVersion(); if (title.equals(Titles.reset())) { - connection.write(TitlePacket.resetForProtocolVersion(connection.getProtocolVersion())); + minecraftConnection.write(TitlePacket.resetForProtocolVersion(protocolVersion)); } else if (title.equals(Titles.hide())) { - connection.write(TitlePacket.hideForProtocolVersion(connection.getProtocolVersion())); + minecraftConnection.write(TitlePacket.hideForProtocolVersion(protocolVersion)); } else if (title instanceof TextTitle) { TextTitle tt = (TextTitle) title; if (tt.isResetBeforeSend()) { - connection - .delayedWrite(TitlePacket.resetForProtocolVersion(connection.getProtocolVersion())); + minecraftConnection.delayedWrite(TitlePacket.resetForProtocolVersion(protocolVersion)); } Optional titleText = tt.getTitle(); @@ -262,7 +262,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { TitlePacket titlePkt = new TitlePacket(); titlePkt.setAction(TitlePacket.SET_TITLE); titlePkt.setComponent(ComponentSerializers.JSON.serialize(titleText.get())); - connection.delayedWrite(titlePkt); + minecraftConnection.delayedWrite(titlePkt); } Optional subtitleText = tt.getSubtitle(); @@ -270,17 +270,17 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { TitlePacket titlePkt = new TitlePacket(); titlePkt.setAction(TitlePacket.SET_SUBTITLE); titlePkt.setComponent(ComponentSerializers.JSON.serialize(subtitleText.get())); - connection.delayedWrite(titlePkt); + minecraftConnection.delayedWrite(titlePkt); } if (tt.areTimesSet()) { - TitlePacket timesPkt = TitlePacket.timesForProtocolVersion(connection.getProtocolVersion()); + TitlePacket timesPkt = TitlePacket.timesForProtocolVersion(protocolVersion); timesPkt.setFadeIn(tt.getFadeIn()); timesPkt.setStay(tt.getStay()); timesPkt.setFadeOut(tt.getFadeOut()); - connection.delayedWrite(timesPkt); + minecraftConnection.delayedWrite(timesPkt); } - connection.flush(); + minecraftConnection.flush(); } else { throw new IllegalArgumentException("Unknown title class " + title.getClass().getName()); } @@ -388,7 +388,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { // In case someone gets creative, assume we want to disconnect the player. disconnect(friendlyReason); } - }, connection.eventLoop()); + }, minecraftConnection.eventLoop()); } } @@ -477,7 +477,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { PluginMessage message = new PluginMessage(); message.setChannel(identifier.getId()); message.setData(data); - connection.write(message); + minecraftConnection.write(message); return true; } @@ -496,7 +496,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { ResourcePackRequest request = new ResourcePackRequest(); request.setUrl(url); request.setHash(""); - connection.write(request); + minecraftConnection.write(request); } @Override @@ -508,7 +508,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { ResourcePackRequest request = new ResourcePackRequest(); request.setUrl(url); request.setHash(ByteBufUtil.hexDump(hash)); - connection.write(request); + minecraftConnection.write(request); } /** @@ -517,10 +517,10 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { * ID last sent by the server. */ public void sendKeepAlive() { - if (connection.getState() == StateRegistry.PLAY) { + if (minecraftConnection.getState() == StateRegistry.PLAY) { KeepAlive keepAlive = new KeepAlive(); keepAlive.setRandomId(ThreadLocalRandom.current().nextLong()); - connection.write(keepAlive); + minecraftConnection.write(keepAlive); } } @@ -632,7 +632,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { // The only remaining value is successful (no need to do anything!) break; } - }, connection.eventLoop()) + }, minecraftConnection.eventLoop()) .thenApply(Result::isSuccessful); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java index effaf945d..ad3610231 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeBackendPhase.java @@ -114,7 +114,7 @@ public enum LegacyForgeHandshakeBackendPhase implements BackendConnectionPhase { serverConnection.setConnectionPhase(newPhase); // Write the packet to the player, we don't need it now. - player.getConnection().write(message); + player.getMinecraftConnection().write(message); return true; } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java index 2ea9724dc..3ba7859a4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/forge/legacy/LegacyForgeHandshakeClientPhase.java @@ -137,7 +137,7 @@ public enum LegacyForgeHandshakeClientPhase implements ClientConnectionPhase { COMPLETE(null) { @Override public void resetConnectionPhase(ConnectedPlayer player) { - player.getConnection().write(LegacyForgeUtil.resetPacket()); + player.getMinecraftConnection().write(LegacyForgeUtil.resetPacket()); player.setPhase(LegacyForgeHandshakeClientPhase.NOT_STARTED); }