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
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
116
117
118
package com.ld.io.netty;
 
import com.ld.io.api.InvokeResult;
import com.ld.io.api.IoSession;
 
import java.util.Date;
import java.util.Objects;
 
public class NettySession implements IoSession {
    private String id;// 随机唯一标识
    private String businessKey;// 业务唯一标识
    private String address;// ip地址
    private Integer port;// 端口
    private Date createTime;// 创建时间
    private String companyId;// 组织编码
 
    private NettyChannel nettyChannel;
    private NettySessionFactory nettySessionFactory;
 
    public String getId() {
        return id;
    }
 
    public String getBusinessKey() {
        return businessKey;
    }
 
    public void setBusinessKey(String businessKey) {
        this.businessKey = businessKey;
    }
 
    public String getAddress() {
        return address;
    }
 
    public Integer getPort() {
        return port;
    }
 
    public Date getCreateTime() {
        return createTime;
    }
 
    void setId(String id) {
        this.id = id;
    }
 
    void setAddress(String address) {
        this.address = address;
    }
 
    void setPort(Integer port) {
        this.port = port;
    }
 
    void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
 
    void setNettyChannel(NettyChannel nettyChannel) {
        this.nettyChannel = nettyChannel;
    }
 
    public NettyChannel getNettyChannel() {
        return nettyChannel;
    }
 
    public void setNettySessionFactory(NettySessionFactory nettySessionFactory) {
        this.nettySessionFactory = nettySessionFactory;
    }
 
    @Override
    public String getCompanyId() {
        return this.companyId;
    }
 
    @Override
    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        NettySession that = (NettySession) o;
        return Objects.equals(id, that.id);
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
 
    public InvokeResult invoke(byte[] bytes) {
        if (nettyChannel == null) {
            return InvokeResult.SOCKET_NOT_CREATE;
        }
        InvokeResult result = nettyChannel.invoke(bytes);
        if (InvokeResult.CHANNEL_CLOSED.equals(result)) {
            this.destroy();
        }
        return result;
    }
 
    public void destroy() {
        nettySessionFactory.destroy(this);
    }
 
    @Override
    public String toString() {
        return "NettySession{" + "id='" + id + '\'' + ", businessKey='"
                + businessKey + '\'' + ", address='" + address + '\''
                + ", port=" + port + ", createTime=" + createTime + '}';
    }
}