mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-30 21:17:53 +01:00
Avoid allocation when writing framed packets
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
1b4e0519ee
commit
00401bed25
@ -9,9 +9,14 @@ import java.nio.ByteBuffer;
|
|||||||
/**
|
/**
|
||||||
* Represents a packet which is already framed. (packet id+payload) + optional compression
|
* Represents a packet which is already framed. (packet id+payload) + optional compression
|
||||||
* Can be used if you want to send the exact same buffer to multiple clients without processing it more than once.
|
* Can be used if you want to send the exact same buffer to multiple clients without processing it more than once.
|
||||||
|
* <p>
|
||||||
|
* The {@link ByteBuffer} will ultimately become a MemorySegment once out of incubation.
|
||||||
*/
|
*/
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public record FramedPacket(int packetId,
|
public record FramedPacket(@NotNull ServerPacket packet,
|
||||||
@NotNull ByteBuffer body,
|
@NotNull ByteBuffer body) {
|
||||||
@NotNull ServerPacket packet) {
|
|
||||||
|
public FramedPacket {
|
||||||
|
body = body.position(0).asReadOnlyBuffer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,7 @@ public class PlayerSocketConnection extends PlayerConnection {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendPacket(@NotNull FramedPacket framedPacket) {
|
public void sendPacket(@NotNull FramedPacket framedPacket) {
|
||||||
write(framedPacket.body().duplicate().position(0));
|
write(framedPacket.body());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
|
@ -120,7 +120,7 @@ public final class PacketUtils {
|
|||||||
if (!PACKET_LISTENER_MANAGER.processServerPacket(packet, players))
|
if (!PACKET_LISTENER_MANAGER.processServerPacket(packet, players))
|
||||||
return;
|
return;
|
||||||
final ByteBuffer finalBuffer = createFramedPacket(packet).flip();
|
final ByteBuffer finalBuffer = createFramedPacket(packet).flip();
|
||||||
final FramedPacket framedPacket = new FramedPacket(packet.getId(), finalBuffer, packet);
|
final FramedPacket framedPacket = new FramedPacket(packet, finalBuffer);
|
||||||
// Send packet to all players
|
// Send packet to all players
|
||||||
for (Player player : players) {
|
for (Player player : players) {
|
||||||
if (!player.isOnline() || !playerValidator.isValid(player))
|
if (!player.isOnline() || !playerValidator.isValid(player))
|
||||||
@ -234,8 +234,8 @@ public final class PacketUtils {
|
|||||||
public static FramedPacket allocateTrimmedPacket(@NotNull ServerPacket packet) {
|
public static FramedPacket allocateTrimmedPacket(@NotNull ServerPacket packet) {
|
||||||
final ByteBuffer temp = PacketUtils.createFramedPacket(packet).flip();
|
final ByteBuffer temp = PacketUtils.createFramedPacket(packet).flip();
|
||||||
final ByteBuffer buffer = ByteBuffer.allocateDirect(temp.remaining())
|
final ByteBuffer buffer = ByteBuffer.allocateDirect(temp.remaining())
|
||||||
.put(temp).flip().asReadOnlyBuffer();
|
.put(temp).flip();
|
||||||
return new FramedPacket(packet.getId(), buffer, packet);
|
return new FramedPacket(packet, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class ViewableStorage {
|
private static final class ViewableStorage {
|
||||||
|
Loading…
Reference in New Issue
Block a user