RuneHive-Game
Loading...
Searching...
No Matches
NetworkService.java
Go to the documentation of this file.
1package com.runehive.game.service;
2
3import com.runehive.Config;
4import com.runehive.RuneHive;
5import com.runehive.net.ServerPipelineInitializer;
6import io.netty.bootstrap.ServerBootstrap;
7import io.netty.buffer.PooledByteBufAllocator;
8import io.netty.channel.ChannelFuture;
9import io.netty.channel.ChannelOption;
10import io.netty.channel.EventLoopGroup;
11import io.netty.channel.WriteBufferWaterMark;
12import io.netty.channel.epoll.Epoll;
13import io.netty.channel.epoll.EpollEventLoopGroup;
14import io.netty.channel.epoll.EpollServerSocketChannel;
15import io.netty.channel.nio.NioEventLoopGroup;
16import io.netty.channel.socket.nio.NioServerSocketChannel;
17import io.netty.util.ResourceLeakDetector;
18import org.apache.logging.log4j.LogManager;
19import org.apache.logging.log4j.Logger;
20
21import java.util.concurrent.TimeUnit;
22
23/**
24 * The bootstrap that will prepare the game and net.
25 * @author Seven
26 */
27public final class NetworkService {
28
29 private static final Logger logger = LogManager.getLogger(NetworkService.class);
30
31 public void start(int port) throws Exception {
32 logger.info("Starting network service on port: " + port);
33
34 ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
35 final EventLoopGroup bossGroup = Epoll.isAvailable() ? new EpollEventLoopGroup(1) : new NioEventLoopGroup(1);
36 final EventLoopGroup workerGroup = Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
37
38 try {
39 ServerBootstrap b = new ServerBootstrap();
40 b.group(bossGroup, workerGroup)
41 .channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
42 .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
43 .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
44 .childOption(ChannelOption.TCP_NODELAY, true)
45 .childOption(ChannelOption.AUTO_READ, true)
46 .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(2 << 16, 2 << 18))
47 .childOption(ChannelOption.SO_SNDBUF, 65536)
48 .childOption(ChannelOption.SO_RCVBUF, 65536)
49 .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30_000)
50 .childOption(ChannelOption.IP_TOS, Config.IP_TOS)
51 .childHandler(new ServerPipelineInitializer());
52
53 ChannelFuture f = b.bind(port).syncUninterruptibly();
54
55 RuneHive.serverStarted.set(true);
56
57 logger.info(String.format("Server built successfully (took %d seconds).", RuneHive.UPTIME.elapsedTime(TimeUnit.SECONDS)));
59 f.channel().closeFuture().sync();
60 } catch (Exception ex) {
61 logger.error("error starting network service.", ex);
62 } finally {
63 bossGroup.shutdownGracefully();
64 workerGroup.shutdownGracefully();
65 }
66 }
67
68}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int IP_TOS
Definition Config.java:101
static final AtomicBoolean serverStarted
Definition RuneHive.java:59
static final Stopwatch UPTIME
Definition RuneHive.java:60
The bootstrap that will prepare the game and net.
The ChannelInitializer implementation that will setup the games networking pipeline.
long elapsedTime(TimeUnit unit)