Small code cleanup for packet caching

This commit is contained in:
themode 2021-03-02 20:42:36 +01:00
parent 25c2dc661f
commit bde027432a

View File

@ -129,26 +129,27 @@ 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(); final long timestamp = cacheablePacket.getTimestamp();
// Try to retrieve the cached buffer // Try to retrieve the cached buffer
TemporaryCache<TimedBuffer> temporaryCache = cacheablePacket.getCache(); TemporaryCache<TimedBuffer> temporaryCache = cacheablePacket.getCache();
TimedBuffer timedBuffer = temporaryCache.retrieve(identifier); TimedBuffer timedBuffer = temporaryCache.retrieve(identifier);
boolean shouldUpdate = false;
// Update the buffer if non-existent or outdated
final boolean shouldUpdate = timedBuffer == null ||
timestamp > timedBuffer.getTimestamp();
if (timedBuffer == null) { if (timedBuffer == null) {
// Buffer not found, create and cache it // Buffer not found, create it
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false); final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false);
timedBuffer = new TimedBuffer(buffer, time); timedBuffer = new TimedBuffer(buffer, timestamp);
shouldUpdate = true;
} else if (time > timedBuffer.getTimestamp()) { // Verify if `serverPacket` is more up-to-date
shouldUpdate = true;
} }
if (shouldUpdate) { if (shouldUpdate) {
temporaryCache.cache(identifier, timedBuffer); temporaryCache.cache(identifier, timedBuffer);
} }
FramedPacket framedPacket = new FramedPacket(timedBuffer.getBuffer()); write(new FramedPacket(timedBuffer.getBuffer()));
write(framedPacket);
} }
} else } else