All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.socket.server.core.FastSocketServerHelper Maven / Gradle / Ivy

package com.fastchar.socket.server.core;

import com.fastchar.core.FastChar;
import com.fastchar.socket.FastSocketServerConfig;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
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.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;

/**
 * @author 沈建(Janesen)
 * @date 2021/4/19 14:01
 */
public class FastSocketServerHelper implements Runnable {

    private boolean start;
    private FastSocketServerConfig serverConfig;
    private final EventLoopGroup parentGroup = new NioEventLoopGroup();
    private final EventLoopGroup childGroup = new NioEventLoopGroup();

    public FastSocketServerConfig getServerConfig() {
        return serverConfig;
    }

    public FastSocketServerHelper setServerConfig(FastSocketServerConfig serverConfig) {
        this.serverConfig = serverConfig;
        return this;
    }

    @Override
    public void run() {
        if (start) {
            return;
        }
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .option(ChannelOption.SO_BACKLOG, 300)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new FastSocketChannelInitializer());

            InetSocketAddress localAddress = new InetSocketAddress(serverConfig.getSocketPort());
            ChannelFuture channelFuture = serverBootstrap.bind(localAddress).sync();
            start = true;

            if (serverConfig.isDebug()) {
                FastChar.getLog().info(this.getClass(), "Socket启动成功!端口:" + localAddress.getPort());
            }

            ChannelFuture closeFuture = channelFuture.channel().closeFuture();
            closeFuture.sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
            start = false;
        }
    }

    public void start() {
        new Thread(this).start();
    }

    public void stop() {
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
        start = false;
        if (serverConfig.isDebug()) {
            FastChar.getLog().info(this.getClass(), "Socket停止成功!端口:" + serverConfig.getSocketPort());
        }
    }

    public boolean isStart() {
        return start;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy