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