package logic;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import logic.handler.CPrivateChatHandler;
import logic.handler.GreetHandler;
import logic.handler.LogicServerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import protobuf.ParseRegistryMap;
import protobuf.code.PacketDecoder;
import protobuf.code.PacketEncoder;
import protobuf.generate.cli2srv.chat.Chat;
import protobuf.generate.internal.Internal;
import java.net.InetSocketAddress;
/**
* Created by Dell on 2016/2/2.
*/
public class LogicServer {
private static final Logger logger = LoggerFactory.getLogger(LogicServer.class);
public static void startLogicServer(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel)
throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("MessageDecoder", new PacketDecoder());
pipeline.addLast("MessageEncoder", new PacketEncoder());
pipeline.addLast("LogicServerHandler", new LogicServerHandler());
}
});
bindConnectionOptions(bootstrap);
bootstrap.bind(new InetSocketAddress(port)).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future)
throws Exception {
if (future.isSuccess()) {
ParseRegistryMap.initRegistry();
HandlerManager.initHandlers();
logger.info("[LogicServer] Started Successed, waiting for other server connect...");
} else {
logger.error("[LogicServer] Started Failed");}
}});
}
protected static void bindConnectionOptions(ServerBootstrap bootstrap) {
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.childOption(ChannelOption.SO_LINGER, 0);
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
bootstrap.childOption(ChannelOption.SO_REUSEADDR, true); //调试用
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); //心跳机制暂时使用TCP选项,之后再自己实现
}
}