Simple server
This commit is contained in:
57
src/main/java/mc/core/netty/proto_125/NettyServer.java
Normal file
57
src/main/java/mc/core/netty/proto_125/NettyServer.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-10
|
||||
*/
|
||||
package mc.core.netty.proto_125;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Server;
|
||||
import mc.core.StartServerException;
|
||||
|
||||
@Slf4j
|
||||
public class NettyServer implements Server {
|
||||
private EventLoopGroup bossGroup, workerGroup;
|
||||
|
||||
private ChannelInitializer buildChannelInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
socketChannel.pipeline().addLast(
|
||||
new LoggingHandler()
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ServerBootstrap buildServerBootstrap() {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
|
||||
bootstrap.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(buildChannelInitializer());
|
||||
|
||||
return bootstrap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(String host, int port) throws StartServerException {
|
||||
log.info("Use protocol 1.2.5");
|
||||
bossGroup = new NioEventLoopGroup(1);
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
|
||||
ServerBootstrap serverBootstrap = buildServerBootstrap();
|
||||
|
||||
try {
|
||||
serverBootstrap.bind(host, port).sync().channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
throw new StartServerException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,5 +10,5 @@
|
||||
<property name="port" value="25565"/>
|
||||
</bean>
|
||||
|
||||
<bean id="server" class="mc.core.netty.proto_1122.NettyServer"/>
|
||||
<bean id="server" class="mc.core.netty.proto_125.NettyServer"/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user