Minestom/src/main/java/net/minestom/server/utils/PacketUtils.java

231 lines
9.4 KiB
Java
Raw Normal View History

2020-04-24 03:25:58 +02:00
package net.minestom.server.utils;
2019-08-22 14:52:32 +02:00
2020-04-17 01:16:02 +02:00
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.network.netty.packet.FramedPacket;
2020-04-24 03:25:58 +02:00
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.callback.validator.PlayerValidator;
2020-11-05 22:20:51 +01:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2019-08-22 14:52:32 +02:00
2021-03-25 16:05:10 +01:00
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.zip.Deflater;
2020-08-15 04:05:15 +02:00
/**
2020-11-13 09:17:53 +01:00
* Utils class for packets. Including writing a {@link ServerPacket} into a {@link ByteBuf}
* for network processing.
2020-08-15 04:05:15 +02:00
*/
public final class PacketUtils {
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
private static final ThreadLocal<Deflater> DEFLATER = ThreadLocal.withInitial(() -> new Deflater(3));
private PacketUtils() {
}
2019-08-22 14:52:32 +02:00
2020-11-13 09:17:53 +01:00
/**
* Sends a {@link ServerPacket} to multiple players.
2020-11-13 09:17:53 +01:00
* <p>
* Can drastically improve performance since the packet will not have to be processed as much.
2020-11-13 09:17:53 +01:00
*
* @param players the players to send the packet to
* @param packet the packet to send to the players
* @param playerValidator optional callback to check if a specify player of {@code players} should receive the packet
2020-11-13 09:17:53 +01:00
*/
public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet,
@Nullable PlayerValidator playerValidator) {
2020-11-20 14:14:55 +01:00
if (players.isEmpty())
return;
if (MinecraftServer.hasGroupedPacket()) {
// Send grouped packet...
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
if (success) {
2021-03-20 11:59:02 +01:00
final ByteBuf finalBuffer = createFramedPacket(packet, true);
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
2020-11-20 04:48:33 +01:00
// Send packet to all players
for (Player player : players) {
if (!player.isOnline())
continue;
// Verify if the player should receive the packet
if (playerValidator != null && !playerValidator.isValid(player))
continue;
2021-03-20 11:59:02 +01:00
finalBuffer.retain();
final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) {
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection;
nettyPlayerConnection.write(framedPacket);
} else {
playerConnection.sendPacket(packet);
}
2021-03-20 11:59:02 +01:00
finalBuffer.release();
}
2021-03-20 11:59:02 +01:00
finalBuffer.release(); // Release last reference
}
} else {
// Write the same packet for each individual players
for (Player player : players) {
// Verify if the player should receive the packet
if (playerValidator != null && !playerValidator.isValid(player))
continue;
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(packet);
}
}
}
/**
* Same as {@link #sendGroupedPacket(Collection, ServerPacket, PlayerValidator)}
* but with the player validator sets to null.
*
* @see #sendGroupedPacket(Collection, ServerPacket, PlayerValidator)
*/
public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet) {
sendGroupedPacket(players, packet, null);
}
2020-08-15 04:05:15 +02:00
/**
2020-10-15 21:16:31 +02:00
* Writes a {@link ServerPacket} into a {@link ByteBuf}.
2020-08-15 04:05:15 +02:00
*
* @param buf the recipient of {@code packet}
* @param packet the packet to write into {@code buf}
*/
2020-11-05 22:20:51 +01:00
public static void writePacket(@NotNull ByteBuf buf, @NotNull ServerPacket packet) {
2021-03-22 14:31:38 +01:00
Utils.writeVarIntBuf(buf, packet.getId());
writePacketPayload(buf, packet);
}
2020-04-17 01:16:02 +02:00
2020-08-15 04:05:15 +02:00
/**
2021-03-22 14:31:38 +01:00
* Writes a packet payload.
2020-08-15 04:05:15 +02:00
*
* @param packet the packet to write
*/
2021-03-22 14:31:38 +01:00
private static void writePacketPayload(@NotNull ByteBuf buffer, @NotNull ServerPacket packet) {
BinaryWriter writer = new BinaryWriter(buffer);
2020-11-19 02:28:56 +01:00
try {
packet.write(writer);
} catch (Exception e) {
MinecraftServer.getExceptionManager().handleException(e);
2020-11-19 02:28:56 +01:00
}
}
2020-11-20 14:05:22 +01:00
/**
* Frames a buffer for it to be understood by a Minecraft client.
* <p>
* The content of {@code packetBuffer} can be either a compressed or uncompressed packet buffer,
* it depends of it the client did receive a {@link net.minestom.server.network.packet.server.login.SetCompressionPacket} packet before.
*
* @param packetBuffer the buffer containing compressed or uncompressed packet data
* @param frameTarget the buffer which will receive the framed version of {@code from}
*/
public static void frameBuffer(@NotNull ByteBuf packetBuffer, @NotNull ByteBuf frameTarget) {
final int packetSize = packetBuffer.readableBytes();
final int headerSize = Utils.getVarIntSize(packetSize);
if (headerSize > 3) {
throw new IllegalStateException("Unable to fit " + headerSize + " into 3");
}
2020-11-20 14:05:22 +01:00
frameTarget.ensureWritable(packetSize + headerSize);
2020-11-20 14:05:22 +01:00
Utils.writeVarIntBuf(frameTarget, packetSize);
frameTarget.writeBytes(packetBuffer, packetBuffer.readerIndex(), packetSize);
}
2020-11-20 14:05:22 +01:00
/**
* Compress using zlib the content of a packet.
* <p>
* {@code packetBuffer} needs to be the packet content without any header (if you want to use it to write a Minecraft packet).
*
* @param deflater the deflater for zlib compression
* @param packetBuffer the buffer containing all the packet fields
* @param compressionTarget the buffer which will receive the compressed version of {@code packetBuffer}
*/
2021-03-25 15:44:02 +01:00
public static void compressBuffer(@NotNull Deflater deflater, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
2020-11-20 14:05:22 +01:00
final int packetLength = packetBuffer.readableBytes();
2021-03-22 14:31:38 +01:00
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0);
if (compression) {
2021-03-25 15:44:02 +01:00
compress(deflater, packetBuffer, compressionTarget);
} else {
2021-03-22 14:31:38 +01:00
compressionTarget.writeBytes(packetBuffer);
}
}
2021-03-25 15:44:02 +01:00
private static void compress(@NotNull Deflater deflater, @NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
2021-03-22 14:31:38 +01:00
deflater.setInput(uncompressed.nioBuffer());
deflater.finish();
2021-03-22 14:31:38 +01:00
while (!deflater.finished()) {
2021-03-25 16:05:10 +01:00
ByteBuffer nioBuffer = compressed.nioBuffer(compressed.writerIndex(), compressed.writableBytes());
compressed.writerIndex(deflater.deflate(nioBuffer) + compressed.writerIndex());
2021-03-25 15:44:02 +01:00
if (compressed.writableBytes() == 0) {
compressed.ensureWritable(8192);
}
}
2021-03-22 14:31:38 +01:00
deflater.reset();
}
2020-11-20 14:05:22 +01:00
/**
2021-03-22 14:31:38 +01:00
* Creates a "framed packet" (packet which can be send and understood by a Minecraft client)
2021-03-21 13:22:49 +01:00
* from a server packet, directly into an output buffer.
2020-11-20 14:05:22 +01:00
* <p>
* Can be used if you want to store a raw buffer and send it later without the additional writing cost.
* Compression is applied if {@link MinecraftServer#getCompressionThreshold()} is greater than 0.
*
* @param serverPacket the server packet to write
*/
2021-03-22 14:31:38 +01:00
@NotNull
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
2021-03-25 15:44:02 +01:00
ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
2021-03-22 14:31:38 +01:00
writePacket(packetBuf, serverPacket);
2021-03-22 14:31:38 +01:00
final int dataLength = packetBuf.readableBytes();
2021-03-22 14:31:38 +01:00
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
final boolean compression = compressionThreshold > 0;
2021-03-22 14:31:38 +01:00
if (compression) {
if (dataLength >= compressionThreshold) {
// Packet large enough
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
Utils.writeVarIntBuf(compressedBuf, dataLength);
final Deflater deflater = DEFLATER.get();
2021-03-25 15:44:02 +01:00
compress(deflater, packetBuf, compressedBuf);
packetBuf.release();
packetBuf = compressedBuf;
} else {
// Packet too small
ByteBuf uncompressedLengthBuffer = Unpooled.buffer();
Utils.writeVarIntBuf(uncompressedLengthBuffer, 0);
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf);
}
}
2021-03-22 14:31:38 +01:00
// Write the final length of the packet
ByteBuf packetLengthBuffer = Unpooled.buffer();
2021-03-22 14:31:38 +01:00
Utils.writeVarIntBuf(packetLengthBuffer, packetBuf.readableBytes());
return Unpooled.wrappedBuffer(packetLengthBuffer, packetBuf);
}
2019-08-22 14:52:32 +02:00
}