mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 09:57:45 +01:00
Fix cached packets being outdated
This commit is contained in:
parent
85998cabd9
commit
a927938677
@ -16,6 +16,7 @@ import net.minestom.server.utils.Utils;
|
|||||||
import net.minestom.server.utils.binary.BinaryWriter;
|
import net.minestom.server.utils.binary.BinaryWriter;
|
||||||
import net.minestom.server.utils.cache.CacheablePacket;
|
import net.minestom.server.utils.cache.CacheablePacket;
|
||||||
import net.minestom.server.utils.cache.TemporaryCache;
|
import net.minestom.server.utils.cache.TemporaryCache;
|
||||||
|
import net.minestom.server.utils.cache.TimedBuffer;
|
||||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||||
import net.minestom.server.world.biomes.Biome;
|
import net.minestom.server.world.biomes.Biome;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -27,7 +28,7 @@ import java.util.UUID;
|
|||||||
public class ChunkDataPacket implements ServerPacket, CacheablePacket {
|
public class ChunkDataPacket implements ServerPacket, CacheablePacket {
|
||||||
|
|
||||||
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
||||||
private static final TemporaryCache<ByteBuf> CACHE = new TemporaryCache<>(10000L);
|
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(10000L);
|
||||||
|
|
||||||
public boolean fullChunk;
|
public boolean fullChunk;
|
||||||
public Biome[] biomes;
|
public Biome[] biomes;
|
||||||
@ -46,12 +47,12 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
|
|||||||
private static final int MAX_BUFFER_SIZE = (Short.BYTES + Byte.BYTES + 5 * Byte.BYTES + (4096 * MAX_BITS_PER_ENTRY / Long.SIZE * Long.BYTES)) * CHUNK_SECTION_COUNT + 256 * Integer.BYTES;
|
private static final int MAX_BUFFER_SIZE = (Short.BYTES + Byte.BYTES + 5 * Byte.BYTES + (4096 * MAX_BITS_PER_ENTRY / Long.SIZE * Long.BYTES)) * CHUNK_SECTION_COUNT + 256 * Integer.BYTES;
|
||||||
|
|
||||||
// Cacheable data
|
// Cacheable data
|
||||||
private UUID identifier;
|
private final UUID identifier;
|
||||||
private long lastUpdate;
|
private final long timestamp;
|
||||||
|
|
||||||
public ChunkDataPacket(@Nullable UUID identifier, long lastUpdate) {
|
public ChunkDataPacket(@Nullable UUID identifier, long timestamp) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.lastUpdate = lastUpdate;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -139,7 +140,7 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public TemporaryCache<ByteBuf> getCache() {
|
public TemporaryCache<TimedBuffer> getCache() {
|
||||||
return CACHE;
|
return CACHE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,4 +148,9 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
|
|||||||
public UUID getIdentifier() {
|
public UUID getIdentifier() {
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package net.minestom.server.network.packet.server.play;
|
package net.minestom.server.network.packet.server.play;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import net.minestom.server.network.packet.server.ServerPacket;
|
import net.minestom.server.network.packet.server.ServerPacket;
|
||||||
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
||||||
import net.minestom.server.utils.binary.BinaryWriter;
|
import net.minestom.server.utils.binary.BinaryWriter;
|
||||||
import net.minestom.server.utils.cache.CacheablePacket;
|
import net.minestom.server.utils.cache.CacheablePacket;
|
||||||
import net.minestom.server.utils.cache.TemporaryCache;
|
import net.minestom.server.utils.cache.TemporaryCache;
|
||||||
|
import net.minestom.server.utils.cache.TimedBuffer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
public class UpdateLightPacket implements ServerPacket, CacheablePacket {
|
public class UpdateLightPacket implements ServerPacket, CacheablePacket {
|
||||||
|
|
||||||
private static final TemporaryCache<ByteBuf> CACHE = new TemporaryCache<>(10000L);
|
private static final TemporaryCache<TimedBuffer> CACHE = new TemporaryCache<>(10000L);
|
||||||
|
|
||||||
public int chunkX;
|
public int chunkX;
|
||||||
public int chunkZ;
|
public int chunkZ;
|
||||||
@ -31,12 +31,12 @@ public class UpdateLightPacket implements ServerPacket, CacheablePacket {
|
|||||||
public List<byte[]> blockLight;
|
public List<byte[]> blockLight;
|
||||||
|
|
||||||
// Cacheable data
|
// Cacheable data
|
||||||
private UUID identifier;
|
private final UUID identifier;
|
||||||
private long lastUpdate;
|
private final long timestamp;
|
||||||
|
|
||||||
public UpdateLightPacket(@Nullable UUID identifier, long lastUpdate) {
|
public UpdateLightPacket(@Nullable UUID identifier, long timestamp) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.lastUpdate = lastUpdate;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -72,7 +72,7 @@ public class UpdateLightPacket implements ServerPacket, CacheablePacket {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public TemporaryCache<ByteBuf> getCache() {
|
public TemporaryCache<TimedBuffer> getCache() {
|
||||||
return CACHE;
|
return CACHE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,4 +80,9 @@ public class UpdateLightPacket implements ServerPacket, CacheablePacket {
|
|||||||
public UUID getIdentifier() {
|
public UUID getIdentifier() {
|
||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import net.minestom.server.network.packet.server.login.SetCompressionPacket;
|
|||||||
import net.minestom.server.utils.PacketUtils;
|
import net.minestom.server.utils.PacketUtils;
|
||||||
import net.minestom.server.utils.cache.CacheablePacket;
|
import net.minestom.server.utils.cache.CacheablePacket;
|
||||||
import net.minestom.server.utils.cache.TemporaryCache;
|
import net.minestom.server.utils.cache.TemporaryCache;
|
||||||
|
import net.minestom.server.utils.cache.TimedBuffer;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -128,15 +129,25 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
// This packet explicitly asks to do not retrieve the cache
|
// This packet explicitly asks to do not retrieve the cache
|
||||||
write(serverPacket);
|
write(serverPacket);
|
||||||
} else {
|
} else {
|
||||||
|
final long time = cacheablePacket.getTimestamp();
|
||||||
// Try to retrieve the cached buffer
|
// Try to retrieve the cached buffer
|
||||||
TemporaryCache<ByteBuf> temporaryCache = cacheablePacket.getCache();
|
TemporaryCache<TimedBuffer> temporaryCache = cacheablePacket.getCache();
|
||||||
ByteBuf buffer = temporaryCache.retrieve(identifier);
|
TimedBuffer timedBuffer = temporaryCache.retrieve(identifier);
|
||||||
if (buffer == null) {
|
boolean shouldUpdate = false;
|
||||||
|
if (timedBuffer == null) {
|
||||||
// Buffer not found, create and cache it
|
// Buffer not found, create and cache it
|
||||||
buffer = PacketUtils.createFramedPacket(serverPacket, false);
|
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false);
|
||||||
temporaryCache.cache(identifier, buffer);
|
timedBuffer = new TimedBuffer(buffer, time);
|
||||||
|
shouldUpdate = true;
|
||||||
|
} else if (time > timedBuffer.getTimestamp()) { // Verify if `serverPacket` is more up-to-date
|
||||||
|
shouldUpdate = true;
|
||||||
}
|
}
|
||||||
FramedPacket framedPacket = new FramedPacket(buffer);
|
|
||||||
|
if (shouldUpdate) {
|
||||||
|
temporaryCache.cache(identifier, timedBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
FramedPacket framedPacket = new FramedPacket(timedBuffer.getBuffer());
|
||||||
write(framedPacket);
|
write(framedPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.minestom.server.utils.cache;
|
package net.minestom.server.utils.cache;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import net.minestom.server.network.packet.server.ServerPacket;
|
import net.minestom.server.network.packet.server.ServerPacket;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -23,7 +22,7 @@ public interface CacheablePacket {
|
|||||||
* @return the temporary packet cache
|
* @return the temporary packet cache
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
TemporaryCache<ByteBuf> getCache();
|
TemporaryCache<TimedBuffer> getCache();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the identifier of this packet.
|
* Gets the identifier of this packet.
|
||||||
@ -35,4 +34,11 @@ public interface CacheablePacket {
|
|||||||
@Nullable
|
@Nullable
|
||||||
UUID getIdentifier();
|
UUID getIdentifier();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the last time this packet changed.
|
||||||
|
*
|
||||||
|
* @return the last packet update time in milliseconds
|
||||||
|
*/
|
||||||
|
long getTimestamp();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
28
src/main/java/net/minestom/server/utils/cache/TimedBuffer.java
vendored
Normal file
28
src/main/java/net/minestom/server/utils/cache/TimedBuffer.java
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package net.minestom.server.utils.cache;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object containing a {@link ByteBuf buffer} and its timestamp.
|
||||||
|
* Used for packet-caching to use the most recent.
|
||||||
|
*/
|
||||||
|
public class TimedBuffer {
|
||||||
|
|
||||||
|
private final ByteBuf buffer;
|
||||||
|
private final long timestamp;
|
||||||
|
|
||||||
|
public TimedBuffer(@NotNull ByteBuf buffer, long timestamp) {
|
||||||
|
this.buffer = buffer;
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public ByteBuf getBuffer() {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user