jiazx0107
2025-09-01 ce4f9b9f72a4269a1f25812dadd59bfb92c7b3cf
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
package com.ld.io.netty;
 
import com.ld.io.api.*;
 
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.ByteToMessageDecoder;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
 
public class NettyServer implements Runnable, IoServer {
    private EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    private EventLoopGroup workerGroup = new NioEventLoopGroup();
 
    private IoServerOption ioServerOption;
    private NettySessionFactory nettySessionFactory;
    private ReadMessageHandler readMessageHandler;
    private SplitDecoderFactory splitDecoderFactory;
 
    public NettyServer(IoServerOption ioServerOption,
                       IoMsgConsumer ioMsgConsumer,
                       IoSessionListener sessionListener) {
        this(ioServerOption, ioMsgConsumer, sessionListener, null);
    }
 
    public NettyServer(IoServerOption ioServerOption,
                       IoMsgConsumer ioMsgConsumer,
                       IoSessionListener sessionListener,
                       HeartbeatProvider heartbeatMessageProvider) {
        this.splitDecoderFactory = new SplitDecoderFactory();
        this.nettySessionFactory = new NettySessionFactory(sessionListener);
        this.ioServerOption = ioServerOption;
 
        this.readMessageHandler = new ReadMessageHandler(ioMsgConsumer, nettySessionFactory, heartbeatMessageProvider);
    }
 
    public void startup() {
        Thread thread = new Thread(this);
        thread.start();
    }
 
    public void shutdown() {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
 
    @Override
    public void run() {
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ioServerOption.getConnectTimeoutMillis())
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            ByteToMessageDecoder splitDecoder = splitDecoderFactory.build(ioServerOption);
                            if(splitDecoder != null)
                            p.addLast("splitDecoder", splitDecoder);
                            p.addLast("idleStateHandler", new IdleStateHandler(ioServerOption.getReaderIdleTime(), 0, 0));
                            p.addLast("byteArrayDecoder", new ByteArrayDecoder());//字符串解码 和 编码
                            p.addLast("byteArrayEncoder", new ByteArrayEncoder());
                            p.addLast("readMessageHandler", readMessageHandler);//读取信息
                        }
                    });
 
            // Bind and start to accept incoming connections.
            ChannelFuture channelFuture = b.bind(ioServerOption.getPort()).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
 
    public IoSessionQuery getSessionQuery() {
        return nettySessionFactory;
    }
}