jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.protocol.quantity.shuhan.server;
 
import com.ld.igds.protocol.quantity.shuhan.ShuHanUtils;
import com.ld.io.api.*;
import com.ld.io.netty.NettyServer;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
 
/**
 * 配置主服务信息
 *
 * @author vince
 */
@Slf4j
public class ShuhanServerEngine {
 
    public static int PORT = 9303;
 
    public static NettyServer ioServer = null;
    // 配置消息接收类
    private static MsgConsumer messageConsume = new MsgConsumer();
    // 监听会话的创建与销毁
    private static SessionListener ioSessionListener = new SessionListener();
 
    // 心跳提供
    private static HeartbeatProvider heartbeatProvider = new HeartbeatProviderImpl();
 
    public static void start() throws Exception {
        start(PORT);
    }
 
    public static void start(int port) throws Exception {
        // 配置Server的配置
        IoServerOption ioServerOption = new IoServerOption(port);
 
        ioServerOption
                .setSplitDecoderType(SplitByteDecoderType.DELIMITER_SYMBOL);
        
        ioServerOption.setDelimiter(ShuHanUtils.MSG_END
                .getBytes(ShuHanUtils.CHARSET));
 
        // 配置心跳执行时间
        ioServerOption.setReaderIdleTime(ShuHanUtils.HEART_BEAT_TIME);
 
        ioServer = new NettyServer(ioServerOption, messageConsume,
                ioSessionListener, heartbeatProvider);
 
        ioServer.startup();
 
        log.info("* ========================");
        log.info("* ");
        log.info("* 数量监测服务启动,端口号={}", port);
        log.info("* ");
        log.info("* ========================");
 
        System.out.println("* ========================");
        System.out.println("* ");
        System.out.println("* 数量监测服务启动,端口号=" + port);
        System.out.println("* ");
        System.out.println("* ========================");
    }
 
    /**
     * 发送信息
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public static InvokeResult push(String ip, int port, byte[] msg) {
        IoSession session = getSession(ip, port);
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(msg);
    }
 
    /**
     * 主动销毁当前连接
     *
     * @param ip
     * @param port
     * @return
     */
    public static InvokeResult destroy(String ip, int port) {
        IoSession session = getSession(ip, port);
 
        if (null == session) {
            return InvokeResult.SUCCESS;
        }
        // 执行连接销毁
        session.destroy();
        return InvokeResult.SUCCESS;
    }
 
    /**
     * 根据SessionKey获取Session信息
     *
     * @param ip
     * @return
     */
    public static IoSession getSession(String ip, int port) {
        IoSessionQuery sessionFactory = ioServer.getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        for (IoSession ioSession : sessions) {
            if (ip.equals(ioSession.getAddress())
                    && port == ioSession.getPort()) {
                return ioSession;
            }
        }
        return null;
    }
}