18045010223
21 小时以前 39d4048dc6fd5a138bd1128c06bccca08fbc72f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cn.org.hentai.jtt1078.app;
 
import cn.org.hentai.jtt1078.server.video.Jtt1078Handler;
import cn.org.hentai.jtt1078.server.video.Jtt1078MessageDecoder;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PreDestroy;
 
@Slf4j
@Component
public class VideoServer {
 
    private ServerBootstrap serverBootstrap;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
 
    @Value("${jt1078.video.port}")
    private int port;
 
    @PreDestroy
    public void destroy() {
        shutdown();
    }
 
    public void start() throws Exception {
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.option(ChannelOption.SO_BACKLOG, 102400);
        bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
        workerGroup = new NioEventLoopGroup();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(final SocketChannel channel) {
                        ChannelPipeline p = channel.pipeline();
                        p.addLast(new Jtt1078MessageDecoder());
                        p.addLast(new Jtt1078Handler());
                    }
                });
 
        Channel ch = serverBootstrap.bind("0.0.0.0", port).sync().channel();
        log.info("🎥 Video Server started on port {}", port);
        // 不要 ch.closeFuture().sync(),会阻塞
    }
 
    public void shutdown() {
        try {
            if (bossGroup != null) bossGroup.shutdownGracefully();
            if (workerGroup != null) workerGroup.shutdownGracefully();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}