Add packet doc

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-17 21:14:10 +01:00
parent 69e9b31fa4
commit 750e9b5324
2 changed files with 34 additions and 0 deletions

View File

@ -8,6 +8,14 @@ import java.lang.ref.SoftReference;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Supplier;
/**
* Represents a packet that is compute only when required (either due to memory demand or invalidated data)
* <p>
* The {@link FramedPacket} is stored in a {@link SoftReference} and is invalidated when the {@link #invalidate()} method is called.
* <p>
* The {@link ServerPacket} may be computed dynamically in another thread, or be a constant to potentially
* improve retrieval performance at the cost of the object staying in the heap.
*/
@ApiStatus.Internal
public final class CachedPacket implements SendablePacket {
private static final AtomicIntegerFieldUpdater<CachedPacket> UPDATER = AtomicIntegerFieldUpdater.newUpdater(CachedPacket.class, "updated");
@ -17,10 +25,20 @@ public final class CachedPacket implements SendablePacket {
private volatile int updated = 0;
private SoftReference<FramedPacket> packet;
/**
* Creates a new cached packet that will force the allocation of a new {@link ServerPacket}
* during invalidation due to memory constraint or invalidation.
* <p>
* {@code packetSupplier} must be thread-safe.
*/
public CachedPacket(@NotNull Supplier<@NotNull ServerPacket> packetSupplier) {
this.packetSupplier = packetSupplier;
}
/**
* Creates a new cached packet from a constant {@link ServerPacket}
* which will be the return value of {@link #packet()}.
*/
public CachedPacket(@NotNull ServerPacket packet) {
this(() -> packet);
}
@ -29,10 +47,22 @@ public final class CachedPacket implements SendablePacket {
this.updated = 0;
}
/**
* Retrieves the packet object without allocating a buffer.
* <p>
* This method can be useful in case the payload is not important (e.g. for packet listening),
* but {@link #retrieve()} should be privileged otherwise.
*/
public @NotNull ServerPacket packet() {
return packetSupplier.get();
}
/**
* Retrieves the packet content. May be recomputed if the packet is invalidated or
* if there is memory demand (handled by a soft reference).
* <p>
* {@link FramedPacket#body()} will contain a buffer allocated by this method.
*/
public @NotNull FramedPacket retrieve() {
if (!PacketUtils.CACHED_PACKET) {
// TODO: Using a local buffer may be possible

View File

@ -1,7 +1,11 @@
package net.minestom.server.network.packet.server;
import net.minestom.server.network.player.PlayerConnection;
import org.jetbrains.annotations.ApiStatus;
/**
* Represents a packet that can be sent to a {@link PlayerConnection}.
*/
@ApiStatus.Experimental
public sealed interface SendablePacket
permits ServerPacket, CachedPacket, FramedPacket {