Archived
0

dumb server

This commit is contained in:
2020-05-09 22:28:43 +03:00
parent e3d041de63
commit 7494c3f92c
5 changed files with 136 additions and 1 deletions

View File

@@ -13,3 +13,32 @@ repositories {
mavenLocal() mavenLocal()
mavenCentral() 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
}

View File

@@ -1,8 +1,16 @@
package mc.server; 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 class Main {
public static void main(String[] args) { 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);
} }
} }

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

View File

@@ -0,0 +1,6 @@
package mc.server.network;
public interface Server {
void start(String host, int port);
}

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