Allow server to define packet handler executor factory

This commit is contained in:
AlexProgrammerDE 2024-11-20 19:02:08 +01:00
parent d6d2a33b54
commit e6acb395fb
2 changed files with 11 additions and 3 deletions

View file

@ -19,17 +19,24 @@ import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
public class TcpServer extends AbstractServer {
private static final TransportHelper.TransportType TRANSPORT_TYPE = TransportHelper.determineTransportMethod();
private static final Logger log = LoggerFactory.getLogger(TcpServer.class);
private final Supplier<Executor> packetHandlerExecutorFactory;
private EventLoopGroup group;
private Channel channel;
public TcpServer(String host, int port, Supplier<? extends PacketProtocol> protocol) {
this(host, port, protocol, DefaultPacketHandlerExecutor::createExecutor);
}
public TcpServer(String host, int port, Supplier<? extends PacketProtocol> protocol, Supplier<Executor> packetHandlerExecutorFactory) {
super(host, port, protocol);
this.packetHandlerExecutorFactory = packetHandlerExecutorFactory;
}
@Override
@ -57,7 +64,7 @@ public class TcpServer extends AbstractServer {
InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
PacketProtocol protocol = createPacketProtocol();
TcpSession session = new TcpServerSession(address.getHostName(), address.getPort(), protocol, TcpServer.this);
TcpSession session = new TcpServerSession(address.getHostName(), address.getPort(), protocol, TcpServer.this, packetHandlerExecutorFactory.get());
session.getPacketProtocol().newServerSession(TcpServer.this, session);
ChannelPipeline pipeline = channel.pipeline();

View file

@ -7,13 +7,14 @@ import org.geysermc.mcprotocollib.network.packet.PacketProtocol;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
public class TcpServerSession extends TcpSession {
private final TcpServer server;
private final PacketCodecHelper codecHelper;
public TcpServerSession(String host, int port, PacketProtocol protocol, TcpServer server) {
super(host, port, protocol, DefaultPacketHandlerExecutor.createExecutor());
public TcpServerSession(String host, int port, PacketProtocol protocol, TcpServer server, Executor packetHandlerExecutor) {
super(host, port, protocol, packetHandlerExecutor);
this.server = server;
this.codecHelper = protocol.createHelper();
}