Archived
0

Simple server

This commit is contained in:
2018-04-10 02:49:22 +03:00
parent e851666bea
commit 858cc2965f
2 changed files with 58 additions and 1 deletions

View 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);
}
}
}

View File

@@ -10,5 +10,5 @@
<property name="port" value="25565"/> <property name="port" value="25565"/>
</bean> </bean>
<bean id="server" class="mc.core.netty.proto_1122.NettyServer"/> <bean id="server" class="mc.core.netty.proto_125.NettyServer"/>
</beans> </beans>