18045010223
2025-07-07 4d55075574ff1d55c1f56f89f5f3f95889258914
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cn.org.hentai.jtt1078.app;
 
import cn.org.hentai.jtt1078.http.GeneralResponseWriter;
import cn.org.hentai.jtt1078.http.NettyHttpServerHandler;
import cn.org.hentai.jtt1078.publisher.PublishManager;
import cn.org.hentai.jtt1078.server.Jtt1078Handler;
import cn.org.hentai.jtt1078.server.Jtt1078MessageDecoder;
import cn.org.hentai.jtt1078.server.SessionManager;
import cn.org.hentai.jtt1078.util.Configs;
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.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
import sun.misc.SignalHandler;
import io.netty.handler.timeout.IdleStateHandler;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;
 
/**
 * Created by matrixy on 2019/4/9.
 */
public class VideoServerApp
{
    private static Logger logger = LoggerFactory.getLogger(VideoServerApp.class);
 
    public static void main(String[] args) throws Exception
    {
        Configs.init("/app.properties");
        PublishManager.init();
        SessionManager.init();
 
        VideoServer videoServer = new VideoServer();
        HttpServer httpServer = new HttpServer();
 
        Signal.handle(new Signal("TERM"), new SignalHandler()
        {
            @Override
            public void handle(Signal signal)
            {
                videoServer.shutdown();
                httpServer.shutdown();
            }
        });
 
        videoServer.start();
        httpServer.start();
    }
 
    static class VideoServer
    {
        private static ServerBootstrap serverBootstrap;
 
        private static EventLoopGroup bossGroup;
        private static EventLoopGroup workerGroup;
 
        private static void start() throws Exception
        {
            serverBootstrap = new ServerBootstrap();
            serverBootstrap.option(ChannelOption.SO_BACKLOG, Configs.getInt("server.backlog", 102400));
            bossGroup = new NioEventLoopGroup(Configs.getInt("server.worker-count", Runtime.getRuntime().availableProcessors()));
            workerGroup = new NioEventLoopGroup();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(final SocketChannel channel) throws Exception {
                            ChannelPipeline p = channel.pipeline();
                            // p.addLast(new IdleStateHandler(10,0,0, TimeUnit.SECONDS));
                            p.addLast(new Jtt1078MessageDecoder());
                            // p.addLast(new Jtt808MessageEncoder());
                            // p.addLast(new JTT808Handler());
                            p.addLast(new Jtt1078Handler());
                        }
                    });
 
            int port = Configs.getInt("server.port", 1078);
            Channel ch = serverBootstrap.bind(InetAddress.getByName("0.0.0.0"), port).sync().channel();
            logger.info("Video Server started at: {}", port);
            ch.closeFuture();
        }
 
        private static void shutdown()
        {
            try
            {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
 
    static class HttpServer
    {
        private static ServerBootstrap serverBootstrap;
 
        private static EventLoopGroup bossGroup;
        private static EventLoopGroup workerGroup;
 
        private static void start() throws Exception
        {
            bossGroup = new NioEventLoopGroup();
            workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
 
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>()
                    {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception
                        {
                            ch.pipeline().addLast(
                                    new GeneralResponseWriter(),
                                    new HttpResponseEncoder(),
                                    new HttpRequestDecoder(),
                                    new HttpObjectAggregator(1024 * 64),
                                    new NettyHttpServerHandler()
                            );
                        }
                    }).option(ChannelOption.SO_BACKLOG, 1024)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            try
            {
                int port = Configs.getInt("server.http.port", 3333);
                ChannelFuture f = bootstrap.bind(InetAddress.getByName("0.0.0.0"), port).sync();
                logger.info("HTTP Server started at: {}", port);
                f.channel().closeFuture().sync();
            }
            catch (InterruptedException e)
            {
                logger.error("http server error", e);
            }
            finally
            {
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }
        }
 
        private static void shutdown()
        {
            try
            {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}