3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Uncache UserConnection on channel close, fix memory leak

Dieser Commit ist enthalten in:
KennyTV 2020-04-23 20:17:09 +02:00
Ursprung 578c661730
Commit 53b8c2328e
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
9 geänderte Dateien mit 147 neuen und 67 gelöschten Zeilen

Datei anzeigen

@ -27,7 +27,7 @@ public class PlayerSneakListener extends ViaBukkitListener {
private final boolean is1_14Fix; private final boolean is1_14Fix;
private Map<Player, Boolean> sneaking; // true = 1.14+, else false private Map<Player, Boolean> sneaking; // true = 1.14+, else false
private Set<UUID> sneakingUuids; private Set<UUID> sneakingUuids;
private Method getHandle; private final Method getHandle;
private Method setSize; private Method setSize;
private boolean useCache; private boolean useCache;

Datei anzeigen

@ -2,10 +2,8 @@ package us.myles.ViaVersion.bukkit.platform;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.ViaVersionPlugin;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
@ -73,13 +71,6 @@ public class BukkitViaLoader implements ViaPlatformLoader {
// Add ProtocolSupport ConnectListener if necessary. // Add ProtocolSupport ConnectListener if necessary.
ClassGenerator.registerPSConnectListener(plugin); ClassGenerator.registerPSConnectListener(plugin);
registerListener(new Listener() {
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
});
/* 1.9 client to 1.8 server */ /* 1.9 client to 1.8 server */
if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) { if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {
storeListener(new ArmorListener(plugin)).register(); storeListener(new ArmorListener(plugin)).register();

Datei anzeigen

@ -2,31 +2,34 @@ package us.myles.ViaVersion;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin; import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.protocol.ProtocolConstants; import net.md_5.bungee.protocol.ProtocolConstants;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaAPI;
import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaConnectionManager;
import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.bungee.commands.BungeeCommand; import us.myles.ViaVersion.bungee.commands.BungeeCommand;
import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler; import us.myles.ViaVersion.bungee.commands.BungeeCommandHandler;
import us.myles.ViaVersion.bungee.commands.BungeeCommandSender; import us.myles.ViaVersion.bungee.commands.BungeeCommandSender;
import us.myles.ViaVersion.bungee.platform.*; import us.myles.ViaVersion.bungee.platform.BungeeTaskId;
import us.myles.ViaVersion.bungee.platform.BungeeViaAPI;
import us.myles.ViaVersion.bungee.platform.BungeeViaConfig;
import us.myles.ViaVersion.bungee.platform.BungeeViaInjector;
import us.myles.ViaVersion.bungee.platform.BungeeViaLoader;
import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; import us.myles.ViaVersion.bungee.service.ProtocolDetectorService;
import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.util.GsonUtil; import us.myles.ViaVersion.util.GsonUtil;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BungeePlugin extends Plugin implements ViaPlatform<ProxiedPlayer>, Listener { public class BungeePlugin extends Plugin implements ViaPlatform<ProxiedPlayer>, Listener {
@ -193,16 +196,4 @@ public class BungeePlugin extends Plugin implements ViaPlatform<ProxiedPlayer>,
public ViaConnectionManager getConnectionManager() { public ViaConnectionManager getConnectionManager() {
return connectionManager; return connectionManager;
} }
@EventHandler
public void onQuit(PlayerDisconnectEvent e) {
UserConnection userConnection = getConnectionManager().getConnectedClient(e.getPlayer().getUniqueId());
if (userConnection != null) {
// Only remove if the connection is disconnected (eg. relogin)
if (userConnection.getChannel() == null || !userConnection.getChannel().isOpen()) {
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
}
}
} }

Datei anzeigen

@ -5,7 +5,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
@ -17,23 +16,25 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
@Data
public class UserConnection { public class UserConnection {
private static final AtomicLong IDS = new AtomicLong();
private final long id = IDS.incrementAndGet();
private final Channel channel; private final Channel channel;
Map<Class, StoredObject> storedObjects = new ConcurrentHashMap<>(); Map<Class, StoredObject> storedObjects = new ConcurrentHashMap<>();
private boolean active = true; private boolean active = true;
private boolean pendingDisconnect = false; private boolean pendingDisconnect;
private Object lastPacket; private Object lastPacket;
private long sentPackets = 0L; private long sentPackets;
private long receivedPackets = 0L; private long receivedPackets;
// Used for tracking pps // Used for tracking pps
private long startTime = 0L; private long startTime;
private long intervalPackets = 0L; private long intervalPackets;
private long packetsPerSecond = -1L; private long packetsPerSecond = -1L;
// Used for handling warnings (over time) // Used for handling warnings (over time)
private int secondsObserved = 0; private int secondsObserved;
private int warnings = 0; private int warnings;
public UserConnection(Channel channel) { public UserConnection(Channel channel) {
this.channel = channel; this.channel = channel;
@ -242,4 +243,109 @@ public class UserConnection {
public void sendRawPacketToServer(ByteBuf packet) { public void sendRawPacketToServer(ByteBuf packet) {
sendRawPacketToServer(packet, false); sendRawPacketToServer(packet, false);
} }
public long getId() {
return id;
}
public Channel getChannel() {
return channel;
}
public Map<Class, StoredObject> getStoredObjects() {
return storedObjects;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isPendingDisconnect() {
return pendingDisconnect;
}
public void setPendingDisconnect(boolean pendingDisconnect) {
this.pendingDisconnect = pendingDisconnect;
}
public Object getLastPacket() {
return lastPacket;
}
public void setLastPacket(Object lastPacket) {
this.lastPacket = lastPacket;
}
public long getSentPackets() {
return sentPackets;
}
public void setSentPackets(long sentPackets) {
this.sentPackets = sentPackets;
}
public long getReceivedPackets() {
return receivedPackets;
}
public void setReceivedPackets(long receivedPackets) {
this.receivedPackets = receivedPackets;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public long getIntervalPackets() {
return intervalPackets;
}
public void setIntervalPackets(long intervalPackets) {
this.intervalPackets = intervalPackets;
}
public long getPacketsPerSecond() {
return packetsPerSecond;
}
public void setPacketsPerSecond(long packetsPerSecond) {
this.packetsPerSecond = packetsPerSecond;
}
public int getSecondsObserved() {
return secondsObserved;
}
public void setSecondsObserved(int secondsObserved) {
this.secondsObserved = secondsObserved;
}
public int getWarnings() {
return warnings;
}
public void setWarnings(int warnings) {
this.warnings = warnings;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserConnection that = (UserConnection) o;
return id == that.id;
}
@Override
public int hashCode() {
return Long.hashCode(id);
}
} }

Datei anzeigen

@ -1,5 +1,6 @@
package us.myles.ViaVersion.api.platform; package us.myles.ViaVersion.api.platform;
import io.netty.channel.ChannelFutureListener;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
@ -18,6 +19,8 @@ public class ViaConnectionManager {
UUID id = connection.get(ProtocolInfo.class).getUuid(); UUID id = connection.get(ProtocolInfo.class).getUuid();
connections.add(connection); connections.add(connection);
clients.put(id, connection); clients.put(id, connection);
connection.getChannel().closeFuture().addListener((ChannelFutureListener) future -> onDisconnect(connection));
} }
public void onDisconnect(UserConnection connection) { public void onDisconnect(UserConnection connection) {

Datei anzeigen

@ -66,4 +66,15 @@ public class ProtocolInfo extends StoredObject {
public void setPipeline(ProtocolPipeline pipeline) { public void setPipeline(ProtocolPipeline pipeline) {
this.pipeline = pipeline; this.pipeline = pipeline;
} }
@Override
public String toString() {
return "ProtocolInfo{" +
"state=" + state +
", protocolVersion=" + protocolVersion +
", serverProtocolVersion=" + serverProtocolVersion +
", username='" + username + '\'' +
", uuid=" + uuid +
'}';
}
} }

Datei anzeigen

@ -1,12 +0,0 @@
package us.myles.ViaVersion.sponge.listeners;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import us.myles.ViaVersion.api.Via;
public class ClientLeaveListener {
@Listener
public void onDisconnect(ClientConnectionEvent.Disconnect disconnect) {
Via.getManager().handleDisconnect(disconnect.getTargetEntity().getUniqueId());
}
}

Datei anzeigen

@ -13,7 +13,6 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider; import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.MovementTransmitterProvider;
import us.myles.ViaVersion.sponge.listeners.ClientLeaveListener;
import us.myles.ViaVersion.sponge.listeners.UpdateListener; import us.myles.ViaVersion.sponge.listeners.UpdateListener;
import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.BlockListener; import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.BlockListener;
import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.DeathListener; import us.myles.ViaVersion.sponge.listeners.protocol1_9to1_8.DeathListener;
@ -50,8 +49,6 @@ public class SpongeViaLoader implements ViaPlatformLoader {
public void load() { public void load() {
// Update Listener // Update Listener
registerListener(new UpdateListener()); registerListener(new UpdateListener());
/* Base Protocol */
registerListener(new ClientLeaveListener());
/* 1.9 client to 1.8 server */ /* 1.9 client to 1.8 server */
if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) { if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_9.getId()) {

Datei anzeigen

@ -3,7 +3,6 @@ package us.myles.ViaVersion;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.DisconnectEvent;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.plugin.PluginContainer;
@ -18,7 +17,6 @@ import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import us.myles.ViaVersion.api.data.MappingDataLoader; import us.myles.ViaVersion.api.data.MappingDataLoader;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.TaskId;
import us.myles.ViaVersion.api.platform.ViaConnectionManager; import us.myles.ViaVersion.api.platform.ViaConnectionManager;
import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.api.platform.ViaPlatform;
@ -27,13 +25,19 @@ import us.myles.ViaVersion.util.GsonUtil;
import us.myles.ViaVersion.velocity.VersionInfo; import us.myles.ViaVersion.velocity.VersionInfo;
import us.myles.ViaVersion.velocity.command.VelocityCommandHandler; import us.myles.ViaVersion.velocity.command.VelocityCommandHandler;
import us.myles.ViaVersion.velocity.command.VelocityCommandSender; import us.myles.ViaVersion.velocity.command.VelocityCommandSender;
import us.myles.ViaVersion.velocity.platform.*; import us.myles.ViaVersion.velocity.platform.VelocityTaskId;
import us.myles.ViaVersion.velocity.platform.VelocityViaAPI;
import us.myles.ViaVersion.velocity.platform.VelocityViaConfig;
import us.myles.ViaVersion.velocity.platform.VelocityViaInjector;
import us.myles.ViaVersion.velocity.platform.VelocityViaLoader;
import us.myles.ViaVersion.velocity.service.ProtocolDetectorService; import us.myles.ViaVersion.velocity.service.ProtocolDetectorService;
import us.myles.ViaVersion.velocity.util.LoggerWrapper; import us.myles.ViaVersion.velocity.util.LoggerWrapper;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@Plugin( @Plugin(
@ -82,17 +86,6 @@ public class VelocityPlugin implements ViaPlatform<Player> {
Via.getManager().init(); Via.getManager().init();
} }
@Subscribe
public void onQuit(DisconnectEvent e) {
UserConnection userConnection = connectionManager.getConnectedClient(e.getPlayer().getUniqueId());
if (userConnection != null) {
// Only remove if the connection is disconnected (eg. relogin)
if (userConnection.getChannel() == null || !userConnection.getChannel().isOpen()) {
Via.getManager().handleDisconnect(e.getPlayer().getUniqueId());
}
}
}
@Override @Override
public String getPlatformName() { public String getPlatformName() {
String proxyImpl = ProxyServer.class.getPackage().getImplementationTitle(); String proxyImpl = ProxyServer.class.getPackage().getImplementationTitle();