vince
2024-04-26 b1c572949997a5d82d9b609163ff280a1c49627d
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
package com.fzzy.protocol.fzzy.server;
 
import com.ld.io.api.*;
import com.ld.io.netty.NettyServer;
import lombok.extern.slf4j.Slf4j;
 
import java.util.List;
 
/**
 * 风正致远-TCP主服务
 */
@Slf4j
public class FzzyServerEngine {
 
    public static final int default_port = 19300;
 
    public static NettyServer ioServer = null;
 
    // 配置消息接收类
    private static MsgConsumer messageConsume = new MsgConsumer();
    // 监听会话的创建与销毁
    private static SessionListener ioSessionListener = new SessionListener();
 
 
    public static void start(Integer port) throws InterruptedException {
 
        if (null == port) port = default_port;
 
        // 配置Server的配置
        IoServerOption ioServerOption = new IoServerOption(port);
 
        ioServerOption.setSplitDecoderType(SplitByteDecoderType.DELIMITER_SYMBOL);
 
        ioServerOption.setDelimiter(ServerUtils.MSG_END.getBytes());
 
        ioServer = new NettyServer(ioServerOption, messageConsume, ioSessionListener);
 
        ioServer.startup();
 
 
        log.info("* ========================");
        log.info("* ");
        log.info("* FZZY-TCP-V3.0,端口号={}", port);
        log.info("* ");
        log.info("* ========================");
 
        System.out.println("* ========================");
        System.out.println("* ");
        System.out.println("* FZZY-TCP-V3.0,端口号=" + port);
        System.out.println("* ");
        System.out.println("* ========================");
    }
 
    /**
     * 发送信息
     *
     * @param ip
     * @param port
     * @param msg
     * @return
     */
    public static InvokeResult push(String ip, int port, byte[] msg) {
        IoSessionQuery sessionFactory = ioServer.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);
    }
}