3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-06 00:00:47 +01:00

Further confinement of preconnect checks to event loop.

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-05-25 10:26:05 -04:00
Ursprung fa954ab717
Commit b0f1398b45

Datei anzeigen

@ -1,5 +1,8 @@
package com.velocitypowered.proxy.connection.client; 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.common.base.Preconditions;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.velocitypowered.api.event.connection.DisconnectEvent; 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.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants;
import com.velocitypowered.proxy.connection.util.ConnectionMessages; 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.connection.util.ConnectionRequestResults.Impl;
import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.Chat;
@ -711,8 +713,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
} }
private Optional<ConnectionRequestBuilder.Status> checkServer(RegisteredServer server) { private Optional<ConnectionRequestBuilder.Status> checkServer(RegisteredServer server) {
Preconditions Preconditions.checkArgument(server instanceof VelocityRegisteredServer,
.checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); "Not a valid Velocity server.");
if (connectionInFlight != null || (connectedServer != null if (connectionInFlight != null || (connectedServer != null
&& !connectedServer.hasCompletedJoin())) { && !connectedServer.hasCompletedJoin())) {
return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS);
@ -723,38 +725,41 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
return Optional.empty(); return Optional.empty();
} }
private CompletableFuture<Optional<Status>> getInitialStatus() {
return CompletableFuture.supplyAsync(() -> checkServer(toConnect), connection.eventLoop());
}
private CompletableFuture<Impl> internalConnect() { private CompletableFuture<Impl> internalConnect() {
Optional<ConnectionRequestBuilder.Status> initialCheck = checkServer(toConnect); return this.getInitialStatus()
if (initialCheck.isPresent()) { .thenCompose(initialCheck -> {
return CompletableFuture if (initialCheck.isPresent()) {
.completedFuture(ConnectionRequestResults.plainResult(initialCheck.get(), toConnect)); return completedFuture(plainResult(initialCheck.get(), toConnect));
}
// Otherwise, initiate the connection.
ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this, toConnect);
return server.getEventManager().fire(event)
.thenComposeAsync(newEvent -> {
Optional<RegisteredServer> connectTo = newEvent.getResult().getServer();
if (!connectTo.isPresent()) {
return CompletableFuture.completedFuture(
ConnectionRequestResults
.plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect)
);
} }
RegisteredServer rs = connectTo.get(); ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this,
Optional<ConnectionRequestBuilder.Status> lastCheck = checkServer(rs); toConnect);
if (lastCheck.isPresent()) { return server.getEventManager().fire(event)
return CompletableFuture .thenComposeAsync(newEvent -> {
.completedFuture(ConnectionRequestResults.plainResult(lastCheck.get(), rs)); Optional<RegisteredServer> newDest = newEvent.getResult().getServer();
} if (!newDest.isPresent()) {
return completedFuture(
plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect)
);
}
VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs; RegisteredServer realDestination = newDest.get();
VelocityServerConnection con = new VelocityServerConnection(vrs, ConnectedPlayer.this, Optional<ConnectionRequestBuilder.Status> check = checkServer(realDestination);
server); if (check.isPresent()) {
connectionInFlight = con; return completedFuture(plainResult(check.get(), realDestination));
return con.connect(); }
}, connection.eventLoop());
VelocityRegisteredServer vrs = (VelocityRegisteredServer) realDestination;
VelocityServerConnection con = new VelocityServerConnection(vrs,
ConnectedPlayer.this, server);
connectionInFlight = con;
return con.connect();
}, connection.eventLoop());
});
} }
@Override @Override