99import io .netty .bootstrap .Bootstrap ;
1010import io .netty .buffer .ByteBufAllocator ;
1111import io .netty .buffer .PooledByteBufAllocator ;
12+ import io .netty .channel .AdaptiveRecvByteBufAllocator ;
1213import io .netty .channel .ChannelFuture ;
1314import io .netty .channel .ChannelFutureListener ;
1415import io .netty .channel .ChannelOption ;
1516import io .netty .channel .EventLoopGroup ;
16- import io .netty .channel .FixedRecvByteBufAllocator ;
1717import io .netty .channel .epoll .Epoll ;
1818import io .netty .channel .epoll .EpollDatagramChannel ;
1919import io .netty .channel .epoll .EpollEventLoopGroup ;
20- import io .netty .channel .nio .NioEventLoopGroup ;
2120import io .netty .channel .socket .InternetProtocolFamily ;
22- import io .netty .channel .socket .nio .NioDatagramChannel ;
2321import io .netty .channel .unix .UnixChannelOption ;
2422import org .apache .logging .log4j .LogManager ;
2523import org .apache .logging .log4j .Logger ;
3331public final class Main {
3432 private static final Logger logger = LogManager .getLogger (Main .class );
3533
36- public static final ByteBufAllocator BYTE_BUF_ALLOCATOR = new PooledByteBufAllocator ( true ) ;
34+ public static final ByteBufAllocator BYTE_BUF_ALLOCATOR = PooledByteBufAllocator . DEFAULT ;
3735 public static EventLoopGroup eventLoopGroup ;
3836 private static Stats stats ;
3937 private static CacheCleaner cacheCleaner ;
@@ -48,53 +46,39 @@ public static void main(String[] args) {
4846 Config .setup (args );
4947
5048 // Use Epoll when available
51- if (Config .Transport .equalsIgnoreCase ("epoll" )) {
52- if (Epoll .isAvailable ()) {
53- eventLoopGroup = new EpollEventLoopGroup (Config .Threads );
54- } else {
55- // Epoll is requested but Epoll is not available so we'll throw error and shut down.
56- System .err .println ("Epoll Transport is not available, shutting down..." );
57- System .exit (1 );
58- }
59- } else if (Config .Transport .equalsIgnoreCase ("nio" )) {
60- eventLoopGroup = new NioEventLoopGroup (Config .Threads );
49+ if (Epoll .isAvailable ()) {
50+ eventLoopGroup = new EpollEventLoopGroup (Config .Threads );
6151 } else {
62- System .err .println ("Invalid Transport Type: " + Config .Transport + ", shutting down..." );
52+ // Epoll is requested but Epoll is not available so we'll throw error and shut down.
53+ System .err .println ("Epoll Transport is not available, shutting down..." );
6354 System .exit (1 );
6455 }
6556
6657 List <ChannelFuture > channelFutureList = new ArrayList <>();
6758
6859 Bootstrap bootstrap = new Bootstrap ()
6960 .group (eventLoopGroup )
70- .channelFactory (() -> {
71- if (Config .Transport .equalsIgnoreCase ("epoll" )) {
72- return new EpollDatagramChannel (InternetProtocolFamily .IPv4 );
73- } else {
74- return new NioDatagramChannel (InternetProtocolFamily .IPv4 );
75- }
76- })
61+ .channelFactory (() -> new EpollDatagramChannel (InternetProtocolFamily .IPv4 ))
7762 .option (ChannelOption .ALLOCATOR , BYTE_BUF_ALLOCATOR )
7863 .option (ChannelOption .SO_SNDBUF , Config .SendBufferSize )
7964 .option (ChannelOption .SO_RCVBUF , Config .ReceiveBufferSize )
80- .option (ChannelOption .RCVBUF_ALLOCATOR , new FixedRecvByteBufAllocator ( Config . FixedReceiveAllocatorBufferSize ))
65+ .option (ChannelOption .RCVBUF_ALLOCATOR , new AdaptiveRecvByteBufAllocator ( ))
8166 .option (UnixChannelOption .SO_REUSEPORT , true )
8267 .handler (new Handler ());
8368
8469 for (int i = 0 ; i < Config .Threads ; i ++) {
8570 // Bind and Start Server
86- ChannelFuture channelFuture = bootstrap .bind (Config .LocalServer .getAddress (), Config .LocalServer .getPort ())
87- .addListener ((ChannelFutureListener ) future -> {
88- if (future .isSuccess ()) {
89- logger .atInfo ().log ("Server Started on Address: {}:{}" ,
90- ((InetSocketAddress ) future .channel ().localAddress ()).getAddress ().getHostAddress (),
91- ((InetSocketAddress ) future .channel ().localAddress ()).getPort ());
92- } else {
93- logger .error ("Caught Error While Starting Server" , future .cause ());
94- System .err .println ("Shutting down..." );
95- System .exit (1 );
96- }
97- });
71+ ChannelFuture channelFuture = bootstrap .bind (Config .LocalServer .getAddress (), Config .LocalServer .getPort ()).addListener ((ChannelFutureListener ) future -> {
72+ if (future .isSuccess ()) {
73+ logger .atInfo ().log ("Server Started on Address: {}:{}" ,
74+ ((InetSocketAddress ) future .channel ().localAddress ()).getAddress ().getHostAddress (),
75+ ((InetSocketAddress ) future .channel ().localAddress ()).getPort ());
76+ } else {
77+ logger .error ("Caught Error While Starting Server" , future .cause ());
78+ System .err .println ("Shutting down..." );
79+ System .exit (1 );
80+ }
81+ });
9882
9983 channelFutureList .add (channelFuture );
10084 }
0 commit comments