Channel handlers as beans
This commit is contained in:
@@ -12,10 +12,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
@Slf4j
|
||||
public class Main {
|
||||
public static ApplicationContext appContext; //FIXME
|
||||
|
||||
public static void main(String[] args) {
|
||||
appContext = new ClassPathXmlApplicationContext("spring.xml");
|
||||
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
|
||||
Config config = appContext.getBean("config", Config.class);
|
||||
|
||||
Server server = appContext.getBean("server", Server.class);
|
||||
|
||||
@@ -5,30 +5,38 @@
|
||||
package mc.core.network.proto_125.netty;
|
||||
|
||||
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 lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.network.Server;
|
||||
import mc.core.network.StartServerException;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class NettyServer implements Server {
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
private EventLoopGroup bossGroup, workerGroup;
|
||||
|
||||
private ChannelInitializer buildChannelInitializer() {
|
||||
return new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(SocketChannel socketChannel) {
|
||||
socketChannel.pipeline().addLast(
|
||||
new LoggingHandler(),
|
||||
new PacketDecoder(),
|
||||
new PacketHandler(),
|
||||
new PacketEncoder()
|
||||
);
|
||||
Map<String, ChannelHandler> beans = applicationContext.getBeansOfType(ChannelHandler.class);
|
||||
beans.forEach(socketChannel.pipeline()::addLast);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -47,7 +55,7 @@ public class NettyServer implements Server {
|
||||
public void start(String host, int port) throws StartServerException {
|
||||
log.info("Use protocol 1.2.5");
|
||||
bossGroup = new NioEventLoopGroup(1);
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
workerGroup = new NioEventLoopGroup(); //TODO сделать изменяемым
|
||||
|
||||
ServerBootstrap serverBootstrap = buildServerBootstrap();
|
||||
|
||||
|
||||
@@ -15,6 +15,9 @@ import mc.core.network.proto_125.packets.HandshakePacket;
|
||||
import mc.core.network.proto_125.packets.KickPacket;
|
||||
import mc.core.network.proto_125.packets.LoginPacket;
|
||||
import mc.core.network.proto_125.packets.PingPacket;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
@@ -22,7 +25,8 @@ import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
private static Config config = Main.appContext.getBean("config", Config.class); //FIXME
|
||||
@Autowired
|
||||
private Config config;
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
<context:annotation-config />
|
||||
|
||||
<bean id="config" class="mc.core.embedded.ConfigFromSpring">
|
||||
<property name="descriptionServer" value="MC Core"/>
|
||||
<property name="maxPlayers" value="100"/>
|
||||
@@ -10,5 +13,11 @@
|
||||
<property name="port" value="25565"/>
|
||||
</bean>
|
||||
|
||||
<!-- Netty Server -->
|
||||
<!--<bean id="pipeline.log" class="io.netty.handler.logging.LoggingHandler" scope="prototype"/>-->
|
||||
<bean id="pipeline.decoder" class="mc.core.network.proto_125.netty.PacketDecoder" scope="prototype"/>
|
||||
<bean id="pipeline.encoder" class="mc.core.network.proto_125.netty.PacketEncoder" scope="prototype"/>
|
||||
<bean id="pipeline.handler" class="mc.core.network.proto_125.netty.PacketHandler" scope="prototype"/>
|
||||
|
||||
<bean id="server" class="mc.core.network.proto_125.netty.NettyServer"/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user