-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeartBeatHelper.java
More file actions
150 lines (130 loc) · 4.05 KB
/
Copy pathHeartBeatHelper.java
File metadata and controls
150 lines (130 loc) · 4.05 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* HeartBeatHelper
* Feature:
*/
public class HeartBeatHelper {
final HeartBeatHelper self = this;
public static final long DefaultHeartBeatInterval = 1000 * 30;
public static final long NoneHeartBeatInterval = -1;
public static final long DefaultRemoteNoReplyAliveTimeout = DefaultHeartBeatInterval * 2;
public static final long NoneRemoteNoReplyAliveTimeout = -1;
/* Constructors */
public HeartBeatHelper(String charsetName) {
this.charsetName = charsetName;
}
/* Public Methods */
public HeartBeatHelper setSendString(String message) {
if (message == null) {
setSendData(null);
}
else {
setSendData(CharsetUtil.stringToData(message, getCharsetName()));
}
return this;
}
public HeartBeatHelper setReceiveString(String message) {
if (message == null) {
setReceiveData(null);
}
else {
setReceiveData(CharsetUtil.stringToData(message, getCharsetName()));
}
return this;
}
/**
* 禁用发送心跳包
*/
public void disableHeartBeat() {
setSendData(null);
}
/**
* 禁用自动断开
*/
public void disableRemoteNoReplyAliveTimeout() {
setRemoteNoReplyAliveTimeout(NoneRemoteNoReplyAliveTimeout);
}
/**
* 是否发送心跳包
* @return
*/
public boolean shouldSendHeartBeat() {
return getSendData() != null && getHeartBeatInterval() != NoneHeartBeatInterval;
}
/**
* 是否在超过设定没有远程响应超时后自动断开
* @return
*/
public boolean shouldAutoDisconnectWhenRemoteNoReplyAliveTimeout() {
return getRemoteNoReplyAliveTimeout() != NoneRemoteNoReplyAliveTimeout;
}
public HeartBeatHelper copy() {
HeartBeatHelper helper = new HeartBeatHelper(getCharsetName());
helper.setSendData(getSendData());
helper.setReceiveData(getReceiveData());
helper.setHeartBeatInterval(getHeartBeatInterval());
helper.setRemoteNoReplyAliveTimeout(getRemoteNoReplyAliveTimeout());
return helper;
}
/* Properties */
private String charsetName;
public HeartBeatHelper setCharsetName(String charsetName) {
this.charsetName = charsetName;
return this;
}
public String getCharsetName() {
return this.charsetName;
}
/**
* 发送心跳包的数据
*/
private byte[] sendData;
public HeartBeatHelper setSendData(byte[] sendData) {
this.sendData = sendData;
return this;
}
public byte[] getSendData() {
return this.sendData;
}
/**
* 接收心跳包的数据,用于过滤远程心跳包
*/
private byte[] receiveData;
public HeartBeatHelper setReceiveData(byte[] receiveData) {
this.receiveData = receiveData;
return this;
}
public byte[] getReceiveData() {
return this.receiveData;
}
/**
* 心跳包发送间隔
*/
private long heartBeatInterval = NoneHeartBeatInterval;
public HeartBeatHelper setHeartBeatInterval(long heartBeatInterval) {
if (heartBeatInterval < 0) {
heartBeatInterval = NoneHeartBeatInterval;
}
this.heartBeatInterval = heartBeatInterval;
return this;
}
public long getHeartBeatInterval() {
return this.heartBeatInterval;
}
/**
* 远程端在一定时间间隔没有消息后自动断开
*/
private long remoteNoReplyAliveTimeout = NoneRemoteNoReplyAliveTimeout;
public HeartBeatHelper setRemoteNoReplyAliveTimeout(long remoteNoReplyAliveTimeout) {
if (remoteNoReplyAliveTimeout < 0) {
remoteNoReplyAliveTimeout = NoneRemoteNoReplyAliveTimeout;
}
this.remoteNoReplyAliveTimeout = remoteNoReplyAliveTimeout;
return this;
}
public long getRemoteNoReplyAliveTimeout() {
return this.remoteNoReplyAliveTimeout;
}
/* Overrides */
/* Delegates */
/* Private Methods */
}