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

105 lines
3.3 KiB
Java
Raw Normal View History

2018-10-07 00:28:00 +02:00
package com.eu.habbo.networking;
import com.eu.habbo.Emulator;
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;
2019-04-22 01:42:00 +02:00
import java.util.concurrent.TimeUnit;
2018-10-07 00:28:00 +02:00
public abstract class Server
{
private final String name;
private final String host;
private final int port;
protected final ServerBootstrap serverBootstrap;
protected final EventLoopGroup bossGroup;
protected final EventLoopGroup workerGroup;
public Server(String name, String host, int port, int bossGroupThreads, int workerGroupThreads) throws Exception
{
this.name = name;
this.host = host;
this.port = port;
this.bossGroup = new NioEventLoopGroup(bossGroupThreads);
this.workerGroup = new NioEventLoopGroup(workerGroupThreads);
this.serverBootstrap = new ServerBootstrap();
}
public void initializePipeline()
{
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, 5120);
this.serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(5120));
this.serverBootstrap.childOption(ChannelOption.ALLOCATOR, new UnpooledByteBufAllocator(false));
}
public void connect()
{
ChannelFuture channelFuture = this.serverBootstrap.bind(this.host, this.port);
while (!channelFuture.isDone())
{}
if (!channelFuture.isSuccess())
{
Emulator.getLogging().logShutdownLine("Failed to connect to the host (" + this.host + ":" + this.port + ")@" + this.name);
System.exit(0);
}
else
{
Emulator.getLogging().logStart("Started GameServer on " + this.host + ":" + this.port + "@" + this.name);
}
}
public void stop()
{
Emulator.getLogging().logShutdownLine("Stopping " + this.name);
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();
2018-10-07 00:28:00 +02:00
}
catch (Exception e)
{
Emulator.getLogging().logErrorLine("Exception during " + this.name + " shutdown... HARD STOP");
}
Emulator.getLogging().logShutdownLine("GameServer Stopped!");
}
public ServerBootstrap getServerBootstrap()
{
2019-03-18 02:22:00 +01:00
return this.serverBootstrap;
2018-10-07 00:28:00 +02:00
}
public EventLoopGroup getBossGroup()
{
2019-03-18 02:22:00 +01:00
return this.bossGroup;
2018-10-07 00:28:00 +02:00
}
public EventLoopGroup getWorkerGroup()
{
2019-03-18 02:22:00 +01:00
return this.workerGroup;
2018-10-07 00:28:00 +02:00
}
public String getHost()
{
return this.host;
}
public int getPort()
{
return this.port;
}
}