Improve grouped packet performance

This commit is contained in:
Felix Cravic 2020-12-08 09:13:22 +01:00
parent 91dc4eb675
commit fec23a9783
5 changed files with 19 additions and 27 deletions

View File

@ -444,16 +444,17 @@ public class Player extends LivingEntity implements CommandSender {
callEvent(PlayerTickEvent.class, playerTickEvent);
// Multiplayer sync
final boolean positionChanged = position.getX() != lastPlayerSyncX ||
position.getY() != lastPlayerSyncY ||
position.getZ() != lastPlayerSyncZ;
final boolean viewChanged = position.getYaw() != lastPlayerSyncYaw ||
position.getPitch() != lastPlayerSyncPitch;
if (!viewers.isEmpty() &&
!CooldownUtils.hasCooldown(time, lastPlayerSynchronizationTime,
TimeUnit.TICK, getPlayerSynchronizationTickDelay(viewers.size()))) {
final boolean syncCooldown = CooldownUtils.hasCooldown(time, lastPlayerSynchronizationTime,
TimeUnit.TICK, getPlayerSynchronizationTickDelay(viewers.size()));
if (!viewers.isEmpty() && !syncCooldown) {
this.lastPlayerSynchronizationTime = time;
final boolean positionChanged = position.getX() != lastPlayerSyncX ||
position.getY() != lastPlayerSyncY ||
position.getZ() != lastPlayerSyncZ;
final boolean viewChanged = position.getYaw() != lastPlayerSyncYaw ||
position.getPitch() != lastPlayerSyncPitch;
if (positionChanged || viewChanged) {
// Player moved since last time

View File

@ -508,7 +508,7 @@ public abstract class Chunk implements Viewable, DataContainer {
*
* @param player the player
*/
public void sendChunk(@NotNull Player player) {
public synchronized void sendChunk(@NotNull Player player) {
// Only send loaded chunk
if (!isLoaded())
return;
@ -521,7 +521,7 @@ public abstract class Chunk implements Viewable, DataContainer {
playerConnection.sendPacket(getLightPacket());
}
public void sendChunk() {
public synchronized void sendChunk() {
if (!isLoaded()) {
return;
}
@ -535,7 +535,7 @@ public abstract class Chunk implements Viewable, DataContainer {
*
* @param player the player to update the chunk to
*/
public void sendChunkUpdate(@NotNull Player player) {
public synchronized void sendChunkUpdate(@NotNull Player player) {
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(getFreshFullDataPacket());
}
@ -543,7 +543,7 @@ public abstract class Chunk implements Viewable, DataContainer {
/**
* Sends a full {@link ChunkDataPacket} to all chunk viewers.
*/
public void sendChunkUpdate() {
public synchronized void sendChunkUpdate() {
PacketUtils.sendGroupedPacket(getViewers(), getFreshFullDataPacket());
}
@ -554,7 +554,7 @@ public abstract class Chunk implements Viewable, DataContainer {
* @param player the player to send the packet to
* @throws IllegalArgumentException if {@code section} is not a valid section
*/
public void sendChunkSectionUpdate(int section, @NotNull Player player) {
public synchronized void sendChunkSectionUpdate(int section, @NotNull Player player) {
if (!PlayerUtils.isNettyClient(player))
return;
Check.argCondition(!MathUtils.isBetween(section, 0, CHUNK_SECTION_COUNT),

View File

@ -78,11 +78,11 @@ public final class NettyServer {
public NettyServer(@NotNull PacketProcessor packetProcessor) {
this.packetProcessor = packetProcessor;
this.globalTrafficHandler = new GlobalChannelTrafficShapingHandler(trafficScheduler, 200) {
this.globalTrafficHandler = new GlobalChannelTrafficShapingHandler(trafficScheduler, 1000) {
@Override
protected void doAccounting(TrafficCounter counter) {
// TODO proper monitoring API
//System.out.println("data " + counter.lastWriteThroughput() / 1000 + " " + counter.lastReadThroughput() / 1000);
//System.out.println("data " + counter.getRealWriteThroughput() / 1e6);
}
};
}

View File

@ -11,17 +11,7 @@ public class GroupedPacketHandler extends MessageToByteEncoder<FramedPacket> {
protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) {
final ByteBuf packet = msg.body;
out.setBytes(0, packet, 0, packet.writerIndex());
out.writerIndex(packet.writerIndex());
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, FramedPacket msg, boolean preferDirect) {
if (preferDirect) {
return ctx.alloc().ioBuffer(msg.body.writerIndex());
} else {
return ctx.alloc().heapBuffer(msg.body.writerIndex());
}
out.writeBytes(packet.retainedSlice());
}
}

View File

@ -45,7 +45,7 @@ public final class PacketUtils {
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
if (success) {
final ByteBuf finalBuffer = createFramedPacket(packet, true);
final ByteBuf finalBuffer = createFramedPacket(packet, false);
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
// Prevent premature release
@ -199,6 +199,7 @@ public final class PacketUtils {
* @param serverPacket the server packet to write
* @return the framed packet from the server one
*/
@NotNull
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
ByteBuf packetBuf = writePacket(serverPacket);