package com.yfd.platform.component.voiceserver;

import com.yfd.platform.component.nettyserver.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VoiceServer {

    private static final Logger logger = LoggerFactory.getLogger(VoiceServer.class);
    private final int port;

    public VoiceServer(int port) {
        this.port = port;
    }
    public void run() throws Exception {
        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop

        try {
            ServerBootstrap b = new ServerBootstrap();

            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //获取到pipeline
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new AudioDataDecoder());
                            //加入自己的业务处理handler
                            pipeline.addLast(new VoicePacketHandler());

                        }
                    });
            logger.info("Voice TCP server is started!（port:" + port + "）");
            ChannelFuture channelFuture = b.bind(port).sync();
            //监听关闭
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
    //    public VoiceServer(int port) {
    //        this.port = port;
    //    }
    //
    //    public void run() throws Exception {
    //        EventLoopGroup bossGroup = new NioEventLoopGroup();
    //        EventLoopGroup workerGroup = new NioEventLoopGroup();
    //        try {
    //            ServerBootstrap b = new ServerBootstrap();
    //            b.group(bossGroup, workerGroup)
    //             .channel(NioServerSocketChannel.class)
    //             .childHandler(new ChannelInitializer<SocketChannel>() {
    //                 @Override
    //                 public void initChannel(SocketChannel ch) {
    //                     //获取到pipeline
    //                     ChannelPipeline pipeline = ch.pipeline();
    //                     pipeline.addLast(new AudioDataDecoder());
    //                     pipeline.addLast(new VoicePacketHandler());
    //                 }
    //             });
    //
    //            ChannelFuture f = b.bind(port).sync();
    //            f.channel().closeFuture().sync();
    //        } finally {
    //            workerGroup.shutdownGracefully();
    //            bossGroup.shutdownGracefully();
    //        }
    //    }

}
