-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathXTcpClient.java
More file actions
456 lines (405 loc) · 15 KB
/
XTcpClient.java
File metadata and controls
456 lines (405 loc) · 15 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package com.blanke.xsocket.tcp.client;
import com.blanke.xsocket.BaseXSocket;
import com.blanke.xsocket.tcp.client.bean.TargetInfo;
import com.blanke.xsocket.tcp.client.bean.TcpMsg;
import com.blanke.xsocket.tcp.client.listener.TcpClientListener;
import com.blanke.xsocket.tcp.client.manager.TcpClientManager;
import com.blanke.xsocket.tcp.client.state.ClientState;
import com.blanke.xsocket.utils.CharsetUtil;
import com.blanke.xsocket.utils.ExceptionUtils;
import com.blanke.xsocket.utils.XSocketLog;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
/**
* tcp客户端
*/
public class XTcpClient extends BaseXSocket {
public static final String TAG = "XTcpClient";
protected TargetInfo mTargetInfo;//目标ip和端口号
protected Socket mSocket;
protected ClientState mClientState;
protected TcpConnConfig mTcpConnConfig;
protected ConnectionThread mConnectionThread;
protected SendThread mSendThread;
protected ReceiveThread mReceiveThread;
protected List<TcpClientListener> mTcpClientListeners;
private LinkedBlockingQueue<TcpMsg> msgQueue;
private XTcpClient() {
super();
}
/**
* 创建tcp连接,需要提供服务器信息
*
* @param targetInfo
* @return
*/
public static XTcpClient getTcpClient(TargetInfo targetInfo) {
return getTcpClient(targetInfo, null);
}
public static XTcpClient getTcpClient(TargetInfo targetInfo, TcpConnConfig tcpConnConfig) {
XTcpClient XTcpClient = TcpClientManager.getTcpClient(targetInfo);
if (XTcpClient == null) {
XTcpClient = new XTcpClient();
XTcpClient.init(targetInfo, tcpConnConfig);
TcpClientManager.putTcpClient(XTcpClient);
}
return XTcpClient;
}
/**
* 根据socket创建client端,目前仅用在socketServer接受client之后
*
* @param socket
* @return
*/
public static XTcpClient getTcpClient(Socket socket, TargetInfo targetInfo) {
return getTcpClient(socket, targetInfo, null);
}
public static XTcpClient getTcpClient(Socket socket, TargetInfo targetInfo, TcpConnConfig connConfig) {
if (!socket.isClosed()) {
ExceptionUtils.throwException("socket is closeed");
}
XTcpClient xTcpClient = new XTcpClient();
xTcpClient.init(targetInfo, connConfig);
xTcpClient.mSocket = socket;
xTcpClient.mClientState = ClientState.Connected;
xTcpClient.onConnectSuccess();
return xTcpClient;
}
private void init(TargetInfo targetInfo, TcpConnConfig connConfig) {
this.mTargetInfo = targetInfo;
mClientState = ClientState.Disconnected;
mTcpClientListeners = new ArrayList<>();
if (mTcpConnConfig == null && connConfig == null) {
mTcpConnConfig = new TcpConnConfig.Builder().create();
} else if (connConfig != null) {
mTcpConnConfig = connConfig;
}
}
public synchronized TcpMsg sendMsg(String message) {
TcpMsg msg = new TcpMsg(message, mTargetInfo, TcpMsg.MsgType.Send);
return sendMsg(msg);
}
public synchronized TcpMsg sendMsg(byte[] message) {
TcpMsg msg = new TcpMsg(message, mTargetInfo, TcpMsg.MsgType.Send);
return sendMsg(msg);
}
public synchronized TcpMsg sendMsg(TcpMsg msg) {
if (isDisconnected()) {
XSocketLog.d(TAG, "发送消息 " + msg + ",当前没有tcp连接,先进行连接");
connect();
}
boolean re = enqueueTcpMsg(msg);
if (re) {
return msg;
}
return null;
}
public synchronized boolean cancelMsg(TcpMsg msg) {
return getSendThread().cancel(msg);
}
public synchronized boolean cancelMsg(int msgId) {
return getSendThread().cancel(msgId);
}
public synchronized void connect() {
if (!isDisconnected()) {
XSocketLog.d(TAG, "已经连接了或正在连接");
return;
}
XSocketLog.d(TAG, "tcp connecting");
setClientState(ClientState.Connecting);//正在连接
getConnectionThread().start();
}
public synchronized Socket getSocket() {
if (mSocket == null || isDisconnected() || !mSocket.isConnected()) {
mSocket = new Socket();
try {
mSocket.setSoTimeout((int) mTcpConnConfig.getReceiveTimeout());
} catch (SocketException e) {
// e.printStackTrace();
}
}
return mSocket;
}
public synchronized void disconnect() {
disconnect("手动关闭tcpclient", null);
}
protected synchronized void onErrorDisConnect(String msg, Exception e) {
if (isDisconnected()) {
return;
}
disconnect(msg, e);
if (mTcpConnConfig.isReconnect()) {//重连
connect();
}
}
protected synchronized void disconnect(String msg, Exception e) {
if (isDisconnected()) {
return;
}
closeSocket();
getConnectionThread().interrupt();
getSendThread().interrupt();
getReceiveThread().interrupt();
setClientState(ClientState.Disconnected);
notifyDisconnected(msg, e);
XSocketLog.d(TAG, "tcp closed");
}
private synchronized boolean closeSocket() {
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
// e.printStackTrace();
}
}
return true;
}
//连接已经连接,接下来的流程,创建发送和接受消息的线程
private void onConnectSuccess() {
XSocketLog.d(TAG, "tcp connect 建立成功");
setClientState(ClientState.Connected);//标记为已连接
getSendThread().start();
getReceiveThread().start();
}
/**
* tcp连接线程
*/
private class ConnectionThread extends Thread {
@Override
public void run() {
try {
int localPort = mTcpConnConfig.getLocalPort();
if (localPort > 0) {
if (!getSocket().isBound()) {
getSocket().bind(new InetSocketAddress(localPort));
}
}
getSocket().connect(new InetSocketAddress(mTargetInfo.getIp(), mTargetInfo.getPort()),
(int) mTcpConnConfig.getConnTimeout());
XSocketLog.d(TAG, "创建连接成功,target=" + mTargetInfo + ",localport=" + localPort);
} catch (Exception e) {
XSocketLog.d(TAG, "创建连接失败,target=" + mTargetInfo + "," + e);
onErrorDisConnect("创建连接失败", e);
return;
}
notifyConnected();
onConnectSuccess();
}
}
public boolean enqueueTcpMsg(final TcpMsg tcpMsg) {
if (tcpMsg == null || getMsgQueue().contains(tcpMsg)) {
return false;
}
try {
getMsgQueue().put(tcpMsg);
return true;
} catch (InterruptedException e) {
// e.printStackTrace();
}
return false;
}
protected LinkedBlockingQueue<TcpMsg> getMsgQueue() {
if (msgQueue == null) {
msgQueue = new LinkedBlockingQueue<>();
}
return msgQueue;
}
private class SendThread extends Thread {
private TcpMsg sendingTcpMsg;
protected SendThread setSendingTcpMsg(TcpMsg sendingTcpMsg) {
this.sendingTcpMsg = sendingTcpMsg;
return this;
}
public TcpMsg getSendingTcpMsg() {
return this.sendingTcpMsg;
}
public boolean cancel(TcpMsg packet) {
return getMsgQueue().remove(packet);
}
public boolean cancel(int tcpMsgID) {
return getMsgQueue().remove(new TcpMsg(tcpMsgID));
}
@Override
public void run() {
TcpMsg msg;
try {
while (isConnected() && !Thread.interrupted() && (msg = getMsgQueue().take()) != null) {
setSendingTcpMsg(msg);//设置正在发送的
XSocketLog.d(TAG, "tcp sending msg=" + msg);
byte[] data = msg.getSourceDataBytes();
if (data == null) {//根据编码转换消息
data = CharsetUtil.stringToData(msg.getSourceDataString(), mTcpConnConfig.getCharsetName());
}
if (data != null && data.length > 0) {
try {
getSocket().getOutputStream().write(data);
getSocket().getOutputStream().flush();
msg.setTime();
notifySended(msg);
} catch (IOException e) {
e.printStackTrace();
onErrorDisConnect("发送消息失败", e);
return;
}
}
}
} catch (InterruptedException e) {
// e.printStackTrace();
}
}
}
private class ReceiveThread extends Thread {
@Override
public void run() {
try {
InputStream is = getSocket().getInputStream();
while (isConnected() && !Thread.interrupted()) {
byte[] result = mTcpConnConfig.getStickPackageHelper().execute(is);//粘包处理
if (result == null) {//报错
XSocketLog.d(TAG, "tcp Receive 粘包处理失败 " + Arrays.toString(result));
onErrorDisConnect("粘包处理中发送错误", null);
break;
}
XSocketLog.d(TAG, "tcp Receive 解决粘包之后的数据 " + Arrays.toString(result));
TcpMsg tcpMsg = new TcpMsg(result, mTargetInfo, TcpMsg.MsgType.Receive);
tcpMsg.setTime();
String msgstr = CharsetUtil.dataToString(result, mTcpConnConfig.getCharsetName());
tcpMsg.setSourceDataString(msgstr);
boolean va = mTcpConnConfig.getValidationHelper().execute(result);
if (!va) {
XSocketLog.d(TAG, "tcp Receive 数据验证失败 ");
notifyValidationFail(tcpMsg);//验证失败
continue;
}
byte[][] decodebytes = mTcpConnConfig.getDecodeHelper().execute(result, mTargetInfo, mTcpConnConfig);
tcpMsg.setEndDecodeData(decodebytes);
XSocketLog.d(TAG, "tcp Receive succ msg= " + tcpMsg);
notifyReceive(tcpMsg);//notify listener
}
} catch (Exception e) {
XSocketLog.d(TAG, "tcp Receive error " + e);
onErrorDisConnect("接受消息错误", e);
}
}
}
protected ReceiveThread getReceiveThread() {
if (mReceiveThread == null || !mReceiveThread.isAlive()) {
mReceiveThread = new ReceiveThread();
}
return mReceiveThread;
}
protected SendThread getSendThread() {
if (mSendThread == null || !mSendThread.isAlive()) {
mSendThread = new SendThread();
}
return mSendThread;
}
protected ConnectionThread getConnectionThread() {
if (mConnectionThread == null || !mConnectionThread.isAlive() || mConnectionThread.isInterrupted()) {
mConnectionThread = new ConnectionThread();
}
return mConnectionThread;
}
public ClientState getClientState() {
return mClientState;
}
protected void setClientState(ClientState state) {
if (mClientState != state) {
mClientState = state;
}
}
public boolean isDisconnected() {
return getClientState() == ClientState.Disconnected;
}
public boolean isConnected() {
return getClientState() == ClientState.Connected;
}
private void notifyConnected() {
TcpClientListener l;
for (TcpClientListener wl : mTcpClientListeners) {
final TcpClientListener finalL = wl;
runOnUiThread(new Runnable() {
@Override
public void run() {
finalL.onConnected(XTcpClient.this);
}
});
}
}
private void notifyDisconnected(final String msg, final Exception e) {
TcpClientListener l;
for (TcpClientListener wl : mTcpClientListeners) {
final TcpClientListener finalL = wl;
runOnUiThread(new Runnable() {
@Override
public void run() {
finalL.onDisconnected(XTcpClient.this, msg, e);
}
});
}
}
private void notifyReceive(final TcpMsg tcpMsg) {
TcpClientListener l;
for (TcpClientListener wl : mTcpClientListeners) {
final TcpClientListener finalL = wl;
runOnUiThread(new Runnable() {
@Override
public void run() {
finalL.onReceive(XTcpClient.this, tcpMsg);
}
});
}
}
private void notifySended(final TcpMsg tcpMsg) {
TcpClientListener l;
for (TcpClientListener wl : mTcpClientListeners) {
final TcpClientListener finalL = wl;
runOnUiThread(new Runnable() {
@Override
public void run() {
finalL.onSended(XTcpClient.this, tcpMsg);
}
});
}
}
private void notifyValidationFail(final TcpMsg tcpMsg) {
TcpClientListener l;
for (TcpClientListener wl : mTcpClientListeners) {
final TcpClientListener finalL = wl;
runOnUiThread(new Runnable() {
@Override
public void run() {
finalL.onValidationFail(XTcpClient.this, tcpMsg);
}
});
}
}
public TargetInfo getTargetInfo() {
return mTargetInfo;
}
public void addTcpClientListener(TcpClientListener listener) {
if (mTcpClientListeners.contains(listener)) {
return;
}
mTcpClientListeners.add(listener);
}
public void removeTcpClientListener(TcpClientListener listener) {
mTcpClientListeners.remove(listener);
}
public void config(TcpConnConfig tcpConnConfig) {
mTcpConnConfig = tcpConnConfig;
}
@Override
public String toString() {
return "XTcpClient{" +
"mTargetInfo=" + mTargetInfo + ",state=" + mClientState + ",isconnect=" + isConnected() +
'}';
}
}