From 06a64936050bef857eb7c0e9bfb68b99d35367ee Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 4 Aug 2018 03:18:57 -0400 Subject: [PATCH] Cleaned up connection code. --- .../backend/LoginSessionHandler.java | 24 ++++++++++--------- .../connection/client/ConnectedPlayer.java | 8 ------- 2 files changed, 13 insertions(+), 19 deletions(-) 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 f86fe7757..e4219dd16 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 @@ -1,5 +1,6 @@ package com.velocitypowered.proxy.connection.backend; +import com.velocitypowered.api.proxy.ConnectionRequestBuilder; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.config.IPForwardingMode; import com.velocitypowered.proxy.connection.VelocityConstants; @@ -13,6 +14,7 @@ import com.velocitypowered.proxy.protocol.packet.*; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelPipeline; import net.kyori.text.TextComponent; import java.util.concurrent.ScheduledFuture; @@ -68,11 +70,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { connection.disconnect(); // Do we have an outstanding notification? If so, fulfill it. - ServerConnection.ConnectionNotifier n = connection.getMinecraftConnection().getChannel() - .pipeline().get(ServerConnection.ConnectionNotifier.class); - if (n != null) { - n.getResult().complete(ConnectionRequestResults.forDisconnect(disconnect)); - } + doNotify(ConnectionRequestResults.forDisconnect(disconnect)); connection.getProxyPlayer().handleConnectionException(connection.getServerInfo(), disconnect); } else if (packet instanceof SetCompression) { @@ -91,12 +89,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { } // Do we have an outstanding notification? If so, fulfill it. - ServerConnection.ConnectionNotifier n = connection.getMinecraftConnection().getChannel() - .pipeline().get(ServerConnection.ConnectionNotifier.class); - if (n != null) { - n.onComplete(); - connection.getMinecraftConnection().getChannel().pipeline().remove(n); - } + doNotify(ConnectionRequestResults.SUCCESSFUL); connection.getMinecraftConnection().setSessionHandler(new BackendPlaySessionHandler(connection)); connection.getProxyPlayer().setConnectedServer(connection); @@ -113,6 +106,15 @@ public class LoginSessionHandler implements MinecraftSessionHandler { connection.getProxyPlayer().handleConnectionException(connection.getServerInfo(), throwable); } + private void doNotify(ConnectionRequestBuilder.Result result) { + ChannelPipeline pipeline = connection.getMinecraftConnection().getChannel().pipeline(); + ServerConnection.ConnectionNotifier n = pipeline.get(ServerConnection.ConnectionNotifier.class); + if (n != null) { + n.getResult().complete(result); + pipeline.remove(ServerConnection.ConnectionNotifier.class); + } + } + private void cancelForwardingCheck() { if (forwardingCheckTask != null) { forwardingCheckTask.cancel(false); 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 f80909338..55f89951a 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 @@ -191,14 +191,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connection.connect(); } - void connect(ServerInfo info) { - Preconditions.checkNotNull(info, "info"); - Preconditions.checkState(connectionInFlight == null, "A connection is already active!"); - ServerConnection connection = new ServerConnection(info, this, VelocityServer.getServer()); - connectionInFlight = connection; - connection.connect(); - } - public void setConnectedServer(ServerConnection serverConnection) { if (this.connectedServer != null && !serverConnection.getServerInfo().equals(connectedServer.getServerInfo())) { this.tryIndex = 0;