Add experimental PlayerConnection#sendFramedPacket

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-20 18:59:15 +02:00
parent 4e58d61345
commit 17807f35da
4 changed files with 20 additions and 15 deletions

View File

@ -130,8 +130,8 @@ public class DynamicChunk extends Chunk {
final FramedPacket chunkPacket = chunkCache.retrieveFramedPacket(lastChange);
if (connection instanceof PlayerSocketConnection) {
PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection;
socketConnection.write(lightPacket);
socketConnection.write(chunkPacket);
socketConnection.sendFramedPacket(lightPacket);
socketConnection.sendFramedPacket(chunkPacket);
} else {
connection.sendPacket(lightPacket.packet());
connection.sendPacket(chunkPacket.packet());

View File

@ -8,7 +8,9 @@ import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.listener.manager.ServerPacketConsumer;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.FramedPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -100,6 +102,11 @@ public abstract class PlayerConnection {
*/
public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating);
@ApiStatus.Experimental
public void sendFramedPacket(@NotNull FramedPacket framedPacket) {
this.sendPacket(framedPacket.packet());
}
/**
* Flush waiting data to the connection.
* <p>

View File

@ -210,7 +210,7 @@ public class PlayerSocketConnection extends PlayerConnection {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
}
write(serverPacket);
writePacket(serverPacket);
} else {
// Player is probably not logged yet
writeAndFlush(serverPacket);
@ -218,6 +218,11 @@ public class PlayerSocketConnection extends PlayerConnection {
}
}
@Override
public void sendFramedPacket(@NotNull FramedPacket framedPacket) {
write(framedPacket.body().duplicate().position(0));
}
@ApiStatus.Internal
public void write(@NotNull ByteBuffer buffer) {
synchronized (bufferLock) {
@ -245,17 +250,13 @@ public class PlayerSocketConnection extends PlayerConnection {
}
}
public void write(@NotNull FramedPacket framedPacket) {
write(framedPacket.body().duplicate().position(0));
}
public void write(@NotNull ServerPacket packet) {
private void writePacket(@NotNull ServerPacket packet) {
write(PacketUtils.createFramedPacket(packet, compressed).flip());
}
public void writeAndFlush(@NotNull ServerPacket packet) {
synchronized (bufferLock) {
write(packet);
writePacket(packet);
flush();
}
}

View File

@ -120,12 +120,7 @@ public final class PacketUtils {
for (Player player : players) {
if (!player.isOnline() || !playerValidator.isValid(player))
continue;
final PlayerConnection connection = player.getPlayerConnection();
if (connection instanceof PlayerSocketConnection) {
((PlayerSocketConnection) connection).write(framedPacket);
} else {
connection.sendPacket(packet);
}
player.getPlayerConnection().sendFramedPacket(framedPacket);
}
} else {
// Write the same packet for each individual players
@ -218,12 +213,14 @@ public final class PacketUtils {
Utils.writeVarIntHeader(buffer, uncompressedIndex, compressed ? packetSize : 0);
}
@ApiStatus.Internal
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet, boolean compression) {
ByteBuffer buffer = PACKET_BUFFER.get();
writeFramedPacket(buffer, packet, compression);
return buffer;
}
@ApiStatus.Internal
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) {
return createFramedPacket(packet, MinecraftServer.getCompressionThreshold() > 0);
}