648540858
2023-04-28 ebc904e4d5fe07ecc269927f0e6669ad4f8bda19
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.genersoft.iot.vmp.jt1078.session;
 
import com.genersoft.iot.vmp.jt1078.proc.Header;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @author QingtaiJiang
 * @date 2023/4/27 18:54
 * @email qingtaij@163.com
 */
public class Session {
    private final static Logger log = LoggerFactory.getLogger(Session.class);
 
    public static final AttributeKey<Session> KEY = AttributeKey.newInstance(Session.class.getName());
 
    // Netty的channel
    protected final Channel channel;
 
    // 原子类的自增ID
    private final AtomicInteger serialNo = new AtomicInteger(0);
 
    // 是否注册成功
    private boolean registered = false;
 
    // 设备ID
    private String devId;
 
    // 创建时间
    private final long creationTime;
 
    // 协议版本号
    private Integer protocolVersion;
 
    private Header header;
 
    protected Session(Channel channel) {
        this.channel = channel;
        this.creationTime = System.currentTimeMillis();
    }
 
    public void writeObject(Object message) {
        log.info("<<<<<<<<<< cmd{},{}", this, message);
        channel.writeAndFlush(message);
    }
 
    /**
     * 获得下一个流水号
     *
     * @return 流水号
     */
    public int nextSerialNo() {
        int current;
        int next;
        do {
            current = serialNo.get();
            next = current > 0xffff ? 0 : current;
        } while (!serialNo.compareAndSet(current, next + 1));
        return next;
    }
 
    /**
     * 注册session
     *
     * @param devId 设备ID
     */
    public void register(String devId, Integer version, Header header) {
        this.devId = devId;
        this.registered = true;
        this.protocolVersion = version;
        this.header = header;
        SessionManager.INSTANCE.put(devId, this);
    }
 
    /**
     * 获取设备号
     *
     * @return 设备号
     */
    public String getDevId() {
        return devId;
    }
 
 
    public boolean isRegistered() {
        return registered;
    }
 
    public long getCreationTime() {
        return creationTime;
    }
 
    public Integer getProtocolVersion() {
        return protocolVersion;
    }
 
    public Header getHeader() {
        return header;
    }
 
    @Override
    public String toString() {
        return "[" +
                "devId=" + devId +
                ", reg=" + registered +
                ", version=" + protocolVersion +
                ",ip=" + channel.remoteAddress() +
                ']';
    }
}