Add SessionDisconnectEvent (#4052)

* Add SessionDisconnectEvent

* Move disconnect event calling to UpstreamSession, debug double calling issues

* Prevent duplicate disconnect calling

* Tidy up disconnection logic, ensure SessionDisconnectEvent is always fired - but only once. Fix /geyser reload command
This commit is contained in:
chris 2023-11-10 23:44:38 +01:00 committed by GitHub
parent 9dad1acfe5
commit 0ebb7232f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 7 deletions

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.bedrock;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.event.connection.ConnectionEvent;
/**
* Called when a Geyser session disconnects.
*/
public class SessionDisconnectEvent extends ConnectionEvent {
private String disconnectReason;
public SessionDisconnectEvent(@NonNull GeyserConnection connection, @NonNull String reason) {
super(connection);
this.disconnectReason = reason;
}
/**
* Gets the disconnect reason.
*
* @return the reason for the disconnect
*/
public @NonNull String disconnectReason() {
return disconnectReason;
}
/**
* Sets the disconnect reason, thereby overriding th original reason.
*
* @param disconnectReason the reason for the disconnect
*/
public void disconnectReason(@NonNull String disconnectReason) {
this.disconnectReason = disconnectReason;
}
}

View File

@ -32,6 +32,8 @@ import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.text.GeyserLocale; import org.geysermc.geyser.text.GeyserLocale;
import java.util.concurrent.TimeUnit;
public class ReloadCommand extends GeyserCommand { public class ReloadCommand extends GeyserCommand {
private final GeyserImpl geyser; private final GeyserImpl geyser;
@ -52,7 +54,8 @@ public class ReloadCommand extends GeyserCommand {
sender.sendMessage(message); sender.sendMessage(message);
geyser.getSessionManager().disconnectAll("geyser.commands.reload.kick"); geyser.getSessionManager().disconnectAll("geyser.commands.reload.kick");
geyser.reload(); //FIXME Without the tiny wait, players do not get kicked - same happens when Geyser tries to disconnect all sessions on shutdown
geyser.getScheduledThread().schedule(geyser::reload, 10, TimeUnit.MILLISECONDS);
} }
@Override @Override

View File

@ -105,6 +105,7 @@ import org.geysermc.geyser.api.bedrock.camera.CameraShake;
import org.geysermc.geyser.api.connection.GeyserConnection; import org.geysermc.geyser.api.connection.GeyserConnection;
import org.geysermc.geyser.api.entity.type.GeyserEntity; import org.geysermc.geyser.api.entity.type.GeyserEntity;
import org.geysermc.geyser.api.entity.type.player.GeyserPlayerEntity; import org.geysermc.geyser.api.entity.type.player.GeyserPlayerEntity;
import org.geysermc.geyser.api.event.bedrock.SessionDisconnectEvent;
import org.geysermc.geyser.api.event.bedrock.SessionLoginEvent; import org.geysermc.geyser.api.event.bedrock.SessionLoginEvent;
import org.geysermc.geyser.api.network.AuthType; import org.geysermc.geyser.api.network.AuthType;
import org.geysermc.geyser.api.network.RemoteServer; import org.geysermc.geyser.api.network.RemoteServer;
@ -1038,10 +1039,14 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.remote.disconnect", authData.name(), remoteServer.address(), disconnectMessage)); geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.remote.disconnect", authData.name(), remoteServer.address(), disconnectMessage));
} }
if (cause != null) { if (cause != null) {
cause.printStackTrace(); GeyserImpl.getInstance().getLogger().error(cause.getMessage());
// GeyserSession is disconnected via session.disconnect() called indirectly be the server
// This only needs to be "initiated" here when there is an exception, hence the cause clause
GeyserSession.this.disconnect(disconnectMessage);
if (geyser.getConfig().isDebugMode()) {
cause.printStackTrace();
}
} }
upstream.disconnect(disconnectMessage);
} }
@Override @Override
@ -1068,17 +1073,30 @@ public class GeyserSession implements GeyserConnection, GeyserCommandSource {
public void disconnect(String reason) { public void disconnect(String reason) {
if (!closed) { if (!closed) {
loggedIn = false; loggedIn = false;
// Fire SessionDisconnectEvent
SessionDisconnectEvent disconnectEvent = new SessionDisconnectEvent(this, reason);
geyser.getEventBus().fire(disconnectEvent);
// Disconnect downstream if necessary
if (downstream != null) { if (downstream != null) {
downstream.disconnect(reason); // No need to disconnect if already closed
if (!downstream.isClosed()) {
downstream.disconnect(reason);
}
} else { } else {
// Downstream's disconnect will fire an event that prints a log message // Downstream's disconnect will fire an event that prints a log message
// Otherwise, we print a message here // Otherwise, we print a message here
String address = geyser.getConfig().isLogPlayerIpAddresses() ? upstream.getAddress().getAddress().toString() : "<IP address withheld>"; String address = geyser.getConfig().isLogPlayerIpAddresses() ? upstream.getAddress().getAddress().toString() : "<IP address withheld>";
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.disconnect", address, reason)); geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.disconnect", address, reason));
} }
// Disconnect upstream if necessary
if (!upstream.isClosed()) { if (!upstream.isClosed()) {
upstream.disconnect(reason); upstream.disconnect(disconnectEvent.disconnectReason());
} }
// Remove from session manager
geyser.getSessionManager().removeSession(this); geyser.getSessionManager().removeSession(this);
if (authData != null) { if (authData != null) {
PendingMicrosoftAuthentication.AuthenticationTask task = geyser.getPendingMicrosoftAuthentication().getTask(authData.xuid()); PendingMicrosoftAuthentication.AuthenticationTask task = geyser.getPendingMicrosoftAuthentication().getTask(authData.xuid());

View File

@ -58,7 +58,7 @@ public class UpstreamSession {
} }
public void disconnect(String reason) { public void disconnect(String reason) {
session.disconnect(reason); this.session.disconnect(reason);
} }
/** /**