package com.zy.core.netty; 
 | 
  
 | 
  
 | 
import com.zy.core.netty.properties.TcpProperties; 
 | 
import io.netty.bootstrap.ServerBootstrap; 
 | 
import io.netty.buffer.PooledByteBufAllocator; 
 | 
import io.netty.channel.Channel; 
 | 
import io.netty.channel.ChannelOption; 
 | 
import io.netty.channel.EventLoopGroup; 
 | 
import io.netty.channel.nio.NioEventLoopGroup; 
 | 
import io.netty.channel.socket.nio.NioServerSocketChannel; 
 | 
import io.netty.util.ResourceLeakDetector; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Component; 
 | 
  
 | 
import javax.annotation.PostConstruct; 
 | 
import javax.annotation.PreDestroy; 
 | 
  
 | 
/** 
 | 
 * TCP/IP协议端口 
 | 
 * Netty Server 引导类 
 | 
 * @author Vincent 
 | 
 */ 
 | 
@Slf4j 
 | 
@Component 
 | 
public class OnlineServer { 
 | 
  
 | 
    private final HandlerInitializer handlerInitializer; 
 | 
    private final TcpProperties tcpProperties; 
 | 
    private Channel channel; 
 | 
    private ServerBootstrap bootstrap; 
 | 
    private EventLoopGroup bossGroup; 
 | 
    private EventLoopGroup workerGroup; 
 | 
  
 | 
    @Autowired 
 | 
    public OnlineServer(TcpProperties tcpProperties, HandlerInitializer handlerInitializer) { ; 
 | 
        this.tcpProperties = tcpProperties; 
 | 
        this.handlerInitializer = handlerInitializer; 
 | 
    } 
 | 
  
 | 
    { 
 | 
        bootstrap = new ServerBootstrap(); 
 | 
        bossGroup = new NioEventLoopGroup(1); 
 | 
        workerGroup = new NioEventLoopGroup(); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     *  tcp server init 
 | 
     */ 
 | 
    @PostConstruct 
 | 
    public void serverStart() throws Exception { 
 | 
        // 开启大端模式 
 | 
//        CStruct.reverse = false; 
 | 
  
 | 
        bootstrap.group(bossGroup, workerGroup) 
 | 
                .channel(NioServerSocketChannel.class) 
 | 
                .childHandler(handlerInitializer) 
 | 
                .option(ChannelOption.SO_BACKLOG, tcpProperties.getBacklog()) 
 | 
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 
 | 
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 
 | 
                .childOption(ChannelOption.SO_KEEPALIVE, tcpProperties.isKeepAlive()) 
 | 
                .childOption(ChannelOption.SO_SNDBUF, tcpProperties.getSndbuf()) 
 | 
                .childOption(ChannelOption.SO_RCVBUF, tcpProperties.getRcvbuf()); 
 | 
  
 | 
        ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED); 
 | 
  
 | 
        log.info("TCP server started successfully, port:{}", tcpProperties.getPort()); 
 | 
  
 | 
        channel = bootstrap.bind(tcpProperties.getPort()).sync().channel(); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * tcp server stop 
 | 
     */ 
 | 
    @PreDestroy 
 | 
    public void destroy() { 
 | 
        if (channel != null && channel.isActive()) { 
 | 
            channel.close(); 
 | 
        } 
 | 
        if (bossGroup != null) { 
 | 
            bossGroup.shutdownGracefully(); 
 | 
        } 
 | 
        if (workerGroup != null) { 
 | 
            workerGroup.shutdownGracefully(); 
 | 
        } 
 | 
        log.info("TCP server stopped successfully, port: {}", tcpProperties.getPort()); 
 | 
    } 
 | 
  
 | 
} 
 |