// Path: cn.ctodb.push.server/src/main/java/cn/ctodb/push/server/ServerHandler.java
// @Sharable
// public class ServerHandler extends ChannelInboundHandlerAdapter {
//     private Logger logger = LoggerFactory.getLogger(ServerHandler.class);
//     public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
// 
//     private PacketReceiver packetReceiver;
// 
//     public ServerHandler(PacketReceiver packetReceiver) {
//         this.packetReceiver = packetReceiver;
//     }
// 
//     @Override
//     public void handlerAdded(ChannelHandlerContext ctx) { // (2)
//         Channel incoming = ctx.channel();
//         logger.debug("handlerAdded : {}", incoming.remoteAddress());
//         channels.add(ctx.channel());
//     }
// 
//     @Override
//     public void handlerRemoved(ChannelHandlerContext ctx) { // (3)
//         Channel incoming = ctx.channel();
//         logger.debug("handlerRemoved : {}", incoming.remoteAddress());
//         channels.remove(ctx.channel());
//     }
// 
//     @Override
//     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // (4)
//         Packet packet = (Packet) msg;
//         Connection connection = new Connection();
//         connection.setChc(ctx);
//         Channel channel = ctx.channel();
//         packetReceiver.onReceive(packet, connection);
//     }
// 
//     @Override
//     public void channelActive(ChannelHandlerContext ctx) throws Exception { // (5)
//         Channel incoming = ctx.channel();
//         logger.debug("channelActive : {}", incoming.remoteAddress());
//     }
// 
//     @Override
//     public void channelInactive(ChannelHandlerContext ctx) throws Exception { // (6)
//         Channel incoming = ctx.channel();
//         logger.debug("channelInactive : {}", incoming.remoteAddress());
//     }
// 
//     @Override
//     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//         // 当出现异常就关闭连接
//         Channel incoming = ctx.channel();
//         logger.error("exceptionCaught : " + incoming.remoteAddress(), cause);
//         ctx.close();
//     }
// }
// 
// Path: cn.ctodb.push.core/src/main/java/cn/ctodb/push/utils/MsgPackDecode.java
// @Sharable
// public class MsgPackDecode extends MessageToMessageDecoder<ByteBuf> {
//     @Override
//     protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
//         final int length = msg.readableBytes();
//         final byte[] array = new byte[length];
//         msg.getBytes(msg.readerIndex(), array, 0, length);
//         out.add(new MessagePack().read(array, Packet.class));
//     }
// }
// 
// Path: cn.ctodb.push.core/src/main/java/cn/ctodb/push/utils/MsgPackEncode.java
// @Sharable
// public class MsgPackEncode extends MessageToByteEncoder<Packet> {
// 
//     @Override
//     protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception {
//         out.writeBytes(new MessagePack().write(packet));
//     }
// 
// }


import cn.ctodb.push.server.ServerHandler;
import cn.ctodb.push.utils.MsgPackDecode;
import cn.ctodb.push.utils.MsgPackEncode;
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 io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import java.util.UUID;
package cn.ctodb.push.server.service;



/**
 * All rights Reserved, Designed By www.ctodb.cn
 *
 * @version V1.0
 * @author: lichaohn@163.com
 * @Copyright: 2018 www.ctodb.cn Inc. All rights reserved.
 */
public class MgsServer implements DisposableBean, Runnable {

    private static final Logger logger = LoggerFactory.getLogger(MgsServer.class);

    /**
     * 用于分配处理业务线程的线程组个数
     */
    protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2; // 默认
    /**
     * 业务出现线程大小
     */
    protected static final int BIZTHREADSIZE = 4;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    private Channel channel;
    private final int port;
    private String status = "stop";
    private String id;

// Path: cn.ctodb.push.server/src/main/java/cn/ctodb/push/server/ServerHandler.java
// @Sharable
// public class ServerHandler extends ChannelInboundHandlerAdapter {
//     private Logger logger = LoggerFactory.getLogger(ServerHandler.class);
//     public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
// 
//     private PacketReceiver packetReceiver;
// 
//     public ServerHandler(PacketReceiver packetReceiver) {
//         this.packetReceiver = packetReceiver;
//     }
// 
//     @Override
//     public void handlerAdded(ChannelHandlerContext ctx) { // (2)
//         Channel incoming = ctx.channel();
//         logger.debug("handlerAdded : {}", incoming.remoteAddress());
//         channels.add(ctx.channel());
//     }
// 
//     @Override
//     public void handlerRemoved(ChannelHandlerContext ctx) { // (3)
//         Channel incoming = ctx.channel();
//         logger.debug("handlerRemoved : {}", incoming.remoteAddress());
//         channels.remove(ctx.channel());
//     }
// 
//     @Override
//     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // (4)
//         Packet packet = (Packet) msg;
//         Connection connection = new Connection();
//         connection.setChc(ctx);
//         Channel channel = ctx.channel();
//         packetReceiver.onReceive(packet, connection);
//     }
// 
//     @Override
//     public void channelActive(ChannelHandlerContext ctx) throws Exception { // (5)
//         Channel incoming = ctx.channel();
//         logger.debug("channelActive : {}", incoming.remoteAddress());
//     }
// 
//     @Override
//     public void channelInactive(ChannelHandlerContext ctx) throws Exception { // (6)
//         Channel incoming = ctx.channel();
//         logger.debug("channelInactive : {}", incoming.remoteAddress());
//     }
// 
//     @Override
//     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//         // 当出现异常就关闭连接
//         Channel incoming = ctx.channel();
//         logger.error("exceptionCaught : " + incoming.remoteAddress(), cause);
//         ctx.close();
//     }
// }
// 
// Path: cn.ctodb.push.core/src/main/java/cn/ctodb/push/utils/MsgPackDecode.java
// @Sharable
// public class MsgPackDecode extends MessageToMessageDecoder<ByteBuf> {
//     @Override
//     protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
//         final int length = msg.readableBytes();
//         final byte[] array = new byte[length];
//         msg.getBytes(msg.readerIndex(), array, 0, length);
//         out.add(new MessagePack().read(array, Packet.class));
//     }
// }
// 
// Path: cn.ctodb.push.core/src/main/java/cn/ctodb/push/utils/MsgPackEncode.java
// @Sharable
// public class MsgPackEncode extends MessageToByteEncoder<Packet> {
// 
//     @Override
//     protected void encode(ChannelHandlerContext ctx, Packet packet, ByteBuf out) throws Exception {
//         out.writeBytes(new MessagePack().write(packet));
//     }
// 
// }


// Path: cn.ctodb.push.server/src/main/java/cn/ctodb/push/server/service/MgsServer.java
import cn.ctodb.push.server.ServerHandler;
import cn.ctodb.push.utils.MsgPackDecode;
import cn.ctodb.push.utils.MsgPackEncode;
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 io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import java.util.UUID;

package cn.ctodb.push.server.service;



/**
 * All rights Reserved, Designed By www.ctodb.cn
 *
 * @version V1.0
 * @author: lichaohn@163.com
 * @Copyright: 2018 www.ctodb.cn Inc. All rights reserved.
 */
public class MgsServer implements DisposableBean, Runnable {

    private static final Logger logger = LoggerFactory.getLogger(MgsServer.class);

    /**
     * 用于分配处理业务线程的线程组个数
     */
    protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2; // 默认
    /**
     * 业务出现线程大小
     */
    protected static final int BIZTHREADSIZE = 4;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    private Channel channel;
    private final int port;
    private String status = "stop";
    private String id;

    public MgsServer(int port, ServerHandler serverHandler, MsgPackDecode msgPackDecode, MsgPackEncode msgPackEncode) {