Add `PlayerConnection#flush` without guarantee

This commit is contained in:
TheMode 2021-08-17 23:23:41 +02:00
parent a43f6f6b62
commit dbf5ff6cd4
5 changed files with 14 additions and 13 deletions

View File

@ -7,7 +7,6 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.monitoring.TickMonitor;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.thread.SingleThreadProvider;
import net.minestom.server.thread.ThreadProvider;
import org.jetbrains.annotations.NotNull;
@ -83,10 +82,7 @@ public final class UpdateManager {
// Flush all waiting packets
for (Player player : connectionManager.getOnlinePlayers()) {
final var connection = player.getPlayerConnection();
if (connection instanceof PlayerSocketConnection) {
((PlayerSocketConnection) connection).flush();
}
player.getPlayerConnection().flush();
}
// Disable thread until next tick

View File

@ -571,9 +571,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
PlayerSpawnEvent spawnEvent = new PlayerSpawnEvent(this, instance, firstSpawn);
EventDispatcher.call(spawnEvent);
if (playerConnection instanceof PlayerSocketConnection) {
((PlayerSocketConnection) playerConnection).flush();
}
this.playerConnection.flush();
}
/**

View File

@ -293,9 +293,7 @@ public final class ConnectionManager {
EventDispatcher.call(asyncPlayerPreLoginEvent);
// Close the player channel if he has been disconnected (kick)
if (!player.isOnline()) {
if (playerConnection instanceof PlayerSocketConnection) {
((PlayerSocketConnection) playerConnection).flush();
}
playerConnection.flush();
//playerConnection.disconnect();
return;
}

View File

@ -50,9 +50,8 @@ public abstract class PlayerConnection {
if (tickCounter % MinecraftServer.TICK_PER_SECOND == 0 && tickCounter > 0) {
tickCounter = 0;
// Retrieve the packet count
final int count = packetCounter.get();
final int count = packetCounter.getAndSet(0);
this.lastPacketCounter.set(count);
this.packetCounter.set(0);
if (count > MinecraftServer.getRateLimit()) {
// Sent too many packets
player.kick(rateLimitKickMessage);
@ -101,6 +100,15 @@ public abstract class PlayerConnection {
*/
public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating);
/**
* Flush waiting data to the connection.
* <p>
* Might not do anything depending on the implementation.
*/
public void flush() {
// Empty
}
protected boolean shouldSendPacket(@NotNull ServerPacket serverPacket) {
return player == null ||
PACKET_LISTENER_MANAGER.processServerPacket(serverPacket, Collections.singleton(player));

View File

@ -252,6 +252,7 @@ public class PlayerSocketConnection extends PlayerConnection {
}
}
@Override
public void flush() {
if (!channel.isOpen()) return;
synchronized (tickBuffer) {