Arcturus-Community/src/main/java/com/eu/habbo/networking/Server.java

95 lines
3.4 KiB
Java
Raw Normal View History

2018-10-07 00:28:00 +02:00
package com.eu.habbo.networking;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.FixedRecvByteBufAllocator;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.DefaultThreadFactory;
2020-05-03 01:46:07 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-10-07 00:28:00 +02:00
2019-04-22 01:42:00 +02:00
import java.util.concurrent.TimeUnit;
2019-05-26 20:14:53 +02:00
public abstract class Server {
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
2018-10-07 00:28:00 +02:00
protected final ServerBootstrap serverBootstrap;
protected final EventLoopGroup bossGroup;
protected final EventLoopGroup workerGroup;
2019-05-26 20:14:53 +02:00
private final String name;
private final String host;
private final int port;
2018-10-07 00:28:00 +02:00
2019-05-26 20:14:53 +02:00
public Server(String name, String host, int port, int bossGroupThreads, int workerGroupThreads) throws Exception {
2018-10-07 00:28:00 +02:00
this.name = name;
this.host = host;
this.port = port;
String threadName = name.replace("Server", "").replace(" ", "");
this.bossGroup = new NioEventLoopGroup(bossGroupThreads, new DefaultThreadFactory(threadName + "Boss"));
this.workerGroup = new NioEventLoopGroup(workerGroupThreads, new DefaultThreadFactory(threadName + "Worker"));
2018-10-07 00:28:00 +02:00
this.serverBootstrap = new ServerBootstrap();
}
2019-05-26 20:14:53 +02:00
public void initializePipeline() {
2018-10-07 00:28:00 +02:00
this.serverBootstrap.group(this.bossGroup, this.workerGroup);
this.serverBootstrap.channel(NioServerSocketChannel.class);
this.serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
this.serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
this.serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
this.serverBootstrap.childOption(ChannelOption.SO_RCVBUF, 4096);
this.serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(4096));
2018-10-07 00:28:00 +02:00
this.serverBootstrap.childOption(ChannelOption.ALLOCATOR, new UnpooledByteBufAllocator(false));
}
2019-05-26 20:14:53 +02:00
public void connect() {
2018-10-07 00:28:00 +02:00
ChannelFuture channelFuture = this.serverBootstrap.bind(this.host, this.port);
2019-05-26 20:14:53 +02:00
while (!channelFuture.isDone()) {
}
2018-10-07 00:28:00 +02:00
2019-05-26 20:14:53 +02:00
if (!channelFuture.isSuccess()) {
2020-05-03 01:46:07 +02:00
LOGGER.info("Failed to connect to the host (" + this.host + ":" + this.port + ")@" + this.name);
2018-10-07 00:28:00 +02:00
System.exit(0);
2019-05-26 20:14:53 +02:00
} else {
2020-05-03 01:46:07 +02:00
LOGGER.info("Started GameServer on " + this.host + ":" + this.port + "@" + this.name);
2018-10-07 00:28:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public void stop() {
2020-05-03 01:46:07 +02:00
LOGGER.info("Stopping " + this.name);
2019-05-26 20:14:53 +02:00
try {
2019-04-22 01:42:00 +02:00
this.workerGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).sync();
this.bossGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).sync();
} catch(InterruptedException e) {
2020-05-03 01:46:07 +02:00
LOGGER.error("Exception during {} shutdown... HARD STOP", this.name, e);
2018-10-07 00:28:00 +02:00
}
2020-05-03 01:46:07 +02:00
LOGGER.info("GameServer Stopped!");
2018-10-07 00:28:00 +02:00
}
2019-05-26 20:14:53 +02:00
public ServerBootstrap getServerBootstrap() {
2019-03-18 02:22:00 +01:00
return this.serverBootstrap;
2018-10-07 00:28:00 +02:00
}
2019-05-26 20:14:53 +02:00
public EventLoopGroup getBossGroup() {
2019-03-18 02:22:00 +01:00
return this.bossGroup;
2018-10-07 00:28:00 +02:00
}
2019-05-26 20:14:53 +02:00
public EventLoopGroup getWorkerGroup() {
2019-03-18 02:22:00 +01:00
return this.workerGroup;
2018-10-07 00:28:00 +02:00
}
2019-05-26 20:14:53 +02:00
public String getHost() {
2018-10-07 00:28:00 +02:00
return this.host;
}
2019-05-26 20:14:53 +02:00
public int getPort() {
2018-10-07 00:28:00 +02:00
return this.port;
}
}