dumb server
This commit is contained in:
29
build.gradle
29
build.gradle
@@ -13,3 +13,32 @@ repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
ext {
|
||||
slf4j_version = '1.7.30'
|
||||
logback_version = '1.2.3'
|
||||
|
||||
library = [
|
||||
guice: ['com.google.inject:guice:4.1.0'],
|
||||
logger: ["ch.qos.logback:logback-core:$logback_version",
|
||||
"ch.qos.logback:logback-classic:$logback_version"],
|
||||
lombok: ['org.projectlombok:lombok:1.18.2'],
|
||||
netty: ['io.netty:netty-all:4.1.22.Final'],
|
||||
slf4j: ["org.slf4j:slf4j-api:$slf4j_version",
|
||||
"org.slf4j:jcl-over-slf4j:$slf4j_version"],
|
||||
]
|
||||
}
|
||||
|
||||
dependencies {
|
||||
/* LOGGER */
|
||||
implementation library.slf4j
|
||||
implementation library.logger
|
||||
|
||||
/* LOMBOK */
|
||||
annotationProcessor library.lombok
|
||||
compileOnly library.lombok
|
||||
|
||||
/* COMPONENTS */
|
||||
implementation library.guice
|
||||
implementation library.netty
|
||||
}
|
||||
@@ -1,8 +1,16 @@
|
||||
package mc.server;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import mc.server.config.NetworkModule;
|
||||
import mc.server.network.Server;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("hello?");
|
||||
final Injector injector = Guice.createInjector(new NetworkModule());
|
||||
|
||||
final Server server = injector.getInstance(Server.class);
|
||||
server.start("127.0.0.1", 25565);
|
||||
}
|
||||
}
|
||||
|
||||
63
src/main/java/mc/server/config/NetworkModule.java
Normal file
63
src/main/java/mc/server/config/NetworkModule.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package mc.server.config;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.name.Named;
|
||||
import com.google.inject.name.Names;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
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 mc.server.network.Server;
|
||||
import mc.server.network.impl.NettyServer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class NetworkModule extends AbstractModule {
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(Server.class).to(NettyServer.class).in(Singleton.class);
|
||||
bind(EventLoopGroup.class).annotatedWith(Names.named("bossGroup")).toInstance(new NioEventLoopGroup(1));
|
||||
bind(EventLoopGroup.class).annotatedWith(Names.named("workerGroup")).toInstance(new NioEventLoopGroup());
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
ServerBootstrap serverBootstrap(@Named("bossGroup") EventLoopGroup bossGroup,
|
||||
@Named("workerGroup") EventLoopGroup workerGroup,
|
||||
ChannelInitializer<SocketChannel> channelChannelInitializer) {
|
||||
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||
|
||||
bootstrap.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(channelChannelInitializer);
|
||||
|
||||
return bootstrap;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
ChannelInitializer<SocketChannel> channelChannelInitializer(List<ChannelHandler> channelHandlerList) {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
final ChannelPipeline pipeline = socketChannel.pipeline();
|
||||
channelHandlerList.forEach(pipeline::addLast);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
List<ChannelHandler> channelHandlerList() {
|
||||
return Collections.singletonList(new LoggingHandler());
|
||||
}
|
||||
}
|
||||
6
src/main/java/mc/server/network/Server.java
Normal file
6
src/main/java/mc/server/network/Server.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package mc.server.network;
|
||||
|
||||
public interface Server {
|
||||
|
||||
void start(String host, int port);
|
||||
}
|
||||
29
src/main/java/mc/server/network/impl/NettyServer.java
Normal file
29
src/main/java/mc/server/network/impl/NettyServer.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package mc.server.network.impl;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.server.network.Server;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class NettyServer implements Server {
|
||||
|
||||
@Inject
|
||||
private Provider<ServerBootstrap> serverBootstrapProvider;
|
||||
|
||||
@Override
|
||||
public void start(String host, int port) {
|
||||
try {
|
||||
log.info("Network starting: {}:{}", host, port);
|
||||
serverBootstrapProvider.get()
|
||||
.bind(host, port)
|
||||
.sync().channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("{}: {}", e.getClass().getSimpleName(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user