Small optimizations to netty.

This commit is contained in:
Eoghanmc22 2020-11-21 13:09:03 -05:00
parent 4e11b16ee1
commit 926c02b7f5
6 changed files with 29 additions and 12 deletions

View File

@ -606,7 +606,7 @@ public final class MinecraftServer {
*
* @return should process netty errors
*/
public static boolean processingNettyErrors() {
public static boolean shouldProcessNettyErrors() {
return processNettyErrors;
}

View File

@ -1,6 +1,7 @@
package net.minestom.server.network.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
@ -100,6 +101,7 @@ public final class NettyServer {
ChannelConfig config = ch.config();
config.setOption(ChannelOption.TCP_NODELAY, true);
config.setOption(ChannelOption.SO_SNDBUF, 1_000_000);
config.setAllocator(ByteBufAllocator.DEFAULT);
ChannelPipeline pipeline = ch.pipeline();

View File

@ -65,8 +65,10 @@ public class ClientChannel extends SimpleChannelInboundHandler<InboundPacket> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LOGGER.info(cause.getMessage());
cause.printStackTrace();
if (MinecraftServer.shouldProcessNettyErrors()) {
LOGGER.info(cause.getMessage());
cause.printStackTrace();
}
ctx.close();
}
}

View File

@ -10,9 +10,22 @@ public class GroupedPacketHandler extends MessageToByteEncoder<FramedPacket> {
@Override
protected void encode(ChannelHandlerContext ctx, FramedPacket msg, ByteBuf out) {
final ByteBuf packet = msg.body;
out.writeBytes(packet.duplicate());
out.setBytes(0, packet, 0, packet.writerIndex());
out.writerIndex(packet.writerIndex());
if (msg.releaseBuf) {
packet.release();
}
}
@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());
}
}
}

View File

@ -143,7 +143,7 @@ public class NettyPlayerConnection extends PlayerConnection {
}
public void write(Object message) {
if (MinecraftServer.processingNettyErrors())
if (MinecraftServer.shouldProcessNettyErrors())
channel.write(message).addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();
@ -154,7 +154,7 @@ public class NettyPlayerConnection extends PlayerConnection {
}
public void writeAndFlush(Object message) {
if (MinecraftServer.processingNettyErrors())
if (MinecraftServer.shouldProcessNettyErrors())
channel.writeAndFlush(message).addListener(future -> {
if (!future.isSuccess()) {
future.cause().printStackTrace();

View File

@ -49,11 +49,11 @@ public class PlayerInit {
instanceContainer.setChunkGenerator(noiseTestGenerator);
// Load some chunks beforehand
final int loopStart = -32;
final int loopEnd = 32;
final int loopStart = -16;
final int loopEnd = 16;
for (int x = loopStart; x < loopEnd; x++)
for (int z = loopStart; z < loopEnd; z++) {
instanceContainer.loadChunk(x, z);
//instanceContainer.loadChunk(x, z);
}
inventory = new Inventory(InventoryType.CHEST_1_ROW, "Test inventory");
@ -172,9 +172,9 @@ public class PlayerInit {
player.addEventCallback(PlayerLoginEvent.class, event -> {
event.setSpawningInstance(instanceContainer);
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 1000 + 500;
int z = Math.abs(ThreadLocalRandom.current().nextInt()) % 1000 + 500;
player.setRespawnPoint(new Position(0, 70f, 0));
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 2000 - 1000;
int z = Math.abs(ThreadLocalRandom.current().nextInt()) % 2000 - 1000;
player.setRespawnPoint(new Position(x, 70f, z));
/*player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
if (slot == -999)