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
package com.ld.igds.io.fzzy.server;
 
import com.ld.igds.io.fzzy.ServerUtils;
import com.ld.io.api.InvokeResult;
import com.ld.io.api.IoServerOption;
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionQuery;
import com.ld.io.netty.NettyServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
/**
 * 风正致远协议Server启动类
 */
@Slf4j
@Service
public class BhznQuantityServerEngine {
 
    public static final Integer PORT = 9306;
 
    @Autowired
    private BhznQuantityMessageConsumer defaultMessageConsumer;
    @Autowired
    private BhznQuantitySessionListener defaultSessionListener;
 
    private IoSessionQuery sessionQuery;
 
    public void start(Integer port) {
        IoServerOption ioServerOption = new IoServerOption();
 
        if (null != port) {
            ioServerOption.setPort(port);
        } else {
            ioServerOption.setPort(PORT);
        }
 
        ioServerOption.setDelimiter(ServerUtils.MSG_END.getBytes());
 
        NettyServer ioServer = new NettyServer(ioServerOption, defaultMessageConsumer, defaultSessionListener);
        ioServer.startup();
 
        sessionQuery = ioServer.getSessionQuery();
        log.info("* ");
        log.info("* ========================");
        log.info("* ");
        log.info("* [QUANTITY-FZZY-SERVER:FZZY_TCP_QUANTITY_V3.5,PORT={}]", PORT);
        log.info("* ");
        log.info("* ========================");
        log.info("* ");
 
        System.out.println("* ========================");
        System.out.println("* ");
        System.out.println("* [QUANTITY-FZZY-SERVER:FZZY_TCP_QUANTITY_V3.5,PORT={}]" + port);
        System.out.println("* ");
        System.out.println("* ========================");
    }
 
    public IoSessionQuery getSessionQuery() {
        return sessionQuery;
    }
 
    /**
     * 发送信息
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public InvokeResult push(String ip, int port, byte[] msg) {
        IoSessionQuery sessionFactory = getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ServerUtils.getServerKey(ip, port).equals(ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
        if (null == session) {
            return InvokeResult.CHANNEL_CLOSED;
        }
        return session.invoke(msg);
    }
 
    /**
     * 主动销毁当前连接
     *
     * @param ip
     * @param port
     * @return
     */
    public InvokeResult destroy(String ip, int port) {
        IoSessionQuery sessionFactory = getSessionQuery();
        List<IoSession> sessions = sessionFactory.getAllSession();
        IoSession session = null;
        for (IoSession ioSession : sessions) {
            if (ServerUtils.getServerKey(ip, port).equals(ioSession.getBusinessKey())) {
                session = ioSession;
                break;
            }
        }
 
        if (null == session) {
            return InvokeResult.SUCCESS;
        }
        // 执行连接销毁
        session.destroy();
        return InvokeResult.SUCCESS;
    }
}