mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2026-02-10 02:46:13 +01:00
Send keep alive frequently. Removed unused packets. Moved initialization in server class
This commit is contained in:
@@ -5,30 +5,42 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import ru.nanit.limbo.LimboConfig;
|
||||
import ru.nanit.limbo.connection.ClientChannelInitializer;
|
||||
import ru.nanit.limbo.connection.ClientConnection;
|
||||
import ru.nanit.limbo.util.Logger;
|
||||
import ru.nanit.limbo.world.DefaultDimension;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public final class LimboServer {
|
||||
|
||||
private AtomicInteger playersCount;
|
||||
private final Map<UUID, ClientConnection> connections = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
public int getPlayersCount(){
|
||||
return playersCount.get();
|
||||
public int getConnectionsCount(){
|
||||
return connections.size();
|
||||
}
|
||||
|
||||
public void incrementPlayers(){
|
||||
playersCount.incrementAndGet();
|
||||
public void addConnection(ClientConnection connection){
|
||||
connections.put(connection.getUuid(), connection);
|
||||
}
|
||||
|
||||
public void decrementPlayers(){
|
||||
playersCount.decrementAndGet();
|
||||
public void removeConnection(ClientConnection connection){
|
||||
connections.remove(connection.getUuid());
|
||||
}
|
||||
|
||||
public void start() throws Exception {
|
||||
Logger.info("Starting server...");
|
||||
|
||||
playersCount = new AtomicInteger();
|
||||
LimboConfig.load(Paths.get("./settings.properties"));
|
||||
DefaultDimension.init();
|
||||
|
||||
executor.scheduleAtFixedRate(this::broadcastKeepAlive, 0L, 5L, TimeUnit.SECONDS);
|
||||
|
||||
ServerBootstrap bootstrap = new ServerBootstrap()
|
||||
.group(new NioEventLoopGroup(), new NioEventLoopGroup())
|
||||
@@ -36,7 +48,12 @@ public final class LimboServer {
|
||||
.childHandler(new ClientChannelInitializer(this));
|
||||
|
||||
bootstrap.bind(LimboConfig.getHost(), LimboConfig.getPort());
|
||||
|
||||
Logger.info("Server started on %s:%d", LimboConfig.getHost(), LimboConfig.getPort());
|
||||
}
|
||||
|
||||
private void broadcastKeepAlive(){
|
||||
connections.values().forEach(ClientConnection::sendKeepAlive);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user