From b0f1398b454ef6a3ef8a1f6701373eac975f231e Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 25 May 2020 10:26:05 -0400 Subject: [PATCH] Further confinement of preconnect checks to event loop. --- .../connection/client/ConnectedPlayer.java | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) 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 49ebc529a..09bfd08e2 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 @@ -1,5 +1,8 @@ package com.velocitypowered.proxy.connection.client; +import static com.velocitypowered.proxy.connection.util.ConnectionRequestResults.plainResult; +import static java.util.concurrent.CompletableFuture.completedFuture; + import com.google.common.base.Preconditions; import com.google.gson.JsonObject; import com.velocitypowered.api.event.connection.DisconnectEvent; @@ -33,7 +36,6 @@ import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants; import com.velocitypowered.proxy.connection.util.ConnectionMessages; -import com.velocitypowered.proxy.connection.util.ConnectionRequestResults; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.packet.Chat; @@ -711,8 +713,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } private Optional checkServer(RegisteredServer server) { - Preconditions - .checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); + Preconditions.checkArgument(server instanceof VelocityRegisteredServer, + "Not a valid Velocity server."); if (connectionInFlight != null || (connectedServer != null && !connectedServer.hasCompletedJoin())) { return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); @@ -723,38 +725,41 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return Optional.empty(); } + private CompletableFuture> getInitialStatus() { + return CompletableFuture.supplyAsync(() -> checkServer(toConnect), connection.eventLoop()); + } + private CompletableFuture internalConnect() { - Optional initialCheck = checkServer(toConnect); - if (initialCheck.isPresent()) { - return CompletableFuture - .completedFuture(ConnectionRequestResults.plainResult(initialCheck.get(), toConnect)); - } - - // Otherwise, initiate the connection. - ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this, toConnect); - return server.getEventManager().fire(event) - .thenComposeAsync(newEvent -> { - Optional connectTo = newEvent.getResult().getServer(); - if (!connectTo.isPresent()) { - return CompletableFuture.completedFuture( - ConnectionRequestResults - .plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect) - ); + return this.getInitialStatus() + .thenCompose(initialCheck -> { + if (initialCheck.isPresent()) { + return completedFuture(plainResult(initialCheck.get(), toConnect)); } - RegisteredServer rs = connectTo.get(); - Optional lastCheck = checkServer(rs); - if (lastCheck.isPresent()) { - return CompletableFuture - .completedFuture(ConnectionRequestResults.plainResult(lastCheck.get(), rs)); - } + ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this, + toConnect); + return server.getEventManager().fire(event) + .thenComposeAsync(newEvent -> { + Optional newDest = newEvent.getResult().getServer(); + if (!newDest.isPresent()) { + return completedFuture( + plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect) + ); + } - VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs; - VelocityServerConnection con = new VelocityServerConnection(vrs, ConnectedPlayer.this, - server); - connectionInFlight = con; - return con.connect(); - }, connection.eventLoop()); + RegisteredServer realDestination = newDest.get(); + Optional check = checkServer(realDestination); + if (check.isPresent()) { + return completedFuture(plainResult(check.get(), realDestination)); + } + + VelocityRegisteredServer vrs = (VelocityRegisteredServer) realDestination; + VelocityServerConnection con = new VelocityServerConnection(vrs, + ConnectedPlayer.this, server); + connectionInFlight = con; + return con.connect(); + }, connection.eventLoop()); + }); } @Override